File: source_encoding.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (90 lines) | stat: -rw-r--r-- 2,770 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
use strict;
use warnings;

my $is_ebcdic = ord("A") == 193;
my $os = lc $^O;

binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    unshift @INC, '.';
    require './test.pl';    # for fresh_perl_is() etc
}

fresh_perl_like("use source::encoding '';",
                qr/Bad argument for source::encoding: ''/,
                { }, "Fails on empty encoding name");

fresh_perl_like("use source::encoding 'asci';",
                qr/Bad argument for source::encoding: 'asci'/,
                { }, "Fails on unknown encoding name");

fresh_perl_like(<<~'EOT',
                 use source::encoding 'ascii';
                 my $var = "¶";
                 EOT
                qr/Use of non-ASCII character 0x[[:xdigit:]]{2} illegal/,
                { }, "Fails on non-ASCII input when ASCII is required");

if (fresh_perl_like(<<~'EOT',
                 use v5.41.0;
                 my $var = "¶";
                 EOT
                qr/Use of non-ASCII character 0x[[:xdigit:]]{2} illegal/,
                { }, ">= 'use 5.39' implies use source::encoding 'ascii'")
   ) {
    fresh_perl_is(<<~'EOT',
                    use v5.41.0;
                    my $var = "A";
                    no source::encoding;
                    $var = "¶";
                    EOT
                "",
                { }, "source encoding can be turned off");
}
else { # Above test depends on the previous one; if that failed, use this
       # alternate one
    fresh_perl_is(<<~'EOT',
                    use source::encoding 'ascii';
                    my $var = "A";
                    no source::encoding;
                    $var = "¶";
                    EOT
                "",
                { }, "source encoding can be turned off");
}

fresh_perl_is(<<~'EOT',
               my $var = "A";
               {
                  use source::encoding 'ascii';
                  $var = "B";
               }
               $var = "¶";
               EOT
              "",
              { }, "source encoding is lexically scoped");

# The test suite is full of tests with ASCII-only code, and with UTF-8 code.
# No need to repeat any of them here.  But we do need to verify that this
# pragma acts like 'use utf8'.
fresh_perl_is(<<~'EOT',
                 use source::encoding 'utf8';
                 my $var = "¶";
                 EOT
                "",
                { }, "Succeeds on non-ASCII input when UTF-8 is allowed");

fresh_perl_is(<<~'EOT',
                 use source::encoding 'utf8';
                 my $var = "¶";
                 no utf8;
                 $var = "¶";
                 EOT
                "",
                { }, "'no utf8' turns off source encoding");

done_testing();