Skip to content

Commit 9bda2cf

Browse files
committed
doc: update docs.
1 parent 7103c6f commit 9bda2cf

File tree

6 files changed

+352
-1
lines changed

6 files changed

+352
-1
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ func TestExample(t *testing.T) {
3131
// assert inequality
3232
assert.NotEqual(t, actual, expect)
3333

34+
// you can also use DeepEqual to assert the equality that also checks the type between the values
35+
assert.DeepEqual(t, actual, expect)
36+
3437
// var object
3538

3639
// assert for nil
@@ -41,6 +44,30 @@ func TestExample(t *testing.T) {
4144
}
4245
```
4346

47+
You can use `True` method to check whether a value is truthy or falsy (is the zero value of the type or not).
48+
49+
```go
50+
func TestExample(t *testing.T) {
51+
assert.True(t, 1) // success
52+
assert.True(t, 0) // fail
53+
assert.True(t, "test") // success
54+
assert.True(t, "") // fail
55+
}
56+
```
57+
58+
If you want to test the value of a string, you can use `Match` method to test it with a regular expression pattern.
59+
60+
```go
61+
func TestExample(t *testing.T) {
62+
pattern := regexp.MustCompile(`^https?:\/\/`)
63+
assert.Match(t, "https://example.com", pattern) // success
64+
assert.Match(t, "example.com", pattern) // fail
65+
66+
// you can also use `MatchString` to test it without compiling the regexp pattern
67+
assert.MatchString(t, "https://example.com", `^https?:\/\/`) // success
68+
}
69+
```
70+
4471
It also provided assertion functions to verify a function will panic or not:
4572

4673
```go
@@ -71,7 +98,7 @@ func TestExample(t *testing.T) {
7198
// The following line will set the test result to fail and stop the execution
7299
assert.EqualNow(t, actual, expect)
73100

74-
// The following lines will never execute if they are not deep equal.
101+
// The following lines will never execute if they are not equal.
75102
// ...
76103
}
77104
```

assertion.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import (
44
"testing"
55
)
66

7+
// Assertion is the extension of the Go builtin `testing.T`.
78
type Assertion struct {
89
*testing.T
910
}
1011

1112
// New returns an assertion instance for verifying invariants.
13+
//
14+
// assertion := assert.New(t)
15+
// assertion.Equal(actual, expect)
16+
// // ...
1217
func New(t *testing.T) *Assertion {
1318
a := new(Assertion)
1419

@@ -26,6 +31,11 @@ func New(t *testing.T) *Assertion {
2631
//
2732
// Run may be called simultaneously from multiple goroutines, but all such calls
2833
// must return before the outer test function for a returns.
34+
//
35+
// assertion := assert.New(t)
36+
// assertion.Run("SubTest", func (a *assert.Assertion) bool {
37+
// // TODO...
38+
// })
2939
func (assertion *Assertion) Run(name string, f func(a *Assertion)) bool {
3040
return assertion.T.Run(name, func(t *testing.T) {
3141
subAssertion := New(t)

0 commit comments

Comments
 (0)