Skip to content

Commit

Permalink
RakuAST: add tests for RakuAST::Name::Part::Expression
Browse files Browse the repository at this point in the history
And more general RakuAST::Name tests
  • Loading branch information
lizmat committed Mar 12, 2023
1 parent b685e67 commit 892d76e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
1 change: 0 additions & 1 deletion t/12-rakuast/TODO
Expand Up @@ -20,7 +20,6 @@ RakuAST::Declaration::Import
RakuAST::Declaration::LexicalPackage
RakuAST::Declaration::Mergeable
RakuAST::Declaration::ResolvedConstant
RakuAST::Name::Part::Expression
RakuAST::Origin
RakuAST::Origin::Match
RakuAST::Origin::Source
Expand Down
63 changes: 61 additions & 2 deletions t/12-rakuast/name.rakutest
@@ -1,15 +1,74 @@
use v6.e.PREVIEW;
use Test;

plan 6;
plan 4;

given RakuAST::Name.from-identifier('foo') {
subtest "name from identifier" => {
$_ = RakuAST::Name.from-identifier('foo');
isa-ok $_, RakuAST::Name, '.from-identifier constructs a name';
ok .is-identifier, 'The element is considered an identifier';
is-deeply .parts.elems, 1, 'Has one part';
isa-ok .parts[0], RakuAST::Name::Part::Simple, 'Name has a single part';
is-deeply .parts[0].name, 'foo', 'Part has expected name';
is-deeply .DEPARSE, 'foo', 'Deparses in an expected way';
is-deeply .raku,
Q|RakuAST::Name.from-identifier("foo")|,
"rakufies in an expected way";
}

subtest "name from identifier parts" => {
$_ = RakuAST::Name.from-identifier-parts('Foo','Bar');
isa-ok $_, RakuAST::Name, '.from-identifier-parts constructs a name';
nok .is-identifier, 'the element is NOT considered an identifier';
is-deeply .parts.elems, 2, 'Has two parts';
isa-ok .parts[0], RakuAST::Name::Part::Simple, 'first part is simple';
isa-ok .parts[1], RakuAST::Name::Part::Simple, 'second part is simple';
is-deeply .parts[0].name, 'Foo', 'part 1 has expected name';
is-deeply .parts[1].name, 'Bar', 'part 2 has expected name';
is-deeply .DEPARSE, 'Foo::Bar', 'deparses in an expected way';
is-deeply .raku,
Q|RakuAST::Name.from-identifier-parts("Foo","Bar")|,
"rakufies in an expected way";
}

subtest "name from different parts" => {
$_ = RakuAST::Name.new(
RakuAST::Name::Part::Simple.new("Foo"),
RakuAST::Name::Part::Empty
);
isa-ok $_, RakuAST::Name, '.new constructs a name';
nok .is-identifier, 'the element is NOT considered an identifier';
is-deeply .parts.elems, 2, 'Has two parts';
isa-ok .parts[0], RakuAST::Name::Part::Simple, 'first part is simple';
isa-ok .parts[1], RakuAST::Name::Part::Empty, 'second part is empty';
is-deeply .parts[0].name, 'Foo', 'part 1 has expected name';
is-deeply .DEPARSE, 'Foo::', 'deparses in an expected way';
is-deeply .raku, q:to/CODE/.chomp, "rakufies in an expected way";
RakuAST::Name.new(
RakuAST::Name::Part::Simple.new("Foo"),
RakuAST::Name::Part::Empty
)
CODE
}

subtest "name from expressions" => {
$_ = RakuAST::Name.new(
RakuAST::Name::Part::Expression.new(
RakuAST::StrLiteral.new("Int")
)
);
isa-ok $_, RakuAST::Name, '.new constructs a name';
nok .is-identifier, 'the element is NOT considered an identifier';
is-deeply .parts.elems, 1, 'Has one parts';
isa-ok .parts[0], RakuAST::Name::Part::Expression, 'second part is ok';
is-deeply .DEPARSE, '::("Int")', 'deparses in an expected way';
is-deeply .raku, q:to/CODE/.chomp, "rakufies in an expected way";
RakuAST::Name.new(
RakuAST::Name::Part::Expression.new(
RakuAST::StrLiteral.new("Int")
)
)
CODE
}

# vim: expandtab shiftwidth=4

0 comments on commit 892d76e

Please sign in to comment.