New AUTOLOAD function.
[artemus.git] / tests / tests5.pl
blobb806db71312d6196aa1f7428935167bbce9fc0ae
1 #!/usr/bin/perl
3 use Art5;
5 $art5 = Art5->new();
7 $tests = 0;
8 $tests_ok = 0;
10 $art5->{op}->{ary1} = sub { [ 'a', 'b', 'c' ]; };
11 $art5->{op}->{ary2} = sub { [ [1, 'a'], [2, 'b'], [3, 'b'], [4, 'c'] ]; };
12 $art5->{op}->{ary3} = sub { [ [3, 'b'], [1, 'a'], [2, 'k'], [9, 'z'], [7, 'q' ]]; };
13 $art5->{op}->{link} = sub { "<a href = '" . $art5->exec(shift) . "'>" . $art5->exec(shift) . "</a>"; };
15 $art5->{op}->{AUTOLOAD} = sub { "Bad opcode '" . $_[0] . "'"};
17 sub try {
18 my $code = shift;
19 my $expected = shift;
21 my $result = $art5->process($code, @_);
23 if ($result eq $expected) {
24 print "OK test: ", $code, "\n";
25 $tests_ok++;
27 else {
28 print "ERROR test: ", $code, "\n";
29 print "\tExpected: '", $expected, "' ",
30 "Got: '", $result, "'\n";
33 $tests++;
36 try('<{}>', '');
37 try('1<{}>2', '12');
38 try('1<{#}>2', '12');
39 try('1<{#this is a comment}>2', '12');
40 try('1<{#this is a comment "hello"}>2', '12');
41 try("1<{#this is a comment\n'hello'}>2", '1hello2');
42 try("1<{'hi'#this is a comment\n'hello'}>2", '1hihello2');
43 try('1<{"hello"}>2', '1hello2');
44 try("1<{'hello'}>2", '1hello2');
45 try('1<{"hello\n"}>2', "1hello\n2");
46 try('1<{%arch}>2', '1Unix2');
47 try('1<{3 4}>2', '1342');
48 try('1<{? 3 4}>2', '1342');
49 try('1<{3 4 5 6}>2', '134562');
50 try('1<{? 3 4 5 6}>2', '134562');
51 try('1<{add 3 4}>2', '172');
52 try('1<{foreach ary1}>2', '1abc2');
53 try('1<{foreach ary1 $0}>2', '1abc2');
54 try('1<{foreach ary1 {? "<" $0 ">"}>2', '1<a><b><c>2');
55 try('1<{foreach ary1 $0 ", "}>2', '1a, b, c2');
56 try('1<{foreach ary2}>2', '112342');
57 try('1<{foreach ary2 $0}>2', '112342');
58 try('1<{foreach ary2 $1}>2', '1abbc2');
59 try('1<{foreach ary2 $0 ", "}>2', '11, 2, 3, 42');
60 try('1<{foreach ary2 $1 ", "}>2', '1a, b, b, c2');
61 try('1<{foreach ary2 $0 ", " {? "[" $1 "]"}}>2', '1[a]1, [b]2, 3, [c]42');
62 try('1<{if {eq 1 2} "equal"}>2', '12');
63 try('1<{if {eq 1 1} "equal"}>2', '1equal2');
64 try('1<{if {eq 1 2} "equal" "different"}>2', '1different2');
65 try('1<{if {eq 1 1} "equal" "different"}>2', '1equal2');
66 try('1<{if {eq 1 2} {"equal"}}>2', '12');
67 try('1<{if {eq 1 1} {"equal"}}>2', '1equal2');
68 try('1<{if {eq 1 2} {"equal"} {"different"}}>2', '1different2');
69 try('1<{if {eq 1 1} {"equal"} "different"}}>2', '1equal2');
70 try('1<{link}>2', "1<a href = ''></a>2");
71 try('1<{link "http://url"}>2', "1<a href = 'http://url'></a>2");
72 try('1<{link "http://url" "label"}>2', "1<a href = 'http://url'>label</a>2");
73 try('1<{= "RESULT" 1000}>2<{RESULT}>3', '1210003');
74 try('1<{size ary1}>2', '132');
75 try('1<{size ary2}>2', '142');
76 try('1<{foreach {seq 1 10}}>2', '1123456789102');
77 try('1<{foreach {reverse {seq 1 10}}}>2', '1109876543212');
78 try('1<{foreach {sort ary3 $1}}>2', '1132792');
79 try('1<{foreach {sort ary3 {add 100 $0}}}>2', '1123792');
80 try('1<{case %arch "Windows" "Is Windows" "Unix" "Is Unix"}>2', '1Is Unix2');
81 try('1<{case %arch "Windows" "Is Windows" "MSDOS" "Is MSDOS" "Is Unix"}>2', '1Is Unix2');
82 try("1<{foreach {& 1 2 3 4}}>2", "112342");
83 try('1<{T @"this" "esto" }><{@"this"}>2', "1esto2");
84 try('<{$0}>+<{$1}>=<{$2}>', '1+2=3', 1, 2, 3);
85 try('<{nonexistent}>', "Bad opcode 'nonexistent'");
87 print "\nTesting success: ", $tests_ok, '/', $tests, ' (', ($tests_ok / $tests) * 100, "%)\n";
89 exit 0;