1#!/usr/local/bin/perl -w
2
3use strict;
4
5use Test::More tests => 20;
6
7BEGIN
8{
9  use_ok('Rose::HTML::Form::Field::Set');
10}
11
12my $field =
13  Rose::HTML::Form::Field::Set->new(
14    label       => 'Set',
15    description => 'set',
16    default     => [ qw(a b c), "one one", q(two " two), '"three"' ]);
17
18my $vals = $field->internal_value;
19
20ok(ref $vals eq 'ARRAY' &&
21   @$vals == 6 &&
22   $vals->[0] eq 'a' &&
23   $vals->[1] eq 'b' &&
24   $vals->[2] eq 'c' &&
25   $vals->[3] eq 'one one' &&
26   $vals->[4] eq 'two " two' &&
27   $vals->[5] eq '"three"',
28   'default 1');
29
30is($field->output_value, 'a, b, c, "one one", "two \" two", "\"three\""', 'output_value 1');
31
32$field->input_value(q(,"foo",bar , "baz \"\nboo", 123,"",,,,));
33
34$vals = $field->internal_value;
35
36ok(ref $vals eq 'ARRAY' &&
37   @$vals == 5 &&
38   $vals->[0] eq 'foo' &&
39   $vals->[1] eq 'bar' &&
40   $vals->[2] eq "baz \"\nboo" &&
41   $vals->[3] eq '123' &&
42   $vals->[4] eq '',
43   'default 2');
44
45is($field->output_value, qq(foo, bar, "baz \\"\nboo", 123, ), 'output_value 2');
46
47$field->input_value(q( , , "foo" ,  bar, "baz \"\nboo" ,123,, ""  ,,,,));
48
49$vals = $field->internal_value;
50
51ok(ref $vals eq 'ARRAY' &&
52   @$vals == 5 &&
53   $vals->[0] eq 'foo' &&
54   $vals->[1] eq 'bar' &&
55   $vals->[2] eq "baz \"\nboo" &&
56   $vals->[3] eq '123' &&
57   $vals->[4] eq '',
58   'default 3');
59
60is($field->output_value, qq(foo, bar, "baz \\"\nboo", 123, ), 'output_value 3');
61
62$field->input_value([ qw(a b c), "one one", q(two " two), '"three"' ]);
63
64$vals = $field->internal_value;
65
66ok(ref $vals eq 'ARRAY' &&
67   @$vals == 6 &&
68   $vals->[0] eq 'a' &&
69   $vals->[1] eq 'b' &&
70   $vals->[2] eq 'c' &&
71   $vals->[3] eq 'one one' &&
72   $vals->[4] eq 'two " two' &&
73   $vals->[5] eq '"three"',
74   'default 4');
75
76is($field->output_value, 'a, b, c, "one one", "two \" two", "\"three\""', 'output_value 4');
77
78$field->default(q(" hello world "));
79
80$field->reset;
81
82$vals = $field->internal_value;
83
84ok(ref $vals eq 'ARRAY' &&
85   @$vals == 1 &&
86   $vals->[0] eq ' hello world ',
87   'default 5');
88
89is($field->output_value, '" hello world "', 'output_value 5');
90
91$field->input_value(q("\\" hello world \\""));
92
93$vals = $field->internal_value;
94
95ok(ref $vals eq 'ARRAY' &&
96   @$vals == 1 &&
97   $vals->[0] eq '" hello world "',
98   'default 6');
99
100is($field->output_value, '"\" hello world \""', 'output_value 6');
101
102$field->input_value(' ABC, DEF GHI, JKL MN ');
103
104$vals = $field->internal_value;
105
106ok(ref $vals eq 'ARRAY' &&
107   @$vals == 5 &&
108   $vals->[0] eq 'ABC' &&
109   $vals->[1] eq 'DEF' &&
110   $vals->[2] eq 'GHI' &&
111   $vals->[3] eq 'JKL' &&
112   $vals->[4] eq 'MN',
113   'default 7');
114
115is($field->output_value, 'ABC, DEF, GHI, JKL, MN', 'output_value 7');
116
117$field->input_value([ 'AB,C', 'D EF', 'G\H', 'I"J' ]);
118
119$vals = $field->internal_value;
120
121ok(ref $vals eq 'ARRAY' &&
122   @$vals == 4 &&
123   $vals->[0] eq 'AB,C' &&
124   $vals->[1] eq 'D EF' &&
125   $vals->[2] eq 'G\H' &&
126   $vals->[3] eq 'I"J',
127   'default 8');
128
129is($field->output_value, '"AB,C", "D EF", "G\\\\H", "I\"J"', 'output_value 8');
130
131$field->input_value(qq("""));
132
133ok(!$field->validate, 'validate 1');
134is($field->error, 'Could not parse input: parse error at [..."]', 'error 1');
135
136SET_FIELD_BUG:
137{
138  package TestForm;
139  use base 'Rose::HTML::Form';
140  sub build_form { shift->add_fields(testfld => { type  => 'set' }); }
141
142  package main;
143  my $f = TestForm->new;
144  eval { $f->field('testfld')->xhtml_hidden_field };
145  ok(!$@, 'empty set');
146}
147