package main
import (
"strings"
"testing"
)
const miscInput string = `{"something":{"nested":"Here is my test value"},"array":["Hello","world","these","are","values"],"people":[{"first_name":"Charlie","last_name":"Johnson","age":22},{"first_name":"Tom","last_name":"Johnson","age":18},{"first_name":"Charlie","last_name":"Chaplin","age":122},{"first_name":"John","last_name":"Johnson","age":48}]}`
const mixedArray string = `{"array":["first",null,3,{"name":"second"},"third"]}`
const mixedArray2 string = `{"array":["first",null,3,"second",{"name":"third"}]}`
func TestSpecificCases(t *testing.T) {
type test struct {
name string
program string
quiet bool
input string
expected string
}
tests := []test {
{
name: "Verbose Extract",
program: `s/#(~(people)~>_@(1>_#(~(first_name)~>_.|(..)>_*)-|(..)>_*)-|(..)>_*)-/p`,
quiet: true,
input: miscInput,
expected: `"Tom"`,
},
{
name: "Extract",
program: `s/#("people">_ @(1>_#("first_name">_ .)-)-)-/p`,
quiet: true,
input: miscInput,
expected: `"Tom"`,
},
{
name: "Simple Extract",
program: "s/#(\"people\" @(1 #(\"first_name\" (.>a))-)-)->_ a))-)->_ `_ :(#(\"age\">_ .)-):)-/p",
quiet: true,
input: miscInput,
expected: `[22,18,122,48]`,
},
{
name: "Low memory count people",
program: "aX/#(\"people\" :(#()#):)#>_ `1`/o es/#()#/{ xs/.*%+/p }",
quiet: true,
input: miscInput,
expected: "4",
},
{
name: "Get full names",
program: "s/#(\"people\">_ .)-/{ s/:():/p as/:(#()#):/{ xdx } s/:(#((\"first_name\"|\"last_name\") .)#)-/X es/@(.#()-)-/{ xs/(#(\"first_name\"\".*>a\")#|#(\"last_name\"\".*>b\")#|.)*>_`\"_.)-/{ s/:():/p as/:(#()#):/{ xdx } X/:(#((\"first_name\"|\"last_name\") .)#)-/o es/@(.#()-)-/{ xX/(#(\"first_name\"\".*>a\")#|#(\"last_name\"\".*>b\")#|.)*_`\"a\"\"last_name\"\".*>b\")#_)`#(\"name\"\"a\"|\"last_name\"\".*>b\"|..)_]-`\"