Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing $*DISTRO values for testing

Tags:

testing

raku

I need to test a feature that includes this line:

if $translate-nl && $*DISTRO.is-win

I have tried to reassign a value to $*DISTRO,

$*DISTRO='Windows 10';

but it says:

Cannot modify an immutable Distro (debian (9.stretch))␤

$*DISTRO is a dynamic variable, and it makes sense that it's not modified. That said, is there any other way that code can be tested (other than going to Windows, of course)

like image 618
jjmerelo Avatar asked Aug 07 '18 17:08

jjmerelo


2 Answers

my $*DISTRO = ...

Hopefully modifying the original is unnecessary. It's generally unreasonable action-at-a-distance -- almost certainly so if someone has arranged for it to be an immutable value. This is the reason why global variables got such a bad reputation.

like image 98
raiph Avatar answered Nov 05 '22 03:11

raiph


To elaborate on raiph's answer: the * in $*DISTRO marks it as a dynamic variable. You can re-declare it any scope, and code called from there will see the redeclared value:

{
    my $*DISTRO = ...;
    # coded called from here sees the redeclared value
}
# code called from here sees the original value

Now, the question remains, what do you put in place of these pesky ...?

In the simplest case, a mock that just has whatever the code under test needs:

{
    my class Distro { has $.is-win }
    my $*DISTRO = Distro.new( :is-win );
    # call your test code here
}

If the code needs more attributes in Distro, just add them to the mock Distro class.

If the code needs a "real* Distro object, for some reason, you can instantiate the built-in one. The constructor .new isn't really documented, but the source code makes it pretty obvious what arguments it expects.

like image 41
moritz Avatar answered Nov 05 '22 04:11

moritz