Skip to content

Commit 6edc4f7

Browse files
committed
consolidate types/, result/ and errors/ into api/
simplify the top-level packages by combining types/, result/ and errors/ into a single `api/` package. Signed-off-by: Jay Pipes <[email protected]>
1 parent 4124262 commit 6edc4f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+463
-500
lines changed

types/assertions.go renamed to api/assertions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package types
5+
package api
66

77
import "context"
88

types/defaults.go renamed to api/defaults.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package types
5+
package api
66

77
// Defaults are a collection of default configuration values
88
type Defaults map[string]interface{}

errors/parse.go renamed to api/error.go

+103-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package errors
5+
package api
66

77
import (
88
"errors"
@@ -11,6 +11,90 @@ import (
1111
"gopkg.in/yaml.v3"
1212
)
1313

14+
var (
15+
// ErrFailure is the base error class for all errors that represent failed
16+
// assertions when evaluating a test.
17+
ErrFailure = errors.New("assertion failed")
18+
// ErrTimeoutExceeded is an ErrFailure when a test's execution exceeds a
19+
// timeout length.
20+
ErrTimeoutExceeded = fmt.Errorf("%s: timeout exceeded", ErrFailure)
21+
// ErrNotEqual is an ErrFailure when an expected thing doesn't equal an
22+
// observed thing.
23+
ErrNotEqual = fmt.Errorf("%w: not equal", ErrFailure)
24+
// ErrIn is an ErrFailure when a thing unexpectedly appears in an
25+
// container.
26+
ErrIn = fmt.Errorf("%w: in", ErrFailure)
27+
// ErrNotIn is an ErrFailure when an expected thing doesn't appear in an
28+
// expected container.
29+
ErrNotIn = fmt.Errorf("%w: not in", ErrFailure)
30+
// ErrNoneIn is an ErrFailure when none of a list of elements appears in an
31+
// expected container.
32+
ErrNoneIn = fmt.Errorf("%w: none in", ErrFailure)
33+
// ErrUnexpectedError is an ErrFailure when an unexpected error has
34+
// occurred.
35+
ErrUnexpectedError = fmt.Errorf("%w: unexpected error", ErrFailure)
36+
)
37+
38+
// TimeoutExceeded returns an ErrTimeoutExceeded when a test's execution
39+
// exceeds a timeout length. The optional failure parameter indicates a failed
40+
// assertion that occurred before a timeout was reached.
41+
func TimeoutExceeded(duration string, failure error) error {
42+
if failure != nil {
43+
return fmt.Errorf(
44+
"%w: timed out waiting for assertion to succeed (%s)",
45+
failure, duration,
46+
)
47+
}
48+
return fmt.Errorf("%s (%s)", ErrTimeoutExceeded, duration)
49+
}
50+
51+
// NotEqualLength returns an ErrNotEqual when an expected length doesn't
52+
// equal an observed length.
53+
func NotEqualLength(exp, got int) error {
54+
return fmt.Errorf(
55+
"%w: expected length of %d but got %d",
56+
ErrNotEqual, exp, got,
57+
)
58+
}
59+
60+
// NotEqual returns an ErrNotEqual when an expected thing doesn't equal an
61+
// observed thing.
62+
func NotEqual(exp, got interface{}) error {
63+
return fmt.Errorf("%w: expected %v but got %v", ErrNotEqual, exp, got)
64+
}
65+
66+
// In returns an ErrIn when a thing unexpectedly appears in a container.
67+
func In(element, container interface{}) error {
68+
return fmt.Errorf(
69+
"%w: expected %v not to contain %v",
70+
ErrIn, container, element,
71+
)
72+
}
73+
74+
// NotIn returns an ErrNotIn when an expected thing doesn't appear in an
75+
// expected container.
76+
func NotIn(element, container interface{}) error {
77+
return fmt.Errorf(
78+
"%w: expected %v to contain %v",
79+
ErrNotIn, container, element,
80+
)
81+
}
82+
83+
// NoneIn returns an ErrNoneIn when none of a list of elements appears in an
84+
// expected container.
85+
func NoneIn(elements, container interface{}) error {
86+
return fmt.Errorf(
87+
"%w: expected %v to contain one of %v",
88+
ErrNoneIn, container, elements,
89+
)
90+
}
91+
92+
// UnexpectedError returns an ErrUnexpectedError when a supplied error is not
93+
// expected.
94+
func UnexpectedError(err error) error {
95+
return fmt.Errorf("%w: %s", ErrUnexpectedError, err)
96+
}
97+
1498
var (
1599
// ErrUnknownSourceType indicates that a From() function was called with an
16100
// unknown source parameter type.
@@ -199,3 +283,21 @@ func FileNotFound(path string, node *yaml.Node) error {
199283
ErrFileNotFound, path, node.Line, node.Column,
200284
)
201285
}
286+
287+
var (
288+
// RuntimeError is the base error class for all errors occurring during
289+
// runtime (and not during the parsing of a scenario or spec)
290+
RuntimeError = errors.New("runtime error")
291+
// ErrRequiredFixture is returned when a required fixture has not
292+
// been registered with the context.
293+
ErrRequiredFixture = fmt.Errorf(
294+
"%w: required fixture missing",
295+
RuntimeError,
296+
)
297+
)
298+
299+
// RequiredFixtureMissing returns an ErrRequiredFixture with the supplied
300+
// fixture name
301+
func RequiredFixtureMissing(name string) error {
302+
return fmt.Errorf("%w: %s", ErrRequiredFixture, name)
303+
}

errors/parse_test.go renamed to api/error_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package errors_test
5+
package api_test
66

77
import (
88
"testing"
99

10-
gdterrors "github.com/gdt-dev/gdt/errors"
10+
"github.com/gdt-dev/gdt/api"
1111
"github.com/stretchr/testify/assert"
1212
)
1313

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

17-
err := gdterrors.UnknownSourceType(1)
17+
err := api.UnknownSourceType(1)
1818
assert.ErrorContains(err, "int")
1919

2020
source := []string{"foo", "bar"}
21-
err = gdterrors.UnknownSourceType(source)
21+
err = api.UnknownSourceType(source)
2222
assert.ErrorContains(err, "[]string")
2323
}

types/evaluable.go renamed to api/evaluable.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package types
5+
package api
66

77
import (
88
"context"
9-
10-
"github.com/gdt-dev/gdt/result"
119
)
1210

1311
// Evaluable represents individual test units in a Scenario
@@ -18,7 +16,7 @@ type Evaluable interface {
1816
//
1917
// Errors returned by Eval() are **RuntimeErrors**, not failures in
2018
// assertions.
21-
Eval(context.Context) (*result.Result, error)
19+
Eval(context.Context) (*Result, error)
2220
// SetBase sets the Evaluable's base Spec
2321
SetBase(Spec)
2422
// Base returns the Evaluable's base Spec

types/fixture.go renamed to api/fixture.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package types
5+
package api
66

77
import "context"
88

types/flexstrings.go renamed to api/flexstrings.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package types
5+
package api
66

77
import (
88
"gopkg.in/yaml.v3"
9-
10-
gdterrors "github.com/gdt-dev/gdt/errors"
119
)
1210

1311
// FlexStrings is a struct used to parse an interface{} that can be either a
@@ -25,7 +23,7 @@ func (f *FlexStrings) Values() []string {
2523
// FlexStrings can be either a string or a slice of strings.
2624
func (f *FlexStrings) UnmarshalYAML(node *yaml.Node) error {
2725
if node.Kind != yaml.ScalarNode && node.Kind != yaml.SequenceNode {
28-
return gdterrors.ExpectedScalarOrSequenceAt(node)
26+
return ExpectedScalarOrSequenceAt(node)
2927
}
3028
var single string
3129
if err := node.Decode(&single); err == nil {
@@ -37,5 +35,5 @@ func (f *FlexStrings) UnmarshalYAML(node *yaml.Node) error {
3735
f.values = many
3836
return nil
3937
}
40-
return gdterrors.ExpectedScalarOrSequenceAt(node)
38+
return ExpectedScalarOrSequenceAt(node)
4139
}

types/flexstrings_test.go renamed to api/flexstrings_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package types_test
5+
package api_test
66

77
import (
88
"testing"
99

10-
gdterrors "github.com/gdt-dev/gdt/errors"
11-
gdttypes "github.com/gdt-dev/gdt/types"
10+
"github.com/gdt-dev/gdt/api"
1211
"github.com/stretchr/testify/assert"
1312
"github.com/stretchr/testify/require"
1413
"gopkg.in/yaml.v3"
1514
)
1615

1716
type foo struct {
18-
Foo gdttypes.FlexStrings `yaml:"foo"`
17+
Foo api.FlexStrings `yaml:"foo"`
1918
}
2019

2120
type foop struct {
22-
Foo *gdttypes.FlexStrings `yaml:"foo"`
21+
Foo *api.FlexStrings `yaml:"foo"`
2322
}
2423

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

3332
require.NotNil(err)
34-
assert.ErrorIs(err, gdterrors.ErrExpectedScalarOrSequence)
33+
assert.ErrorIs(err, api.ErrExpectedScalarOrSequence)
3534
}
3635

3736
func TestFlexStrings(t *testing.T) {

types/plugin.go renamed to api/plugin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package types
5+
package api
66

77
import "gopkg.in/yaml.v3"
88

result/result.go renamed to api/result.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package result
5+
package api
66

77
// Result is returned from a `Evaluable.Eval` execution. It serves two
88
// purposes:
@@ -80,8 +80,8 @@ func WithFailures(failures ...error) ResultModifier {
8080
}
8181
}
8282

83-
// New returns a new Result
84-
func New(mods ...ResultModifier) *Result {
83+
// NewResult returns a new Result
84+
func NewResult(mods ...ResultModifier) *Result {
8585
r := &Result{}
8686
for _, mod := range mods {
8787
mod(r)

types/retry.go renamed to api/retry.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// See the COPYING file in the root project directory for full text.
44

5-
package types
5+
package api
66

77
import (
88
"time"

0 commit comments

Comments
 (0)