summaryrefslogtreecommitdiff
path: root/t/basic.t
blob: 8d6680c2ec7b9fc5dd0eb0289968b688409880c9 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!perl 
use strict;
use warnings;
use Test::Most;
use ACME::AutoRedact;
 
sub test_redact {
    my ($o,$redacted,$revealed) = @_;
 
    cmp_deeply(
        $o,
        str($revealed),
        'should stringify to the original value',
    );
 
    {
        ACME::AutoRedact->redact;
        cmp_deeply(
            $o,
            str($redacted),
            'inside a ->redact, it should stringify to asterisks',
        );
    }
 
    cmp_deeply(
        $o,
        str($revealed),
        'outside the ->redact, it should stringify to the value again',
    );
}
 
sub test_reveal {
    my ($o,$redacted,$revealed) = @_;
 
    cmp_deeply(
        $o,
        str($redacted),
        'should stringify to asterisks',
    );
 
    {
        ACME::AutoRedact->reveal;
        cmp_deeply(
            $o,
            str($revealed),
            'inside a ->reveal, it should stringify to the original value',
        );
    }
 
    cmp_deeply(
        $o,
        str($redacted),
        'outside the ->revealed, it should stringify to asterisks again',
    );
}
 
sub simulate_redact return '*' x length($_[0]) }
 
subtest 'just the object' => sub {
    my $value = 'a password';
    my $o = ACME::AutoRedact->new({
        value => $value,
    });
    test_redact($o,simulate_redact($value),$value);
};
 
subtest 'redact by default' => sub {
    my $value = 'another password';
    my $o = ACME::AutoRedact->new({
        value => $value,
        default_behaviour => 'redact',
    });
    test_reveal($o,simulate_redact($value),$value);
};
 
subtest 'concatenation' => sub {
    my $value = 'some secret';
    my $o = ACME::AutoRedact->new($value);
    my $prefix = 'embedding ';
    my $suffix = ' in a bigger string';
 
    my $string = "$prefix$o$suffix";
 
    test_redact(
        $string,
        $prefix.simulate_redact($value).$suffix,
        "$prefix$value$suffix",
    );
};
 
subtest 'sprintf' => sub {
    my $value = 'some secret';
    my $o = ACME::AutoRedact->new($value);
    my $pattern = 'embedding %s in a bigger string';
 
    my $string = sprintf $pattern,$o;
 
    local $TODO = q{I don't think we can make printf work};
    test_redact(
        $string,
        sprintf($pattern,simulate_redact($value)),
        sprintf($pattern,$value),
    );
};
 
done_testing;