File: recursive_data_protection.t

package info (click to toggle)
libjson-validator-perl 5.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,160 kB
  • sloc: perl: 3,015; makefile: 14
file content (42 lines) | stat: -rw-r--r-- 1,122 bytes parent folder | download | duplicates (2)
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
use Mojo::Base -strict;
use JSON::Validator;
use JSON::Validator::Schema;
use Mojo::Util 'monkey_patch';
use Scalar::Util qw(refaddr);
use Test::More;

my ($original_validate, %ref_counts) = (\&JSON::Validator::Schema::_validate);
monkey_patch 'JSON::Validator::Schema', _validate => sub {
  my ($self, $data, $path, $schema) = @_;
  $ref_counts{refaddr($data)}++ if ref $data;
  goto &$original_validate;
};

for ([1, 1], [0, 3]) {
  my ($enabled, $exp_ref_counts) = @$_;
  my $object = {level1 => {level2 => {level3 => 'Test'}}};
  my $data   = [$object, $object, $object];

  %ref_counts = ();

  JSON::Validator->new->recursive_data_protection($enabled)->schema(schema())->validate($data);

  is $ref_counts{refaddr($object->{level1}{level2})}, $exp_ref_counts, "recursive_data_protection($enabled)";
}

done_testing;

sub schema {
  return {
    type  => 'array',
    items => {
      type       => 'object',
      properties => {
        level1 => {
          type       => 'object',
          properties => {level2 => {type => 'object', properties => {level3 => {type => 'string'}}}}
        }
      }
    }
  };
}