File: 09-unicode.t

package info (click to toggle)
libtest-json-schema-acceptance-perl 1.003%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 652 kB
  • sloc: perl: 521; makefile: 2
file content (97 lines) | stat: -rw-r--r-- 3,157 bytes parent folder | download
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
# vim: set ts=8 sts=2 sw=2 tw=100 et :
use strict;
use warnings;
no if "$]" >= 5.031009, feature => 'indirect';
use utf8;
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8

use Test::More 0.88;
use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
use Test::JSON::Schema::Acceptance;
use lib 't/lib';
use SchemaParser;

my $accepter = Test::JSON::Schema::Acceptance->new(test_dir => 't/tests/unicode');
my $parser = SchemaParser->new;

$accepter->acceptance(
  tests => {
    file => 'unicode.json',
    group_description => 'latin1 schema',
    test_description => 'latin1 data',
  },
  validate_data => sub {
    my ($schema, $data) = @_;
    note 'validate_data passed data "'.$data.'", schema "'.$schema->{const}.'"';

    is(index($schema->{const}, 'Les hivers de mon enfance étaient'), 0,
        'schema was decoded from data file correctly')
      &&
    is(index($data, 'Les hivers de mon enfance étaient'), 0, 'data was decoded from file correctly')
      &&
    is($data, $schema->{const}, 'data and schema decode identically');
  },
);

$accepter->acceptance(
  tests => {
    file => 'unicode.json',
    group_description => 'very wide schema',
    test_description => 'very wide data',
  },
  validate_data => sub {
    my ($schema, $data) = @_;
    note 'validate_data passed data "'.$data.'", schema "'.$schema->{const}.'"';

    is($schema->{const}, 'ಠ_ಠ', 'schema was decoded from data file correctly')
      &&
    is($data, 'ಠ_ಠ', 'data was decoded from file correctly, and properly passed characters that occupy multiple bytes in unicode')
      &&
    is($data, $schema->{const}, 'data and schema decode identically');
  },
);


$accepter->acceptance(
  tests => {
    file => 'unicode.json',
    group_description => 'latin1 schema',
    test_description => 'latin1 data',
  },
  validate_json_string => sub {
    my ($schema, $data) = @_;
    note 'validate_data passed data "'.$data.'", schema "'.$schema->{const}.'"';

    is(index($schema->{const}, 'Les hivers de mon enfance étaient'), 0,
        'schema was decoded from data file correctly')
      &&
    is(index($data, "\"Les hivers de mon enfance \x{c3}\x{a9}taient"), 0,
      'data contains utf8-encoded data (latin-1 character is encoded as two bytes in utf8')
      &&
    is(JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1)->decode($data), $schema->{const},
      'data can be decoded and compares correctly');
  },
);

$accepter->acceptance(
  tests => {
    file => 'unicode.json',
    group_description => 'very wide schema',
    test_description => 'very wide data',
  },
  validate_json_string => sub {
    my ($schema, $data) = @_;
    note 'validate_data passed data "'.$data.'", schema "'.$schema->{const}.'"';

    is($schema->{const}, 'ಠ_ಠ', 'schema was decoded from data file correctly')
      &&
    is($data, "\"\x{e0}\x{b2}\x{a0}_\x{e0}\x{b2}\x{a0}\"",
      'data contains utf8-encoded data (each character is encoded as three bytes in utf8')
      &&
    is(JSON::MaybeXS->new(utf8 => 1, allow_nonref => 1)->decode($data), $schema->{const},
      'data can be decoded and compares correctly');
  },
);

done_testing;