Skip to content

Commit ce63abd

Browse files
authored
Merge pull request #758 from gopcua/issue-757-stretchr-testify
Issue #757: use stretchr/testify/require
2 parents 74c8438 + a246812 commit ce63abd

30 files changed

+370
-761
lines changed

client_test.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,19 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/pascaldekloe/goe/verify"
8-
97
"github.com/gopcua/opcua/id"
108
"github.com/gopcua/opcua/ua"
9+
"github.com/stretchr/testify/require"
1110
)
1211

1312
func TestClient_Send_DoesNotPanicWhenDisconnected(t *testing.T) {
1413
c, err := NewClient("opc.tcp://example.com:4840")
15-
if err != nil {
16-
t.Fatal(err)
17-
}
14+
require.NoError(t, err, "NewClient failed")
15+
1816
err = c.Send(context.Background(), &ua.ReadRequest{}, func(i ua.Response) error {
1917
return nil
2018
})
21-
verify.Values(t, "", err, ua.StatusBadServerNotConnected)
19+
require.Equal(t, ua.StatusBadServerNotConnected, err)
2220
}
2321

2422
func TestCloneReadRequest(t *testing.T) {
@@ -100,7 +98,7 @@ func TestCloneReadRequest(t *testing.T) {
10098
for _, tt := range tests {
10199
t.Run(tt.name, func(t *testing.T) {
102100
got := cloneReadRequest(tt.req)
103-
verify.Values(t, "", got, tt.want)
101+
require.Equal(t, tt.want, got)
104102
})
105103
}
106104
}
@@ -181,7 +179,7 @@ func TestCloneBrowseRequest(t *testing.T) {
181179
for _, tt := range tests {
182180
t.Run(tt.name, func(t *testing.T) {
183181
got := cloneBrowseRequest(tt.req)
184-
verify.Values(t, "", got, tt.want)
182+
require.Equal(t, tt.want, got)
185183
})
186184
}
187185
}

config_test.go

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ import (
1212
"testing"
1313
"time"
1414

15-
"github.com/pascaldekloe/goe/verify"
16-
1715
"github.com/gopcua/opcua/ua"
1816
"github.com/gopcua/opcua/uacp"
1917
"github.com/gopcua/opcua/uapolicy"
2018
"github.com/gopcua/opcua/uasc"
19+
"github.com/stretchr/testify/require"
2120
)
2221

2322
// test certificate generated with
@@ -129,11 +128,7 @@ func TestOptions(t *testing.T) {
129128
randomRequestID = func() uint32 { return 125 }
130129
defer func() { randomRequestID = nil }()
131130

132-
d, err := os.MkdirTemp("", "gopcua")
133-
if err != nil {
134-
t.Fatal(err)
135-
}
136-
defer os.RemoveAll(d)
131+
d := t.TempDir()
137132

138133
var (
139134
certDERFile = filepath.Join(d, "cert.der")
@@ -152,18 +147,17 @@ func TestOptions(t *testing.T) {
152147
}
153148
}
154149

155-
if err := os.WriteFile(certDERFile, certDER, 0644); err != nil {
156-
t.Fatal(err)
157-
}
158-
if err := os.WriteFile(certPEMFile, certPEM, 0644); err != nil {
159-
t.Fatal(err)
160-
}
161-
if err := os.WriteFile(keyDERFile, keyDER, 0644); err != nil {
162-
t.Fatal(err)
163-
}
164-
if err := os.WriteFile(keyPEMFile, keyPEM, 0644); err != nil {
165-
t.Fatal(err)
166-
}
150+
err := os.WriteFile(certDERFile, certDER, 0644)
151+
require.NoError(t, err, "WriteFile(certDERFile) failed")
152+
153+
err = os.WriteFile(certPEMFile, certPEM, 0644)
154+
require.NoError(t, err, "WriteFile(certPEMFile) failed")
155+
156+
err = os.WriteFile(keyDERFile, keyDER, 0644)
157+
require.NoError(t, err, "WriteFile(keyDERFile) failed")
158+
159+
err = os.WriteFile(keyPEMFile, keyPEM, 0644)
160+
require.NoError(t, err, "WriteFile(keyPEMFile) failed")
167161
defer os.Remove(keyPEMFile)
168162

169163
tests := []struct {
@@ -814,15 +808,10 @@ func TestOptions(t *testing.T) {
814808

815809
cfg, err := ApplyConfig(tt.opt)
816810
if got, want := errstr(err), errstr(tt.err); got != "" || want != "" {
817-
if got != want {
818-
t.Fatalf("got error %q want %q", got, want)
819-
}
811+
require.Equal(t, want, got, "got error %q want %q", got, want)
820812
return
821813
}
822-
if !verify.Values(t, "", cfg, tt.cfg) {
823-
t.Logf("got %#v", cfg)
824-
t.Logf("want %#v", tt.cfg)
825-
}
814+
require.Equal(t, tt.cfg, cfg)
826815
})
827816
}
828817
}

errors/errors_test.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package errors
22

3-
import "testing"
3+
import (
4+
"errors"
5+
"testing"
46

5-
func TestErrors(t *testing.T) {
6-
err := Errorf("hello %s", "world")
7-
if err.Error() != "opcua: hello world" {
8-
t.Fatalf("got %s, wanted %s", err.Error(), "opcua: hello world")
9-
}
10-
11-
err = New("hello")
12-
if err.Error() != "opcua: hello" {
13-
t.Fatalf("got %s, wanted %s", err.Error(), "opcua: hello")
14-
}
7+
"github.com/stretchr/testify/require"
8+
)
159

16-
err = New("hello %s")
17-
if err.Error() != "opcua: hello %s" {
18-
t.Fatalf("got %s, wanted %s", err.Error(), "opcua: %s")
19-
}
10+
func TestErrors(t *testing.T) {
11+
t.Run("expand", func(t *testing.T) {
12+
err := Errorf("hello %s", "world")
13+
require.Error(t, err, errors.New("opcua: hello world"))
14+
})
15+
t.Run("simple", func(t *testing.T) {
16+
err := New("hello")
17+
require.Error(t, err, errors.New("opcua: hello"))
18+
})
19+
t.Run("parameter", func(t *testing.T) {
20+
err := New("hello %s")
21+
require.Error(t, err, errors.New("opcua: hello %s"))
22+
})
2023
}

go.mod

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ module github.com/gopcua/opcua
33
go 1.22.0
44

55
require (
6-
github.com/google/uuid v1.3.0
7-
github.com/pascaldekloe/goe v0.1.1
6+
github.com/google/uuid v1.6.0
87
github.com/stretchr/testify v1.10.0
98
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d
10-
golang.org/x/term v0.8.0
9+
golang.org/x/term v0.27.0
1110
)
1211

1312
require (
1413
github.com/davecgh/go-spew v1.1.1 // indirect
1514
github.com/pmezard/go-difflib v1.0.0 // indirect
16-
golang.org/x/sys v0.8.0 // indirect
15+
golang.org/x/sys v0.28.0 // indirect
1716
gopkg.in/yaml.v3 v3.0.1 // indirect
1817
)
1918

go.sum

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
4-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5-
github.com/pascaldekloe/goe v0.1.1 h1:Ah6WQ56rZONR3RW3qWa2NCZ6JAVvSpUcoLBaOmYFt9Q=
6-
github.com/pascaldekloe/goe v0.1.1/go.mod h1:KSyfaxQOh0HZPjDP1FL/kFtbqYqrALJTaMafFUIccqU=
3+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
75
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
86
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
97
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
108
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
119
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0=
1210
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
13-
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
14-
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15-
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
16-
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
11+
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
12+
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
13+
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
14+
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
1715
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1816
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1917
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

stats/stats_test.go

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

99
"github.com/gopcua/opcua/ua"
10-
"github.com/pascaldekloe/goe/verify"
10+
"github.com/stretchr/testify/require"
1111
)
1212

1313
func newExpVarInt(i int64) *expvar.Int {
@@ -20,13 +20,13 @@ func TestConvienienceFuncs(t *testing.T) {
2020
Reset()
2121

2222
Client().Add("a", 1)
23-
verify.Values(t, "", Client().Get("a"), newExpVarInt(1))
23+
require.Equal(t, newExpVarInt(1), Client().Get("a"))
2424

2525
Error().Add("b", 2)
26-
verify.Values(t, "", Error().Get("b"), newExpVarInt(2))
26+
require.Equal(t, newExpVarInt(2), Error().Get("b"))
2727

2828
Subscription().Add("c", 3)
29-
verify.Values(t, "", Subscription().Get("c"), newExpVarInt(3))
29+
require.Equal(t, newExpVarInt(3), Subscription().Get("c"))
3030
}
3131

3232
func TestRecordError(t *testing.T) {
@@ -46,8 +46,7 @@ func TestRecordError(t *testing.T) {
4646
t.Run(tt.key, func(t *testing.T) {
4747
s := NewStats()
4848
s.RecordError(tt.err)
49-
got, want := s.Error.Get(tt.key), newExpVarInt(1)
50-
verify.Values(t, "", got, want)
49+
require.Equal(t, newExpVarInt(1), s.Error.Get(tt.key))
5150
})
5251
}
5352
}

tests/go/namespace_test.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/pascaldekloe/goe/verify"
12-
1311
"github.com/gopcua/opcua"
1412
"github.com/gopcua/opcua/ua"
13+
"github.com/stretchr/testify/require"
1514
)
1615

1716
func TestNamespace(t *testing.T) {
@@ -21,41 +20,32 @@ func TestNamespace(t *testing.T) {
2120
defer srv.Close()
2221

2322
c, err := opcua.NewClient("opc.tcp://localhost:4840", opcua.SecurityMode(ua.MessageSecurityModeNone))
24-
if err != nil {
25-
t.Fatal(err)
26-
}
27-
if err := c.Connect(ctx); err != nil {
28-
t.Fatal(err)
29-
}
23+
require.NoError(t, err, "NewClient failed")
24+
25+
err = c.Connect(ctx)
26+
require.NoError(t, err, "Connect failed")
3027
defer c.Close(ctx)
3128

3229
time.Sleep(2 * time.Second)
3330

3431
t.Run("NamespaceArray", func(t *testing.T) {
3532
got, err := c.NamespaceArray(ctx)
36-
if err != nil {
37-
t.Fatal(err)
38-
}
33+
require.NoError(t, err, "NamespaceArray failed")
34+
3935
want := []string{
4036
"http://opcfoundation.org/UA/",
4137
"NodeNamespace",
4238
"http://gopcua.com/",
4339
}
44-
verify.Values(t, "", got, want)
40+
require.Equal(t, want, got, "NamespaceArray not equal")
4541
})
4642
t.Run("FindNamespace", func(t *testing.T) {
4743
ns, err := c.FindNamespace(ctx, "http://gopcua.com/")
48-
if err != nil {
49-
t.Fatal(err)
50-
}
51-
if got, want := ns, uint16(2); got != want {
52-
t.Fatalf("got namespace id %d want %d", got, want)
53-
}
44+
require.NoError(t, err, "FindNamespace failed")
45+
require.Equal(t, uint16(2), ns, "namespace id not equal")
5446
})
5547
t.Run("UpdateNamespaces", func(t *testing.T) {
5648
err := c.UpdateNamespaces(ctx)
57-
if err != nil {
58-
t.Fatal(err)
59-
}
49+
require.NoError(t, err, "UpdateNamespaces failed")
6050
})
6151
}

tests/go/read_test.go

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/pascaldekloe/goe/verify"
12-
1311
"github.com/gopcua/opcua"
1412
"github.com/gopcua/opcua/ua"
13+
"github.com/stretchr/testify/require"
1514
)
1615

1716
// TestRead performs an integration test to read values
@@ -38,12 +37,10 @@ func TestRead(t *testing.T) {
3837
time.Sleep(2 * time.Second)
3938

4039
c, err := opcua.NewClient("opc.tcp://localhost:4840", opcua.SecurityMode(ua.MessageSecurityModeNone))
41-
if err != nil {
42-
t.Fatal(err)
43-
}
44-
if err := c.Connect(ctx); err != nil {
45-
t.Fatal(err)
46-
}
40+
require.NoError(t, err, "NewClient failed")
41+
42+
err = c.Connect(ctx)
43+
require.NoError(t, err, "Connect failed")
4744
defer c.Close(ctx)
4845

4946
for _, tt := range tests {
@@ -68,15 +65,9 @@ func testRead(t *testing.T, ctx context.Context, c *opcua.Client, v interface{},
6865
},
6966
TimestampsToReturn: ua.TimestampsToReturnBoth,
7067
})
71-
if err != nil {
72-
t.Fatalf("Read failed: %s", err)
73-
}
74-
if resp.Results[0].Status != ua.StatusOK {
75-
t.Fatalf("Status not OK: %v", resp.Results[0].Status)
76-
}
77-
if got, want := resp.Results[0].Value.Value(), v; !verify.Values(t, "", got, want) {
78-
t.Fail()
79-
}
68+
require.NoError(t, err, "Read failed")
69+
require.Equal(t, ua.StatusOK, resp.Results[0].Status, "Status not OK")
70+
require.Equal(t, v, resp.Results[0].Value.Value(), "Results[0].Value not equal")
8071
}
8172

8273
func testRegisteredRead(t *testing.T, ctx context.Context, c *opcua.Client, v interface{}, id *ua.NodeID) {
@@ -85,9 +76,7 @@ func testRegisteredRead(t *testing.T, ctx context.Context, c *opcua.Client, v in
8576
resp, err := c.RegisterNodes(ctx, &ua.RegisterNodesRequest{
8677
NodesToRegister: []*ua.NodeID{id},
8778
})
88-
if err != nil {
89-
t.Fatalf("RegisterNodes failed: %s", err)
90-
}
79+
require.NoError(t, err, "RegisterNodes failed")
9180

9281
testRead(t, ctx, c, v, resp.RegisteredNodeIDs[0])
9382
testRead(t, ctx, c, v, resp.RegisteredNodeIDs[0])
@@ -98,7 +87,5 @@ func testRegisteredRead(t *testing.T, ctx context.Context, c *opcua.Client, v in
9887
_, err = c.UnregisterNodes(ctx, &ua.UnregisterNodesRequest{
9988
NodesToUnregister: []*ua.NodeID{id},
10089
})
101-
if err != nil {
102-
t.Fatalf("UnregisterNodes failed: %s", err)
103-
}
90+
require.NoError(t, err, "UnregisterNodes failed")
10491
}

0 commit comments

Comments
 (0)