-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·108 lines (86 loc) · 2.51 KB
/
test.sh
File metadata and controls
executable file
·108 lines (86 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env bash
# Test suite for jsonl
# Run: ./test.sh
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
JSONL="$SCRIPT_DIR/jsonl"
pass=0
fail=0
assert_eq() {
local name="$1"
local expected="$2"
local actual="$3"
if [ "$expected" = "$actual" ]; then
echo "PASS: $name"
pass=$((pass + 1))
else
echo "FAIL: $name"
echo " expected: $expected"
echo " actual: $actual"
fail=$((fail + 1))
fi
}
assert_exit() {
local name="$1"
local expected_exit="$2"
local cmd="$3"
set +e
eval "$cmd" >/dev/null 2>&1
local actual_exit=$?
set -e
if [ "$expected_exit" = "$actual_exit" ]; then
echo "PASS: $name (exit $actual_exit)"
pass=$((pass + 1))
else
echo "FAIL: $name"
echo " expected exit: $expected_exit"
echo " actual exit: $actual_exit"
fail=$((fail + 1))
fi
}
echo "=== jsonl test suite ==="
echo ""
# Test: count
result=$(printf '{"a":1}\n{"b":2}\n{"c":3}\n' | "$JSONL" count)
assert_eq "count: 3 lines" '{"type":"summary","count":3}' "$result"
result=$(printf '' | "$JSONL" count)
assert_eq "count: 0 lines" '{"type":"summary","count":0}' "$result"
# Test: validate
result=$(printf '{"valid":true}\n' | "$JSONL" validate)
assert_eq "validate: valid JSONL" '{"type":"summary","valid":true,"lines":1}' "$result"
assert_exit "validate: invalid JSONL exits 1" 1 "printf 'not json\n' | \"$JSONL\" validate"
# Test: head
result=$(printf '{"n":1}\n{"n":2}\n{"n":3}\n' | "$JSONL" head 2)
expected='{"n":1}
{"n":2}'
assert_eq "head: first 2 lines" "$expected" "$result"
# Test: tail
result=$(printf '{"n":1}\n{"n":2}\n{"n":3}\n' | "$JSONL" tail 2)
expected='{"n":2}
{"n":3}'
assert_eq "tail: last 2 lines" "$expected" "$result"
# Test: get
result=$(printf '{"name":"alice","age":30}\n{"name":"bob","age":25}\n' | "$JSONL" get name)
expected='"alice"
"bob"'
assert_eq "get: extract field" "$expected" "$result"
# Test: compact
result=$(printf '{"a": 1, "b": 2}\n' | "$JSONL" compact)
assert_eq "compact: remove whitespace" '{"a":1,"b":2}' "$result"
# Test: help
assert_exit "help: exits 1" 1 "\"$JSONL\" --help"
# Test: unknown command
assert_exit "unknown command: exits 1" 1 "printf '' | \"$JSONL\" foobar"
# Test: keys
result=$(printf '{"a":1,"b":2}\n{"a":3,"c":4}\n' | "$JSONL" keys)
expected='a
b
c'
assert_eq "keys: unique keys sorted" "$expected" "$result"
echo ""
echo "=== Results ==="
echo "Passed: $pass"
echo "Failed: $fail"
if [ "$fail" -gt 0 ]; then
exit 1
fi