-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute_test.go
175 lines (140 loc) · 4.6 KB
/
attribute_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
package acmelib
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_StringAttribute(t *testing.T) {
assert := assert.New(t)
strAtt := NewStringAttribute("str_att", "def_val")
assert.Equal(AttributeTypeString, strAtt.Type())
assert.Equal("str_att", strAtt.Name())
assert.Equal("def_val", strAtt.DefValue())
_, err := strAtt.ToString()
assert.NoError(err)
_, err = strAtt.ToInteger()
assert.Error(err)
_, err = strAtt.ToFloat()
assert.Error(err)
_, err = strAtt.ToEnum()
assert.Error(err)
}
func Test_IntegerAttribute(t *testing.T) {
assert := assert.New(t)
intAtt, err := NewIntegerAttribute("int_att", 10, 0, 100)
assert.NoError(err)
assert.Equal(AttributeTypeInteger, intAtt.Type())
assert.Equal("int_att", intAtt.Name())
assert.Equal(10, intAtt.DefValue())
assert.Equal(0, intAtt.Min())
assert.Equal(100, intAtt.Max())
// should return an error because min is greater then max
_, err = NewIntegerAttribute("int_att", 0, 100, 0)
assert.Error(err)
// should return an error because the default value is out of range
_, err = NewIntegerAttribute("int_att", 1000, 0, 100)
assert.Error(err)
_, err = intAtt.ToInteger()
assert.NoError(err)
_, err = intAtt.ToString()
assert.Error(err)
_, err = intAtt.ToFloat()
assert.Error(err)
_, err = intAtt.ToEnum()
assert.Error(err)
}
func Test_FloatAttribute(t *testing.T) {
assert := assert.New(t)
floatAtt, err := NewFloatAttribute("float_att", 10, 0, 100)
assert.NoError(err)
assert.Equal(AttributeTypeFloat, floatAtt.Type())
assert.Equal("float_att", floatAtt.Name())
assert.Equal(10.0, floatAtt.DefValue())
assert.Equal(0.0, floatAtt.Min())
assert.Equal(100.0, floatAtt.Max())
// should return an error because min is greater then max
_, err = NewFloatAttribute("float_att", 0, 100, 0)
assert.Error(err)
// should return an error because the default value is out of range
_, err = NewFloatAttribute("float_att", 1000, 0, 100)
assert.Error(err)
_, err = floatAtt.ToFloat()
assert.NoError(err)
_, err = floatAtt.ToString()
assert.Error(err)
_, err = floatAtt.ToInteger()
assert.Error(err)
_, err = floatAtt.ToEnum()
assert.Error(err)
}
func Test_EnumAttribute(t *testing.T) {
assert := assert.New(t)
enumAtt, err := NewEnumAttribute("enum_att", "val_0", "val_1", "val_2")
assert.NoError(err)
assert.Equal(AttributeTypeEnum, enumAtt.Type())
assert.Equal("enum_att", enumAtt.Name())
assert.Equal("val_0", enumAtt.DefValue())
assert.Equal(3, len(enumAtt.Values()))
expectedValues := []string{"val_0", "val_1", "val_2"}
for idx, val := range enumAtt.Values() {
assert.Equal(expectedValues[idx], val)
}
// should return an error because there are no values defined
_, err = NewEnumAttribute("enum_att")
assert.Error(err)
// should compact the values because val_1 is duplicated
compEnumAtt, err := NewEnumAttribute("enum_att", "val_0", "val_1", "val_2", "val_1")
assert.NoError(err)
assert.Equal(3, len(compEnumAtt.Values()))
val, err := enumAtt.GetValueAtIndex(2)
assert.NoError(err)
assert.Equal("val_2", val)
// should return an error because index is negative
_, err = enumAtt.GetValueAtIndex(-1)
assert.Error(err)
// should return an error because index is out of range
_, err = enumAtt.GetValueAtIndex(5)
assert.Error(err)
_, err = enumAtt.ToEnum()
assert.NoError(err)
_, err = enumAtt.ToString()
assert.Error(err)
_, err = enumAtt.ToInteger()
assert.Error(err)
_, err = enumAtt.ToFloat()
assert.Error(err)
}
func Test_AttributeAssignment(t *testing.T) {
assert := assert.New(t)
att := NewStringAttribute("att", "")
bus := NewBus("bus")
node := NewNode("node", 1, 1)
msg := NewMessage("msg", 1, 1)
sig, err := NewStandardSignal("sig", NewFlagSignalType("flag_type"))
assert.NoError(err)
attValues := []string{"bus_att", "node_att", "msg_att", "sig_att"}
assert.NoError(bus.AssignAttribute(att, attValues[0]))
assert.NoError(node.AssignAttribute(att, attValues[1]))
assert.NoError(msg.AssignAttribute(att, attValues[2]))
assert.NoError(sig.AssignAttribute(att, attValues[3]))
assert.Len(att.References(), 4)
for _, attAss := range att.References() {
switch attAss.entity.EntityKind() {
case EntityKindBus:
assert.Equal(attValues[0], attAss.Value())
_, err := attAss.ToBusEntity()
assert.NoError(err)
case EntityKindNode:
assert.Equal(attValues[1], attAss.Value())
_, err := attAss.ToNodeEntity()
assert.NoError(err)
case EntityKindMessage:
assert.Equal(attValues[2], attAss.Value())
_, err := attAss.ToMessageEntity()
assert.NoError(err)
case EntityKindSignal:
assert.Equal(attValues[3], attAss.Value())
_, err := attAss.ToSignalEntity()
assert.NoError(err)
}
}
}