2021-01-12 19:29:05 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
2023-10-18 01:11:53 +00:00
|
|
|
"bufio"
|
|
|
|
"strings"
|
2021-01-12 19:29:05 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
var evaluateNodesScenario = []expressionScenario{
|
|
|
|
{
|
|
|
|
document: `a: hello`,
|
|
|
|
expression: `.a`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!str)::hello\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: `a: hello`,
|
|
|
|
expression: `.`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D0, P[], (!!map)::a: hello\n",
|
2021-01-12 19:29:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
document: `- a: "yes"`,
|
|
|
|
expression: `.[] | has("a")`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[0], (!!bool)::true\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:35:57 +00:00
|
|
|
func TestAllAtOnceEvaluateNodes(t *testing.T) {
|
|
|
|
var evaluator = NewAllAtOnceEvaluator()
|
2023-10-18 01:11:53 +00:00
|
|
|
// logging.SetLevel(logging.DEBUG, "")
|
2021-01-12 19:29:05 +00:00
|
|
|
for _, tt := range evaluateNodesScenario {
|
2023-10-18 01:11:53 +00:00
|
|
|
decoder := NewYamlDecoder(NewDefaultYamlPreferences())
|
|
|
|
reader := bufio.NewReader(strings.NewReader(tt.document))
|
|
|
|
err := decoder.Init(reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
candidateNode, errorReading := decoder.Decode()
|
|
|
|
|
|
|
|
if errorReading != nil {
|
|
|
|
t.Error(errorReading)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
list, _ := evaluator.EvaluateNodes(tt.expression, candidateNode)
|
2021-11-13 23:51:18 +00:00
|
|
|
test.AssertResultComplex(t, tt.expected, resultsToString(t, list))
|
2021-01-12 19:29:05 +00:00
|
|
|
}
|
|
|
|
}
|