Skip to content

Commit

Permalink
require: finish package ua
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Dec 16, 2024
1 parent 645ad93 commit 6bcb738
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 173 deletions.
29 changes: 9 additions & 20 deletions ua/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package ua

import (
"bytes"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"
)

type A struct {
Expand Down Expand Up @@ -384,32 +385,22 @@ func TestCodec(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if reflect.ValueOf(tt.v).Kind() != reflect.Ptr {
t.Fatalf("%T is not a pointer", tt.v)
}
require.Equal(t, reflect.Ptr, reflect.ValueOf(tt.v).Kind(), "%T is not a pointer", tt.v)

t.Run("decode", func(t *testing.T) {
// create a new instance of the same type as tt.v
// v then contains a pointer to the new instance
typ := reflect.ValueOf(tt.v).Type()
v := reflect.New(typ.Elem())

if _, err := Decode(tt.b, v.Interface()); err != nil {
t.Fatal(err)
}

if got, want := v.Interface(), tt.v; !reflect.DeepEqual(got, want) {
t.Fatalf("got %#v, want %#v", got, want)
}
_, err := Decode(tt.b, v.Interface())
require.NoError(t, err, "Decode failed")
require.Equal(t, tt.v, v.Interface(), "Decoded payload not equal")
})
t.Run("encode", func(t *testing.T) {
b, err := Encode(tt.v)
if err != nil {
t.Fatal(err)
}
if got, want := b, tt.b; !bytes.Equal(got, want) {
t.Fatalf("\ngot %#v\nwant %#v", got, want)
}
require.NoError(t, err, "Encode failed")
require.Equal(t, tt.b, b, "Encoded payload not equal")
})
})
}
Expand All @@ -426,7 +417,5 @@ func TestFailDecodeArray(t *testing.T) {
}
var a [2]int32
_, err := Decode(b, &a)
if err == nil {
t.Fatalf("was expecting error for tryig to decode a stream of bytes with length 3 into an array of size 2")
}
require.Error(t, err, "was expecting error for tryig to decode a stream of bytes with length 3 into an array of size 2")
}
10 changes: 3 additions & 7 deletions ua/expanded_node_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ package ua

import (
"math"
"reflect"
"testing"

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

func TestExpandedNodeID(t *testing.T) {
Expand Down Expand Up @@ -101,12 +101,8 @@ func TestParseExpandedNodeID(t *testing.T) {
for _, c := range cases {
t.Run(c.s, func(t *testing.T) {
n, err := ParseExpandedNodeID(c.s, c.ns)
if got, want := err, c.err; !errors.Equal(got, want) {
t.Fatalf("got error %v want %v", got, want)
}
if got, want := n, c.n; !reflect.DeepEqual(got, want) {
t.Fatalf("\ngot %#v\nwant %#v", got, want)
}
require.Equal(t, c.err, err, "Errors not equal")
require.Equal(t, c.n, n, "ExpandedNodeID not equal")
})
}
}
118 changes: 36 additions & 82 deletions ua/node_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"encoding/base64"
"encoding/json"
"math"
"reflect"
"testing"

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

func TestNodeID(t *testing.T) {
Expand Down Expand Up @@ -173,12 +173,8 @@ func TestParseNodeID(t *testing.T) {
for _, c := range cases {
t.Run(c.s, func(t *testing.T) {
n, err := ParseNodeID(c.s)
if got, want := err, c.err; !errors.Equal(got, want) {
t.Fatalf("got error %v want %v", got, want)
}
if got, want := n, c.n; !reflect.DeepEqual(got, want) {
t.Fatalf("\ngot %#v\nwant %#v", got, want)
}
require.Equal(t, c.err, err, "Error not equal")
require.Equal(t, c.n, n, "Parsed NodeID not equal")
})
}
}
Expand All @@ -197,9 +193,7 @@ func TestStringID(t *testing.T) {

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got, want := c.n.String(), c.s; got != want {
t.Fatalf("got %s want %s", got, want)
}
require.Equal(t, c.s, c.n.String())
})
}
}
Expand Down Expand Up @@ -266,22 +260,17 @@ func TestSetIntID(t *testing.T) {
v := tt.n.IntID()

// sanity check
if before, after := v, tt.v; before == after {
t.Fatalf("before == after: %d == %d", before, after)
}
require.NotEqual(t, tt.v, v, "before == after: %d == %d", v, tt.v)

err := tt.n.SetIntID(tt.v)
if got, want := err, tt.err; !errors.Equal(got, want) {
t.Fatalf("got error %v want %v", got, want)
}
require.Equal(t, tt.err, err)

// if the test should fail and the error was correct
// we need to stop here.
if tt.err != nil {
return
}
if got, want := tt.n.IntID(), tt.v; got != want {
t.Fatalf("got value %d want %d", got, want)
}
require.Equal(t, tt.v, tt.n.IntID(), "IntID not equal")
})
}
}
Expand Down Expand Up @@ -342,22 +331,17 @@ func TestSetStringID(t *testing.T) {
v := tt.n.StringID()

// sanity check
if before, after := v, tt.v; before == after {
t.Fatalf("before == after: %s == %s", before, after)
}
require.NotEqual(t, tt.v, v, "before == after: %s == %s", v, tt.v)

err := tt.n.SetStringID(tt.v)
if got, want := err, tt.err; !errors.Equal(got, want) {
t.Fatalf("got error %q (%T) want %q (%T)", got, got, want, want)
}
require.Equal(t, tt.err, err)

// if the test should fail and the error was correct
// we need to stop here.
if tt.err != nil {
return
}
if got, want := tt.n.StringID(), tt.v; got != want {
t.Fatalf("got value %s want %s", got, want)
}
require.Equal(t, tt.v, tt.n.StringID())
})
}
}
Expand Down Expand Up @@ -404,82 +388,58 @@ func TestSetNamespace(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.n.SetNamespace(tt.v)
if got, want := err, tt.err; !errors.Equal(got, want) {
t.Fatalf("got error %v want %v", got, want)
}
require.Equal(t, tt.err, err)

// if the test should fail and the error was correct
// we need to stop here.
if tt.err != nil {
return
}
if got, want := tt.n.Namespace(), tt.v; got != want {
t.Fatalf("got value %d want %d", got, want)
}
require.Equal(t, tt.v, tt.n.Namespace())
})
}
}

func TestNodeIDJSON(t *testing.T) {
t.Run("value", func(t *testing.T) {
n, err := ParseNodeID(`ns=4;s=abc`)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)

b, err := json.Marshal(n)
if err != nil {
t.Fatal(err)
}
if got, want := string(b), `"ns=4;s=abc"`; got != want {
t.Fatalf("got %s want %s", got, want)
}
require.NoError(t, err)
require.Equal(t, `"ns=4;s=abc"`, string(b))

var nn NodeID
if err := json.Unmarshal(b, &nn); err != nil {
t.Fatal(err)
}
if got, want := nn.String(), n.String(); got != want {
t.Fatalf("got %s want %s", got, want)
}
err = json.Unmarshal(b, &nn)
require.NoError(t, err)
require.Equal(t, n.String(), nn.String(), "NodeIDs not equal")
})

t.Run("nil", func(t *testing.T) {
var n *NodeID
b, err := json.Marshal(n)
if err != nil {
t.Fatal(err)
}
if got, want := string(b), "null"; got != want {
t.Fatalf("got %s want %s", got, want)
}
require.NoError(t, err)
require.Equal(t, "null", string(b))
})

type X struct{ N *NodeID }
t.Run("struct", func(t *testing.T) {
x := X{NewStringNodeID(4, "abc")}
b, err := json.Marshal(x)
if err != nil {
t.Fatal(err)
}
if got, want := string(b), `{"N":"ns=4;s=abc"}`; got != want {
t.Fatalf("got %s want %s", got, want)
}
require.NoError(t, err)
require.Equal(t, `{"N":"ns=4;s=abc"}`, string(b))
})

t.Run("nil struct", func(t *testing.T) {
var x X
b, err := json.Marshal(x)
if err != nil {
t.Fatal(err)
}
if got, want := string(b), `{"N":null}`; got != want {
t.Fatalf("got %s want %s", got, want)
}
require.NoError(t, err)
require.Equal(t, `{"N":null}`, string(b))

var xx X
if err := json.Unmarshal(b, &xx); err != nil {
t.Fatal(err)
}
if got, want := xx, x; !reflect.DeepEqual(got, want) {
t.Fatalf("got %s want %s", got, want)
}
err = json.Unmarshal(b, &xx)
require.NoError(t, err)
require.Equal(t, x, xx)
})
}

Expand Down Expand Up @@ -507,12 +467,8 @@ func TestNodeIDToString(t *testing.T) {
for _, tt := range tests {
t.Run(tt.s, func(t *testing.T) {
n, err := ParseNodeID(tt.s)
if err != nil {
t.Fatal(err)
}
if got, want := n.String(), tt.want; got != want {
t.Fatalf("got %s want %s", got, want)
}
require.NoError(t, err)
require.Equal(t, tt.want, n.String())
})
}
}
Expand Down Expand Up @@ -578,9 +534,7 @@ func TestNewNodeIDFromExpandedNodeID(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewNodeIDFromExpandedNodeID(tt.args.id); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewNodeIDFromExpandedNodeID() = %#v, want %#v", got, tt.want)
}
require.Equal(t, tt.want, NewNodeIDFromExpandedNodeID(tt.args.id))
})
}
}
Loading

0 comments on commit 6bcb738

Please sign in to comment.