Skip to content

Commit

Permalink
RakuAST: add role test with attribute
Browse files Browse the repository at this point in the history
And move a type test to its proper file
  • Loading branch information
lizmat committed Mar 14, 2023
1 parent 199ff77 commit db608f1
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
41 changes: 37 additions & 4 deletions t/12-rakuast/package.rakutest
Expand Up @@ -126,10 +126,6 @@ subtest 'Create a class with a submethod' => {
}
}

is-deeply EVAL(RakuAST::Type::Simple.new(RakuAST::Name.from-identifier-parts('Proc', 'Async'))),
Proc::Async,
'Can resolve a multi-part type name from the setting';

subtest 'Check lexically resolving of a class' => {
# my class LexicalTestClass { }; LexicalTestClass
ast RakuAST::StatementList.new(
Expand Down Expand Up @@ -507,4 +503,41 @@ subtest 'creating an empty role' => {
}
}

subtest 'creating a role with an attribute' => {
# role A { has $.a = 42 }
ast RakuAST::Package.new(
scope => 'my',
declarator => 'role',
name => RakuAST::Name.from-identifier('A'),
body => RakuAST::Block.new(
body => RakuAST::Blockoid.new(
RakuAST::StatementList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::VarDeclaration::Simple.new(
scope => "has",
name => '$.a',
initializer => RakuAST::Initializer::Assign.new(
RakuAST::IntLiteral.new(42)
)
)
)
)
)
)
);
is-deeply $deparsed, 'my role A { has $.a = 42 }', 'deparse';

for 'AST', $ast, 'Str', $deparsed, 'Raku', EVAL($raku) -> $type, $it {
my $role := EVAL($it);
is $role.^name, 'A', "$type: role gets correct name";
my $none := $role.new;
isa-ok $none, $role, "$type: does the role auto-pun (1)";
is-deeply $none.a, 42, "$type: did the attribute get initialized (1)";

my $one := $role.new(a => 666);
isa-ok $one, $role, "$type: does the role auto-pun (2)";
is-deeply $one.a, 666, "$type: did the attribute get initialized (2)";
}
}

# vim: expandtab shiftwidth=4
12 changes: 11 additions & 1 deletion t/12-rakuast/types.rakutest
@@ -1,7 +1,7 @@
use v6.e.PREVIEW;
use Test;

plan 8;
plan 9;

my $ast;
my $deparsed;
Expand All @@ -14,6 +14,16 @@ sub ast(RakuAST::Node:D $node --> Nil) {
diag $deparsed.chomp;
}

subtest 'look up an existing type' => {
ast RakuAST::Type::Simple.new(
RakuAST::Name.from-identifier-parts('Proc', 'Async')
);
is-deeply $deparsed, 'Proc::Async', 'deparse';

is-deeply $_, Proc::Async, @type[$++]
for EVAL($ast), EVAL($deparsed), EVAL(EVAL $raku);
}

subtest 'type with definedness constraint' => {
# Int:D
ast RakuAST::Type::Definedness.new(
Expand Down

0 comments on commit db608f1

Please sign in to comment.