[Bps-public-commit] Path-Dispatcher branch, test-manual, created. a00f1d3d7ae5422894ba47f1f63c63fbd74ac48e

Shawn Moore sartak at bestpractical.com
Fri Apr 22 19:10:51 EDT 2011


The branch, test-manual has been created
        at  a00f1d3d7ae5422894ba47f1f63c63fbd74ac48e (commit)

- Log -----------------------------------------------------------------
commit 0e8d74e68ae6b79c7795ca8eacd56b4cc18dd0e5
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Wed Apr 20 13:31:38 2011 -0400

    Move current t directory to old-t

diff --git a/t/000-compile.t b/old-t/000-compile.t
similarity index 100%
rename from t/000-compile.t
rename to old-t/000-compile.t
diff --git a/t/001-api.t b/old-t/001-api.t
similarity index 100%
rename from t/001-api.t
rename to old-t/001-api.t
diff --git a/t/002-rule.t b/old-t/002-rule.t
similarity index 100%
rename from t/002-rule.t
rename to old-t/002-rule.t
diff --git a/t/003-404.t b/old-t/003-404.t
similarity index 100%
rename from t/003-404.t
rename to old-t/003-404.t
diff --git a/t/004-run.t b/old-t/004-run.t
similarity index 100%
rename from t/004-run.t
rename to old-t/004-run.t
diff --git a/t/005-multi-rule.t b/old-t/005-multi-rule.t
similarity index 100%
rename from t/005-multi-rule.t
rename to old-t/005-multi-rule.t
diff --git a/t/006-abort.t b/old-t/006-abort.t
similarity index 100%
rename from t/006-abort.t
rename to old-t/006-abort.t
diff --git a/t/007-coderef-matcher.t b/old-t/007-coderef-matcher.t
similarity index 100%
rename from t/007-coderef-matcher.t
rename to old-t/007-coderef-matcher.t
diff --git a/t/009-args.t b/old-t/009-args.t
similarity index 100%
rename from t/009-args.t
rename to old-t/009-args.t
diff --git a/t/010-return.t b/old-t/010-return.t
similarity index 100%
rename from t/010-return.t
rename to old-t/010-return.t
diff --git a/t/011-next-rule.t b/old-t/011-next-rule.t
similarity index 100%
rename from t/011-next-rule.t
rename to old-t/011-next-rule.t
diff --git a/t/012-under.t b/old-t/012-under.t
similarity index 100%
rename from t/012-under.t
rename to old-t/012-under.t
diff --git a/t/013-tokens.t b/old-t/013-tokens.t
similarity index 100%
rename from t/013-tokens.t
rename to old-t/013-tokens.t
diff --git a/t/014-tokens-prefix.t b/old-t/014-tokens-prefix.t
similarity index 100%
rename from t/014-tokens-prefix.t
rename to old-t/014-tokens-prefix.t
diff --git a/t/015-regex-prefix.t b/old-t/015-regex-prefix.t
similarity index 100%
rename from t/015-regex-prefix.t
rename to old-t/015-regex-prefix.t
diff --git a/t/017-intersection.t b/old-t/017-intersection.t
similarity index 100%
rename from t/017-intersection.t
rename to old-t/017-intersection.t
diff --git a/t/018-metadata.t b/old-t/018-metadata.t
similarity index 100%
rename from t/018-metadata.t
rename to old-t/018-metadata.t
diff --git a/t/019-intersection-metadata.t b/old-t/019-intersection-metadata.t
similarity index 100%
rename from t/019-intersection-metadata.t
rename to old-t/019-intersection-metadata.t
diff --git a/t/022-numbers-undef.t b/old-t/022-numbers-undef.t
similarity index 100%
rename from t/022-numbers-undef.t
rename to old-t/022-numbers-undef.t
diff --git a/t/023-alternation.t b/old-t/023-alternation.t
similarity index 100%
rename from t/023-alternation.t
rename to old-t/023-alternation.t
diff --git a/t/024-sequence.t b/old-t/024-sequence.t
similarity index 100%
rename from t/024-sequence.t
rename to old-t/024-sequence.t
diff --git a/t/025-sequence-custom-rule.t b/old-t/025-sequence-custom-rule.t
similarity index 100%
rename from t/025-sequence-custom-rule.t
rename to old-t/025-sequence-custom-rule.t
diff --git a/t/026-named-captures.t b/old-t/026-named-captures.t
similarity index 100%
rename from t/026-named-captures.t
rename to old-t/026-named-captures.t
diff --git a/t/027-custom-named-captures.t b/old-t/027-custom-named-captures.t
similarity index 100%
rename from t/027-custom-named-captures.t
rename to old-t/027-custom-named-captures.t
diff --git a/t/030-exceptions.t b/old-t/030-exceptions.t
similarity index 100%
rename from t/030-exceptions.t
rename to old-t/030-exceptions.t
diff --git a/t/031-structured-match.t b/old-t/031-structured-match.t
similarity index 100%
rename from t/031-structured-match.t
rename to old-t/031-structured-match.t
diff --git a/t/032-multiple-delimiter.t b/old-t/032-multiple-delimiter.t
similarity index 100%
rename from t/032-multiple-delimiter.t
rename to old-t/032-multiple-delimiter.t
diff --git a/t/033-chain.t b/old-t/033-chain.t
similarity index 100%
rename from t/033-chain.t
rename to old-t/033-chain.t
diff --git a/t/100-match-object.t b/old-t/100-match-object.t
similarity index 100%
rename from t/100-match-object.t
rename to old-t/100-match-object.t
diff --git a/t/901-return-values.t b/old-t/901-return-values.t
similarity index 100%
rename from t/901-return-values.t
rename to old-t/901-return-values.t
diff --git a/t/902-coderef.t b/old-t/902-coderef.t
similarity index 100%
rename from t/902-coderef.t
rename to old-t/902-coderef.t

commit b1b3c09218d257890f1edc222ec74e43367102c0
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Wed Apr 20 13:38:41 2011 -0400

    hello world

diff --git a/t/000-hello.t b/t/000-hello.t
new file mode 100644
index 0000000..81e3105
--- /dev/null
+++ b/t/000-hello.t
@@ -0,0 +1,16 @@
+use strict;
+use warnings;
+use Test::More;
+
+# Welcome to the Path::Dispatcher test suite! This test suite is intended to
+# be a readable introduction to the Path::Dispatcher features. You can be sure
+# that these examples will work because they are written as actual tests.
+
+# You're encouraged to tweak and re-run the tests as you learn
+# Path::Dispatcher. Feel free to send patches or pull requests if this
+# testumentation could be improved!
+
+use_ok 'Path::Dispatcher';
+
+done_testing;
+

commit f8c26afed8d4b7d97cf5cdeb51d2b161df112ad0
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Wed Apr 20 14:49:29 2011 -0400

    Tell Dispatch what the path was

diff --git a/lib/Path/Dispatcher.pm b/lib/Path/Dispatcher.pm
index 45822da..aa42873 100644
--- a/lib/Path/Dispatcher.pm
+++ b/lib/Path/Dispatcher.pm
@@ -17,7 +17,9 @@ sub dispatch {
     my $self = shift;
     my $path = $self->_autobox_path(shift);
 
-    my $dispatch = $self->dispatch_class->new;
+    my $dispatch = $self->dispatch_class->new(
+        path => $path,
+    );
 
     for my $rule ($self->rules) {
         $self->_dispatch_rule(
diff --git a/lib/Path/Dispatcher/Dispatch.pm b/lib/Path/Dispatcher/Dispatch.pm
index 439227c..46efb22 100644
--- a/lib/Path/Dispatcher/Dispatch.pm
+++ b/lib/Path/Dispatcher/Dispatch.pm
@@ -4,6 +4,12 @@ use Try::Tiny;
 
 use Path::Dispatcher::Match;
 
+has path => (
+    is       => 'ro',
+    isa      => 'Path::Dispatcher::Path',
+    required => 1,
+);
+
 has _matches => (
     is        => 'ro',
     isa       => 'ArrayRef',

commit 1671456a9e1b9f2af75177a629583333cc9f20a1
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Wed Apr 20 14:53:19 2011 -0400

    t/001-essentials.t

diff --git a/t/001-essentials.t b/t/001-essentials.t
new file mode 100644
index 0000000..1387960
--- /dev/null
+++ b/t/001-essentials.t
@@ -0,0 +1,47 @@
+use strict;
+use warnings;
+use Test::More;
+use Path::Dispatcher;
+
+# The most valuable object is Path::Dispatcher itself. We can start with an
+# empty one if you want and add rules gradually.
+my $dispatcher = Path::Dispatcher->new;
+isa_ok($dispatcher, 'Path::Dispatcher');
+
+# We didn't define any rules, so it'd be weird if we had one to begin with.
+is($dispatcher->rules, 0, 'Path::Dispatcher objects start without rules');
+
+# Now let's create a rule. We'll use one of the simplest types of rule, Eq,
+# which just checks for string equality.
+my $eq_hello = Path::Dispatcher::Rule::Eq->new(
+    string => 'Hello!',
+);
+
+# Each rule is an object with some potentially interesting properties. Eq
+# rules can be case sensitive or case insensitive.
+is($eq_hello->string, 'Hello!');
+ok($eq_hello->case_sensitive, 'Eq rules are case sensitive by default');
+
+# And now finally add our new rule to the dispatcher we already created.
+$dispatcher->add_rule($eq_hello);
+is($dispatcher->rules, 1, 'Added the rule to our dispatcher');
+
+# Now let's try dispatching!
+my $dispatch = $dispatcher->dispatch('Hello!');
+
+# The Path::Dispatcher::Dispatch object has lots of information about what
+# just happened, but for now all we're interested in is whether there was a
+# match.
+ok($dispatch->has_matches, 'matched Hello!');
+
+# If dispatch doesn't match anything, you will still get a Dispatch object, but
+# it will have no matches. You can use this to fall back to "command not found"
+# or 404 logic.
+$dispatch = $dispatcher->dispatch('Hola!');
+ok(!$dispatch->has_matches, 'did not match Hola!');
+
+# That's the bare essentials of Path::Dispatcher. Our story continues on disc
+# two, t/002-basics.t
+
+done_testing;
+

commit 6633ac528f53d524dd58eec70fc86d4e7c756048
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Fri Apr 22 10:50:16 2011 -0400

    More typical usage in t/002-basics.t

diff --git a/t/002-basics.t b/t/002-basics.t
new file mode 100644
index 0000000..dd2792f
--- /dev/null
+++ b/t/002-basics.t
@@ -0,0 +1,97 @@
+use strict;
+use warnings;
+use Test::More;
+use Path::Dispatcher;
+
+# This test will present a more typical, useful application of
+# Path::Dispatcher. We will demonstrate multiple rules, variable capture, and
+# code blocks. The dispatcher we create will support two commands:
+# "buy (something)" to add an item to @cart, and "list" to get the purchases
+# back out.
+my @cart;
+my $dispatcher = Path::Dispatcher->new;
+
+# The "list" rule is the simpler of the two. It uses the Eq rule type
+# introduced in the previous test. The new bit here is the "block" which runs
+# when the rule matches the path. Here we're just returning a string to
+# present to the user as a response to their command.
+$dispatcher->add_rule(
+    Path::Dispatcher::Rule::Eq->new(
+        string => "list",
+        block  => sub {
+            return "Your cart is empty." if @cart == 0;
+            return "Cart: " . join ", ", @cart;
+        },
+    ),
+);
+
+# This "buy (item)" rule introduces some new concepts. It uses a new rule type,
+# Regex, which of course just does a regular expression match. In the
+# codeblock, we are pulling out "shift->pos(1)" which is the $1 created by
+# the regex match. This value represents the new item to be bought. The argument
+# we are shifting off is a Path::Dispatcher::Match object, which contains
+# information about this rule's match against the path. We are using it to grab
+# the first positional (as opposed to named) capture. We then again create some
+# output for the user.
+$dispatcher->add_rule(
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr/^buy (.+)$/,
+        block => sub {
+            my $item = shift->pos(1);
+            push @cart, $item;
+            return "Bought $item";
+        },
+    ),
+);
+
+# ->run is just like ->dispatch, except it will also invoke the matched rule's
+# codeblock and pass along its return value. In basic Path::Dispatcher usage
+# like this, you don't need to care that Path::Dispatcher has separate dispatch
+# and execute cycles.
+my $response = $dispatcher->run("list");
+is($response, "Your cart is empty.");
+
+$response = $dispatcher->run("buy bananas");
+is($response, "Bought bananas");
+
+$response = $dispatcher->run("list");
+is($response, "Cart: bananas", "mmm potassium");
+
+
+# ... but if you prefer, you certainly can break up the dispatch and run
+# cycles. This lets you inspect the rule that matched before executing its
+# codeblock.
+my $dispatch = $dispatcher->dispatch("buy Social Networks");
+isa_ok($dispatch, 'Path::Dispatcher::Dispatch');
+ok($dispatch->has_match, 'got a match');
+
+# The match object is the same that the codeblock received. It remembers
+# captures for you, which is kind of nice.
+my $match = $dispatch->first_match;
+isa_ok($match, 'Path::Dispatcher::Match');
+is($match->pos(1), 'Social Networks');
+
+# And the match remembers which rule created it, so you can inspect all the way
+# down.
+my $rule = $match->rule;
+isa_ok($rule, 'Path::Dispatcher::Rule::Regex');
+is($rule->regex, qr/^buy (.+)$/);
+
+# Note that at this point, even though we've dispatched "buy Social Networks",
+# we still have not run its codeblock!
+$response = $dispatcher->run("list");
+is($response, "Cart: bananas", "still only bananas");
+
+# But now we can run the dispatch, which invokes the codeblock of the rule that
+# matched, to finally commit to buying Friendster.
+$response = $dispatch->run;
+is($response, "Bought Social Networks", "hope it wasn't MySpace");
+
+$response = $dispatcher->run("list");
+is($response, "Cart: bananas, Social Networks", "big spendah!");
+
+# Next time we'll investigate what happens when a dispatch goes wrong! You'll
+# get some mispatches in your travels.
+
+done_testing;
+

commit e80db19e8a73f78739b711eca59f04d3428226af
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Fri Apr 22 11:01:38 2011 -0400

    More words!

diff --git a/t/000-hello.t b/t/000-hello.t
index 81e3105..1466ef7 100644
--- a/t/000-hello.t
+++ b/t/000-hello.t
@@ -2,9 +2,10 @@ use strict;
 use warnings;
 use Test::More;
 
-# Welcome to the Path::Dispatcher test suite! This test suite is intended to
-# be a readable introduction to the Path::Dispatcher features. You can be sure
-# that these examples will work because they are written as actual tests.
+# Welcome to the Path::Dispatcher test suite! This test suite is intended to be
+# a readable introduction to Path::Dispatcher's features. You can be sure that
+# this documentation really does what it says because it is written as actual
+# tests.
 
 # You're encouraged to tweak and re-run the tests as you learn
 # Path::Dispatcher. Feel free to send patches or pull requests if this
diff --git a/t/002-basics.t b/t/002-basics.t
index dd2792f..b3e251d 100644
--- a/t/002-basics.t
+++ b/t/002-basics.t
@@ -26,28 +26,26 @@ $dispatcher->add_rule(
 );
 
 # This "buy (item)" rule introduces some new concepts. It uses a new rule type,
-# Regex, which of course just does a regular expression match. In the
-# codeblock, we are pulling out "shift->pos(1)" which is the $1 created by
-# the regex match. This value represents the new item to be bought. The argument
-# we are shifting off is a Path::Dispatcher::Match object, which contains
-# information about this rule's match against the path. We are using it to grab
-# the first positional (as opposed to named) capture. We then again create some
-# output for the user.
+# Regex, which of course just does a regular expression match. Path::Dispatcher
+# passes a Path::Dispatcher::Match object to codeblocks. This object contains
+# information about that rule's match against the path. We are pulling out
+# ->pos(1) from the match, which is the $1 created by the regex. This value
+# represents the new purchase. We then again create some output for the user.
 $dispatcher->add_rule(
     Path::Dispatcher::Rule::Regex->new(
         regex => qr/^buy (.+)$/,
         block => sub {
-            my $item = shift->pos(1);
-            push @cart, $item;
-            return "Bought $item";
+            my $purchase = shift->pos(1);
+            push @cart, $purchase;
+            return "Bought $purchase";
         },
     ),
 );
 
 # ->run is just like ->dispatch, except it will also invoke the matched rule's
 # codeblock and pass along its return value. In basic Path::Dispatcher usage
-# like this, you don't need to care that Path::Dispatcher has separate dispatch
-# and execute cycles.
+# like this test file, you don't need to care that Path::Dispatcher has
+# separate dispatch and execute cycles.
 my $response = $dispatcher->run("list");
 is($response, "Your cart is empty.");
 
@@ -57,16 +55,15 @@ is($response, "Bought bananas");
 $response = $dispatcher->run("list");
 is($response, "Cart: bananas", "mmm potassium");
 
-
 # ... but if you prefer, you certainly can break up the dispatch and run
-# cycles. This lets you inspect the rule that matched before executing its
+# cycles. This lets you inspect the rule that matched before you execute its
 # codeblock.
 my $dispatch = $dispatcher->dispatch("buy Social Networks");
 isa_ok($dispatch, 'Path::Dispatcher::Dispatch');
 ok($dispatch->has_match, 'got a match');
 
-# The match object is the same that the codeblock received. It remembers
-# captures for you, which is kind of nice.
+# The match object is the same that the codeblock received. It of course still
+# remembers those captures for you.
 my $match = $dispatch->first_match;
 isa_ok($match, 'Path::Dispatcher::Match');
 is($match->pos(1), 'Social Networks');
@@ -77,12 +74,12 @@ my $rule = $match->rule;
 isa_ok($rule, 'Path::Dispatcher::Rule::Regex');
 is($rule->regex, qr/^buy (.+)$/);
 
-# Note that at this point, even though we've dispatched "buy Social Networks",
-# we still have not run its codeblock!
+# At this point, even though we've dispatched "buy Social Networks" and got a
+# match, we still have not run its codeblock!
 $response = $dispatcher->run("list");
 is($response, "Cart: bananas", "still only bananas");
 
-# But now we can run the dispatch, which invokes the codeblock of the rule that
+# We can run the dispatch, which invokes the codeblock of the rule that
 # matched, to finally commit to buying Friendster.
 $response = $dispatch->run;
 is($response, "Bought Social Networks", "hope it wasn't MySpace");
@@ -90,8 +87,8 @@ is($response, "Bought Social Networks", "hope it wasn't MySpace");
 $response = $dispatcher->run("list");
 is($response, "Cart: bananas, Social Networks", "big spendah!");
 
-# Next time we'll investigate what happens when a dispatch goes wrong! You'll
-# get some mispatches in your travels.
+# We'll investigate what happens when a dispatch goes wrong in the next
+# episode, titled Path::Mispatcher.
 
 done_testing;
 

commit fb4aa18036363b62e9e6a43f28d973792b516fb0
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Fri Apr 22 11:26:34 2011 -0400

    t/003 will not be about fails, so introduce the git helper early

diff --git a/t/001-essentials.t b/t/001-essentials.t
index 1387960..bf17b12 100644
--- a/t/001-essentials.t
+++ b/t/001-essentials.t
@@ -36,7 +36,8 @@ ok($dispatch->has_matches, 'matched Hello!');
 
 # If dispatch doesn't match anything, you will still get a Dispatch object, but
 # it will have no matches. You can use this to fall back to "command not found"
-# or 404 logic.
+# or 404 logic, or if you're really on the ball, something like git's "Did you
+# mean this?" command suggestion.
 $dispatch = $dispatcher->dispatch('Hola!');
 ok(!$dispatch->has_matches, 'did not match Hola!');
 
diff --git a/t/002-basics.t b/t/002-basics.t
index b3e251d..a235b10 100644
--- a/t/002-basics.t
+++ b/t/002-basics.t
@@ -87,8 +87,5 @@ is($response, "Bought Social Networks", "hope it wasn't MySpace");
 $response = $dispatcher->run("list");
 is($response, "Cart: bananas, Social Networks", "big spendah!");
 
-# We'll investigate what happens when a dispatch goes wrong in the next
-# episode, titled Path::Mispatcher.
-
 done_testing;
 

commit ca0eee508fbb40d924bb3d3e7d2f69e9aaed36cc
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Fri Apr 22 13:52:32 2011 -0400

    Chapter 3 is about Under

diff --git a/t/003-under.t b/t/003-under.t
new file mode 100644
index 0000000..765874d
--- /dev/null
+++ b/t/003-under.t
@@ -0,0 +1,14 @@
+use strict;
+use warnings;
+use Test::More;
+use Path::Dispatcher;
+
+# This test introduces a new kind of rule called Under. Under rules let you
+# match a prefix common to several rules, so that you can group related rules
+# together and match them more efficiently, by only checking for the prefix
+# once.
+
+my $dispatcher = Path::Dispatcher->new;
+
+done_testing;
+

commit a00f1d3d7ae5422894ba47f1f63c63fbd74ac48e
Author: Shawn M Moore <sartak at bestpractical.com>
Date:   Fri Apr 22 14:29:20 2011 -0400

    under will be t/004

diff --git a/t/003-under.t b/t/004-under.t
similarity index 100%
rename from t/003-under.t
rename to t/004-under.t

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list