File tree Expand file tree Collapse file tree 4 files changed +122
-2
lines changed
Expand file tree Collapse file tree 4 files changed +122
-2
lines changed Original file line number Diff line number Diff line change 1+ // SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+ package assert_test
5+
6+ import (
7+ "fmt"
8+ "testing"
9+
10+ "github.com/go-openapi/testify/v2/assert"
11+ )
12+
13+ func ExampleAssertions_with_generics () {
14+ t := new (testing.T ) // normally provided by test
15+
16+ a := assert .New (t )
17+
18+ const expected = "hello"
19+ goodValue := "hello"
20+ wrongValue := "world"
21+
22+ r0 := a .Equal (expected , goodValue ) // classic reflect-based assertion
23+ fmt .Printf ("good value is %t\n " , r0 )
24+
25+ r1 := assert .EqualT (a .T (), expected , goodValue ) // usage with generic assertion
26+ fmt .Printf ("good value is %t\n " , r1 )
27+
28+ r2 := assert .EqualT (a .T (), expected , wrongValue )
29+ fmt .Printf ("wrong value is %t\n " , r2 )
30+
31+ // Output:
32+ // good value is true
33+ // good value is true
34+ // wrong value is false
35+ }
Original file line number Diff line number Diff line change 33
44package assertions
55
6- // Assertions provides assertion methods around the [T] interface.
6+ import "context"
7+
8+ var (
9+ _ T = & Assertions {}
10+ _ H = & Assertions {}
11+ _ failNower = & Assertions {}
12+ _ namer = & Assertions {}
13+ _ contextualizer = & Assertions {}
14+ )
15+
16+ // Assertions is the base type to provide assertion methods around the [T] interface.
17+ //
18+ // It implements [T] and exposes the other useful methods from [testing.T]:
19+ // [testing.T.Helper], [testing.T.FailNow] and [testing.T.Context].
20+ //
21+ // These methods are optional but useful in this package.
722type Assertions struct {
8- t T //nolint:unused // the internal version of this type doesn't use this field, but generated copies do.
23+ t T
24+ }
25+
26+ func New (t T ) * Assertions {
27+ return & Assertions {
28+ t : t ,
29+ }
30+ }
31+
32+ func (a * Assertions ) Errorf (format string , args ... any ) {
33+ a .t .Errorf (format , args ... )
34+ }
35+
36+ func (a * Assertions ) Helper () {
37+ if h , ok := a .t .(H ); ok {
38+ h .Helper ()
39+ }
40+ }
41+
42+ func (a * Assertions ) FailNow () {
43+ if f , ok := a .t .(failNower ); ok {
44+ f .FailNow ()
45+ }
46+ }
47+
48+ func (a * Assertions ) Name () string {
49+ if n , ok := a .t .(namer ); ok {
50+ return n .Name ()
51+ }
52+
53+ return ""
54+ }
55+
56+ func (a * Assertions ) Context () context.Context {
57+ if c , ok := a .t .(contextualizer ); ok {
58+ return c .Context ()
59+ }
60+
61+ return context .Background ()
962}
Original file line number Diff line number Diff line change @@ -18,4 +18,6 @@ func TestAssertionNew(t *testing.T) {
1818 if a .t == nil {
1919 FailNow (t , "assertion should contain a T" )
2020 }
21+
22+ // TODO: check methods
2123}
Original file line number Diff line number Diff line change 1+ // SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers
2+ // SPDX-License-Identifier: Apache-2.0
3+
4+ package require_test
5+
6+ import (
7+ "fmt"
8+ "testing"
9+
10+ "github.com/go-openapi/testify/v2/require"
11+ )
12+
13+ func ExampleAssertions_with_generics () {
14+ t := new (testing.T ) // normally provided by test
15+
16+ a := require .New (t )
17+
18+ const expected = "hello"
19+ goodValue := "hello"
20+
21+ a .Equal (expected , goodValue ) // classic reflect-based assertion
22+ fmt .Println ("good value" )
23+
24+ require .EqualT (a .T (), expected , goodValue ) // usage with generic assertion
25+ fmt .Println ("good value" )
26+
27+ // Output:
28+ // good value
29+ // good value
30+ }
You can’t perform that action at this time.
0 commit comments