This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
&CORE::foo() for nullary functions
[perl5.git] / t / op / coresubs.t
CommitLineData
7fa5bd9b
FC
1#!./perl
2
3# This file tests the results of calling subroutines in the CORE::
4# namespace with ampersand syntax. In other words, it tests the bodies of
5# the subroutines themselves, not the ops that they might inline themselves
6# as when called as barewords.
7
8# coreinline.t tests the inlining of these subs as ops. Since it was
9# convenient, I also put the prototype and undefinedness checking in that
10# file, even though those have nothing to do with inlining. (coreinline.t
11# reads the list in keywords.pl, which is why it’s convenient.)
12
13BEGIN {
14 chdir 't' if -d 't';
15 @INC = qw(. ../lib);
16 require "test.pl";
17 $^P |= 0x100;
18}
19# Since tests inside evals can too easily fail silently, we cannot rely
20# on done_testing. It’s much easier to count the tests as we go than to
21# declare the plan up front, so this script ends with a test that makes
22# sure the right number of tests have happened.
23
24sub lis($$;$) {
25 &is(map(@$_ ? "[@{[map $_//'~~u~~', @$_]}]" : 'nought', @_[0,1]), $_[2]);
26}
27
28# This tests that the &{} syntax respects the number of arguments implied
29# by the prototype.
30sub test_proto {
31 my($o) = shift;
32
33 # Create an alias, for the caller’s convenience.
34 *{"my$o"} = \&{"CORE::$o"};
35
36 my $p = prototype "CORE::$o";
37
38 if ($p eq '') {
39 $tests ++;
40
41 eval " &CORE::$o(1) ";
42 like $@, qr/^Too many arguments for $o at /, "&$o with too many args";
43
44 }
45
46 else {
47 die "Please add tests for the $p prototype";
48 }
49}
50
51test_proto '__FILE__';
52test_proto '__LINE__';
53test_proto '__PACKAGE__';
54
55is file(), 'frob' , '__FILE__ does check its caller' ; ++ $tests;
56is line(), 5 , '__LINE__ does check its caller' ; ++ $tests;
57is pakg(), 'stribble', '__PACKAGE__ does check its caller'; ++ $tests;
58
59test_proto 'continue';
60$tests ++;
61CORE::given(1) {
62 CORE::when(1) {
63 &mycontinue();
64 }
65 pass "&continue";
66}
67
68test_proto $_ for qw(
69 endgrent endhostent endnetent endprotoent endpwent endservent
70);
71
72test_proto "get$_" for qw '
73 grent hostent login
74 netent ppid protoent
75 servent
76';
77
78test_proto "set$_" for qw '
79 grent pwent
80';
81
82test_proto 'time';
83$tests += 2;
84like &mytime, '^\d+\z', '&time in scalar context';
85like join('-', &mytime), '^\d+\z', '&time in list context';
86
87test_proto 'times';
88$tests += 2;
89like &mytimes, '^[\d.]+\z', '&times in scalar context';
90like join('-',&mytimes), '^[\d.]+-[\d.]+-[\d.]+-[\d.]+\z',
91 '&times in list context';
92
93test_proto 'wait';
94
95
96# Add new tests above this line.
97
98# ------------ END TESTING ----------- #
99
100is curr_test, $tests+1, 'right number of tests';
101done_testing;
102
103#line 3 frob
104
105sub file { &CORE::__FILE__ }
106sub line { &CORE::__LINE__ } # 5
107package stribble;
108sub main::pakg { &CORE::__PACKAGE__ }
109
110# Please do not add new tests here.