Skip to content

Commit

Permalink
streamline snapshot test
Browse files Browse the repository at this point in the history
  • Loading branch information
myaaaaaaaaa committed Sep 21, 2024
1 parent f94d502 commit 9c2df4a
Showing 1 changed file with 16 additions and 28 deletions.
44 changes: 16 additions & 28 deletions gojqx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package jqx

import (
"slices"
"strings"
"testing"
)

Expand Down Expand Up @@ -36,36 +35,25 @@ func TestError(t *testing.T) {
assertEqual(t, err("|") != nil, true)
assertEqual(t, err("maybe") != nil, true)
}
func TestSnapshot(t *testing.T) {
func TestState(t *testing.T) {
state := State{}
query := state.Compile(`to_entries[] | snapshot(.key;.value) | .key`)
query := state.Compile(`
fromjson | to_entries[] |
snapshot(.key;.value+.value) | .key+.key
`)

entries := map[string]any{
"a.txt": "aaa",
"b/b.json": "ugh",
"c/c.json": []any{2, 8},
"d.json": map[string]any{"e": 10},
"f.json": 8,
}

filenames := slices.Collect(query(entries))
cat := []string{}

fsys := toFS(state.Files, nil)
for _, filename := range filenames {
filename := filename.(string)
cat = append(cat, fileText(fsys, filename))
}
keys := slices.Collect(query(`{"a":"aa", "c":[3], "q":{"e":10}, "r":5}`))

assertEqual(t, len(filenames), 5)
assertEqual(t, len(keys), 4)
assertEqual(t, len(state.Files), 4)

assertEqual(t,
[5]any(filenames),
[...]any{"a.txt", "b/b.json", "c/c.json", "d.json", "f.json"},
)
state = State{Globals: map[string]any{
"$k": keys,
"$v": state.Files,
}}
query = state.Compile(`[$k[],$v[]] | tostring`)

assertEqual(t,
strings.Join(cat, " "),
`aaa ugh [2,8] {"e":10} 8`,
)
got := slices.Collect(query(nil))
assertEqual(t, len(got), 1)
assertEqual(t, got[0], `["aa","cc","qq","rr","aaaa",[3,3],{"e":10},10]`)
}

0 comments on commit 9c2df4a

Please sign in to comment.