This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/op/filehandle.t: Provide descriptions for all tests.
[perl5.git] / t / op / heredoc.t
1 # tests for heredocs besides what is tested in base/lex.t
2
3 BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6    require './test.pl';
7 }
8
9 use strict;
10 plan(tests => 9);
11
12
13 # heredoc without newline (#65838)
14 {
15     my $string = <<'HEREDOC';
16 testing for 65838
17 HEREDOC
18
19     my $code = "<<'HEREDOC';\n${string}HEREDOC";  # HD w/o newline, in eval-string
20     my $hd = eval $code or warn "$@ ---";
21     is($hd, $string, "no terminating newline in string-eval");
22 }
23
24
25 # here-doc edge cases
26 {
27     my $string = "testing for 65838";
28
29     fresh_perl_is(
30         "print <<'HEREDOC';\n${string}\nHEREDOC",
31         $string,
32         {},
33         "heredoc at EOF without trailing newline"
34     );
35
36     fresh_perl_is(
37         "print <<;\n$string\n",
38         $string,
39         { switches => ['-X'] },
40         "blank-terminated heredoc at EOF"
41     );
42     fresh_perl_is(
43         "print <<\n$string\n",
44         $string,
45         { switches => ['-X'] },
46         "blank-terminated heredoc at EOF and no semicolon"
47     );
48     fresh_perl_is(
49         "print <<foo\r\nick and queasy\r\nfoo\r\n",
50         'ick and queasy',
51         { switches => ['-X'] },
52         "crlf-terminated heredoc"
53     );
54     fresh_perl_is(
55         "print qq|\${\\<<foo}|\nick and queasy\nfoo\n",
56         'ick and queasy',
57         { switches => ['-w'], stderr => 1 },
58         'no warning for qq|${\<<foo}| in file'
59     );
60 }
61
62
63 # here-doc parse failures
64 {
65     fresh_perl_like(
66         "print <<HEREDOC;\nwibble\n HEREDOC",
67         qr/find string terminator/,
68         {},
69         "string terminator must start at newline"
70     );
71
72     fresh_perl_like(
73         "print <<;\nno more newlines",
74         qr/find string terminator/,
75         { switches => ['-X'] },
76         "empty string terminator still needs a newline"
77     );
78
79     fresh_perl_like(
80         "print <<ThisTerminatorIsLongerThanTheData;\nno more newlines",
81         qr/find string terminator/,
82         {},
83         "long terminator fails correctly"
84     );
85 }