forked from mikefarah/yq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_divide_test.go
121 lines (118 loc) · 2.91 KB
/
operator_divide_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
package yqlib
import (
"testing"
)
var divideOperatorScenarios = []expressionScenario{
{
skipDoc: true,
document: `[{a: foo_bar, b: _}, {a: 4, b: 2}]`,
expression: ".[] | .a / .b",
expected: []string{
"D0, P[0 a], (!!seq)::- foo\n- bar\n",
"D0, P[1 a], (!!float)::2\n",
},
},
{
skipDoc: true,
document: `{}`,
expression: "(.a / .b) as $x | .",
expected: []string{
"D0, P[], (doc)::{}\n",
},
},
{
description: "String split",
document: `{a: cat_meow, b: _}`,
expression: `.c = .a / .b`,
expected: []string{
"D0, P[], (doc)::{a: cat_meow, b: _, c: [cat, meow]}\n",
},
},
{
description: "Number division",
subdescription: "The result during divison is calculated as a float",
document: `{a: 12, b: 2.5}`,
expression: `.a = .a / .b`,
expected: []string{
"D0, P[], (doc)::{a: 4.8, b: 2.5}\n",
},
},
{
skipDoc: true,
description: "Custom types: that are really strings",
document: "a: !horse cat_meow\nb: !goat _",
expression: `.a = .a / .b`,
expected: []string{
"D0, P[], (doc)::a: !horse\n - cat\n - meow\nb: !goat _\n",
},
},
{
skipDoc: true,
description: "Custom types: that are really numbers",
document: "a: !horse 1.2\nb: !goat 2.3",
expression: `.a = .a / .b`,
expected: []string{
"D0, P[], (doc)::a: !horse 0.5217391304347826\nb: !goat 2.3\n",
},
},
{
skipDoc: true,
document: "a: 2\nb: !goat 2.3",
expression: `.a = .a / .b`,
expected: []string{
"D0, P[], (doc)::a: 0.8695652173913044\nb: !goat 2.3\n",
},
},
{
skipDoc: true,
description: "Custom types: that are really ints",
document: "a: !horse 2\nb: !goat 3",
expression: `.a = .a / .b`,
expected: []string{
"D0, P[], (doc)::a: !horse 0.6666666666666666\nb: !goat 3\n",
},
},
{
skipDoc: true,
description: "Keep anchors",
document: "a: &horse [1]",
expression: `.a[1] = .a[0] / 2`,
expected: []string{
"D0, P[], (doc)::a: &horse [1, 0.5]\n",
},
},
{
skipDoc: true,
description: "Divide int by string",
document: "a: 123\nb: '2'",
expression: `.a / .b`,
expectedError: "!!int cannot be divided by !!str",
},
{
skipDoc: true,
description: "Divide string by int",
document: "a: 2\nb: '123'",
expression: `.b / .a`,
expectedError: "!!str cannot be divided by !!int",
},
{
skipDoc: true,
description: "Divide map by int",
document: "a: {\"a\":1}\nb: 2",
expression: `.a / .b`,
expectedError: "!!map (a) cannot be divided by !!int (b)",
},
{
skipDoc: true,
description: "Divide array by str",
document: "a: [1,2]\nb: '2'",
expression: `.a / .b`,
expectedError: "!!seq (a) cannot be divided by !!str (b)",
},
}
func TestDivideOperatorScenarios(t *testing.T) {
for _, tt := range divideOperatorScenarios {
testScenario(t, &tt)
}
documentOperatorScenarios(t, "divide", divideOperatorScenarios)
}