Skip to content

Instantly share code, notes, and snippets.

@atoomic
Created January 8, 2020 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atoomic/98ee3a7af8e26989fb6bae6085c8e3ed to your computer and use it in GitHub Desktop.
Save atoomic/98ee3a7af8e26989fb6bae6085c8e3ed to your computer and use it in GitHub Desktop.
#!perl
use Test::More;
my %storage;
use constant SAMPLE_CONSTANT => defined eval { $storage{cache} = get_string('abcd') };
sub get_string {
my ( $str ) = @_;
return ":$str:";
}
is SAMPLE_CONSTANT, 1, "constant is set to true";
is $storage{cache}, ':abcd:', "cache is set at compile time";
done_testing;
@atoomic
Copy link
Author

atoomic commented Jan 8, 2020

This test is failing

extra/constant-eval.t ..
not ok 1 - constant is set to true
not ok 2 - cache is set at compile time

#   Failed test 'constant is set to true'
#   at extra/constant-eval.t line 22.
#          got: ''
#     expected: '1'

#   Failed test 'cache is set at compile time'
#   at extra/constant-eval.t line 23.
#          got: undef
#     expected: ':abcd:'
1..2
# Looks like you failed 2 tests of 2.
Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/2 subtests

Test Summary Report
-------------------
extra/constant-eval.t (Wstat: 512 Tests: 2 Failed: 2)
  Failed tests:  1-2
  Non-zero exit status: 2
Files=1, Tests=2,  0 wallclock secs ( 0.04 usr  0.00 sys +  0.09 cusr  0.01 csys =  0.14 CPU)
Result: FAIL

@atoomic
Copy link
Author

atoomic commented Jan 8, 2020

working better once the get_string function is moved above

use strict;
use warnings;

use Test::More;

my %storage;

sub get_string {
  my ( $str ) = @_;  
  return ":$str:";
}

use constant SAMPLE_CONSTANT => defined eval { $storage{cache} = get_string('abcd') };

is SAMPLE_CONSTANT, 1, "constant is set to true";
is $storage{cache}, ':abcd:', "cache is set at compile time";

done_testing;

@atoomic
Copy link
Author

atoomic commented Jan 8, 2020

extra/constant-eval.t ..
ok 1 - constant is set to true
ok 2 - cache is set at compile time
1..2
ok
All tests successful.
Files=1, Tests=2,  1 wallclock secs ( 0.03 usr  0.00 sys +  0.07 cusr  0.01 csys =  0.11 CPU)
Result: PASS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment