1#!perl -T
2use strict;
3use Config;
4use Test::More;
5require "../../t/loc_tools.pl";
6
7plan skip_all => "I18N::Langinfo or POSIX unavailable"
8    if $Config{'extensions'} !~ m!\bI18N/Langinfo\b!;
9
10my @times  = qw( MON_1 MON_2 MON_3 MON_4 MON_5 MON_6 MON_7
11                 MON_8 MON_9 MON_10 MON_11 MON_12
12                 DAY_1 DAY_2 DAY_3 DAY_4 DAY_5 DAY_6 DAY_7);
13my @constants = qw(ABDAY_1 DAY_1 ABMON_1 RADIXCHAR AM_STR THOUSEP D_T_FMT
14                   D_FMT T_FMT);
15push @constants, @times;
16
17my %want =
18    (
19        RADIXCHAR	=> ".",
20        THOUSEP	=> "",
21     );
22
23# Abbreviated and full are swapped in many locales in early netbsd
24if (   $Config{osname} !~ / netbsd /ix
25    || $Config{osvers} !~ / ^ [1-6] \. /x)
26{
27    $want{ABDAY_1} = "Sun";
28    $want{DAY_1}   = "Sunday";
29    $want{ABMON_1} = "Jan";
30    $want{MON_1}   = "January";
31}
32
33my @want = sort keys %want;
34
35plan tests => 1 + 3 * @constants + keys(@want) + 1 + 2;
36
37use_ok('I18N::Langinfo', 'langinfo', @constants, 'CRNCYSTR');
38
39use POSIX;
40setlocale(LC_ALL, "C");
41
42for my $constant (@constants) {
43    SKIP: {
44        my $string = eval { langinfo(eval "$constant()") };
45        is( $@, '', "calling langinfo() with $constant" );
46        skip "returned string was empty, skipping next two tests", 2 unless $string;
47        ok( defined $string, "checking if the returned string is defined" );
48        cmp_ok( length($string), '>=', 1, "checking if the returned string has a positive length" );
49    }
50}
51
52for my $i (1..@want) {
53    my $try = $want[$i-1];
54    eval { I18N::Langinfo->import($try) };
55    SKIP: {
56        skip "$try not defined", 1, if $@;
57        no strict 'refs';
58        is (langinfo(&$try), $want{$try}, "$try => '$want{$try}'");
59    }
60}
61
62my $comma_locale;
63for (find_locales( [ 'LC_NUMERIC' ] )) {
64    use POSIX;
65    use locale;
66    setlocale(LC_NUMERIC, $_) or next;
67    my $in = 4.2; # avoid any constant folding bugs
68    my $s = sprintf("%g", $in);
69    if ($s eq "4,2")  {
70        $comma_locale = $_;
71        last;
72    }
73}
74
75SKIP: {
76    skip "Couldn't find a locale with a comma decimal pt", 1
77                                                        unless $comma_locale;
78
79    no strict 'refs';
80    is (langinfo(&RADIXCHAR), ",",
81        "Returns ',' for decimal pt for locale '$comma_locale'");
82}
83
84SKIP: {
85
86    my $found_time = 0;
87    my $found_monetary = 0;
88    my @locales = find_locales( [ 'LC_TIME', 'LC_CTYPE', 'LC_MONETARY' ]);
89
90    while (defined (my $utf8_locale = find_utf8_ctype_locale(\@locales))) {
91        if (! $found_time) {
92            setlocale(&LC_TIME, $utf8_locale);
93            foreach my $time_item (@times) {
94                my $eval_string = "langinfo(&$time_item)";
95                my $time_name = eval $eval_string;
96                if ($@) {
97                    fail("'$eval_string' failed: $@");
98                    last SKIP;
99                }
100                if (! defined $time_name) {
101                    fail("'$eval_string' returned undef");
102                    last SKIP;
103                }
104                if ($time_name eq "") {
105                    fail("'$eval_string' returned an empty name");
106                    last SKIP;
107                }
108
109                if ($time_name =~ /\P{ASCII}/) {
110                    ok(utf8::is_utf8($time_name), "The name for '$time_item' in $utf8_locale is a UTF8 string");
111                    $found_time = 1;
112                    last;
113                }
114            }
115        }
116
117        if (! $found_monetary) {
118            setlocale(&LC_MONETARY, $utf8_locale);
119            my $eval_string = "langinfo(&CRNCYSTR)";
120            my $symbol = eval $eval_string;
121            if ($@) {
122                fail("'$eval_string' failed: $@");
123                last SKIP;
124            }
125            if (! defined $symbol) {
126                fail("'$eval_string' returned undef");
127                last SKIP;
128            }
129            if ($symbol =~ /\P{ASCII}/) {
130                ok(utf8::is_utf8($symbol), "The name for 'CRNCYSTR' in $utf8_locale is a UTF8 string");
131                $found_monetary = 1;
132            }
133        }
134
135        last if $found_monetary && $found_time;
136
137        # Remove this locale from the list, and loop to find another utf8
138        # locale
139        @locales = grep { $_ ne $utf8_locale } @locales;
140    }
141
142    if ($found_time + $found_monetary < 2) {
143        my $message = "";
144        $message .= "time name" unless $found_time;
145        if (! $found_monetary) {
146            $message .= " nor" if $message;
147            "monetary name";
148        }
149        skip("Couldn't find a locale with a non-ascii $message", 2 - $found_time - $found_monetary);
150    }
151}
152