#!/bin/sh -ex

jose fmt -j '{}' -O
! jose fmt -j '{}' -A
! jose fmt -j '{}' -S
! jose fmt -j '{}' -I
! jose fmt -j '{}' -R
! jose fmt -j '{}' -N
! jose fmt -j '{}' -T
! jose fmt -j '{}' -F
! jose fmt -j '{}' -B
! jose fmt -j '{}' -0

! jose fmt -j '{}' -XO
jose fmt -j '{}' -XA
jose fmt -j '{}' -XS
jose fmt -j '{}' -XI
jose fmt -j '{}' -XR
jose fmt -j '{}' -XN
jose fmt -j '{}' -XT
jose fmt -j '{}' -XF
jose fmt -j '{}' -XB
jose fmt -j '{}' -X0

jose fmt -j '[]' -A
! jose fmt -j '[]' -XA
jose fmt -j '""' -S
! jose fmt -j '""' -XS
jose fmt -j '8' -I
! jose fmt -j '8' -XI
jose fmt -j '2.5' -R
! jose fmt -j '2.5' -XR
jose fmt -j '8' -N
! jose fmt -j '8' -XN
jose fmt -j 'true' -T
! jose fmt -j 'true' -XT
jose fmt -j 'false' -F
! jose fmt -j 'false' -XF
jose fmt -j 'true' -B
! jose fmt -j 'true' -XB
jose fmt -j 'null' -0
! jose fmt -j 'null' -X0

jose fmt -j 7 -j 7 -E
! jose fmt -j 7 -j 7 -XE
! jose fmt -j 7 -j 8 -E
! jose fmt -j 7 -j 8 -XE

jose fmt -j '"foo"' -j '"foo"' -E
! jose fmt -j '"foo"' -j '"foo"' -XE
! jose fmt -j '"foo"' -j '"bar"' -E
! jose fmt -j '"foo"' -j '"bar"' -XE

jose fmt -q foo -S -j '"foo"' -SE

! jose fmt -o-
test `jose fmt -j '{}' -o-` = "{}"
test `jose fmt -j '[1,2,3]' -f- | wc -l` = "3"
test `jose fmt -j '[1,2,3]' -f- | head -n 1` = "1"
test `jose fmt -j '[1,2,3]' -f- | tail -n 1` = "3"
test `jose fmt -j '{"a":1,"b":2}' -f- | wc -l` = "2"
test `jose fmt -j '{"a":1,"b":2}' -f- | head -n 1` = "a=1"
test `jose fmt -j '{"a":1,"b":2}' -f- | tail -n 1` = "b=2"

test "`jose fmt -j '"foo"' -u-`" = "foo"
! jose fmt -j 7 -u-

! jose fmt -c
test "`jose fmt -j '{}' -j '{"a":1}' -s x -j 7 -s a -UUo-`" = '{"x":{"a":7}}'
test "`jose fmt -j '{}' -j '{"a":1}' -s x -cj 7 -s a -UUUo-`" = '{"x":{"a":1}}'

test "`jose fmt -Qo-`" = "[]"
test "`jose fmt -j 7 -j 8 -j 9 -Qo-`" = "[9,8,7]"

! jose fmt -M 1
test "`jose fmt -j 1 -j 2 -j 3 -M 1 -o-`" = "2"
test "`jose fmt -j 1 -j 2 -j 3 -M 1 -Uo-`" = "3"
test "`jose fmt -j 1 -j 2 -j 3 -M 2 -o-`" = "2"
test "`jose fmt -j 1 -j 2 -j 3 -M 2 -Uo-`" = "1"
test "`jose fmt -j 1 -j 2 -j 3 -M 2 -UUo-`" = "3"

! jose fmt -t 0
! jose fmt -j 7 -t 0
! jose fmt -j '{}' -t 0
test "`jose fmt -j '[1,2,3]' -t 0 -lo-`" = "0"
test "`jose fmt -j '[1,2,3]' -t 1 -lo-`" = "1"
test "`jose fmt -j '[1,2,3]' -t 2 -lo-`" = "2"
test "`jose fmt -j '[1,2,3]' -t 3 -lo-`" = "3"
test "`jose fmt -j '[1,2,3]' -t 4 -lo-`" = "3"

! jose fmt -i 0
! jose fmt -j '[]' -i 0
! jose fmt -j 7 -j 8 -i 0
test "`jose fmt -j '[1,2]' -j 3 -i 0 -Uo-`" = "[3,1,2]"
test "`jose fmt -j '[1,2]' -j 3 -i 1 -Uo-`" = "[1,3,2]"
test "`jose fmt -j '[1,2]' -j 3 -i 2 -Uo-`" = "[1,2,3]"

! jose fmt -a
! jose fmt -j '[]' -a
! jose fmt -j 7 -j 8 -a
test "`jose fmt -j '[1,2]' -j 3 -aUo-`" = "[1,2,3]"
test "`jose fmt -j '{"foo":1}' -j '{"foo":2,"bar":2}' -aUo-`" = '{"bar":2,"foo":1}'

! jose fmt -x
! jose fmt -j '[]' -x
! jose fmt -j 7 -j 8 -x
test "`jose fmt -j '[1,2]' -j '[3,4]' -xUo-`" = "[1,2,3,4]"
test "`jose fmt -j '{"foo":1}' -j '{"foo":2,"bar":2}' -xUo-`" = '{"bar":2,"foo":2}'

! jose fmt -d 0
! jose fmt -j 7 -d 0
! jose fmt -j '[]' -d 0
test "`jose fmt -j '[1,2]' -d 0 -o-`" = "[2]"
test "`jose fmt -j '[1,2]' -d 1 -o-`" = "[1]"

! jose fmt -l
! jose fmt -j 7 -l
test "`jose fmt -j '{}' -lo-`" = "0" 
test "`jose fmt -j '{"foo":1}' -lo-`" = "1"
test "`jose fmt -j '{"foo":1,"bar":2}' -lo-`" = "2"

! jose fmt -e
! jose fmt -j 7 -e
test "`jose fmt -j '[1,2,3,4]' -eo-`" = "[]"
test "`jose fmt -j '{"foo":1}' -eo-`" = "{}"

! jose fmt -g bar
! jose fmt -g 0
! jose fmt -j 7 -g bar
! jose fmt -j 7 -g 0
! jose fmt -j '{"foo":1}' -g bar
! jose fmt -j '[]' -g 0
test "`jose fmt -j '{"foo":1}' -g foo -o-`" = "1"
test "`jose fmt -j '[1]' -g 0 -o-`" = "1"

! jose fmt -s foo
! jose fmt -s 0
! jose fmt -j '{}' -s foo
! jose fmt -j '[]' -s 0
! jose fmt -j 7 -j 8 -s foo
! jose fmt -j 7 -j 8 -s 0
! jose fmt -j '[]' -j 8 -s 0
test "`jose fmt -j '{}' -j 7 -s "foo" -Uo-`" = '{"foo":7}'
test "`jose fmt -j '[1,2]' -j 7 -s 0 -Uo-`" = '[7,2]'
test "`jose fmt -j '[1,2]' -j 7 -s 1 -Uo-`" = '[1,7]'

! jose fmt -y
! jose fmt -Y
test "`jose fmt -j '{}' -YSu-`" = "e30"
test "`jose fmt -j '"e30"' -yOo-`" = "{}"
