Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create generator for tests #63

Closed
kotp opened this issue Dec 8, 2016 · 7 comments
Closed

Create generator for tests #63

kotp opened this issue Dec 8, 2016 · 7 comments

Comments

@kotp
Copy link
Member

kotp commented Dec 8, 2016

This will have the advantage of directly using the canonical data in x-common.

An example of doing this can be found with the Go and Ruby tracks.

Go executable
Go test helper

ruby generate executable
ruby generate library
Sample test cases handler

@kotp kotp added the Epic label Jan 17, 2017
@m-dango
Copy link
Member

m-dango commented Feb 5, 2017

Recently I've been putting code in exercises to allow as much interchangeability as possible. We can likely use it in a generator.

#!/usr/bin/env perl6
use v6;
use Test;
use lib IO::Path.new($?FILE).parent.path;

my $exercise = 'MyExercise';
my $version = v1;
my $module = %*ENV<EXERCISM> ?? 'Example' !! $exercise;
plan 2;

use-ok $module or bail-out;
require ::($module);
if ::($exercise).^ver !~~ $version {
  warn "\nExercise version mismatch. Further tests may fail!"
    ~ "\n$exercise is $(::($exercise).^ver.gist). "
    ~ "Test is $($version.gist).\n";
  bail-out 'Example version must match test version.' if %*ENV<EXERCISM>;
}

It should be possible to drop this into any test with only a change to $exercise, and $version for any updates.

my @subs;
BEGIN { @subs = <&my-sub> };
subtest 'Subroutine(s)', {
  plan 1;
  eval-lives-ok "use $module; ::('$_').defined or die '$_ is not defined.'", $_ for @subs;
} or bail-out 'All subroutines must be defined and exported.';
require ::($module) @subs.eager;

For modules with subroutines. Only @subs needs changing here and will check all the subroutines needed in the exercise are present. We need to bail-out on failure as we won't get past require if a subroutine is missing.

subtest 'Class method(s)', {
  plan 1;
  ok ::($exercise).can($_), $_ for <my-method>;
}

For classes. We can use the safe call method operator .?my-method in subsequent tests as it will return Nil if the method doesn't exist.

INIT {
  require JSON::Tiny <&from-json>;
  %cases := from-json 
    {"my":"json"}
  
}

We can stick this at the bottom of the test suite for any JSON we use from canonical data. my %cases; still needs declaring earlier on in the file, but it's better than shoving the JSON into the middle of everything 🙂

@m-dango
Copy link
Member

m-dango commented Mar 27, 2017

I've created a working example which is coming along nicely so far 🙂

@m-dango
Copy link
Member

m-dango commented Mar 28, 2017

The script could use some improvements in terms of user friendliness, but I believe it is functionally sound. I've added the generator and template in #124, along with a handful of yaml files for some exercises, which all give functional test files when put through the generator.

@m-dango
Copy link
Member

m-dango commented Apr 2, 2017

Added in #124.

@m-dango m-dango closed this as completed Apr 2, 2017
@kotp
Copy link
Member Author

kotp commented Apr 3, 2017

There is another issue tied to this Epic @mienaikage should it also be closed then, or is this epic not completed quite yet?

@m-dango
Copy link
Member

m-dango commented Apr 3, 2017

Oh? What's the issue?

@kotp
Copy link
Member Author

kotp commented Apr 4, 2017

#36 I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants