<commit_before>#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::Exception;
use YAML qw/DumpFile LoadFile/;
use XML::Simple;
use File::Slurp;
use Module::Runtime;

use Paws;

my $debug = $ENV{DEBUG_TESTS} || 0;
my $aws = Paws->new();

use Data::Dumper;

my $dir = 't/10_responses';
opendir(my $dh, $dir);
my @files = @ARGV;
if (not @files) {
  @files = map { "$dir/$_" } grep { $_ =~ m/\.response$/ } sort readdir($dh);
} else {
  @files = grep { $_ =~ m/\.response$/ } @files;
}

BAIL_OUT("No test cases to execute") if (not @files);

foreach my $file (@files) {
  test_file($file);
}

done_testing;

sub test_file {
  my ($file) = @_;

  my $mode = 1;
  my $test_def_file = "$file.test.yml";

  my $test = LoadFile($test_def_file);

  SKIP: {
    skip "$test_def_file is lacking service or call entry",1 if (not $test->{service} or not $test->{call});
    local $TODO = "$test_def_file is TODO: $test->{todo}" if (defined $test->{todo});

    my $service = $aws->service($test->{service},
      region => 'fake_region',
    );

    my $call_class = $service->meta->name . '::' . $test->{ call };
    Paws->load_class($call_class);

    my $res;
    my $passed = lives_ok {
      my $content = read_file($file);
      my $unserialized_struct = $service->unserialize_response( $content );

      if ($debug){
        diag("DATASTRUCUTRE FROM RESPONSE");
        diag(Dumper($unserialized_struct));
      }

      $res = $service->response_to_object($unserialized_struct, $call_class);
    } "Call $test->{service}\-\>$test->{ call } from $file";

    diag(Dumper($res)) if ($debug);

    if (not $passed) {
      ok(0, "Can't test method access because something went horribly wrong in the call to $test->{ call }");
      next;
    }

    next if (ref($test->{ tests }) ne 'ARRAY');
    foreach my $t (@{ $test->{ tests } }){
      my $got;
      my $path;
      if (defined $t->{path}){
        $path = $t->{path};
        $got = eval { resolve_path($t->{path}, $res) };
        if ($@) {
          my $message = $@;
          chomp $message;
          ok(0, "Exception accessing $t->{path}: $message");<commit_msg>Skip the inner tests if the test is marked as TODO<commit_after>#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;
use Test::Exception;
use YAML qw/DumpFile LoadFile/;
use XML::Simple;
use File::Slurp;
use Module::Runtime;

use Paws;

my $debug = $ENV{DEBUG_TESTS} || 0;
my $aws = Paws->new();

use Data::Dumper;

my $dir = 't/10_responses';
opendir(my $dh, $dir);
my @files = @ARGV;
if (not @files) {
  @files = map { "$dir/$_" } grep { $_ =~ m/\.response$/ } sort readdir($dh);
} else {
  @files = grep { $_ =~ m/\.response$/ } @files;
}

BAIL_OUT("No test cases to execute") if (not @files);

foreach my $file (@files) {
  test_file($file);
}

done_testing;

sub test_file {
  my ($file) = @_;

  my $mode = 1;
  my $test_def_file = "$file.test.yml";

  my $test = LoadFile($test_def_file);

  SKIP: {
    skip "$test_def_file is lacking service or call entry",1 if (not $test->{service} or not $test->{call});
    local $TODO = "$test_def_file is TODO: $test->{todo}" if (defined $test->{todo});

    my $service = $aws->service($test->{service},
      region => 'fake_region',
    );

    my $call_class = $service->meta->name . '::' . $test->{ call };
    Paws->load_class($call_class);

    my $res;
    my $passed = lives_ok {
      my $content = read_file($file);
      my $unserialized_struct = $service->unserialize_response( $content );

      if ($debug){
        diag("DATASTRUCUTRE FROM RESPONSE");
        diag(Dumper($unserialized_struct));
      }

      $res = $service->response_to_object($unserialized_struct, $call_class);
    } "Call $test->{service}\-\>$test->{ call } from $file";

    diag(Dumper($res)) if ($debug);

    if (not $passed or $TODO) {
      ok(0, "Can't test method access because something went horribly wrong in the call to $test->{ call }");
      next;
    }

    next if (ref($test->{ tests }) ne 'ARRAY');
    foreach my $t (@{ $test->{ tests } }){
      my $got;
      my $path;
      if (defined $t->{path}){
        $path = $t->{path};
        $got = eval { resolve_path($t->{path}, $res) };
        if ($@) {
          my $message = $@;
          chomp $message;
          ok(0, "Exception accessing $t->{path}: $message");
{'fraction_non_alphanumeric': 0.18877079482439926, 'fraction_numerical': 0.003234750462107209, 'mean_word_length': 2.9972299168975067, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2175908', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1554, 'n_words': 411}