1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00
stedolan-jq/c/testdata

215 lines
2.3 KiB
Plaintext

# Tests are groups of three lines: program, input, expected output
# Blank lines and lines starting with # are ignored
#
# Simple value tests to check parser. Input is irrelevant
#
true
null
true
false
null
false
# null
# 42
# null
1
null
1
# FIXME: much more number testing needed
{}
null
{}
[]
null
[]
# FIXME: string literals
#
# Dictionary construction syntax
#
{a: 1}
null
{"a":1}
# FIXME: string literals
#
# Field access, piping
#
.foo
{"foo": 42, "bar": 43}
42
.foo | .bar
{"foo": {"bar": 42}, "bar": "badvalue"}
42
.foo.bar
{"foo": {"bar": 42}, "bar": "badvalue"}
42
.["foo"].bar
{"foo": {"bar": 42}, "bar": "badvalue"}
42
#
# Multiple outputs, iteration
#
.[]
[1,2,3]
1
2
3
1,1
[]
1
1
[.]
[2]
[[2]]
[[2]]
[3]
[[2]]
[{}]
[2]
[{}]
[.[]]
["a"]
["a"]
[(.,1),((.,.[]),(2,3))]
["a","b"]
[["a","b"],1,["a","b"],"a","b",2,3]
[([5,5][]),.,.[]]
[1,2,3]
[5,5,[1,2,3],1,2,3]
{x: (1,2)},{x:3} | .x
null
1
2
3
#
# Variables
#
1 as $x | 2 as $y | [$x,$y,$x]
null
[1,2,1]
[1,2,3][] as $x | [[4,5,6,7][$x]]
null
[5]
[6]
[7]
1 as $x | [$x,$x,$x as $x | $x]
null
[1,1,1]
# [.,(.[] | {x:.},.),.,.[]]
#
# Builtin functions
#
# FIXME: floats vs. integer
1+1
null
2
.+4
15
19.0
[1,2,3] + [.]
null
[1,2,3,null]
#
# User-defined functions
# Oh god.
#
def f: . + 1; def g: def g: . + 100; f | g | f; (f | g), g
3.0
106.0
105.0
def f: (1000,2000); f
123412345
1000
2000
([1,2] + [4,5])
[1,2,3]
[1,2,4,5]
true
[1]
true
null,1,null
"hello"
null
1
null
[1,2,3]
[5,6]
[1,2,3]
def f(x): x | x; f([.], . + [42])
[1,2,3]
[[[1,2,3]]]
[[1,2,3],42]
[[1,2,3,42]]
[1,2,3,42,42]
# test closures and lexical scoping
def id(x):x; 2000 as $x | def f(x):1 as $x | id([$x, x, x]); def g(x): 100 as $x | f($x,$x+x); g($x)
"more testing"
[1,100,2100.0,100,2100.0]
# test backtracking through function calls and returns
# this test is *evil*
[[20,10][1,0] as $x | def f: (100,200) as $y | def g: [$x + $y, .]; . + $x | g; f[0] | [f][0][1] | f]
"woo, testing!"
[[110.0, 130.0], [210.0, 130.0], [110.0, 230.0], [210.0, 230.0], [120.0, 160.0], [220.0, 160.0], [120.0, 260.0], [220.0, 260.0]]
#
# Assignment
#
.message = "goodbye"
{"message": "hello"}
{"message": "goodbye"}
.foo = .bar
{"bar":42}
{"foo":42, "bar":42}
def inc(x): x |= .+1; inc(.[].a)
[{"a":1,"b":2},{"a":2,"b":4},{"a":7,"b":8}]
[{"a":2,"b":2},{"a":3,"b":4},{"a":8,"b":8}]