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

Automate code snippet tests #8

Open
ranguard opened this issue Aug 29, 2011 · 4 comments
Open

Automate code snippet tests #8

ranguard opened this issue Aug 29, 2011 · 4 comments

Comments

@ranguard
Copy link
Member

Run tests against the code snippets.

Moose::Manual apparently has some useful examples of doing this

@doherty
Copy link

doherty commented Dec 15, 2011

Test::Inline lets you put test snippets in the files themselves, then extract them into test files - but I think it will only extract from .pm files...

@ranguard
Copy link
Member Author

I think the main problem is most of the examples will be partial code, not fully complete (as otherwise it would bloat the faq too much), so there would need to be some other way of wrapping the code snippet with whatever was needed to make it into a test. So this may be a non-starter.

@doherty
Copy link

doherty commented Dec 15, 2011

Right - the code examples aren't themselves tests. What Test::Inline would let you do is:

=pod

Here's how you get rid of newlines when reading a file:

    while(<>) { # puts each line in $_
        chomp; # $_ is implicit
        # Now $_ lacks the newline at the end
    }

=begin testing

open my $in, '<', __FILE__ or die "Couldn't open myself: $!";
while (<$in>) {
    chomp;
    unlike $_, qr/\n$/;
}

done_testing($.);

=done testing

=cut

So, you get to write your tests beside the FAQ they relate to. You still have to write stuff as a test. That block between begin testing and end testing gets extracted, put in a test script template so it has use strict, use Test::More etc, and then dumped in t/ (which could easily be automated with Dist::Zilla).

I'm not sure how much better that is than putting the tests in t/ directly from the start. I guess with it in the POD, you're more likely to be reminded to update the test if you change the FAQs' answer?

@ranguard
Copy link
Member Author

Ahh, this sounds good, we should try that going forward, Inline is much better than t/ as so many people may edit the file over time and it is easier to track.

Would be good to check DZIL doesn't add any modules used in the tests as dependencies. We'd only want these tests to run before release, rather than during normal test/install.

ranguard added a commit that referenced this issue May 12, 2013
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