Skip to content

Commit d800f2c

Browse files
committed
feat(bdd): api enhancement
1 parent 02d7b8d commit d800f2c

File tree

17 files changed

+343
-121
lines changed

17 files changed

+343
-121
lines changed

datauri/data_uri.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
"strings"
1010
)
1111

12-
var (
13-
ErrInvalidDataURI = errors.New("invalid data uri")
14-
)
12+
var ErrInvalidDataURI = errors.New("invalid data uri")
1513

1614
func Parse(dataURI string) (*DataURI, error) {
1715
withoutPrefix := ""

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ tool mvdan.cc/gofumpt
77
require (
88
github.com/go-json-experiment/json v0.0.0-20251027170946-4849db3c2f7e
99
github.com/google/go-cmp v0.7.0
10-
golang.org/x/sync v0.17.0
11-
golang.org/x/text v0.30.0
12-
golang.org/x/tools v0.38.0
10+
golang.org/x/sync v0.19.0
11+
golang.org/x/text v0.33.0
12+
golang.org/x/tools v0.41.0
1313
)
1414

1515
require (
16-
golang.org/x/mod v0.29.0 // indirect
16+
golang.org/x/mod v0.32.0 // indirect
1717
mvdan.cc/gofumpt v0.9.2 // indirect
1818
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,21 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
1212
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
1313
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
1414
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
15+
golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c=
16+
golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU=
1517
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
1618
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
19+
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
20+
golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
1721
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
1822
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
1923
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
2024
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
25+
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
26+
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
2127
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
2228
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
29+
golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc=
30+
golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg=
2331
mvdan.cc/gofumpt v0.9.2 h1:zsEMWL8SVKGHNztrx6uZrXdp7AX8r421Vvp23sz7ik4=
2432
mvdan.cc/gofumpt v0.9.2/go.mod h1:iB7Hn+ai8lPvofHd9ZFGVg2GOr8sBUw1QUWjNbmIL/s=

testing/bdd/bdd.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ import (
66
)
77

88
type TB interface {
9+
TempDir() string
10+
911
Chdir(dir string)
1012
Setenv(key, value string)
1113

1214
Skip(args ...any)
1315
Skipped() bool
1416

17+
Fatal(args ...any)
18+
1519
Context() context.Context
20+
21+
Cleanup(func())
22+
}
23+
24+
type WithHelper interface {
25+
Helper()
1626
}
1727

1828
type T interface {

testing/bdd/bdd_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"testing"
55

66
"github.com/octohelm/x/testing/bdd"
7+
"github.com/octohelm/x/testing/snapshot"
78
)
89

910
func TestFeature(t *testing.T) {
10-
t.Run("case 1", bdd.ScenarioT(func(t bdd.T) {
11-
t.Given("initial value with 1", func(t bdd.T) {
11+
t.Run("case 1", bdd.ScenarioT(func(b bdd.T) {
12+
b.Given("initial value with 1", func(t bdd.T) {
1213
value := 1
1314

1415
t.When("add 1", func(c bdd.T) {
@@ -32,4 +33,16 @@ func TestFeature(t *testing.T) {
3233
})
3334
})
3435
}))
36+
37+
t.Run("snapshot", bdd.GivenT(func(b bdd.T) {
38+
b.Then("match",
39+
bdd.EqualDoValue(
40+
bdd.Snapshot("test"),
41+
func() (*snapshot.Snapshot, error) {
42+
return snapshot.Files(
43+
snapshot.FileFromRaw("x.txt", []byte("1231")),
44+
), nil
45+
}),
46+
)
47+
}))
3548
}

testing/bdd/checkers.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"testing"
99

1010
"github.com/octohelm/x/testing/internal"
11-
"github.com/octohelm/x/testing/snapshot"
1211
)
1312

1413
func SliceHaveLen[Slice ~[]E, E any](expect int, actual Slice) Checker {
@@ -37,7 +36,7 @@ func ErrorIs(expect error, err error) Checker {
3736
}
3837

3938
func HasError(err error) Checker {
40-
matcher := internal.NewMatcher[error]("hash error", func(e error) bool {
39+
matcher := internal.NewMatcher[error]("has error", func(e error) bool {
4140
return e != nil
4241
})
4342
return asChecker(matcher, err)
@@ -87,10 +86,6 @@ func NotEqual[V any](expect V, actual V) Checker {
8786
return asChecker(matcher, actual)
8887
}
8988

90-
func MatchSnapshot(build func(s *snapshot.Snapshot), snapshotName string) Checker {
91-
return asChecker(snapshot.Match(snapshotName), Build(build))
92-
}
93-
9489
func asChecker[T any](matcher internal.Matcher[T], actual T) Checker {
9590
return &checker[T]{
9691
Matcher: matcher,
@@ -109,6 +104,7 @@ func (c *checker[T]) Check(t TB) {
109104
case interface{ Unwrap() *testing.T }:
110105
tt := x.Unwrap()
111106
tt.Helper()
107+
112108
internal.Expect(tt, c.actual, c.Matcher)
113109
}
114110
}

testing/bdd/do.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package bdd
2+
3+
func Do(t TB, action func() error, args ...any) {
4+
if x, ok := t.(WithHelper); ok {
5+
x.Helper()
6+
}
7+
8+
if err := action(); err != nil {
9+
if len(args) > 0 {
10+
t.Fatal(append([]any{err}, args...)...)
11+
} else {
12+
t.Fatal(err)
13+
}
14+
}
15+
}
16+
17+
func DoValue[T any](t TB, action func() (T, error), args ...any) T {
18+
if x, ok := t.(WithHelper); ok {
19+
x.Helper()
20+
}
21+
22+
x, err := action()
23+
if err != nil {
24+
if len(args) > 0 {
25+
t.Fatal(append([]any{err}, args...)...)
26+
} else {
27+
t.Fatal(err)
28+
}
29+
}
30+
return x
31+
}
32+
33+
func DoValues[A any, B any](t TB, action func() (A, B, error), args ...any) (A, B) {
34+
if x, ok := t.(WithHelper); ok {
35+
x.Helper()
36+
}
37+
38+
a, b, err := action()
39+
if err != nil {
40+
if len(args) > 0 {
41+
t.Fatal(append([]any{err}, args...)...)
42+
} else {
43+
t.Fatal(err)
44+
}
45+
}
46+
return a, b
47+
}

testing/bdd/do_checkers.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package bdd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/octohelm/x/testing/internal"
7+
)
8+
9+
func NotEqualDoValue[V any](expect V, do func() (V, error)) Checker {
10+
matcher := internal.NotEqual(expect)
11+
return asDoValueChecker(matcher, do)
12+
}
13+
14+
func EqualDoValue[V any](expect V, do func() (V, error)) Checker {
15+
matcher := internal.Equal(expect)
16+
return asDoValueChecker(matcher, do)
17+
}
18+
19+
func asDoValueChecker[T any](matcher internal.Matcher[T], do func() (T, error)) Checker {
20+
return &doValueChecker[T]{
21+
Matcher: matcher,
22+
do: do,
23+
}
24+
}
25+
26+
type doValueChecker[T any] struct {
27+
internal.Matcher[T]
28+
29+
do func() (T, error)
30+
}
31+
32+
func (c *doValueChecker[T]) Check(t TB) {
33+
switch x := t.(type) {
34+
case interface{ Unwrap() *testing.T }:
35+
tt := x.Unwrap()
36+
tt.Helper()
37+
38+
actual, err := c.do()
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
internal.Expect(tt, actual, c.Matcher)
44+
}
45+
}

testing/bdd/helper.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bdd
22

33
import (
44
"iter"
5+
"testing"
56
)
67

78
func Cases[C any](cases ...C) iter.Seq[C] {
@@ -17,3 +18,9 @@ func Build[T any](build func(v *T)) *T {
1718
build(v)
1819
return v
1920
}
21+
22+
func Then(t *testing.T, summary string, checkers ...Checker) {
23+
t.Helper()
24+
25+
FromT(t).Then(summary, checkers...)
26+
}

testing/bdd/must.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ package bdd
22

33
import "encoding"
44

5+
func Must[T any](v T, err error) T {
6+
if err != nil {
7+
panic(err)
8+
}
9+
return v
10+
}
11+
12+
// MustDo
13+
// Deprecated use DoValue instead
514
func MustDo[T any](action func() (T, error)) T {
615
x, err := action()
716
if err != nil {
@@ -10,6 +19,8 @@ func MustDo[T any](action func() (T, error)) T {
1019
return x
1120
}
1221

22+
// MustDo2
23+
// Deprecated use DoValues instead
1324
func MustDo2[A any, B any](action func() (A, B, error)) (A, B) {
1425
a, b, err := action()
1526
if err != nil {
@@ -18,13 +29,6 @@ func MustDo2[A any, B any](action func() (A, B, error)) (A, B) {
1829
return a, b
1930
}
2031

21-
func Must[T any](v T, err error) T {
22-
if err != nil {
23-
panic(err)
24-
}
25-
return v
26-
}
27-
2832
func Must2[A any, B any](a A, b B, err error) (A, B) {
2933
if err != nil {
3034
panic(err)

0 commit comments

Comments
 (0)