File Coverage

File:t/00-load.t
Coverage:100.0%

linestmtbrancondsubpodtimecode
1
1
1
1
1780
1
19
use strict;
2
1
1
1
2
1
15
use warnings;
3
4
1
1
1
263
9702
4
use Test::More;
5
1
1
1
385
5312
3
use Test::Deep;
6
1
1
1
368
4384
38
use Data::Dumper::Concise;
7
1
1
1
212
14571
130
use Capture::Tiny ':all';
8
9BEGIN {
10
1
1
1
1
1
3
380
1
1
3
    use_ok( 'Message::String', MSG001     => 'Test message %s.' );
11
1
1
1
1
1
203
62
1
1
2
    use_ok( 'Message::String', 'MSG002:N' => 'Test message %s.' );
12
13    # Geez, this done properly would be handy if it was in Test::Deep :(
14    sub cmp_not_deeply
15    {
16
1
2
        my ( $s1, $s2, $name ) = @_;
17
1
2
        my ( $identical ) = Test::Deep::cmp_details( $s1, $s2 );
18
1
1920
        ok( !$identical, $name );
19    }
20}
21
22BEGIN {
23
24    package Foo;
25
1
1
1
180
1
1
    use Message::String EXPORT    => INF003 => 'Info message %s.';
26
1
1
1
2
1
3
    use Message::String EXPORT_OK => INF004 => 'Info message 4.';
27
1
2
    use Message::String ':TAG'    => INF005 => 'Info message 5.',
28
1
1
1
1
        INF006                    => 'Info message 6.';
29
1
1
1
2
1
1
    use Message::String EXPORT => I_007       => 'Info message 7.';
30
1
1
1
2
1
1
    use Message::String EXPORT => FOO_008_I   => 'Info message 8.';
31
1
1
1
2
1
1
    use Message::String EXPORT => FOO_009_INF => 'Info message 9.';
32
1
1
1
2
1
1
    use Message::String EXPORT => XXX         => 'message 10.';
33
1
1
1
3
0
2
    use Message::String EXPORT => R_011       => 'message 11.';
34
1
1
1
2
1
1
    use Message::String EXPORT => I_012       => 'message 12 %s.';
35}
36
37BEGIN {
38
1
33
    Foo->import();
39
1
15
    Foo->import( 'INF004' );
40
1
3966
    Foo->import( ':TAG' );
41}
42
43# Give the types system a damn good thrashing
44{
45
1
1
76296
3
    my $result = message->_initial_types;
46
1
2
    is( $result, 'ACDEIMNRW', '_initial_types' );
47}
48
49{
50
1
1
181
2
    my $result = [ message->_initial_types ];
51
1
4
    is_deeply( $result, [qw/A C D E I M N R W/], '_initial_types' );
52}
53
54{
55    # INSTANCE->_types same as CLASS->_types initially
56
1
1
299
2
    my $class_types    = message->_types;
57
1
2
    my $instance_types = MSG001->_types;
58
1
2
    is( $instance_types, $class_types, '_types' );
59
60    # Until they're copied for write
61
1
131
    $instance_types = MSG001->_types( 1 );
62
1
2
    isnt( $instance_types, $class_types, '_types' );
63
64    # And once copied, that's the instance world view
65
1
205
    my $copied_types = $instance_types;
66
1
2
    $instance_types = MSG001->_types;
67
1
2
    is( $instance_types, $copied_types, '_types' );
68
69    # Until the instance is reset
70
1
132
    MSG001->_reset;
71
1
2
    $class_types    = message->_types;
72
1
2
    $instance_types = MSG001->_types;
73
1
2
    is( $instance_types, $class_types, '_types' );
74
75    # Copied again, let's confirm they're identical in every way
76
1
131
    $class_types    = message->_types;
77
1
2
    $instance_types = MSG001->_types( 1 );
78
1
3
    cmp_deeply( $class_types, $instance_types, '_types' );
79
80    # Set the default level for Type M to 100
81
1
15258
    message->_type_level( 'M', 100 );
82    # And make sure the class and instance structures differ
83
1
2
    cmp_not_deeply( $class_types, $instance_types, '_types' );
84    # Now reset the types structure to its private backup
85
1
140
    message->_reset;
86    # And make sure we're back to normal
87
1
2
    $class_types = message->_types;
88
1
12
    cmp_deeply( $class_types, $instance_types, '_types' );
89}
90
91{
92
1
1
7005
2
    my $result = [ message->_message_types ];
93
1
6
    is_deeply( $result, [qw/A C D E I M N R W/], '_message_types' );
94}
95
96{
97
1
1
315
2
    is( message->_type_level(),        undef, '_type_level' );
98
1
141
    is( message->_type_level( undef ), undef, '_type_level' );
99
1
138
    is( message->_type_level( 'X' ),   undef, '_type_level' );
100
1
118
    message->_type_level( 'M', '0E0' );
101
1
2
    is( message->_type_level( 'M' ), 6, '_type_level' );
102}
103
104{
105    # Instance changes level of a class of message
106
1
1
124
2
    my $level = MSG001->_type_level( 'M' );
107
1
3
    is( $level, 6, '_type_level' );
108
1
123
    MSG001->_type_level( 'M', 20 );
109    # Did it work?
110
1
2
    is( MSG001->_type_level( 'M' ), 20, '_type_level' );
111    # Did it set the instance level, too?
112
1
126
    is( MSG001->level, 20, '_type_level' );
113    # Make sure we can make global change to ACEW messages
114
1
145
    message->_type_level( 'A', 100 );
115
1
1
    is( message->_type_level( 'A' ), 1, '_type_level' );
116
1
124
    message->_type_level( 'C', 100 );
117
1
1
    is( message->_type_level( 'C' ), 2, '_type_level' );
118
1
123
    message->_type_level( 'E', 100 );
119
1
2
    is( message->_type_level( 'E' ), 3, '_type_level' );
120
1
149
    message->_type_level( 'W', 100 );
121
1
2
    is( message->_type_level( 'W' ), 4, '_type_level' );
122}
123
124{
125
1
1
131
2
    is( message->_type_id(),        undef, '_type_id' );
126
1
125
    is( message->_type_id( undef ), undef, '_type_id' );
127
1
124
    is( message->_type_id( 'X' ),   undef, '_type_id' );
128    # Embed message id in Type M messages
129
1
120
    message->_type_id( 'M', 1 );
130
1
1
    is( message->_type_id( 'M' ), 1, '_type_id' );
131    # Don't embed message id in any type of message
132
1
122
    message->_type_id( 0 );
133
1
2
    is( message->_type_id( 'A' ), '', '_type_id' );
134
1
153
    is( message->_type_id( 'C' ), '', '_type_id' );
135
1
132
    is( message->_type_id( 'E' ), '', '_type_id' );
136
1
128
    is( message->_type_id( 'W' ), '', '_type_id' );
137
1
127
    is( message->_type_id( 'N' ), '', '_type_id' );
138
1
125
    is( message->_type_id( 'I' ), '', '_type_id' );
139
1
124
    is( message->_type_id( 'D' ), '', '_type_id' );
140
1
124
    is( message->_type_id( 'R' ), '', '_type_id' );
141
1
122
    is( message->_type_id( 'M' ), '', '_type_id' );
142    # Don't embed message id in any type of message
143
1
123
    message->_type_id( '' );
144
1
2
    is( message->_type_id( 'A' ), '', '_type_id' );
145
1
123
    is( message->_type_id( 'C' ), '', '_type_id' );
146
1
125
    is( message->_type_id( 'E' ), '', '_type_id' );
147
1
123
    is( message->_type_id( 'W' ), '', '_type_id' );
148
1
125
    is( message->_type_id( 'N' ), '', '_type_id' );
149
1
123
    is( message->_type_id( 'I' ), '', '_type_id' );
150
1
123
    is( message->_type_id( 'D' ), '', '_type_id' );
151
1
123
    is( message->_type_id( 'R' ), '', '_type_id' );
152
1
123
    is( message->_type_id( 'M' ), '', '_type_id' );
153    # Embed message id in all types of message
154
1
122
    message->_type_id( 1 );
155
1
2
    is( message->_type_id( 'A' ), 1, '_type_id' );
156
1
123
    is( message->_type_id( 'C' ), 1, '_type_id' );
157
1
122
    is( message->_type_id( 'E' ), 1, '_type_id' );
158
1
123
    is( message->_type_id( 'W' ), 1, '_type_id' );
159
1
122
    is( message->_type_id( 'N' ), 1, '_type_id' );
160
1
123
    is( message->_type_id( 'I' ), 1, '_type_id' );
161
1
122
    is( message->_type_id( 'D' ), 1, '_type_id' );
162
1
123
    is( message->_type_id( 'R' ), 1, '_type_id' );
163
1
121
    is( message->_type_id( 'M' ), 1, '_type_id' );
164}
165
166{
167
1
1
121
2
    is( message->_type_timestamp(),        undef, '_type_timestamp' );
168
1
120
    is( message->_type_timestamp( undef ), undef, '_type_timestamp' );
169
1
118
    is( message->_type_timestamp( 'X' ),   undef, '_type_timestamp' );
170
1
119
    is( message->_type_timestamp( 'A' ),   0,     '_type_timestamp' );
171
1
122
    message->_type_timestamp( 'A', 1 );
172
1
2
    is( message->_type_timestamp( 'A' ), 1, '_type_timestamp' );
173
1
122
    message->_type_timestamp( 'A', '' );
174
1
2
    is( message->_type_timestamp( 'A' ), '', '_type_timestamp' );
175
1
123
    message->_type_timestamp( 1 );
176
1
1
    is( message->_type_timestamp( 'C' ), 1, '_type_timestamp' );
177
1
124
    message->_type_timestamp( 0 );
178
1
2
    is( message->_type_timestamp( 'C' ), 0, '_type_timestamp' );
179
1
123
    message->_type_timestamp( '' );
180
1
2
    is( message->_type_timestamp( 'C' ), '', '_type_timestamp' );
181}
182
183{
184
1
1
141
13
    is( message->_type_tlc(),        undef, '_type_tlc' );
185
1
145
    is( message->_type_tlc( undef ), undef, '_type_tlc' );
186
1
124
    is( message->_type_tlc( 'X' ),   undef, '_type_tlc' );
187
1
121
    is( message->_type_tlc( 'A' ),   '',    '_type_tlc' );
188
1
125
    message->_type_tlc( 'A', 'YYZ' );
189
1
1
    is( message->_type_tlc( 'A' ), 'YYZ', '_type_tlc' );
190
1
122
    message->_type_tlc( 'A', 'ALTYYZ' );
191
1
2
    is( message->_type_tlc( 'A' ), 'ALT', '_type_tlc' );
192}
193
194{
195
1
1
122
2
    my @x = message->_type_aliases();
196
1
2
    is_deeply( \@x, [], '_type_aliases' );
197
1
244
    is( message->_type_aliases(),        undef, '_type_aliases' );
198
1
117
    is( message->_type_aliases( undef ), undef, '_type_aliases' );
199
1
117
    is( message->_type_aliases( 'X' ),   undef, '_type_aliases' );
200
1
116
    my $aliases = message->_type_aliases( 'A' );
201
1
2
    message->_type_aliases( 'A', undef );
202
1
2
    is_deeply(
203        [ message->_type_aliases( 'A' ) ], [],
204        '_type_aliases' );
205
1
235
    message->_type_aliases( 'A', 'AFOO' );
206
1
2
    is_deeply(
207        [ message->_type_aliases( 'A' ) ], ['AFOO'],
208        '_type_aliases' );
209
1
242
    message->_type_aliases( 'A', [qw/ALT ALR ALERT/] );
210
1
2
    is_deeply(
211        [ message->_type_aliases( 'A' ) ], [ 'ALT', 'ALR', 'ALERT' ],
212        '_type_aliases' );
213
1
242
    my @array = message->_type_aliases( 'A' );
214
1
3
    is_deeply(
215        \@array, [ 'ALT', 'ALR', 'ALERT' ],
216        '_type_aliases' );
217}
218
219{
220
1
1
239
3
    message->_update_type_on_id_change( 1 );
221
1
1
    is( message->_update_type_on_id_change, 1,
222        '_update_type_on_id_change' );
223
1
160
    message->_update_level_on_type_change( 1 );
224
1
1
    is( message->_update_level_on_type_change, 1,
225        '_update_level_on_type_change' );
226
1
137
    is( message->_minimum_verbosity, 3, '_minimum_verbosity' );
227
1
133
    is( message->verbosity,          7, 'verbosity' );
228
1
124
    message->verbosity( '0E0' );
229
1
2
    is( message->verbosity, 7, 'verbosity' );
230
1
126
    message->verbosity( 7 );
231
1
1
    is( message->verbosity, 7, 'verbosity' );
232
1
124
    message->verbosity( 0 );
233
1
2
    is( message->verbosity, 3, 'verbosity' );
234
1
122
    message->verbosity( 'D' );
235
1
2
    is( message->verbosity, 7, 'verbosity' );
236
1
121
    message->verbosity( 'DIAGNOSTIC' );
237
1
2
    is( message->verbosity, 7, 'verbosity' );
238}
239
240{
241
1
1
123
3
    is( message->_default_timestamp_format, '%a %x %T',
242        '_default_timestamp_format' );
243
1
121
    message->_default_timestamp_format( '' );
244
1
1
    is( message->_default_timestamp_format, '',
245        '_default_timestamp_format' );
246
1
123
    message->_default_timestamp_format( '%a %x %T' );
247
1
1
    is( message->_default_timestamp_format, '%a %x %T',
248        '_default_timestamp_format' );
249}
250
251{
252
1
1
122
3
    message->_reset;
253
1
2
    MSG001->_reset;
254
1
2
    message->_type_timestamp( 'M', 1 );
255
1
1
    message->_type_tlc( 'M', 'MSG' );
256
1
2
    message->_type_id( 'M', 1 );
257
1
1
1
19
705
2
    my ( $stdout ) = capture_stdout { MSG001( 'Foo' ); 1; };
258
1
1712
    like(
259        $stdout,
260        qr/\A\w{3} \w{3} (?:\s\d|\d{2}), \d{4} \d{2}:\d{2}:\d{2} \*MSG\* MSG001 Test message Foo\.\n\z/s,
261        'correct message issued with adornments'
262    );
263
1
160
    message->_type_timestamp( 'M', '%a %x %T' );
264
1
1
1
19
329
2
    ( $stdout ) = capture_stdout { MSG001( 'Foo' ); 1; };
265
1
355
    like(
266        $stdout,
267        qr/\A\w{3} \w{3} (?:\s\d|\d{2}), \d{4} \d{2}:\d{2}:\d{2} \*MSG\* MSG001 Test message Foo\.\n\z/s,
268        'correct message issued with adornments'
269    );
270
1
164
    message->_type_timestamp( 'M', '' );
271
1
1
1
19
305
1
    ( $stdout ) = capture_stdout { MSG001( 'Foo' ); 1; };
272
1
306
    like(
273        $stdout, qr/\A\*MSG\* MSG001 Test message Foo\.\n\z/s,
274        'correct message issued with adornments' );
275
1
1
1
150
358
3
    my ( $stderr ) = capture_stderr { MSG002( 'Foo' ); 1; };
276
1
461
    like( $stderr, qr/\ATest message Foo\.\n\z/s,
277          'correct message issued with adornments' );
278
1
159
    MSG002->level( 6 );
279
1
3
    is( MSG002->level, 6, 'level' );
280
1
133
    MSG002->level( 'M' );
281
1
1
    is( MSG002->level, 6, 'level' );
282
1
129
    MSG002->level( 'MESSAGE' );
283
1
2
    is( MSG002->level, 6, 'level' );
284
1
130
    MSG002->type( 'INFO' );
285
1
2
    is( MSG002->level,   6,                   'level' );
286
1
129
    is( MSG002->type,    'I',                 'type' );
287
1
158
    is( MSG002,          'Test message %s.',  'stringify' );
288
1
188
    is( MSG002( 'Foo' ), 'Test message Foo.', 'stringify' );
289
1
1
134
3
    MSG002->_rebless( foo => sub {'foo'} );
290
1
2
    is( MSG002->foo, 'foo', '_rebless' );
291
1
1
1
149
423
2
    ( $stdout ) = capture_stdout { INF003( 'Foo' ); 1; };
292
1
436
    like( $stdout, qr/\AInfo message Foo\.\n\z/s,
293          'correct message issued' );
294
1
1
1
210
378
2
    ( $stdout ) = capture_stdout { INF004; 1; };
295
1
361
    like( $stdout, qr/\AInfo message 4.\n\z/s,
296          'correct message issued' );
297
1
1
1
159
307
2
    ( $stdout ) = capture_stdout { INF005; 1; };
298
1
306
    like( $stdout, qr/\AInfo message 5.\n\z/s,
299          'correct message issued' );
300
1
141
    like( INF006->to_string, qr/\AInfo message 6.\z/s,
301          'to_string' );
302
1
124
    like( I_007->to_string, qr/\AInfo message 7.\z/s,
303          'to_string' );
304
1
124
    like( FOO_008_I->to_string, qr/\AInfo message 8.\z/s,
305          'to_string' );
306
1
124
    like( FOO_009_INF->to_string, qr/\AInfo message 9.\z/s,
307          'to_string' );
308
1
122
    like( XXX->to_string, qr/\A\*MSG\* XXX message 10.\z/s,
309          'to_string' );
310
1
123
    is( R_011->response, undef, 'response' );
311
1
126
    is( R_011->response('foo'), R_011, 'response' );
312
1
125
    is( I_012->to_string, 'message 12 %s.', 'to_string' );
313}
314
315
1
126
done_testing;