Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Jul 4, 2019
1 parent 6b9a64b commit 290a608
Showing 1 changed file with 78 additions and 1 deletion.
79 changes: 78 additions & 1 deletion impl/impl_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package impl

import (
"github.com/crabmusket/gosunspec/spi"
"encoding/binary"
"math"
"testing"

sunspec "github.com/crabmusket/gosunspec"
"github.com/crabmusket/gosunspec/smdx"
"github.com/crabmusket/gosunspec/spi"
"github.com/crabmusket/gosunspec/typelabel"
)

func TestCompletePointInterface(t *testing.T) {
Expand All @@ -16,3 +22,74 @@ func TestCompleteDevice(t *testing.T) {
func TestCompleteArray(t *testing.T) {
_ = spi.ArraySPI((&array{}))
}

func TestNotImplemented(t *testing.T) {
cases := []struct {
e bool
v interface{}
}{
{false, float32(0)},
{true, math.Float32frombits(0x7fc00000)},
{false, int16(0)},
{false, int32(0)},
{false, int64(0)},
{true, int16(-0x8000)},
{true, int32(-0x80000000)},
{true, int64(-0x8000000000000000)},
{false, uint16(0)},
{false, uint32(0)},
{false, uint64(0)},
{true, uint16(0xFFFF)},
{true, uint32(0xFFFFFFFF)},
{true, uint64(0xFFFFFFFFFFFFFFFF)},
{false, sunspec.Ipaddr{0xFF, 0xFF, 0xFF, 0xFF}},
{false, sunspec.Ipv6addr{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
{true, sunspec.Ipaddr{0, 0, 0, 0}},
{true, sunspec.Ipv6addr{0, 0, 0, 0, 0, 0}},
{false, sunspec.Eui48{0, 0, 0, 0, 0, 0}},
{true, sunspec.Eui48{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
{false, sunspec.Bitfield16(0)},
{false, sunspec.Bitfield32(0)},
{true, sunspec.Bitfield16(0xFFFF)},
{true, sunspec.Bitfield32(0xFFFFFFFF)},
{false, sunspec.Enum16(0)},
{false, sunspec.Enum32(0)},
{true, sunspec.Enum16(0xFFFF)},
{true, sunspec.Enum32(0xFFFFFFFF)},
}

for _, c := range cases {
p := point{
err: nil,
value: c.v,
}

if c.e != p.NotImplemented() {
t.Errorf("expected %v, got %v", c.e, c.v)
}

if v, ok := p.Value().(sunspec.NotImplemented); !ok {
t.Errorf("expected sunspec.NotImplemented, got %v", v)
}
}
}

func TestMarshalEui48(t *testing.T) {
p := point{
err: nil,
smdx: &smdx.PointElement{
Type: typelabel.Eui48,
},
}

p.Unmarshal([]byte{1, 2, 3, 4, 5, 6, 7, 8})

v := p.Value().(sunspec.Eui48)
if binary.BigEndian.Uint64(v[:]) != 0x0102030405060708 {
t.Errorf("unexpected value result, got %v", v)
}

if v := p.MarshalXML(); v != "03:04:05:06:07:08" {
t.Errorf("unexpected xml result, got %v", v)
}
}

0 comments on commit 290a608

Please sign in to comment.