Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 108 lines (82 sloc) 1.98 kB
4bb04af @masak [S06-macros/macros-d1.t] new test file
masak authored
1 use v6;
2
3 use Test;
c677b73 @masak [S06-macros/macros-d1.t] fixed a test a bit
masak authored
4 plan 10;
4bb04af @masak [S06-macros/macros-d1.t] new test file
masak authored
5
6 # Just to avoid tedium, the macros in this file are
7 # named after Santa's reindeers.
8
9 { # macro called like a sub
10 my $macro_visits;
11
12 macro dasher() {
13 $macro_visits++;
14 quasi {}
15 }
16
17 dasher();
18 is $macro_visits, 2, "calls to macro are at parse time";
19 dasher();
20
21 my $total_args;
22 macro dancer($a, $b?) {
23 $total_args++ if defined $a;
24 $total_args++ if defined $b;
25 quasi {}
26 }
27
28 dancer(17);
29 is $total_args, 3, "macro call with arguments works";
30 dancer(15, 10);
31 }
32
33 { # macro called like a list prefix
34 my $macro_visits;
35
36 macro prancer() {
37 $macro_visits++;
38 quasi {}
39 }
40
41 prancer;
42 is $macro_visits, 2, "macro calls without parens work";
43 prancer;
44
45 my $total_args;
46 macro vixen($a, $b?) {
47 $total_args++ if defined $a;
48 $total_args++ if defined $b;
49 quasi {}
50 }
51
52 vixen 17;
53 is $total_args, 3, "macro call with arguments works";
54 vixen 15, 10;
55 }
56
57 # macro defined as an operator, and used as one
58
59 {
60 macro infix:<comet>($rhs, $lhs) {
61 quasi { "comet!" }
62 }
63
64 my $result = 1 comet 2;
65 is $result, "comet!", "can define an entirely new operator";
66 }
67
68 {
69 macro infix:<+>($rhs, $lhs) {
70 quasi { "chickpeas" }
71 }
72
73 my $result = "grasshopper" + "motor oil";
74 is $result, "chickpeas", "can shadow an existing operator";
75 }
d217498 @masak [S06-macros/macros-d1.t] three new tests
masak authored
76
77 {
78 macro cupid {
79 my $a = "I'm cupid!";
80
81 quasi { $a }
82 }
83
84 my $result = cupid;
85 is $result, "I'm cupid!", "lexical lookup from quasi to macro works";
86 }
87
88 {
89 my $counter = 0;
90
91 macro donner {
92 quasi { ++$counter }
93 }
94
95 is donner, 1, "lexical lookup from quasi to outside macro works";
96 is donner, 2, "...twice";
97 }
98
99 {
100 macro blitzen($param) {
c677b73 @masak [S06-macros/macros-d1.t] fixed a test a bit
masak authored
101 quasi { $param }
d217498 @masak [S06-macros/macros-d1.t] three new tests
masak authored
102 }
103
c677b73 @masak [S06-macros/macros-d1.t] fixed a test a bit
masak authored
104 my $result = blitzen("ho ho ho");
105 is $result, "ho ho ho",
d217498 @masak [S06-macros/macros-d1.t] three new tests
masak authored
106 "lexical lookup from quasi to macro params works";
107 }
Something went wrong with that request. Please try again.