2020-11-06 01:11:38 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var documentIndexScenarios = []expressionScenario{
|
|
|
|
{
|
|
|
|
description: "Retrieve a document index",
|
|
|
|
document: "a: cat\n---\na: frog\n",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `.a | document_index`,
|
2020-11-06 01:11:38 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!int)::0\n",
|
|
|
|
"D1, P[a], (!!int)::1\n",
|
|
|
|
},
|
|
|
|
},
|
2021-01-05 02:28:37 +00:00
|
|
|
{
|
|
|
|
description: "Retrieve a document index, shorthand",
|
|
|
|
document: "a: cat\n---\na: frog\n",
|
|
|
|
expression: `.a | di`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[a], (!!int)::0\n",
|
|
|
|
"D1, P[a], (!!int)::1\n",
|
|
|
|
},
|
|
|
|
},
|
2020-11-06 01:11:38 +00:00
|
|
|
{
|
|
|
|
description: "Filter by document index",
|
|
|
|
document: "a: cat\n---\na: frog\n",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `select(document_index == 1)`,
|
2020-11-06 01:11:38 +00:00
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D1, P[], (!!map)::a: frog\n",
|
2020-11-06 01:11:38 +00:00
|
|
|
},
|
|
|
|
},
|
2021-01-05 02:28:37 +00:00
|
|
|
{
|
|
|
|
description: "Filter by document index shorthand",
|
|
|
|
document: "a: cat\n---\na: frog\n",
|
|
|
|
expression: `select(di == 1)`,
|
|
|
|
expected: []string{
|
2023-10-18 01:11:53 +00:00
|
|
|
"D1, P[], (!!map)::a: frog\n",
|
2021-01-05 02:28:37 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-06 03:37:01 +00:00
|
|
|
{
|
|
|
|
description: "Print Document Index with matches",
|
|
|
|
document: "a: cat\n---\na: frog\n",
|
2022-02-10 22:05:17 +00:00
|
|
|
expression: `.a | ({"match": ., "doc": document_index})`,
|
2020-11-06 03:37:01 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!map)::match: cat\ndoc: 0\n",
|
2023-10-18 01:11:53 +00:00
|
|
|
"D1, P[], (!!map)::match: frog\ndoc: 1\n",
|
2020-11-06 03:37:01 +00:00
|
|
|
},
|
|
|
|
},
|
2020-11-06 01:11:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDocumentIndexScenarios(t *testing.T) {
|
|
|
|
for _, tt := range documentIndexScenarios {
|
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
documentOperatorScenarios(t, "document-index", documentIndexScenarios)
|
2020-11-06 01:11:38 +00:00
|
|
|
}
|