Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 474d585

Browse files
committed
Add comparable assertions
1 parent 8588959 commit 474d585

File tree

4 files changed

+107
-15
lines changed

4 files changed

+107
-15
lines changed

build/build.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"os/exec"
67

78
"github.com/goyek/goyek"
89
"github.com/mattn/go-shellwords"
@@ -19,10 +20,10 @@ func flow() *goyek.Flow {
1920

2021
test := flow.Register(taskTest())
2122
lint := flow.Register(taskLint())
22-
markdownlint := flow.Register(taskMarkdownLint())
2323
misspell := flow.Register(taskMisspell())
24+
markdownlint := flow.Register(taskMarkdownLint())
2425
all := flow.Register(taskAll(goyek.Deps{
25-
test, lint, markdownlint, misspell,
26+
test, lint, misspell, markdownlint,
2627
}))
2728

2829
flow.DefaultTask = all
@@ -50,11 +51,26 @@ func taskLint() goyek.Task {
5051
}
5152
}
5253

54+
func taskMisspell() goyek.Task {
55+
return goyek.Task{
56+
Name: "misspell",
57+
Usage: "misspell",
58+
Action: func(tf *goyek.TF) {
59+
Exec(tf, buildDir, "go install github.com/client9/misspell/cmd/misspell")
60+
Exec(tf, "", "misspell -error -locale=US *.md")
61+
},
62+
}
63+
}
64+
5365
func taskMarkdownLint() goyek.Task {
5466
return goyek.Task{
5567
Name: "markdownlint",
56-
Usage: "markdownlint-cli (requires docker)",
68+
Usage: "markdownlint-cli (uses docker)",
5769
Action: func(tf *goyek.TF) {
70+
if _, err := exec.LookPath("docker"); err != nil {
71+
tf.Skip(err)
72+
}
73+
5874
curDir, err := os.Getwd()
5975
if err != nil {
6076
tf.Fatal(err)
@@ -68,17 +84,6 @@ func taskMarkdownLint() goyek.Task {
6884
}
6985
}
7086

71-
func taskMisspell() goyek.Task {
72-
return goyek.Task{
73-
Name: "misspell",
74-
Usage: "misspell",
75-
Action: func(tf *goyek.TF) {
76-
Exec(tf, buildDir, "go install github.com/client9/misspell/cmd/misspell")
77-
Exec(tf, "", "misspell -error -locale=US *.md")
78-
},
79-
}
80-
}
81-
8287
func taskAll(deps goyek.Deps) goyek.Task {
8388
return goyek.Task{
8489
Name: "all",

f/comparable.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package f
2+
3+
// FluentComparable encapsulates assertions for comparable object.
4+
type FluentComparable[T comparable] struct {
5+
FluentObj[T]
6+
}
7+
8+
// Comparable is used for testing a comparable object.
9+
func Comparable[T comparable](got T) FluentComparable[T] {
10+
return FluentComparable[T]{FluentObj[T]{got}}
11+
}
12+
13+
// Equal tests if the objects are equal using == operator.
14+
func (x FluentComparable[T]) Equal(want T) FailureMessage {
15+
if x.Got == want {
16+
return ""
17+
}
18+
return "the objects are not equal"
19+
}
20+
21+
// NotEqual tests if the objects are equal using == operator.
22+
func (x FluentComparable[T]) NotEqual(want T) FailureMessage {
23+
if x.Got != want {
24+
return ""
25+
}
26+
return "the objects are equal"
27+
}

f/comparable_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package f_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pellared/fluentassert/f"
7+
)
8+
9+
func TestComparable(t *testing.T) {
10+
type A struct {
11+
Str string
12+
Bool bool
13+
}
14+
15+
t.Run("Equal", func(t *testing.T) {
16+
t.Run("Passed", func(t *testing.T) {
17+
want := A{Str: "string", Bool: true}
18+
got := A{Str: "string", Bool: true}
19+
msg := f.Comparable(got).Equal(want)
20+
assertPassed(t, msg)
21+
})
22+
t.Run("Failed", func(t *testing.T) {
23+
want := A{Str: "string", Bool: true}
24+
got := A{Str: "wrong", Bool: true}
25+
msg := f.Comparable(got).Equal(want)
26+
assertFailed(t, msg, "the objects are not equal")
27+
})
28+
t.Run("nil", func(t *testing.T) {
29+
var got *A
30+
msg := f.Comparable(got).Equal(nil)
31+
assertPassed(t, msg)
32+
})
33+
})
34+
35+
t.Run("NotEqual", func(t *testing.T) {
36+
t.Run("Passed", func(t *testing.T) {
37+
want := A{Str: "string", Bool: true}
38+
got := A{Str: "wrong", Bool: true}
39+
msg := f.Comparable(got).NotEqual(want)
40+
assertPassed(t, msg)
41+
})
42+
t.Run("Failed", func(t *testing.T) {
43+
want := A{Str: "string", Bool: true}
44+
got := A{Str: "string", Bool: true}
45+
msg := f.Comparable(got).NotEqual(want)
46+
assertFailed(t, msg, "the objects are equal")
47+
})
48+
t.Run("nil", func(t *testing.T) {
49+
var got *A
50+
msg := f.Comparable(got).NotEqual(nil)
51+
assertFailed(t, msg, "the objects are equal")
52+
})
53+
})
54+
55+
t.Run("has assertions from Obj", func(t *testing.T) {
56+
want := A{}
57+
got := f.Comparable(want).FluentObj.Got // type embedding done properly
58+
assertEqual(t, got, want)
59+
})
60+
}

f/obj.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package f
22

33
import "github.com/google/go-cmp/cmp"
44

5-
// FluentObj encapsulates assertions for any objet.
5+
// FluentObj encapsulates assertions for any object.
66
type FluentObj[T any] struct {
77
Got T
88
}

0 commit comments

Comments
 (0)