Fixed load operator bug

This commit is contained in:
Mike Farah 2022-10-25 13:30:38 +11:00
parent c1640fb10d
commit c62e18f9b2
3 changed files with 32 additions and 4 deletions

View File

@ -48,7 +48,7 @@ func loadYaml(filename string, decoder Decoder) (*CandidateNode, error) {
} else {
sequenceNode := &CandidateNode{Node: &yaml.Node{Kind: yaml.SequenceNode}}
for doc := documents.Front(); doc != nil; doc = doc.Next() {
sequenceNode.Node.Content = append(sequenceNode.Node.Content, doc.Value.(*CandidateNode).Node)
sequenceNode.Node.Content = append(sequenceNode.Node.Content, unwrapDoc(doc.Value.(*CandidateNode).Node))
}
return sequenceNode, nil
}

View File

@ -5,6 +5,22 @@ import (
)
var loadScenarios = []expressionScenario{
{
skipDoc: true,
description: "Load empty file",
expression: `load("../../examples/empty.yaml")`,
expected: []string{
"D0, P[], (!!null)::\n",
},
},
{
skipDoc: true,
description: "Load multiple documents",
expression: `load("../../examples/multiple_docs_small.yaml")`,
expected: []string{
"D0, P[], ()::- a: Easy! as one two three\n- another:\n document: here\n- - 1\n - 2\n",
},
},
{
description: "Simple example",
document: `{myFile: "../../examples/thing.yml"}`,

View File

@ -23,17 +23,29 @@ func ParseData(rawData string) yaml.Node {
return parsedData
}
func printDifference(t *testing.T, expectedValue interface{}, actualValue interface{}) {
opts := []write.Option{write.TerminalColor()}
var differenceBuffer bytes.Buffer
expectedString := fmt.Sprintf("%v", expectedValue)
actualString := fmt.Sprintf("%v", actualValue)
if err := diff.Text("expected", "actual", expectedString, actualString, bufio.NewWriter(&differenceBuffer), opts...); err != nil {
t.Error(err)
} else {
t.Error(differenceBuffer.String())
}
}
func AssertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
t.Helper()
if expectedValue != actualValue {
t.Error("Expected <", expectedValue, "> but got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
printDifference(t, expectedValue, actualValue)
}
}
func AssertResultComplex(t *testing.T, expectedValue interface{}, actualValue interface{}) {
t.Helper()
if !reflect.DeepEqual(expectedValue, actualValue) {
t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
printDifference(t, expectedValue, actualValue)
}
}
@ -41,7 +53,7 @@ func AssertResultComplexWithContext(t *testing.T, expectedValue interface{}, act
t.Helper()
if !reflect.DeepEqual(expectedValue, actualValue) {
t.Error(context)
t.Error("\nExpected <", expectedValue, ">\nbut got <", actualValue, ">", fmt.Sprintf("%T", actualValue))
printDifference(t, expectedValue, actualValue)
}
}