summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2012-12-28 17:15:34 -0600
committerJesse Luehrs <doy@tozt.net>2012-12-28 17:15:34 -0600
commita34c723255bcc33e331a093f2df4a557fb117760 (patch)
treee100ef72c54b269a3c7c4ef597f0a90e9fc2356f
parenta5e691813e5ffc37142186a352a48eaf9a5bb564 (diff)
downloadp6-bread-board-a34c723255bcc33e331a093f2df4a557fb117760.tar.gz
p6-bread-board-a34c723255bcc33e331a093f2df4a557fb117760.zip
implement include
-rw-r--r--lib/Bread/Board.pm5
-rw-r--r--t/027_sugar_w_include.t70
-rw-r--r--t/lib/bad.bb1
-rw-r--r--t/lib/false.bb1
-rw-r--r--t/lib/logger.bb11
5 files changed, 88 insertions, 0 deletions
diff --git a/lib/Bread/Board.pm b/lib/Bread/Board.pm
index 1132fd3..1934ec0 100644
--- a/lib/Bread/Board.pm
+++ b/lib/Bread/Board.pm
@@ -451,4 +451,9 @@ sub wire_names (*@names) is export {
return @names.map(-> $name { $name => depends_on($name) }).hash;
}
+sub include (Str $path) is export {
+ my $contents = slurp $path;
+ eval $contents;
+}
+
# vim:ft=perl6:foldmethod=manual
diff --git a/t/027_sugar_w_include.t b/t/027_sugar_w_include.t
new file mode 100644
index 0000000..84a180f
--- /dev/null
+++ b/t/027_sugar_w_include.t
@@ -0,0 +1,70 @@
+use v6;
+use Test;
+
+use Bread::Board;
+
+my $DIR = $?FILE.split('/')[0..*-2].join('/');
+
+sub like ($str, Regex $rx, $reason = '') {
+ ok($str ~~ $rx, $reason);
+}
+
+sub throws_ok (Callable $closure, Regex $rx, $reason = '') {
+ try {
+ $closure.();
+ CATCH {
+ # XXX is there a way to serialize $rx to something readable?
+ like($_, $rx, $reason) || diag("$_ doesn't match the regex");
+ default {}
+ }
+ }
+}
+
+# XXX better error?
+throws_ok
+ { include "$DIR/lib/bad.bb" },
+ rx/"Undeclared routine" .* function_doesnt_exist/;
+throws_ok
+ { include "$DIR/lib/doesnt_exist.bb" },
+ rx/"Unable to open" .* "doesnt_exist.bb"/;
+throws_ok
+ { include "$DIR/lib/false.bb" },
+ rx/"false.bb" .* "doesn't return a true value"/;
+
+class FileLogger {
+ has $.log_file;
+}
+
+class MyApplication {
+ has FileLogger $.logger;
+}
+
+my $c = container 'MyApp', {
+ service 'log_file', 'logfile.log';
+
+ include "$DIR/lib/logger.bb";
+
+ service 'application', (
+ class => MyApplication,
+ dependencies => {
+ logger => depends_on('logger'),
+ },
+ );
+};
+
+my $logger = $c.resolve(service => 'logger');
+isa_ok($logger, FileLogger);
+is($logger.log_file, 'logfile.log');
+
+is($c.fetch('logger/log_file').service, $c.fetch('log_file'));
+is($c.fetch('logger/log_file').get, 'logfile.log');
+
+my $app = $c.resolve(service => 'application');
+isa_ok($app, MyApplication);
+
+isa_ok($app.logger, FileLogger);
+is($app.logger, $logger);
+
+done;
+
+# vim:ft=perl6:foldmethod=manual
diff --git a/t/lib/bad.bb b/t/lib/bad.bb
new file mode 100644
index 0000000..77dde36
--- /dev/null
+++ b/t/lib/bad.bb
@@ -0,0 +1 @@
+function_doesnt_exist 'foo';
diff --git a/t/lib/false.bb b/t/lib/false.bb
new file mode 100644
index 0000000..0f57817
--- /dev/null
+++ b/t/lib/false.bb
@@ -0,0 +1 @@
+0;
diff --git a/t/lib/logger.bb b/t/lib/logger.bb
new file mode 100644
index 0000000..d14c256
--- /dev/null
+++ b/t/lib/logger.bb
@@ -0,0 +1,11 @@
+use v6;
+
+use Bread::Board;
+
+service 'logger', (
+ class => FileLogger,
+ lifecycle => Singleton,
+ dependencies => {
+ log_file => depends_on('log_file'),
+ }
+);