File: text_facets.t

package info (click to toggle)
libbadger-perl 0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,400 kB
  • sloc: perl: 11,004; makefile: 9
file content (145 lines) | stat: -rw-r--r-- 4,656 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
#============================================================= -*-perl-*-
#
# t/data/text_facets.t
#
# Test the Badger::Data::Facets module for text facets.
#
# Written by Andy Wardley <abw@wardley.org>
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
#========================================================================

use lib qw( ./lib ../lib ../../lib );

#use Badger::Debug
#    modules => 'Badger::Factory';
    
use Badger::Test 
    tests => 30,
    debug => 'Badger::Data::Facet::Text',
    args  => \@ARGV;

use Badger::Data::Facets;
use constant FACETS => 'Badger::Data::Facets';


#-----------------------------------------------------------------------
# length
#-----------------------------------------------------------------------

my $length = FACETS->facet( 'text.length' => 23 );
ok( $length, 'got text.length facet from number' );
is( $length->length, 23, 'got length facet with value 23' );

$length = FACETS->facet( text_length => { length => 6 } );
ok( $length, 'got text_length facet from hash ref' );
is( $length->length, 6, 'got length facet with value 6' );

my $text = 'abcdef';
ok( $length->validate(\$text), 'text is 6 characters long' );

$text = 'abcde';
ok( ! $length->try( validate => \$text ), 'text is only 5 characters long' );
is( $length->reason->type, 
    'data.facet.text.length',
    'got short text error message' 
);
is( $length->reason->info, 
    'Text should be 6 characters long (got 5)', 
    'got short text error message' 
);


#-----------------------------------------------------------------------
# min_length
#-----------------------------------------------------------------------

$length = FACETS->facet( 'text.min_length' => 3 );
ok( $length, 'got text.min_length facet' );

$length = FACETS->facet( text_min_length => 3 );
ok( $length, 'got text_min_length facet' );

$text = 'ab';
ok( ! $length->try( validate => \$text ), 'min length fail on 2 characters' );
is( $length->reason->info, 'Text should be at least 3 characters long (got 2)', 'min length text reason' );

$text = 'abc';
ok( $length->validate(\$text), 'min length on 3 characters' );

$text = 'abcdef';
ok( $length->validate(\$text), 'min length on 6 characters' );


#-----------------------------------------------------------------------
# max_length
#-----------------------------------------------------------------------

$length = FACETS->facet( 'text.max_length' => 3 );
ok( $length, 'got text.max_length facet' );

$length = FACETS->facet( text_max_length => { max_length => 3 } );
ok( $length, 'got text_max_length facet' );

$text = 'abcd';
ok( ! $length->try( validate => \$text ), 'max length fail on 4 characters' );
is( $length->reason->info, 'Text should be at most 3 characters long (got 4)', 'max length text reason' );

$text = 'abc';
ok( $length->validate(\$text), 'max length on 3 characters' );

$text = 'ab';
ok( $length->validate(\$text), 'max length on 2 characters' );


#-----------------------------------------------------------------------
# pattern
#-----------------------------------------------------------------------

my $pattern = FACETS->facet( 'text.pattern' => '^\w+$' );
ok( $pattern, 'got pattern facet' );

$text = 'Hello World!';
ok( ! $pattern->try( validate => \$text ), 'pattern fail on 2 words' );
is( $pattern->reason->info, 'Text does not match pattern: ^\w+$', 'pattern fail reason' );

$text = 'foo';
ok( $pattern->validate(\$text), 'pattern match on foo' );


#-----------------------------------------------------------------------
# whitespace
#-----------------------------------------------------------------------

my $fold = FACETS->facet( 'text.whitespace' => 'fold' );
ok( $fold, 'got whitespace folding facet' );

$text = "Hello\nWorld!";
ok( $fold->validate(\$text), 'called whitespace folding facet' );
is( $text, 'Hello World!', 'folded whitespace' );

my $collapse = FACETS->facet( 'text.whitespace' => 'collapse' );
ok( $collapse, 'got whitespace collapsing facet' );

$text = "   \n\nHello\n\n\nBadger!\n\n\  ";
ok( $collapse->validate(\$text), 'called whitespace collapsing facet' );
is( $text, 'Hello Badger!', 'collapsed whitespace' );



__END__


#-----------------------------------------------------------------------
# any
#-----------------------------------------------------------------------

my $any = FACETS->facet( any => ['foo', 'bar'] );
ok( $any, 'got any facet' );
ok( ! $any->try( validate => 'baz' ), 'any fail on baz' );
is( $any->reason->info, 'Text does not match any of the permitted values: baz', 'any fail reason' );
ok( $any->validate('foo'), 'any match on foo' );