| 1 |
# vim: set ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use 5.020; |
| 5 |
use strictures 2; |
| 6 |
use stable 0.031 'postderef'; |
| 7 |
use experimental 'signatures'; |
| 8 |
no autovivification warn => qw(fetch store exists delete); |
| 9 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 10 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 11 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 12 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 13 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 14 |
no feature 'switch'; |
| 15 |
use utf8; |
| 16 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 17 |
|
| 18 |
use Test2::V0 -no_pragmas => 1; |
| 19 |
use if $ENV{AUTHOR_TESTING}, 'Test2::Warnings'; |
| 20 |
use Test::JSON::Schema::Acceptance; |
| 21 |
use Test::File::ShareDir -share => { -dist => { 'Test-JSON-Schema-Acceptance' => 'share' } }; |
| 22 |
|
| 23 |
use lib 't/lib'; |
| 24 |
use SchemaParser; |
| 25 |
|
| 26 |
my $accepter = Test::JSON::Schema::Acceptance->new(test_dir => 't/tests/unicode'); |
| 27 |
my $parser = SchemaParser->new; |
| 28 |
|
| 29 |
$accepter->acceptance( |
| 30 |
tests => { |
| 31 |
file => 'unicode.json', |
| 32 |
group_description => 'latin1 schema', |
| 33 |
test_description => 'latin1 data', |
| 34 |
}, |
| 35 |
validate_data => sub ($schema, $data) { |
| 36 |
note 'validate_data passed data "'.$data.'", schema "'.$schema->{const}.'"'; |
| 37 |
|
| 38 |
is(index($schema->{const}, 'Les hivers de mon enfance étaient'), 0, |
| 39 |
'schema was decoded from data file correctly') |
| 40 |
&& |
| 41 |
is(index($data, 'Les hivers de mon enfance étaient'), 0, 'data was decoded from file correctly') |
| 42 |
&& |
| 43 |
is($data, $schema->{const}, 'data and schema decode identically'); |
| 44 |
}, |
| 45 |
); |
| 46 |
|
| 47 |
$accepter->acceptance( |
| 48 |
tests => { |
| 49 |
file => 'unicode.json', |
| 50 |
group_description => 'very wide schema', |
| 51 |
test_description => 'very wide data', |
| 52 |
}, |
| 53 |
validate_data => sub ($schema, $data) { |
| 54 |
note 'validate_data passed data "'.$data.'", schema "'.$schema->{const}.'"'; |
| 55 |
|
| 56 |
is($schema->{const}, 'ಠ_ಠ', 'schema was decoded from data file correctly') |
| 57 |
&& |
| 58 |
is($data, 'ಠ_ಠ', 'data was decoded from file correctly, and properly passed characters that occupy multiple bytes in unicode') |
| 59 |
&& |
| 60 |
is($data, $schema->{const}, 'data and schema decode identically'); |
| 61 |
}, |
| 62 |
); |
| 63 |
|
| 64 |
|
| 65 |
$accepter->acceptance( |
| 66 |
tests => { |
| 67 |
file => 'unicode.json', |
| 68 |
group_description => 'latin1 schema', |
| 69 |
test_description => 'latin1 data', |
| 70 |
}, |
| 71 |
validate_json_string => sub ($schema, $data) { |
| 72 |
note 'validate_data passed data "'.$data.'", schema "'.$schema->{const}.'"'; |
| 73 |
|
| 74 |
is(index($schema->{const}, 'Les hivers de mon enfance étaient'), 0, |
| 75 |
'schema was decoded from data file correctly') |
| 76 |
&& |
| 77 |
is(index($data, "\"Les hivers de mon enfance \x{c3}\x{a9}taient"), 0, |
| 78 |
'data contains utf8-encoded data (latin-1 character is encoded as two bytes in utf8') |
| 79 |
&& |
| 80 |
is( |
| 81 |
Test::JSON::Schema::Acceptance::_JSON_BACKEND->new->utf8(1)->allow_nonref(1)->decode($data), |
| 82 |
$schema->{const}, |
| 83 |
'data can be decoded and compares correctly', |
| 84 |
); |
| 85 |
}, |
| 86 |
); |
| 87 |
|
| 88 |
$accepter->acceptance( |
| 89 |
tests => { |
| 90 |
file => 'unicode.json', |
| 91 |
group_description => 'very wide schema', |
| 92 |
test_description => 'very wide data', |
| 93 |
}, |
| 94 |
validate_json_string => sub ($schema, $data) { |
| 95 |
note 'validate_data passed data "'.$data.'", schema "'.$schema->{const}.'"'; |
| 96 |
|
| 97 |
is($schema->{const}, 'ಠ_ಠ', 'schema was decoded from data file correctly') |
| 98 |
&& |
| 99 |
is($data, "\"\x{e0}\x{b2}\x{a0}_\x{e0}\x{b2}\x{a0}\"", |
| 100 |
'data contains utf8-encoded data (each character is encoded as three bytes in utf8') |
| 101 |
&& |
| 102 |
is(Test::JSON::Schema::Acceptance::_JSON_BACKEND->new->utf8(1)->allow_nonref(1)->decode($data), $schema->{const}, |
| 103 |
'data can be decoded and compares correctly'); |
| 104 |
}, |
| 105 |
); |
| 106 |
|
| 107 |
done_testing; |