File: vwt_test.go

package info (click to toggle)
golang-github-adrianmo-go-nmea 1.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 900 kB
  • sloc: makefile: 15
file content (92 lines) | stat: -rw-r--r-- 2,195 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package nmea

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestVWT(t *testing.T) {
	var tests = []struct {
		name string
		raw  string
		err  string
		msg  VWT
	}{
		{ // these examples are from SignalK
			name: "good sentence",
			raw:  "$IIVWT,75,R,1.0,N,0.51,M,1.85,K*6A",
			msg: VWT{
				TrueAngle:        75,
				TrueDirectionBow: Right,
				SpeedKnots:       1,
				SpeedKnotsUnit:   SpeedKnots,
				SpeedMPS:         0.51,
				SpeedMPSUnit:     SpeedMeterPerSecond,
				SpeedKPH:         1.85,
				SpeedKPHUnit:     SpeedKilometerPerHour,
			},
		},
		{
			name: "good sentence, shorter but still valid",
			raw:  "$IIVWT,024,L,018,N,,,,*58",
			msg: VWT{
				TrueAngle:        24,
				TrueDirectionBow: Left,
				SpeedKnots:       18,
				SpeedKnotsUnit:   SpeedKnots,
				SpeedMPS:         0,
				SpeedMPSUnit:     "",
				SpeedKPH:         0,
				SpeedKPHUnit:     "",
			},
		},
		{
			name: "good sentence, handle empty values",
			raw:  "$IIVWT,,,,,,,,*55",
			msg: VWT{
				TrueAngle:        0,
				TrueDirectionBow: "",
				SpeedKnots:       0,
				SpeedKnotsUnit:   "",
				SpeedMPS:         0,
				SpeedMPSUnit:     "",
				SpeedKPH:         0,
				SpeedKPHUnit:     "",
			},
		},
		{
			name: "invalid nmea: DirectionBow",
			raw:  "$IIVWT,75,x,1.0,N,0.51,M,1.85,K*40",
			err:  "nmea: IIVWT invalid true wind direction to bow: x",
		},
		{
			name: "invalid nmea: SpeedKnotsUnit",
			raw:  "$IIVWT,75,R,1.0,x,0.51,M,1.85,K*5c",
			err:  "nmea: IIVWT invalid wind speed in knots unit: x",
		},
		{
			name: "invalid nmea: SpeedMPSUnit",
			raw:  "$IIVWT,75,R,1.0,N,0.51,x,1.85,K*5f",
			err:  "nmea: IIVWT invalid wind speed in meters per second unit: x",
		},
		{
			name: "invalid nmea: SpeedKPHUnit",
			raw:  "$IIVWT,75,R,1.0,N,0.51,M,1.85,x*59",
			err:  "nmea: IIVWT invalid wind speed in kilometers per hour unit: x",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			m, err := Parse(tt.raw)
			if tt.err != "" {
				assert.Error(t, err)
				assert.EqualError(t, err, tt.err)
			} else {
				assert.NoError(t, err)
				vwt := m.(VWT)
				vwt.BaseSentence = BaseSentence{}
				assert.Equal(t, tt.msg, vwt)
			}
		})
	}
}