StevieD
StevieD

Reputation: 7443

How can I unit test a sub that's not exported by a module in Raku?

Got this code:

unit module Command::CanRun;

enum OS <win nix>;

sub determine-os {
    return 'nix' when $*SPEC.gist.contains('unix', :i);
    return 'win' when $*DISTRO.is-win;
}

I would like to unit test this:

ok Command::CanRun::determine-os, 'can determine os';

However, I can't do this without exporting the determine-os sub:

Could not find symbol '&determine-os' in 'Command::CanRun'

Haven't been able to find any guidance on how to do this for non-exported subs in a module. Thanks.

Upvotes: 6

Views: 120

Answers (1)

Jonathan Worthington
Jonathan Worthington

Reputation: 29454

A sub defaults to lexical scope, meaning that it cannot be accessed from the outside. One can make it available by the fully-qualified package name by making it our scoped:

our sub determine-os {
    return 'nix' when $*SPEC.gist.contains('unix', :i);
    return 'win' when $*DISTRO.is-win;
}

An alternative solution would be to export it under a tag (for example, is export(:internals)), which means that one would have to explicitly import it (use Command::CanRun :internals;), keeping it out of the standard API exported by the module.

Upvotes: 9

Related Questions