Skip to content

Commit

Permalink
Merge pull request #35 from jaypipes/api
Browse files Browse the repository at this point in the history
consolidate types/, result/ and errors/ into api/
  • Loading branch information
jaypipes authored Jun 25, 2024
2 parents 4124262 + 6edc4f7 commit f051721
Show file tree
Hide file tree
Showing 46 changed files with 463 additions and 500 deletions.
2 changes: 1 addition & 1 deletion types/assertions.go → api/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// See the COPYING file in the root project directory for full text.

package types
package api

import "context"

Expand Down
2 changes: 1 addition & 1 deletion types/defaults.go → api/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// See the COPYING file in the root project directory for full text.

package types
package api

// Defaults are a collection of default configuration values
type Defaults map[string]interface{}
Expand Down
104 changes: 103 additions & 1 deletion errors/parse.go → api/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// See the COPYING file in the root project directory for full text.

package errors
package api

import (
"errors"
Expand All @@ -11,6 +11,90 @@ import (
"gopkg.in/yaml.v3"
)

var (
// ErrFailure is the base error class for all errors that represent failed
// assertions when evaluating a test.
ErrFailure = errors.New("assertion failed")
// ErrTimeoutExceeded is an ErrFailure when a test's execution exceeds a
// timeout length.
ErrTimeoutExceeded = fmt.Errorf("%s: timeout exceeded", ErrFailure)
// ErrNotEqual is an ErrFailure when an expected thing doesn't equal an
// observed thing.
ErrNotEqual = fmt.Errorf("%w: not equal", ErrFailure)
// ErrIn is an ErrFailure when a thing unexpectedly appears in an
// container.
ErrIn = fmt.Errorf("%w: in", ErrFailure)
// ErrNotIn is an ErrFailure when an expected thing doesn't appear in an
// expected container.
ErrNotIn = fmt.Errorf("%w: not in", ErrFailure)
// ErrNoneIn is an ErrFailure when none of a list of elements appears in an
// expected container.
ErrNoneIn = fmt.Errorf("%w: none in", ErrFailure)
// ErrUnexpectedError is an ErrFailure when an unexpected error has
// occurred.
ErrUnexpectedError = fmt.Errorf("%w: unexpected error", ErrFailure)
)

// TimeoutExceeded returns an ErrTimeoutExceeded when a test's execution
// exceeds a timeout length. The optional failure parameter indicates a failed
// assertion that occurred before a timeout was reached.
func TimeoutExceeded(duration string, failure error) error {
if failure != nil {
return fmt.Errorf(
"%w: timed out waiting for assertion to succeed (%s)",
failure, duration,
)
}
return fmt.Errorf("%s (%s)", ErrTimeoutExceeded, duration)
}

// NotEqualLength returns an ErrNotEqual when an expected length doesn't
// equal an observed length.
func NotEqualLength(exp, got int) error {
return fmt.Errorf(
"%w: expected length of %d but got %d",
ErrNotEqual, exp, got,
)
}

// NotEqual returns an ErrNotEqual when an expected thing doesn't equal an
// observed thing.
func NotEqual(exp, got interface{}) error {
return fmt.Errorf("%w: expected %v but got %v", ErrNotEqual, exp, got)
}

// In returns an ErrIn when a thing unexpectedly appears in a container.
func In(element, container interface{}) error {
return fmt.Errorf(
"%w: expected %v not to contain %v",
ErrIn, container, element,
)
}

// NotIn returns an ErrNotIn when an expected thing doesn't appear in an
// expected container.
func NotIn(element, container interface{}) error {
return fmt.Errorf(
"%w: expected %v to contain %v",
ErrNotIn, container, element,
)
}

// NoneIn returns an ErrNoneIn when none of a list of elements appears in an
// expected container.
func NoneIn(elements, container interface{}) error {
return fmt.Errorf(
"%w: expected %v to contain one of %v",
ErrNoneIn, container, elements,
)
}

// UnexpectedError returns an ErrUnexpectedError when a supplied error is not
// expected.
func UnexpectedError(err error) error {
return fmt.Errorf("%w: %s", ErrUnexpectedError, err)
}

var (
// ErrUnknownSourceType indicates that a From() function was called with an
// unknown source parameter type.
Expand Down Expand Up @@ -199,3 +283,21 @@ func FileNotFound(path string, node *yaml.Node) error {
ErrFileNotFound, path, node.Line, node.Column,
)
}

var (
// RuntimeError is the base error class for all errors occurring during
// runtime (and not during the parsing of a scenario or spec)
RuntimeError = errors.New("runtime error")
// ErrRequiredFixture is returned when a required fixture has not
// been registered with the context.
ErrRequiredFixture = fmt.Errorf(
"%w: required fixture missing",
RuntimeError,
)
)

// RequiredFixtureMissing returns an ErrRequiredFixture with the supplied
// fixture name
func RequiredFixtureMissing(name string) error {
return fmt.Errorf("%w: %s", ErrRequiredFixture, name)
}
8 changes: 4 additions & 4 deletions errors/parse_test.go → api/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
//
// See the COPYING file in the root project directory for full text.

package errors_test
package api_test

import (
"testing"

gdterrors "github.com/gdt-dev/gdt/errors"
"github.com/gdt-dev/gdt/api"
"github.com/stretchr/testify/assert"
)

func TestUnknownSourceType(t *testing.T) {
assert := assert.New(t)

err := gdterrors.UnknownSourceType(1)
err := api.UnknownSourceType(1)
assert.ErrorContains(err, "int")

source := []string{"foo", "bar"}
err = gdterrors.UnknownSourceType(source)
err = api.UnknownSourceType(source)
assert.ErrorContains(err, "[]string")
}
6 changes: 2 additions & 4 deletions types/evaluable.go → api/evaluable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
//
// See the COPYING file in the root project directory for full text.

package types
package api

import (
"context"

"github.com/gdt-dev/gdt/result"
)

// Evaluable represents individual test units in a Scenario
Expand All @@ -18,7 +16,7 @@ type Evaluable interface {
//
// Errors returned by Eval() are **RuntimeErrors**, not failures in
// assertions.
Eval(context.Context) (*result.Result, error)
Eval(context.Context) (*Result, error)
// SetBase sets the Evaluable's base Spec
SetBase(Spec)
// Base returns the Evaluable's base Spec
Expand Down
2 changes: 1 addition & 1 deletion types/fixture.go → api/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// See the COPYING file in the root project directory for full text.

package types
package api

import "context"

Expand Down
8 changes: 3 additions & 5 deletions types/flexstrings.go → api/flexstrings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
//
// See the COPYING file in the root project directory for full text.

package types
package api

import (
"gopkg.in/yaml.v3"

gdterrors "github.com/gdt-dev/gdt/errors"
)

// FlexStrings is a struct used to parse an interface{} that can be either a
Expand All @@ -25,7 +23,7 @@ func (f *FlexStrings) Values() []string {
// FlexStrings can be either a string or a slice of strings.
func (f *FlexStrings) UnmarshalYAML(node *yaml.Node) error {
if node.Kind != yaml.ScalarNode && node.Kind != yaml.SequenceNode {
return gdterrors.ExpectedScalarOrSequenceAt(node)
return ExpectedScalarOrSequenceAt(node)
}
var single string
if err := node.Decode(&single); err == nil {
Expand All @@ -37,5 +35,5 @@ func (f *FlexStrings) UnmarshalYAML(node *yaml.Node) error {
f.values = many
return nil
}
return gdterrors.ExpectedScalarOrSequenceAt(node)
return ExpectedScalarOrSequenceAt(node)
}
11 changes: 5 additions & 6 deletions types/flexstrings_test.go → api/flexstrings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
//
// See the COPYING file in the root project directory for full text.

package types_test
package api_test

import (
"testing"

gdterrors "github.com/gdt-dev/gdt/errors"
gdttypes "github.com/gdt-dev/gdt/types"
"github.com/gdt-dev/gdt/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

type foo struct {
Foo gdttypes.FlexStrings `yaml:"foo"`
Foo api.FlexStrings `yaml:"foo"`
}

type foop struct {
Foo *gdttypes.FlexStrings `yaml:"foo"`
Foo *api.FlexStrings `yaml:"foo"`
}

func TestFlexStringsError(t *testing.T) {
Expand All @@ -31,7 +30,7 @@ func TestFlexStringsError(t *testing.T) {
err := yaml.Unmarshal(contents, &f)

require.NotNil(err)
assert.ErrorIs(err, gdterrors.ErrExpectedScalarOrSequence)
assert.ErrorIs(err, api.ErrExpectedScalarOrSequence)
}

func TestFlexStrings(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion types/plugin.go → api/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// See the COPYING file in the root project directory for full text.

package types
package api

import "gopkg.in/yaml.v3"

Expand Down
6 changes: 3 additions & 3 deletions result/result.go → api/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// See the COPYING file in the root project directory for full text.

package result
package api

// Result is returned from a `Evaluable.Eval` execution. It serves two
// purposes:
Expand Down Expand Up @@ -80,8 +80,8 @@ func WithFailures(failures ...error) ResultModifier {
}
}

// New returns a new Result
func New(mods ...ResultModifier) *Result {
// NewResult returns a new Result
func NewResult(mods ...ResultModifier) *Result {
r := &Result{}
for _, mod := range mods {
mod(r)
Expand Down
2 changes: 1 addition & 1 deletion types/retry.go → api/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// See the COPYING file in the root project directory for full text.

package types
package api

import (
"time"
Expand Down
Loading

0 comments on commit f051721

Please sign in to comment.