Skip to content

Commit c62e18f

Browse files
committed
Fixed load operator bug
1 parent c1640fb commit c62e18f

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

pkg/yqlib/operator_load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func loadYaml(filename string, decoder Decoder) (*CandidateNode, error) {
4848
} else {
4949
sequenceNode := &CandidateNode{Node: &yaml.Node{Kind: yaml.SequenceNode}}
5050
for doc := documents.Front(); doc != nil; doc = doc.Next() {
51-
sequenceNode.Node.Content = append(sequenceNode.Node.Content, doc.Value.(*CandidateNode).Node)
51+
sequenceNode.Node.Content = append(sequenceNode.Node.Content, unwrapDoc(doc.Value.(*CandidateNode).Node))
5252
}
5353
return sequenceNode, nil
5454
}

pkg/yqlib/operator_load_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ import (
55
)
66

77
var loadScenarios = []expressionScenario{
8+
{
9+
skipDoc: true,
10+
description: "Load empty file",
11+
expression: `load("../../examples/empty.yaml")`,
12+
expected: []string{
13+
"D0, P[], (!!null)::\n",
14+
},
15+
},
16+
{
17+
skipDoc: true,
18+
description: "Load multiple documents",
19+
expression: `load("../../examples/multiple_docs_small.yaml")`,
20+
expected: []string{
21+
"D0, P[], ()::- a: Easy! as one two three\n- another:\n document: here\n- - 1\n - 2\n",
22+
},
23+
},
824
{
925
description: "Simple example",
1026
document: `{myFile: "../../examples/thing.yml"}`,

test/utils.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,37 @@ func ParseData(rawData string) yaml.Node {
2323
return parsedData
2424
}
2525

26+
func printDifference(t *testing.T, expectedValue interface{}, actualValue interface{}) {
27+
opts := []write.Option{write.TerminalColor()}
28+
var differenceBuffer bytes.Buffer
29+
expectedString := fmt.Sprintf("%v", expectedValue)
30+
actualString := fmt.Sprintf("%v", actualValue)
31+
if err := diff.Text("expected", "actual", expectedString, actualString, bufio.NewWriter(&differenceBuffer), opts...); err != nil {
32+
t.Error(err)
33+
} else {
34+
t.Error(differenceBuffer.String())
35+
}
36+
}
37+
2638
func AssertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
2739
t.Helper()
2840
if expectedValue != actualValue {
29-
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
41+
printDifference(t, expectedValue, actualValue)
3042
}
3143
}
3244

3345
func AssertResultComplex(t *testing.T, expectedValue interface{}, actualValue interface{}) {
3446
t.Helper()
3547
if !reflect.DeepEqual(expectedValue, actualValue) {
36-
t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
48+
printDifference(t, expectedValue, actualValue)
3749
}
3850
}
3951

4052
func AssertResultComplexWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
4153
t.Helper()
4254
if !reflect.DeepEqual(expectedValue, actualValue) {
4355
t.Error(context)
44-
t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
56+
printDifference(t, expectedValue, actualValue)
4557
}
4658
}
4759

0 commit comments

Comments
 (0)