File: Helper.pm

package info (click to toggle)
libjson-validator-perl 4.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 828 kB
  • sloc: perl: 2,816; makefile: 14
file content (183 lines) | stat: -rw-r--r-- 6,566 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package t::Helper;
use Mojo::Base -base;

use JSON::Validator;
use Mojo::File;
use Mojo::JSON qw(decode_json encode_json);
use Mojo::Util qw(monkey_patch);
use Test::More;

$ENV{TEST_VALIDATOR_CLASS} = 'JSON::Validator';

sub acceptance {
  my ($class, $schema_class, %acceptance_params) = @_;

  Test::More::plan(skip_all => 'cpanm Test::JSON::Schema::Acceptance')
    unless eval 'use Test::JSON::Schema::Acceptance 1.000 ();1';
  Test::More::plan(skip_all => 'cpanm Test2::Tools::Compare') unless eval 'use Test2::Tools::Compare 0.0001 ();1';
  Test::More::plan(skip_all => $@)                            unless eval "require $schema_class;1";

  my $test = sub { +{file => $_[0], group_description => $_[1], test_description => $_[2]} };
  my $ua   = _acceptance_ua($schema_class);

  $acceptance_params{todo_tests} = [map { $test->(@$_) } @{$acceptance_params{todo_tests}}]
    if $acceptance_params{todo_tests};

  my $specification = $schema_class =~ m!::(\w+)$! ? lc $1 : 'unknown';
  $specification = 'draft2019-09' if $specification eq 'draft201909';
  Test::JSON::Schema::Acceptance->new(specification => $specification)->acceptance(
    tests => $test->(split '/', $ENV{TEST_ACCEPTANCE} || ''),
    %acceptance_params,
    validate_data => sub {
      my ($schema_p, $data_p) = map { Mojo::JSON::Pointer->new(shift @_) } qw(schema data);
      my ($schema_d, $data_d) = map { clone($_->data) } $schema_p, $data_p;

      my $schema = $schema_class->new($schema_d, ua => $ua);
      return 0 if @{$schema->errors};

      my @errors = $schema->validate($data_d);

      # Doing internal tests on mutation, since I think Test::JSON::Schema::Acceptance is a bit too strict
      Test2::Tools::Compare::is(encode_json($data_d),   encode_json($data_p->data),   'data structure is the same');
      Test2::Tools::Compare::is(encode_json($schema_d), encode_json($schema_p->data), 'schema structure is the same')
        unless _skip_schema_is($schema_p);

      return @errors ? 0 : 1;
    },
  );
}

sub clone {
  return decode_json(encode_json($_[0]));
}

sub edj {
  return Mojo::JSON::decode_json(Mojo::JSON::encode_json(@_));
}

sub joi_ok {
  my ($data, $joi, @expected) = @_;
  my $description ||= @expected ? "errors: @expected" : "valid: " . encode_json($data);
  my @errors = JSON::Validator::Joi->new($joi)->validate($data);
  Test::More::is_deeply([map { $_->TO_JSON } sort { $a->path cmp $b->path } @errors],
    [map { $_->TO_JSON } sort { $a->path cmp $b->path } @expected], $description)
    or Test::More::diag(encode_json(\@errors));
}

sub jv { state $obj = $ENV{TEST_VALIDATOR_CLASS}->new }

sub schema { state $schema; $schema = $_[1] if $_[1]; $schema }

sub schema_validate_ok {
  my ($data, $schema, @expected) = @_;
  my $description = @expected ? "errors: @expected" : "valid: " . encode_json($data);

  my @errors = t::Helper->schema->resolve($schema)->validate($data);
  local $Test::Builder::Level = $Test::Builder::Level + 1;
  Test::More::is_deeply([map { $_->TO_JSON } sort { $a->path cmp $b->path } @errors],
    [map { $_->TO_JSON } sort { $a->path cmp $b->path } @expected], $description)
    or Test::More::diag(encode_json(\@errors));
}

sub test {
  my ($class, $category, @methods) = @_;
  my $test_class = "t::test::$category";
  eval "require $test_class;1" or die $@;
  subtest "$category $_", sub { $test_class->$_ }
    for @methods;
}

sub validate_ok {
  my ($data, $schema, @expected) = @_;
  my $description = @expected ? "errors: @expected" : "valid: " . encode_json($data);
  my @errors      = jv()->schema($schema)->validate($data);
  local $Test::Builder::Level = $Test::Builder::Level + 1;
  Test::More::is_deeply([map { $_->TO_JSON } sort { $a->path cmp $b->path } @errors],
    [map { $_->TO_JSON } sort { $a->path cmp $b->path } @expected], $description)
    or Test::More::diag(encode_json(\@errors));
}

sub import {
  my $class  = shift;
  my $caller = caller;

  eval "package $caller; use Test::Deep; use Test::More; 1" or die $@;
  $_->import for qw(strict warnings);
  feature->import(':5.10');

  monkey_patch $caller => E                  => \&JSON::Validator::E;
  monkey_patch $caller => done_testing       => \&Test::More::done_testing;
  monkey_patch $caller => edj                => \&edj;
  monkey_patch $caller => false              => \&Mojo::JSON::false;
  monkey_patch $caller => joi_ok             => \&joi_ok;
  monkey_patch $caller => jv                 => \&jv;
  monkey_patch $caller => schema_validate_ok => \&schema_validate_ok;
  monkey_patch $caller => true               => \&Mojo::JSON::true;
  monkey_patch $caller => validate_ok        => \&validate_ok;
}

sub _acceptance_ua {
  my $schema_class = shift;
  require Mojo::UserAgent;
  require Mojolicious;
  my $ua  = Mojo::UserAgent->new;
  my $app = Mojolicious->new;

  $app->static->paths([Mojo::File->new(qw(t spec remotes))->to_string]);
  $ua->server->app($app);

  $ua->on(
    $_ => sub {
      my ($ua, $tx) = @_;
      my $url = $tx->req->url;
      $url->scheme(undef)->host(undef)->port(undef) if $url->host and $url->host eq 'localhost';
    }
  ) for qw(prepare start);

  my $app_base_url = $ua->get('/')->req->url->to_abs->to_string;
  $app_base_url =~ s!/$!!;

  my $orig_load_schema = $schema_class->can('_load_schema');
  monkey_patch $schema_class => _load_schema => sub {
    my ($self, $url) = @_;
    my $cached;
    return $cached, $url if $cached = $self->_store($url);
    $url =~ s!^https?://localhost:1234!$app_base_url!;
    return $self->$orig_load_schema($url);
  };

  #my $orig_resolve_ref = $schema_class->can('_resolve_ref');
  #monkey_patch $schema_class => _resolve_ref => sub {
  #  my ($self, $ref_url, $base_url, $schema) = @_;
  #  $ref_url  =~ s!^https?://localhost:1234!$app_base_url!;
  #  $base_url =~ s!^https?://localhost:1234!$app_base_url!;
  #  $self->$orig_resolve_ref($ref_url, $base_url, $schema);
  #};

  #my $orig_store       = $schema_class->can('_store');
  #monkey_patch $schema_class => _store => sub {
  #  my ($self, $id, $schema) = @_;
  #  $id =~ s!^https?://localhost:1234!$app_base_url!;
  #  $self->$orig_store($id, $schema);
  #};

  return $ua;
}

sub _skip_schema_is {
  my $p     = shift;
  my @paths = ('', '/properties/foo');

  # The URL has been changed by _acceptance_ua()
  return 1 if encode_json($p->data) =~ m!localhost:1234!;

  # JSON::Validator always normalizes $ref with multiple keys
  for my $path (@paths) {
    my $ref = $p->get($path);
    return 1 if ref $ref eq 'HASH' && $ref->{'$ref'} && 1 != keys %$ref;
  }

  return 0;
}

1;