-
Notifications
You must be signed in to change notification settings - Fork 3
/
unit_test.go
108 lines (84 loc) · 2.62 KB
/
unit_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
package xbrl
import (
"encoding/xml"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUnmarshalUnit(t *testing.T) {
t.Run("simple unit", func(t *testing.T) {
// language=xml
unitXML := `<unit>
<measure>shares</measure>
</unit>`
var unit Unit
require.NoError(t, xml.Unmarshal([]byte(unitXML), &unit))
require.Len(t, unit.Measures, 1)
assert.Equal(t, "shares", unit.Measures[0].Value)
assert.Equal(t, "shares", unit.Measures[0].String())
assert.Nil(t, unit.Divide)
assert.Equal(t, unit.String(), "shares")
})
t.Run("product of measures", func(t *testing.T) {
// language=xml
unitXML := `<unit>
<measure>myns:feet</measure>
<measure>myns:feet</measure>
</unit>`
var unit Unit
require.NoError(t, xml.Unmarshal([]byte(unitXML), &unit))
require.Len(t, unit.Measures, 2)
assert.Equal(t, "myns:feet", unit.Measures[0].Value)
assert.Equal(t, "feet", unit.Measures[0].String())
assert.Equal(t, "myns:feet", unit.Measures[1].Value)
assert.Equal(t, "feet", unit.Measures[1].String())
assert.Nil(t, unit.Divide)
assert.Equal(t, "feet * feet", unit.String())
})
t.Run("ratio of simple measures", func(t *testing.T) {
// language=xml
unitXML := `<unit>
<divide>
<unitNumerator>
<measure>iso4127:USD</measure>
</unitNumerator>
<unitDenominator>
<measure>shares</measure>
</unitDenominator>
</divide>
</unit>`
var unit Unit
require.NoError(t, xml.Unmarshal([]byte(unitXML), &unit))
require.Len(t, unit.Measures, 0)
assert.NotNil(t, unit.Divide)
assert.Len(t, unit.Divide.Numerator, 1)
assert.Equal(t, "iso4127:USD", unit.Divide.Numerator[0].Value)
assert.Len(t, unit.Divide.Denominator, 1)
assert.Equal(t, "shares", unit.Divide.Denominator[0].Value)
assert.Equal(t, "USD / shares", unit.String())
})
t.Run("ratio of products of measures", func(t *testing.T) {
// language=xml
unitXML := `<unit>
<divide>
<unitNumerator>
<measure>iso4127:USD</measure>
</unitNumerator>
<unitDenominator>
<measure>myns:feet</measure>
<measure>myns:feet</measure>
</unitDenominator>
</divide>
</unit>`
var unit Unit
require.NoError(t, xml.Unmarshal([]byte(unitXML), &unit))
require.Len(t, unit.Measures, 0)
assert.NotNil(t, unit.Divide)
assert.Len(t, unit.Divide.Numerator, 1)
assert.Equal(t, "iso4127:USD", unit.Divide.Numerator[0].Value)
assert.Len(t, unit.Divide.Denominator, 2)
assert.Equal(t, "myns:feet", unit.Divide.Denominator[0].Value)
assert.Equal(t, "myns:feet", unit.Divide.Denominator[1].Value)
assert.Equal(t, "USD / feet * feet", unit.String())
})
}