Permalink
Browse files

fix dir structure, add tests

  • Loading branch information...
1 parent 2b7beb0 commit 94bd2bc3a444af2e9fecdfae450e1694064a5297 Ilya Belikin committed Feb 15, 2009
Showing with 133 additions and 0 deletions.
  1. 0 { → lib}/URI.pm
  2. 0 { → lib}/URI/Grammar.pm
  3. +108 −0 t/01.t
  4. +25 −0 t/02-add_rules.t
View
File renamed without changes.
File renamed without changes.
View
108 t/01.t
@@ -0,0 +1,108 @@
+use v6;
+
+use Test;
+plan 18;
+
+use Dispatcher;
+ok(1,'We use Dispatcher and we are still alive');
+
+use Dispatcher::Rule;
+ok(1,'We use Dispatcher::Rule and we are still alive');
+
+my $d = Dispatcher.new;
+
+dies_ok( { $d.add: Dispatcher::Rule.new },
+ 'Dispatch .add adds only complete Rule objects' );
+
+$d.add: Dispatcher::Rule.new( :tokens(''), action => { "Krevedko" } );
+
+is( $d.dispatch(['']),
+ 'Krevedko',
+ "Dispatch to Rule ['']"
+);
+
+ok( $d.add( ['foo', 'bar'], { "Yay" } ),
+ '.add fith @tokens and $action -- shortcut for fast add Rule object' );
+
+nok( $d.dispatch(['foo']),
+ 'Dispatcher return False if can`t find match Rule and do not have default' );
+
+
+is( $d.dispatch(['foo', 'bar']),
+ "Yay",
+ "Dispatch to Rule ['foo', 'bar'])"
+);
+
+$d.default = { "Woow" };
+
+is( $d.dispatch(['foo', 'bar', 'baz']),
+ "Woow",
+ 'Dispatch to default, when have no matched Rule'
+);
+
+$d.add: ['foo', 'a'|'b'], { "Zzzz" };
+
+is( $d.dispatch(['foo', 'a']),
+ 'Zzzz',
+ 'Dispatch to Rule with Junction a'
+);
+
+is( $d.dispatch(['foo', 'b']),
+ 'Zzzz',
+ 'Dispatch to Rule with Junction (foo/a|b) b'
+);
+
+$d.add: ['foo', /^ \d+ $/], { $^d };
+
+is( $d.dispatch(['foo', '50']),
+ '50',
+ "Dispatch to Rule with regexp ['foo', /^ \d+ \$/])"
+);
+
+$d.add( [/^ \w+ $/], { "Yep!" if $^w.WHAT eq 'Match' } );
+
+is( $d.dispatch(['so']),
+ 'Yep!',
+ "Argument is Match"
+);
+
+$d.add: ['foo', / \d+ /], { $^d + 10 };
+
+is( $d.dispatch(['foo', '50']),
+ '60',
+ "Dispatch ['foo', '50'] to last matched Rule"
+);
+
+is( $d.dispatch(['foo', 'a50z']),
+ '60',
+ 'Rule that catches the right arg'
+);
+
+$d.add: ['foo', / \d+ /, 'bar' ], { $^d + 1 };
+
+is( $d.dispatch(['foo', 'item4', 'bar']),
+ '5',
+ 'Rule with regexp in the middle (foo/\d+/bar)'
+);
+
+$d.add: ['summ', / \d+ /, / \d+ / ], { $^a + $^b };
+
+
+is( $d.dispatch(['summ', '2', '3']),
+ '5',
+ 'Dispatch to Rule with two regexps'
+);
+
+$d.add: ['summ', / \w+ /, 1|2 ], { $^a ~ "oo" };
+
+is( $d.dispatch(['summ', 'Z', '2']),
+ 'Zoo',
+ 'Rule with a regexp and a junction'
+);
+
+is( $d.dispatch(['foo', 'bar']),
+ "Yay",
+ 'Dispatch to simple Rule, test after adding so many Rules'
+);
+
+# vim:ft=perl6
View
@@ -0,0 +1,25 @@
+use v6;
+
+use Test;
+plan 6;
+
+use Dispatcher;
+
+my $d = Dispatcher.new;
+
+my @rules =
+ ['foo'], { "A" },
+ [/\d+/], { "B" },
+ ['foo', 'bar'], { "C" },
+ ['her'|'boo'], { "D" };
+
+is($d.add_rules(@rules), 4, "add list of rules, get the number added back");
+
+dies_ok( { $d.add_rules([1,2,3]) }, "die if you calling .add_rules an odd numbers of elements" );
+
+is($d.dispatch(['foo']), "A", "Dispatch rule ['foo']");
+is($d.dispatch(['123']), "B", "Dispatch rule /\\d+/");
+is($d.dispatch(['foo', 'bar']), "C", "Dispatch ['foo', 'bar']");
+is($d.dispatch(['boo']), "D", "Dispatch ['boo']");
+
+# vim:ft=perl6

0 comments on commit 94bd2bc

Please sign in to comment.