Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #757: use stretchr/testify/require #758

Merged
merged 11 commits into from
Dec 20, 2024
14 changes: 6 additions & 8 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import (
"context"
"testing"

"github.com/pascaldekloe/goe/verify"

"github.com/gopcua/opcua/id"
"github.com/gopcua/opcua/ua"
"github.com/stretchr/testify/require"
)

func TestClient_Send_DoesNotPanicWhenDisconnected(t *testing.T) {
c, err := NewClient("opc.tcp://example.com:4840")
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NewClient failed")

err = c.Send(context.Background(), &ua.ReadRequest{}, func(i ua.Response) error {
return nil
})
verify.Values(t, "", err, ua.StatusBadServerNotConnected)
require.Equal(t, ua.StatusBadServerNotConnected, err)
}

func TestCloneReadRequest(t *testing.T) {
Expand Down Expand Up @@ -100,7 +98,7 @@ func TestCloneReadRequest(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := cloneReadRequest(tt.req)
verify.Values(t, "", got, tt.want)
require.Equal(t, tt.want, got)
})
}
}
Expand Down Expand Up @@ -181,7 +179,7 @@ func TestCloneBrowseRequest(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := cloneBrowseRequest(tt.req)
verify.Values(t, "", got, tt.want)
require.Equal(t, tt.want, got)
})
}
}
41 changes: 15 additions & 26 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (
"testing"
"time"

"github.com/pascaldekloe/goe/verify"

"github.com/gopcua/opcua/ua"
"github.com/gopcua/opcua/uacp"
"github.com/gopcua/opcua/uapolicy"
"github.com/gopcua/opcua/uasc"
"github.com/stretchr/testify/require"
)

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

d, err := os.MkdirTemp("", "gopcua")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(d)
d := t.TempDir()

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

if err := os.WriteFile(certDERFile, certDER, 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(certPEMFile, certPEM, 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(keyDERFile, keyDER, 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(keyPEMFile, keyPEM, 0644); err != nil {
t.Fatal(err)
}
err := os.WriteFile(certDERFile, certDER, 0644)
require.NoError(t, err, "WriteFile(certDERFile) failed")

err = os.WriteFile(certPEMFile, certPEM, 0644)
require.NoError(t, err, "WriteFile(certPEMFile) failed")

err = os.WriteFile(keyDERFile, keyDER, 0644)
require.NoError(t, err, "WriteFile(keyDERFile) failed")

err = os.WriteFile(keyPEMFile, keyPEM, 0644)
require.NoError(t, err, "WriteFile(keyPEMFile) failed")
defer os.Remove(keyPEMFile)

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

cfg, err := ApplyConfig(tt.opt)
if got, want := errstr(err), errstr(tt.err); got != "" || want != "" {
if got != want {
t.Fatalf("got error %q want %q", got, want)
}
require.Equal(t, want, got, "got error %q want %q", got, want)
return
}
if !verify.Values(t, "", cfg, tt.cfg) {
t.Logf("got %#v", cfg)
t.Logf("want %#v", tt.cfg)
}
require.Equal(t, tt.cfg, cfg)
})
}
}
33 changes: 18 additions & 15 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package errors

import "testing"
import (
"errors"
"testing"

func TestErrors(t *testing.T) {
err := Errorf("hello %s", "world")
if err.Error() != "opcua: hello world" {
t.Fatalf("got %s, wanted %s", err.Error(), "opcua: hello world")
}

err = New("hello")
if err.Error() != "opcua: hello" {
t.Fatalf("got %s, wanted %s", err.Error(), "opcua: hello")
}
"github.com/stretchr/testify/require"
)

err = New("hello %s")
if err.Error() != "opcua: hello %s" {
t.Fatalf("got %s, wanted %s", err.Error(), "opcua: %s")
}
func TestErrors(t *testing.T) {
t.Run("expand", func(t *testing.T) {
err := Errorf("hello %s", "world")
require.Error(t, err, errors.New("opcua: hello world"))
})
t.Run("simple", func(t *testing.T) {
err := New("hello")
require.Error(t, err, errors.New("opcua: hello"))
})
t.Run("parameter", func(t *testing.T) {
err := New("hello %s")
require.Error(t, err, errors.New("opcua: hello %s"))
})
}
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ module github.com/gopcua/opcua
go 1.22.0

require (
github.com/google/uuid v1.3.0
github.com/pascaldekloe/goe v0.1.1
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.10.0
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d
golang.org/x/term v0.8.0
golang.org/x/term v0.27.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/sys v0.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

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

"github.com/gopcua/opcua/ua"
"github.com/pascaldekloe/goe/verify"
"github.com/stretchr/testify/require"
)

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

Client().Add("a", 1)
verify.Values(t, "", Client().Get("a"), newExpVarInt(1))
require.Equal(t, newExpVarInt(1), Client().Get("a"))

Error().Add("b", 2)
verify.Values(t, "", Error().Get("b"), newExpVarInt(2))
require.Equal(t, newExpVarInt(2), Error().Get("b"))

Subscription().Add("c", 3)
verify.Values(t, "", Subscription().Get("c"), newExpVarInt(3))
require.Equal(t, newExpVarInt(3), Subscription().Get("c"))
}

func TestRecordError(t *testing.T) {
Expand All @@ -46,8 +46,7 @@ func TestRecordError(t *testing.T) {
t.Run(tt.key, func(t *testing.T) {
s := NewStats()
s.RecordError(tt.err)
got, want := s.Error.Get(tt.key), newExpVarInt(1)
verify.Values(t, "", got, want)
require.Equal(t, newExpVarInt(1), s.Error.Get(tt.key))
})
}
}
32 changes: 11 additions & 21 deletions tests/go/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
"testing"
"time"

"github.com/pascaldekloe/goe/verify"

"github.com/gopcua/opcua"
"github.com/gopcua/opcua/ua"
"github.com/stretchr/testify/require"
)

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

c, err := opcua.NewClient("opc.tcp://localhost:4840", opcua.SecurityMode(ua.MessageSecurityModeNone))
if err != nil {
t.Fatal(err)
}
if err := c.Connect(ctx); err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NewClient failed")

err = c.Connect(ctx)
require.NoError(t, err, "Connect failed")
defer c.Close(ctx)

time.Sleep(2 * time.Second)

t.Run("NamespaceArray", func(t *testing.T) {
got, err := c.NamespaceArray(ctx)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NamespaceArray failed")

want := []string{
"http://opcfoundation.org/UA/",
"NodeNamespace",
"http://gopcua.com/",
}
verify.Values(t, "", got, want)
require.Equal(t, want, got, "NamespaceArray not equal")
})
t.Run("FindNamespace", func(t *testing.T) {
ns, err := c.FindNamespace(ctx, "http://gopcua.com/")
if err != nil {
t.Fatal(err)
}
if got, want := ns, uint16(2); got != want {
t.Fatalf("got namespace id %d want %d", got, want)
}
require.NoError(t, err, "FindNamespace failed")
require.Equal(t, uint16(2), ns, "namespace id not equal")
})
t.Run("UpdateNamespaces", func(t *testing.T) {
err := c.UpdateNamespaces(ctx)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err, "UpdateNamespaces failed")
})
}
33 changes: 10 additions & 23 deletions tests/go/read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
"testing"
"time"

"github.com/pascaldekloe/goe/verify"

"github.com/gopcua/opcua"
"github.com/gopcua/opcua/ua"
"github.com/stretchr/testify/require"
)

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

c, err := opcua.NewClient("opc.tcp://localhost:4840", opcua.SecurityMode(ua.MessageSecurityModeNone))
if err != nil {
t.Fatal(err)
}
if err := c.Connect(ctx); err != nil {
t.Fatal(err)
}
require.NoError(t, err, "NewClient failed")

err = c.Connect(ctx)
require.NoError(t, err, "Connect failed")
defer c.Close(ctx)

for _, tt := range tests {
Expand All @@ -68,15 +65,9 @@ func testRead(t *testing.T, ctx context.Context, c *opcua.Client, v interface{},
},
TimestampsToReturn: ua.TimestampsToReturnBoth,
})
if err != nil {
t.Fatalf("Read failed: %s", err)
}
if resp.Results[0].Status != ua.StatusOK {
t.Fatalf("Status not OK: %v", resp.Results[0].Status)
}
if got, want := resp.Results[0].Value.Value(), v; !verify.Values(t, "", got, want) {
t.Fail()
}
require.NoError(t, err, "Read failed")
require.Equal(t, ua.StatusOK, resp.Results[0].Status, "Status not OK")
require.Equal(t, v, resp.Results[0].Value.Value(), "Results[0].Value not equal")
}

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

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