# Basic logical replication test
use strict;
use warnings;
use TestLib qw(command_like $windows_os);

use Test::More tests => 83;

my $exe = $windows_os ? ".exe" : "";

# We don't install test_json, so we pick it up from the
# place it  gets built, namely $ENV{TESTDIR}

my $test_json = "$ENV{TESTDIR}/test_json$exe";

ok(-f $test_json, "test_json file exists");
ok(-x $test_json, "test_json file is executable");

# Verify some valid JSON is accepted by our parser
command_like( [$test_json, q/null/       ], qr{\bVALID\b}, "null");
command_like( [$test_json, q/{}/         ], qr{\bVALID\b}, "empty object");
command_like( [$test_json, q/[]/         ], qr{\bVALID\b}, "empty array");
command_like( [$test_json, q/-12345/     ], qr{\bVALID\b}, "negative integer");
command_like( [$test_json, q/-1/         ], qr{\bVALID\b}, "negative integer");
command_like( [$test_json, q/0/          ], qr{\bVALID\b}, "zero");
command_like( [$test_json, q/1/          ], qr{\bVALID\b}, "positive integer");
command_like( [$test_json, q/12345/      ], qr{\bVALID\b}, "positive integer");
command_like( [$test_json, q/-1.23456789/], qr{\bVALID\b}, "negative float");
command_like( [$test_json, q/1.23456789/ ], qr{\bVALID\b}, "positive float");
command_like( [$test_json, q/{"a": "b"}/ ], qr{\bVALID\b}, "object");
command_like( [$test_json, q/["a", "b"]/ ], qr{\bVALID\b}, "array");
command_like( [$test_json, q/"pigs feet"/], qr{\bVALID\b}, 'text string');

# Verify some invalid JSON is rejected by our parser
command_like( [$test_json, q/{/          ], qr{\bINVALID\b}, 'unclosed object');
command_like( [$test_json, q/[/          ], qr{\bINVALID\b}, 'unclosed array');
command_like( [$test_json, q/(/          ], qr{\bINVALID\b}, 'unclosed parenthesis');
command_like( [$test_json, q/}/          ], qr{\bINVALID\b}, 'unopened object');
command_like( [$test_json, q/]/          ], qr{\bINVALID\b}, 'unopened array');
command_like( [$test_json, q/)/          ], qr{\bINVALID\b}, 'unopened parenthesis');
command_like( [$test_json, q/{{{}}/      ], qr{\bINVALID\b}, 'unbalanced object curlies');
command_like( [$test_json, q/{{}}}/      ], qr{\bINVALID\b}, 'unbalanced object curlies');
command_like( [$test_json, q/[[[]]/      ], qr{\bINVALID\b}, 'unbalanced array braces');
command_like( [$test_json, q/[[]]]/      ], qr{\bINVALID\b}, 'unbalanced array braces');
command_like( [$test_json, q/((())/      ], qr{\bINVALID\b}, 'unbalanced array braces');
command_like( [$test_json, q/(()))/      ], qr{\bINVALID\b}, 'unbalanced array braces');
command_like( [$test_json, q/1 7 13/     ], qr{\bINVALID\b}, 'integer sequence');
command_like( [$test_json, q/{"a", "b"}/ ], qr{\bINVALID\b}, 'mixed object and array syntax');
