2021-01-12 19:29:05 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
2023-04-11 05:33:32 +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-06-05 22:27:59 +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-04-12 09:04:02 +00:00
|
|
|
// logging.SetLevel(logging.DEBUG, "")
|
2021-01-12 19:29:05 +00:00
|
|
|
for _, tt := range evaluateNodesScenario {
|
2023-04-11 05:33:32 +00:00
|
|
|
decoder := NewYamlDecoder(NewDefaultYamlPreferences())
|
|
|
|
reader := bufio.NewReader(strings.NewReader(tt.document))
|
2023-05-09 03:51:21 +00:00
|
|
|
err := decoder.Init(reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2023-04-11 05:33:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|