File: 51-typetiny-validation.t

package info (click to toggle)
libdata-tablereader-perl 0.021-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 392 kB
  • sloc: perl: 2,340; makefile: 2; sh: 1
file content (59 lines) | stat: -rw-r--r-- 2,046 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
#! /usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Try::Tiny;
use File::Spec::Functions 'catfile';
use Log::Any '$log';
use Log::Any::Adapter 'TAP', filter => 'warn';
use Data::TableReader;

plan skip_all => 'Requires Types::Standard to be installed'
	unless eval { require Types::Standard };

my $camelcase= Types::Standard::Str()
	->where(sub { /^[A-Z][a-z]*([0-9]|[A-Z][a-z]*)*\Z/ })
	->plus_coercions(Types::Standard::Str() => sub {
		(my $x= $_[0]) =~ s/_([a-z0-9])/uc($1)/ge;
		$x =~ s/^([a-z])/uc($1)/e;
		$x;
	});

subtest trim_validate_coerce => sub {
	my $tr= new_ok( 'Data::TableReader', [
			input => [
                           [ 'id', 'name   ', 'description' ],
                           [ '1 ', '   test', '' ],
                           [ '2',  'Test_2', 'Some details' ],
                           [ '3',  'test_three ', '  yada yada' ],
                           [ '4',  "can't coerce", '' ],
                           [ ' ',  'cant_coerce_id', '' ],
                        ],
			fields => [
				{ name => 'id', trim => 1, type => Types::Standard::Int() },
				{ name => 'name', trim => 1, type => $camelcase, coerce => 1 },
				{ name => 'description', trim => 1, type => Types::Standard::Maybe([Types::Standard::Str()]) },
			],
			on_validation_error => sub {
				my ($reader, $failures, $record, $data_iter)= @_;
				for (@$failures) {
					my ($field, $value_ref, $message)= @$_;
					$$value_ref= [ $$value_ref, $message ];
				}
				return 'use';
			},
			log => $log,
		], 'TableReader' );
	my $i= $tr->iterator;
	is_deeply( $i->all,
		[
			{ id => 1, name => 'Test', description => undef },
			{ id => 2, name => 'Test2', description => 'Some details' },
			{ id => 3, name => 'TestThree', description => 'yada yada' },
			{ id => 4, name => [ "can't coerce", q{Value "can't coerce" did not pass type constraint} ], description => undef },
			{ id => [ undef, q{Undef did not pass type constraint "Int"} ], name => 'CantCoerceId', description => undef },
		],
		'valid row' );
};

done_testing;