File: custom_message.t

package info (click to toggle)
libsyntax-keyword-assert-perl 0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 212 kB
  • sloc: perl: 98; pascal: 69; makefile: 3
file content (173 lines) | stat: -rw-r--r-- 6,026 bytes parent folder | download
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use Test2::V0;
use Syntax::Keyword::Assert;

use lib 't/lib';
use TestUtil;

subtest 'basic custom message' => sub {
    like dies { assert(0, "Something went wrong") },
        qr/Something went wrong/;
    like dies { assert(undef, "Value is undef") },
        qr/Value is undef/;
    like dies { assert('', "Empty string") },
        qr/Empty string/;
    like dies { assert('0', "String zero") },
        qr/String zero/;
};

subtest 'success cases with custom message' => sub {
    ok lives { assert(1, "This should not appear") };
    ok lives { assert("hello", "This should not appear") };
    ok lives { assert(100, "This should not appear") };
};

subtest 'custom message with numeric comparison' => sub {
    like dies { assert(1 == 0, "1 should equal 0") },
        qr/1 should equal 0/;
    like dies { assert(5 != 5, "5 should not equal 5") },
        qr/5 should not equal 5/;
    like dies { assert(5 < 3, "5 should be less than 3") },
        qr/5 should be less than 3/;
    like dies { assert(2 > 10, "2 should be greater than 10") },
        qr/2 should be greater than 10/;
    like dies { assert(10 <= 5, "10 should be <= 5") },
        qr/10 should be <= 5/;
    like dies { assert(3 >= 10, "3 should be >= 10") },
        qr/3 should be >= 10/;

    # success cases
    ok lives { assert(1 == 1, "This should not appear") };
    ok lives { assert(5 > 3, "This should not appear") };
    ok lives { assert(3 < 5, "This should not appear") };
};

subtest 'custom message with string comparison' => sub {
    like dies { assert("foo" eq "bar", "strings should match") },
        qr/strings should match/;
    like dies { assert("same" ne "same", "strings should differ") },
        qr/strings should differ/;
    like dies { assert("z" lt "a", "z should be lt a") },
        qr/z should be lt a/;
    like dies { assert("a" gt "z", "a should be gt z") },
        qr/a should be gt z/;
    like dies { assert("z" le "a", "z should be le a") },
        qr/z should be le a/;
    like dies { assert("a" ge "z", "a should be ge z") },
        qr/a should be ge z/;

    # success cases
    ok lives { assert("foo" eq "foo", "This should not appear") };
    ok lives { assert("a" lt "b", "This should not appear") };
};

subtest 'lazy evaluation of custom message' => sub {
    subtest 'message not evaluated when condition is true' => sub {
        my $evaluated = 0;
        my $get_msg = sub { $evaluated++; return "should not see this" };

        ok lives { assert(1, $get_msg->()) };
        is $evaluated, 0, "message expression is NOT evaluated when condition is true";
    };

    subtest 'message evaluated when condition is false' => sub {
        my $evaluated = 0;
        my $get_msg = sub { $evaluated++; return "assertion failed!" };

        like dies { assert(0, $get_msg->()) },
            qr/assertion failed!/;
        is $evaluated, 1, "message expression is evaluated when condition is false";
    };

    subtest 'expensive computation skipped when true' => sub {
        my @log;
        my $expensive = sub { push @log, "computed"; return "error msg" };

        ok lives { assert("truthy value", $expensive->()) };
        is scalar(@log), 0, "expensive computation skipped when condition is true";
    };

    subtest 'side effects only on false' => sub {
        my $side_effect_count = 0;
        my $msg_with_side_effect = sub {
            $side_effect_count++;
            return "Side effect triggered $side_effect_count times";
        };

        # Multiple true assertions - side effects should NOT happen
        ok lives { assert(1, $msg_with_side_effect->()) };
        ok lives { assert("yes", $msg_with_side_effect->()) };
        ok lives { assert(100, $msg_with_side_effect->()) };

        is $side_effect_count, 0, "no side effects when all conditions are true";

        # Now a false assertion - side effect SHOULD happen
        like dies { assert(0, $msg_with_side_effect->()) },
            qr/Side effect triggered/;
        is $side_effect_count, 1, "side effect happened on false assertion";
    };
};

subtest 'custom message with variables' => sub {
    subtest 'basic' => sub {
        my $x = 0;
        my $msg = "Variable x is falsy";
        like dies { assert($x, $msg) },
            qr/Variable x is falsy/;

        my $y = undef;
        like dies { assert($y, "Value is undef") },
            qr/Value is undef/;

        my $empty = '';
        like dies { assert($empty, "Empty string") },
            qr/Empty string/;

        # success
        my $z = 1;
        ok lives { assert($z, "This should not appear") };
    };

    subtest 'numeric comparison' => sub {
        my $a = 10;
        my $b = 20;
        like dies { assert($a == $b, "values should be equal(a:$a, b:$b)") },
            qr/values should be equal\(a:10, b:20\)/;

        my $x = 5;
        my $y = 5;
        like dies { assert($x != $y, "$x should not equal $y") },
            qr/5 should not equal 5/;

        like dies { assert($x < 3, "$x should be less than 3") },
            qr/5 should be less than 3/;

        like dies { assert($x > 10, "$x should be greater than 10") },
            qr/5 should be greater than 10/;

        # success
        ok lives { assert($a < $b, "This should not appear") };
        ok lives { assert($b > $a, "This should not appear") };
    };

    subtest 'string comparison' => sub {
        my $str1 = "hello";
        my $str2 = "world";
        like dies { assert($str1 eq $str2, "strings should match") },
            qr/strings should match/;

        my $same = "foo";
        like dies { assert($same ne $same, "$same should differ from itself") },
            qr/foo should differ from itself/;

        my $a = "z";
        my $b = "a";
        like dies { assert($a lt $b, "$a should be lt $b") },
            qr/z should be lt a/;

        # success
        ok lives { assert($str1 ne $str2, "This should not appear") };
        ok lives { assert($b lt $a, "This should not appear") };
    };
};

done_testing;