1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use FindBin;
6
7use Test::More;
8use Test::Fatal;
9
10use Bread::Board;
11
12
13like(exception { local $SIG{__WARN__} = sub { }; include "$FindBin::Bin/lib/bad.bb" },
14     qr/Couldn't compile.*bad\.bb.*syntax error.*function_doesnt_exist/,
15     "we get appropriate errors for invalid files");
16
17like(exception { include "$FindBin::Bin/lib/doesnt_exist.bb" },
18     qr/Couldn't open.*doesnt_exist\.bb.*for reading/,
19     "we get appropriate errors for files that don't exist");
20
21like(exception { include "$FindBin::Bin/lib/false.bb" },
22     qr/false\.bb.*doesn't return a true value/,
23     "we get appropriate errors for files that evaluate to false");
24
25{
26    package FileLogger;
27    use Moose;
28    has 'log_file' => (is => 'ro', required => 1);
29
30    package MyApplication;
31    use Moose;
32    has 'logger' => (is => 'ro', isa => 'FileLogger', required => 1);
33}
34
35my $c = container 'MyApp' => as {
36
37    service 'log_file' => "logfile.log";
38
39    include "$FindBin::Bin/lib/logger.bb";
40
41    service 'application' => (
42        class        => 'MyApplication',
43        dependencies => {
44            logger => depends_on('logger'),
45        }
46    );
47};
48
49my $logger = $c->resolve( service => 'logger' );
50isa_ok($logger, 'FileLogger');
51
52is($logger->log_file, 'logfile.log', '... got the right logfile dep');
53
54is($c->fetch('logger/log_file')->service, $c->fetch('log_file'), '... got the right value');
55is($c->fetch('logger/log_file')->get, 'logfile.log', '... got the right value');
56
57my $app = $c->resolve( service => 'application' );
58isa_ok($app, 'MyApplication');
59
60isa_ok($app->logger, 'FileLogger');
61is($app->logger, $logger, '... got the right logger (singleton)');
62
63done_testing;
64