Skip to content

Commit

Permalink
Use subtest for each test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ecc1 committed Jul 26, 2018
1 parent e01ebc4 commit bc6f972
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 83 deletions.
10 changes: 6 additions & 4 deletions crc16_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ func TestCRC16(t *testing.T) {
{[]byte{0x01, 0x07, 0x00, 0x10, 0x04}, 0xB88B},
}
for _, c := range cases {
sum := crc16(c.msg)
if sum != c.sum {
t.Errorf("crc16(% X) == %X, want %X", c.msg, sum, c.sum)
}
t.Run("", func(t *testing.T) {
sum := crc16(c.msg)
if sum != c.sum {
t.Errorf("crc16(% X) == %X, want %X", c.msg, sum, c.sum)
}
})
}
}
103 changes: 62 additions & 41 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dexcom

import (
"bytes"
"fmt"
"math"
"testing"
)
Expand All @@ -16,14 +17,18 @@ func TestMarshalUint16(t *testing.T) {
{math.MaxUint16, []byte{0xFF, 0xFF}},
}
for _, c := range cases {
rep := marshalUint16(c.val)
if !bytes.Equal(rep, c.rep) {
t.Errorf("marshalUint16(%04X) == % X, want % X", c.val, rep, c.rep)
}
val := unmarshalUint16(c.rep)
if val != c.val {
t.Errorf("unmarshalUint16(% X) == %04X, want %04X", c.rep, val, c.val)
}
t.Run(fmt.Sprintf("marshal_%d", c.val), func(t *testing.T) {
rep := marshalUint16(c.val)
if !bytes.Equal(rep, c.rep) {
t.Errorf("marshalUint16(%04X) == % X, want % X", c.val, rep, c.rep)
}
})
t.Run(fmt.Sprintf("unmarshal_%d", c.val), func(t *testing.T) {
val := unmarshalUint16(c.rep)
if val != c.val {
t.Errorf("unmarshalUint16(% X) == %04X, want %04X", c.rep, val, c.val)
}
})
}
}

Expand All @@ -41,14 +46,18 @@ func TestMarshalInt16(t *testing.T) {
{math.MinInt16, []byte{0x00, 0x80}},
}
for _, c := range cases {
rep := marshalInt16(c.val)
if !bytes.Equal(rep, c.rep) {
t.Errorf("marshalInt16(%d) == % X, want % X", c.val, rep, c.rep)
}
val := unmarshalInt16(c.rep)
if val != c.val {
t.Errorf("unmarshalInt16(% X) == %d, want %d", c.rep, val, c.val)
}
t.Run(fmt.Sprintf("marshal_%d", c.val), func(t *testing.T) {
rep := marshalInt16(c.val)
if !bytes.Equal(rep, c.rep) {
t.Errorf("marshalInt16(%d) == % X, want % X", c.val, rep, c.rep)
}
})
t.Run(fmt.Sprintf("unmarshal_%d", c.val), func(t *testing.T) {
val := unmarshalInt16(c.rep)
if val != c.val {
t.Errorf("unmarshalInt16(% X) == %d, want %d", c.rep, val, c.val)
}
})
}
}

Expand All @@ -62,14 +71,18 @@ func TestMarshalUint32(t *testing.T) {
{math.MaxUint32, []byte{0xFF, 0xFF, 0xFF, 0xFF}},
}
for _, c := range cases {
rep := marshalUint32(c.val)
if !bytes.Equal(rep, c.rep) {
t.Errorf("marshalUint32(%08X) == % X, want % X", c.val, rep, c.rep)
}
val := unmarshalUint32(c.rep)
if val != c.val {
t.Errorf("unmarshalUint32(% X) == %08X, want %08X", c.rep, val, c.val)
}
t.Run(fmt.Sprintf("marshal_%d", c.val), func(t *testing.T) {
rep := marshalUint32(c.val)
if !bytes.Equal(rep, c.rep) {
t.Errorf("marshalUint32(%08X) == % X, want % X", c.val, rep, c.rep)
}
})
t.Run(fmt.Sprintf("unmarshal_%d", c.val), func(t *testing.T) {
val := unmarshalUint32(c.rep)
if val != c.val {
t.Errorf("unmarshalUint32(% X) == %08X, want %08X", c.rep, val, c.val)
}
})
}
}

Expand All @@ -87,18 +100,22 @@ func TestMarshalInt32(t *testing.T) {
{math.MinInt32, []byte{0, 0, 0, 0x80}},
}
for _, c := range cases {
rep := marshalInt32(c.val)
if !bytes.Equal(rep, c.rep) {
t.Errorf("marshalInt32(%d) == % X, want % X", c.val, rep, c.rep)
}
val := unmarshalInt32(c.rep)
if val != c.val {
t.Errorf("unmarshalInt32(% X) == %d, want %d", c.rep, val, c.val)
}
t.Run(fmt.Sprintf("marshal_%d", c.val), func(t *testing.T) {
rep := marshalInt32(c.val)
if !bytes.Equal(rep, c.rep) {
t.Errorf("marshalInt32(%d) == % X, want % X", c.val, rep, c.rep)
}
})
t.Run(fmt.Sprintf("unmarshal_%d", c.val), func(t *testing.T) {
val := unmarshalInt32(c.rep)
if val != c.val {
t.Errorf("unmarshalInt32(% X) == %d, want %d", c.rep, val, c.val)
}
})
}
}

func TestMarshalUint64(t *testing.T) {
func TestUnmarshalUint64(t *testing.T) {
cases := []struct {
val uint64
rep []byte
Expand All @@ -108,10 +125,12 @@ func TestMarshalUint64(t *testing.T) {
{math.MaxUint64, []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}},
}
for _, c := range cases {
val := unmarshalUint64(c.rep)
if val != c.val {
t.Errorf("unmarshalUint64(% X) == %016X, want %016X", c.rep, val, c.val)
}
t.Run(fmt.Sprintf("unmarshal_%d", c.val), func(t *testing.T) {
val := unmarshalUint64(c.rep)
if val != c.val {
t.Errorf("unmarshalUint64(% X) == %016X, want %016X", c.rep, val, c.val)
}
})
}
}

Expand All @@ -126,9 +145,11 @@ func TestUnmarshalFloat64(t *testing.T) {
{[]byte{0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40}, 3.141592653589793},
}
for _, c := range cases {
val := unmarshalFloat64(c.rep)
if val != c.val {
t.Errorf("unmarshalFloat64(% X) == %v, want %v", c.rep, val, c.val)
}
t.Run(fmt.Sprintf("unmarshal_%.3g", c.val), func(t *testing.T) {
val := unmarshalFloat64(c.rep)
if val != c.val {
t.Errorf("unmarshalFloat64(% X) == %v, want %v", c.rep, val, c.val)
}
})
}
}
20 changes: 12 additions & 8 deletions nightscout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,12 @@ func TestNightscoutEntry(t *testing.T) {
{r4, e4},
}
for _, c := range cases {
e := Entry(c.r.nightscoutEntry())
if e != c.e {
t.Errorf("nightscoutEntry(%v) == %v, want %v", c.r, e, c.e)
}
t.Run("", func(t *testing.T) {
e := Entry(c.r.nightscoutEntry())
if e != c.e {
t.Errorf("nightscoutEntry(%v) == %v, want %v", c.r, e, c.e)
}
})
}
}

Expand All @@ -152,10 +154,12 @@ func TestNightscoutEntries(t *testing.T) {
},
}
for _, c := range cases {
e := convertEntries(NightscoutEntries(c.r))
if !equalEntries(e, c.e) {
t.Errorf("NightscoutEntries(%v) == %v, want %v", c.r, e, c.e)
}
t.Run("", func(t *testing.T) {
e := convertEntries(NightscoutEntries(c.r))
if !equalEntries(e, c.e) {
t.Errorf("NightscoutEntries(%v) == %v, want %v", c.r, e, c.e)
}
})
}
}

Expand Down
10 changes: 6 additions & 4 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ func TestPacket(t *testing.T) {
[]byte{0x01, 0x0C, 0x00, 0x11, 0x05, 0x26, 0x00, 0x00, 0x00, 0x01, 0x5E, 0xC3}},
}
for _, c := range cases {
msg := marshalPacket(c.cmd, c.params)
if !bytes.Equal(msg, c.msg) {
t.Errorf("Command(%X, %X) == %X, want %X", c.cmd, c.params, msg, c.msg)
}
t.Run("", func(t *testing.T) {
msg := marshalPacket(c.cmd, c.params)
if !bytes.Equal(msg, c.msg) {
t.Errorf("Command(%X, %X) == %X, want %X", c.cmd, c.params, msg, c.msg)
}
})
}
}
38 changes: 20 additions & 18 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,27 @@ func TestUnmarshalPage(t *testing.T) {
},
}
for _, c := range cases {
page, err := unmarshalPage(hexdata(c.page))
if err != nil {
t.Errorf("UnmarshalPage returned %v", err)
}
if page.Type != c.pageType {
t.Errorf("UnmarshalPage: page type == %d, want %d", page.Type, c.pageType)
}
if page.Number != c.pageNumber {
t.Errorf("UnmarshalPage: page number == %d, want %d", page.Number, c.pageNumber)
}
if len(page.Records) != len(c.records) {
t.Errorf("UnmarshalPage: #records == %d, want %d", len(page.Records), len(c.records))
}
for i, v := range page.Records {
r := hexdata(c.records[i])
if !bytes.Equal(v, r) {
t.Errorf("UnmarshalPage: record #%d == % X, want % X", i, v, r)
t.Run(c.pageType.String(), func(t *testing.T) {
page, err := unmarshalPage(hexdata(c.page))
if err != nil {
t.Errorf("UnmarshalPage returned %v", err)
}
}
if page.Type != c.pageType {
t.Errorf("UnmarshalPage: page type == %d, want %d", page.Type, c.pageType)
}
if page.Number != c.pageNumber {
t.Errorf("UnmarshalPage: page number == %d, want %d", page.Number, c.pageNumber)
}
if len(page.Records) != len(c.records) {
t.Errorf("UnmarshalPage: #records == %d, want %d", len(page.Records), len(c.records))
}
for i, v := range page.Records {
r := hexdata(c.records[i])
if !bytes.Equal(v, r) {
t.Errorf("UnmarshalPage: record #%d == % X, want % X", i, v, r)
}
}
})
}
}

Expand Down
18 changes: 10 additions & 8 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ func TestTime(t *testing.T) {
{0x0EDCBA98, parseTime("2016-11-25 22:58:32")},
}
for _, c := range cases {
tv := toTime(c.n)
if !tv.Equal(c.t) {
t.Errorf("toTime(%X) == %v, want %v", c.n, tv, c.t)
}
n := fromTime(c.t)
if n != c.n {
t.Errorf("fromTime(%v) == %X, want %X", c.t, n, c.n)
}
t.Run(c.t.String(), func(t *testing.T) {
tv := toTime(c.n)
if !tv.Equal(c.t) {
t.Errorf("toTime(%X) == %v, want %v", c.n, tv, c.t)
}
n := fromTime(c.t)
if n != c.n {
t.Errorf("fromTime(%v) == %X, want %X", c.t, n, c.n)
}
})
}
}

Expand Down

0 comments on commit bc6f972

Please sign in to comment.