Skip to content

Commit

Permalink
Clean up unused options in templates, pass test array directly into t…
Browse files Browse the repository at this point in the history
…emplate (#733)
  • Loading branch information
m-dango authored Jul 31, 2024
1 parent 23adfa6 commit 6d9271c
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 90 deletions.
7 changes: 0 additions & 7 deletions bin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ If it finds a `canonical-data.json` file for the exercise in question (via `bin/

Example of a yaml file:
```yaml
modules:
- use: Data::Dump
- use: Foo::Bar

# For class methods
methods: 'foo bar'

# This is a string containing Raku code, to be inserted before any properties
tests: |-
my $baz;
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/affine-cipher/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
methods: encode decode
properties:
encode:
test: |-
Expand Down Expand Up @@ -42,6 +41,7 @@ properties:
}
unit: class

example: |-
constant @alphabet = 'a'..'z';
constant $m = @alphabet.elems;
Expand Down
3 changes: 2 additions & 1 deletion exercises/practice/clock/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
methods: time add subtract
properties:
create:
test: |-
Expand Down Expand Up @@ -54,6 +53,7 @@ properties:
}
unit: class

example: |-
has Int:D $.hour = 0;
has Int:D $.minute = 0;
Expand All @@ -76,6 +76,7 @@ example: |-
$!hour = ($!hour + $!minute div 60) % 24;
$!minute %= 60;
}
stub: |-
has $.hour;
has $.minute;
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/grade-school/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
methods: add roster
tests: |
my GradeSchool $grade-school;
properties:
add:
test: |-
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/linked-list/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
methods: push pop shift unshift count delete
properties:
list:
test: |-
Expand Down Expand Up @@ -27,6 +26,7 @@ properties:
}).join("\n") ~ "\n};\n";
unit: class

example: |-
class Node {
has Node ( $.head, $.tail ) is rw;
Expand Down
3 changes: 2 additions & 1 deletion exercises/practice/queen-attack/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package: Queen
methods: can-attack

tests: |
my Queen %queen;
properties:
create:
test: |-
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/robot-name/.meta/template-data.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package: Robot
methods: name reset-name
tests: |-

tests: |
srand 1; # begin: 1
my Robot:D $robot := Robot.new;
my Str:D @robot-names = $robot.name;
Expand Down
8 changes: 3 additions & 5 deletions lib/Exercism/Generator.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ submethod build-property-tests {
@output[0].=trim-trailing;
@output[0] ~= " # begin: %case<uuid>\n";
}
@tests.push(@output.join.trim-trailing ~ ' # ' ~ (@output > 1 ?? 'end' !! 'case') ~ ": %case<uuid>\n");
@tests.push(@output.join.trim-trailing ~ ' # ' ~ (@output > 1 ?? 'end' !! 'case') ~ ": %case<uuid>");
}
else {
@tests.push("flunk; # case: %case<uuid>\n")
@tests.push("flunk; # case: %case<uuid>")
}
}
return @tests;
Expand Down Expand Up @@ -112,12 +112,10 @@ method examples ( --> Hash() ) {

method !render ( Str $module_file? --> Str:D ) {
my %data = %.data;
%data<cases> //= $.json-tests;
%data<package> //= $.package;
%data<unit> //= 'module';
if %data<properties> {
%data<tests> ~= ("\n" if %data<tests>) ~ self.property-tests.join("\n").trim;
%data<cases> = Nil;
%data<property_tests> = self.property-tests;
}

Template::Mustache.render(
Expand Down
2 changes: 1 addition & 1 deletion t/generator/methods.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ subtest '1 case UUID' => {
:uuid<af9ffe10-dc13-42d8-a742-e7bdafac449d>,
).item], 'Case taken from UUID';

is-deeply .property-tests.Array, ["Hello, World! # case: af9ffe10-dc13-42d8-a742-e7bdafac449d\n"];
is-deeply .property-tests.Array, ['Hello, World! # case: af9ffe10-dc13-42d8-a742-e7bdafac449d'];

is .json-tests, q:to/JSON/.trim, 'Cases converted to JSON array';
[
Expand Down
56 changes: 1 addition & 55 deletions t/generator/renders.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use lib ( my $base-dir = $?FILE.IO.resolve.parent(3) ).add('lib');
use Exercism::Generator;

subtest 'Rendered test files' => {
plan 5;

given new-generator() {
is .test, q:to/TEST/, 'No data';
#!/usr/bin/env raku
Expand All @@ -16,17 +14,7 @@ subtest 'Rendered test files' => {
TEST
}

given new-generator( :data(:plan(1)) ) {
is .test, q:to/TEST/, 'A plan';
#!/usr/bin/env raku
use Test;
use lib $?FILE.IO.parent(2).add('lib');
use TestExercise;
plan 1;
TEST
}

given new-generator( :data(:tests('ok True;')) ) {
given new-generator( :data(:tests("ok True;\n")) ) {
is .test, q:to/TEST/, 'A test';
#!/usr/bin/env raku
use Test;
Expand All @@ -38,48 +26,6 @@ subtest 'Rendered test files' => {
done-testing;
TEST
}

given new-generator( :json-tests<[]> ) {
is .test, q:to/TEST/, 'JSON tests';
#!/usr/bin/env raku
use Test;
use JSON::Fast;
use lib $?FILE.IO.parent(2).add('lib');
use TestExercise;

my @test-cases = from-json($=pod[*-1].contents).List;

done-testing;

=head2 Test Cases
=begin code
[]
=end code
TEST
}

given new-generator(
:data(:tests('ok $_ for @test-cases;')),
:json-tests<[true]>,
) {
is .test, q:to/TEST/, 'Tests with JSON';
#!/usr/bin/env raku
use Test;
use JSON::Fast;
use lib $?FILE.IO.parent(2).add('lib');
use TestExercise;

my @test-cases = from-json($=pod[*-1].contents).List;
ok $_ for @test-cases;

done-testing;

=head2 Test Cases
=begin code
[true]
=end code
TEST
}
}

done-testing;
Expand Down
22 changes: 7 additions & 15 deletions templates/test.mustache
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
#!/usr/bin/env raku{{=#`{{ }}=}}#`{{! Mustache tags double up as Raku embedded comments}}
use Test;#`{{#cases}}
use JSON::Fast;#`{{/cases}}#`{{#modules}}
use #`{{&use}};#`{{/modules}}
use Test;
use lib $?FILE.IO.parent(2).add('lib');#`{{#lib_comment}} #`[#`{{&lib_comment}}]#`{{/lib_comment}}
use #`{{&package}};#`{{#plan}}
plan #`{{&plan}};#`{{#plan_comment}} #`[#`{{&plan_comment}}]#`{{/plan_comment}}#`{{/plan}}#`{{#cases}}
use #`{{&package}};
#`{{#tests}}

my @test-cases = from-json($=pod[*-1].contents).List;#`{{/cases}}#`{{#tests}}#`{{^cases}}
#`{{/cases}}
#`{{&tests}}#`{{/tests}}#`{{#property_tests}}
#`{{&.}}
#`{{/property_tests}}

#`{{&tests}}#`{{/tests}}#`{{^plan}}

done-testing;#`{{/plan}}#`{{#cases}}

=head2 Test Cases
=begin code
#`{{&cases}}
=end code#`{{/cases}}
done-testing;

0 comments on commit 6d9271c

Please sign in to comment.