File: 1d-messages.t

package info (click to toggle)
libcgi-formbuilder-perl 3.03.01-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 960 kB
  • ctags: 250
  • sloc: perl: 5,116; makefile: 7; sh: 7
file content (125 lines) | stat: -rwxr-xr-x 3,905 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
#!/usr/bin/perl -Ilib -I../lib

# Copyright (c) 2000-2006 Nathan Wiger <nate@wiger.org>.
# All Rights Reserved. If you're reading this, you're bored.
# 1d-messages.t - messages and localization

use strict;
use vars qw($TESTING $DEBUG);
$TESTING = 1;
$DEBUG = $ENV{DEBUG} || 0;
use Test;

# use a BEGIN block so we print our plan before CGI::FormBuilder is loaded
my @pm;
BEGIN { 
    # try to load all the .pm's except templates from MANIFEST
    open(M, "<MANIFEST") || warn "Can't open MANIFEST ($!) - skipping imports";
    chomp(@pm = grep m#Messages/[a-z]+_.*#, grep /\.pm$/, <M>);
    close(M);

    #
    # There are 34 keys, times the number of modules, plus one load of the module.
    # Then, also add in our 30 custom tests as well.
    #

    my $numtests = (34 * @pm) + @pm + 34;

    plan tests => $numtests;

    # success if we said NOTEST
    if ($ENV{NOTEST}) {
        ok(1) for 1..$numtests;
        exit;
    }
}

# Messages, both inline and file
my %messages = (
    form_invalid_text   => 'You fucked up',
    js_invalid_text     => 'Yep, shit sucks!',
    form_select_default => '*<- choose ->*',
    taco_salad          => 'is delicious',
    parade              => [1,2,3],

    form_invalid_opentag  => '<font color="red"><b>',
    form_invalid_closetag => '</b></font>',
    form_invalid_input    => 'Invalid entry',
    form_invalid_select   => 'Select an option from this list',
    form_invalid_checkbox => 'Check one or more options',
    form_invalid_radio    => 'Choose an option',
    form_invalid_password => 'Invalid entry',
    form_invalid_textarea => 'Please fill this in',
    form_invalid_file     => 'Invalid filename',
    form_invalid_default  => 'Invalid entry',
);

my $locale = "fb_FAKE";
my $messages = "messages.$locale";
open(M, ">$messages") || warn "Can't write $messages: $!";
while (my($k,$v) = each %messages) {
    print M join(' ', $k, ref($v) ? @$v : $v), "\n";
}
close(M);

# Fake a submission request
$ENV{REQUEST_METHOD} = 'GET';
$ENV{QUERY_STRING}   = 'ticket=111&user=pete&replacement=TRUE&action=Unsubscribe&name=Pete+Peteson&email=pete%40peteson.com&extra=junk&_submitted=1&blank=&two=&two=';

use CGI::FormBuilder;

# Now manually try a whole bunch of things
my $hash = CGI::FormBuilder->new(
                debug => $DEBUG,
                fields => [qw/user name email/],
                messages => \%messages
           );

for my $k (sort keys %messages) {
    #local $" = ', ';
    ok($hash->messages->$k, ref($messages{$k}) ? "@{$messages{$k}}" : $messages{$k});
}

my $file = CGI::FormBuilder->new(
                debug => $DEBUG,
                fields => [qw/user name email/],
                messages => $messages,
           );

for my $k (sort keys %messages) {
    #local $" = ', ';
    ok($file->messages->$k, ref($messages{$k}) ? "@{$messages{$k}}" : $messages{$k});
}

unlink $messages;

# Check to ensure our lang and charset work correctly
{   local $TESTING = 0;
    ok($file->charset, 'iso-8859-1');
    ok($file->lang,    'en_US');
    ok($file->dtd, <<EOD);
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
EOD
    ok($file->charset('yo.momma'), 'yo.momma');
}

# Final test set is to just make sure we have all the keys for all modules
require CGI::FormBuilder::Messages::default;
my %need = CGI::FormBuilder::Messages::locale->messages;
my @keys = keys %need;
for my $pm (@pm) {
    my($lang) = $pm =~ /([a-z]+_[A-Z]+)/;
    my $skip = $lang ? undef : "skip: Can't get language from $pm";
    my $form;
    eval { $form = CGI::FormBuilder->new(messages => ":$lang"); };
    skip($skip, !$@);
    for (@keys) {
        skip($skip, $form->{messages}{$_}) || warn "Locale $lang: missing $_\n";
    }
}