-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
ev3dev_conv_test.go
187 lines (165 loc) · 7.29 KB
/
ev3dev_conv_test.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright ©2016 The ev3go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package ev3dev
import (
"errors"
"fmt"
"math"
"reflect"
"strings"
"testing"
"time"
)
var intFromTest = []struct {
data string
attr string
err error
wantInt int
wantErr error
}{
{data: "", attr: "empty", err: nil, wantInt: -1, wantErr: errors.New(`ev3dev: failed to parse mock empty attribute path/mock/empty: strconv.Atoi: parsing "": invalid syntax at ev3dev_conv_test.go:`)},
{data: "1", attr: "one", err: nil, wantInt: 1, wantErr: nil},
{data: "0", attr: "zero", err: nil, wantInt: 0, wantErr: nil},
{data: "-1", attr: "minus_one", err: nil, wantInt: -1, wantErr: nil},
{data: "0", attr: "prior", err: errors.New("prior error"), wantInt: -1, wantErr: errors.New("prior error")},
}
func TestIntFrom(t *testing.T) {
for _, test := range intFromTest {
gotInt, gotErr := intFrom(mockDevice{}, test.data, test.attr, test.err)
if !strings.HasPrefix(fmt.Sprint(gotErr), fmt.Sprint(test.wantErr)) {
t.Errorf("unexpected error:\ngot:\n\t%v\nwant prefix:\n\t%v", gotErr, test.wantErr)
}
if gotInt != test.wantInt {
t.Errorf("unexpected integer result: got:%d want:%d", gotInt, test.wantInt)
}
}
}
func isSame(a, b float64) bool {
return a == b || (math.IsNaN(a) && math.IsNaN(b))
}
var float64FromTest = []struct {
data string
attr string
err error
wantFloat64 float64
wantErr error
}{
{data: "", attr: "empty", err: nil, wantFloat64: math.NaN(), wantErr: errors.New(`ev3dev: failed to parse mock empty attribute path/mock/empty: strconv.ParseFloat: parsing "": invalid syntax at ev3dev_conv_test.go:`)},
{data: "1", attr: "one", err: nil, wantFloat64: 1, wantErr: nil},
{data: "0", attr: "zero", err: nil, wantFloat64: 0, wantErr: nil},
{data: "-1", attr: "minus_one", err: nil, wantFloat64: -1, wantErr: nil},
{data: "0", attr: "prior", err: errors.New("prior error"), wantFloat64: math.NaN(), wantErr: errors.New("prior error")},
}
func TestFloat64From(t *testing.T) {
for _, test := range float64FromTest {
gotFloat64, gotErr := float64From(mockDevice{}, test.data, test.attr, test.err)
if !strings.HasPrefix(fmt.Sprint(gotErr), fmt.Sprint(test.wantErr)) {
t.Errorf("unexpected error:\ngot:\n\t%v\nwant prefix:\n\t%v", gotErr, test.wantErr)
}
if !isSame(gotFloat64, test.wantFloat64) {
t.Errorf("unexpected float64 result: got:%f want:%f", gotFloat64, test.wantFloat64)
}
}
}
var durationFromTest = []struct {
data string
attr string
err error
wantDuration time.Duration
wantErr error
}{
{data: "", attr: "empty", err: nil, wantDuration: -1, wantErr: errors.New(`ev3dev: failed to parse mock empty attribute path/mock/empty: strconv.Atoi: parsing "": invalid syntax at ev3dev_conv_test.go:`)},
{data: "1", attr: "one", err: nil, wantDuration: 1 * time.Millisecond, wantErr: nil},
{data: "0", attr: "zero", err: nil, wantDuration: 0, wantErr: nil},
{data: "-1", attr: "minus_one", err: nil, wantDuration: -1 * time.Millisecond, wantErr: nil},
{data: "0", attr: "prior", err: errors.New("prior error"), wantDuration: -1, wantErr: errors.New("prior error")},
}
func TestDurationFrom(t *testing.T) {
for _, test := range durationFromTest {
gotDuration, gotErr := durationFrom(mockDevice{}, test.data, test.attr, test.err)
if !strings.HasPrefix(fmt.Sprint(gotErr), fmt.Sprint(test.wantErr)) {
t.Errorf("unexpected error:\ngot:\n\t%v\nwant prefix:\n\t%v", gotErr, test.wantErr)
}
if gotDuration != test.wantDuration {
t.Errorf("unexpected duration result: got:%v want:%v", gotDuration, test.wantDuration)
}
}
}
var stringSliceFromTest = []struct {
data string
attr string
err error
wantStrings []string
wantErr error
}{
{data: "", attr: "empty", err: nil, wantStrings: nil, wantErr: nil},
{data: "1", attr: "one", err: nil, wantStrings: []string{"1"}, wantErr: nil},
{data: "0 1", attr: "two", err: nil, wantStrings: []string{"0", "1"}, wantErr: nil},
{data: "0\t1", attr: "tab", err: nil, wantStrings: []string{"0\t1"}, wantErr: nil},
{data: "0", attr: "prior", err: errors.New("prior error"), wantStrings: nil, wantErr: errors.New("prior error")},
}
func TestStringSliceFrom(t *testing.T) {
for _, test := range stringSliceFromTest {
gotStrings, gotErr := stringSliceFrom(mockDevice{}, test.data, test.attr, test.err)
if fmt.Sprint(gotErr) != fmt.Sprint(test.wantErr) {
t.Errorf("unexpected error:\ngot:\n\t%v\nwant prefix:\n\t%v", gotErr, test.wantErr)
}
if !reflect.DeepEqual(gotStrings, test.wantStrings) {
t.Errorf("unexpected strings result: got:%v want:%v", gotStrings, test.wantStrings)
}
}
}
var stateFromTest = []struct {
data string
attr string
err error
wantState MotorState
wantErr error
}{
{data: "", attr: "empty", err: nil, wantState: 0, wantErr: nil},
{data: running, attr: running, err: nil, wantState: Running, wantErr: nil},
{data: ramping, attr: ramping, err: nil, wantState: Ramping, wantErr: nil},
{data: holding, attr: holding, err: nil, wantState: Holding, wantErr: nil},
{data: overloaded, attr: overloaded, err: nil, wantState: Overloaded, wantErr: nil},
{data: stalled, attr: stalled, err: nil, wantState: Stalled, wantErr: nil},
{data: running + " " + stalled, attr: running + " " + stalled, err: nil, wantState: Running | Stalled, wantErr: nil},
{data: "invalid", attr: "invalid", err: nil, wantState: 0, wantErr: errors.New(`ev3dev: unrecognized motor state for mock state: "invalid" (valid:["holding" "overloaded" "ramping" "running" "stalled"]) at ev3dev.go:`)},
{data: "0", attr: "prior", err: errors.New("prior error"), wantState: 0, wantErr: errors.New("prior error")},
}
func TestStateFrom(t *testing.T) {
for _, test := range stateFromTest {
gotState, gotErr := stateFrom(mockDevice{}, test.data, test.attr, test.err)
if !strings.HasPrefix(fmt.Sprint(gotErr), fmt.Sprint(test.wantErr)) {
t.Errorf("unexpected error:\ngot:\n\t%v\nwant prefix:\n\t%v", gotErr, test.wantErr)
}
if gotState != test.wantState {
t.Errorf("unexpected state result: got:%v want:%v", gotState, test.wantState)
}
}
}
type ue map[string]string
var ueventFromTest = []struct {
data string
attr string
err error
wantUevents map[string]string
wantErr error
}{
{data: "", attr: "empty", err: nil, wantUevents: nil, wantErr: nil},
{data: "one=1", attr: "one", err: nil, wantUevents: ue{"one": "1"}, wantErr: nil},
{data: "zero=0\none=1", attr: "two", err: nil, wantUevents: ue{"zero": "0", "one": "1"}, wantErr: nil},
{data: "0", attr: "zero", err: nil, wantUevents: nil, wantErr: errors.New(`ev3dev: failed to parse mock zero attribute path/mock/zero: unexpected line: "0" at ev3dev_conv_test.go:`)},
{data: "0", attr: "prior", err: errors.New("prior error"), wantUevents: nil, wantErr: errors.New("prior error")},
}
func TestUeventFrom(t *testing.T) {
for _, test := range ueventFromTest {
gotUevents, gotErr := ueventFrom(mockDevice{}, test.data, test.attr, test.err)
if !strings.HasPrefix(fmt.Sprint(gotErr), fmt.Sprint(test.wantErr)) {
t.Errorf("unexpected error:\ngot:\n\t%v\nwant prefix:\n\t%v", gotErr, test.wantErr)
}
if !reflect.DeepEqual(gotUevents, test.wantUevents) {
t.Errorf("unexpected uevent result: got:%v want:%v", gotUevents, test.wantUevents)
}
}
}