summaryrefslogtreecommitdiffstats
path: root/t/027_sugar_w_include.t
blob: 84a180fa07c1d211a44b5891c86fa2688a7dfa2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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