package #Hide from PAUSE
use 5.006001;
use strict;
use warnings;
use Import::Into;
use Test::More;

use Carp qw(croak);

use parent 'Exporter';

our @EXPORT;
BEGIN { @EXPORT=qw(eval_dies_ok eval_lives_ok eval_dies_like eval_is_var
    line_mark_string); }

=head1 NAME

Kit - vars::i test kit


Test helpers.  These make it easier to trap undefined variables using string
eval and C<use strict>.


=head2 eval_dies_ok

    eval_dies_ok "Code string" [, "message"];

Runs the code string; tests that the code died.
Any exception will be reported at the same line in the caller as the
C<eval_dies_ok> invocation.


sub eval_dies_ok {
    my (undef, $filename, $line) = caller;
    eval line_mark_string($filename, $line-1, $_[0]);
    ok($@, $_[1] || ('Died as expected: ' . $_[0]));

=head2 eval_lives_ok

    eval_lives_ok "Code string" [, "message"];

Runs the code string; tests that the code did not throw an exception.


sub eval_lives_ok {
    eval $_[0];
    is($@, '', $_[1] || ('Lived as expected: ' . $_[0]));

=head2 eval_dies_like

    eval_dies_like "Code string", qr/regex/ [, "message"];

Runs the code string; tests that the code threw an exception matching C<regex>.


sub eval_dies_like {
    eval $_[0];
    like($@, $_[1], $_[2] || ('Died with exception matching ' . $_[1]));

=head2 eval_is_var

    eval_is_var '$Package::var', value [, "message"];

Tests that C<$Package::var> exists, and that C<$Package::var eq value>.


sub eval_is_var {
    my ($varname, $expected, $msg) = @_;
    $msg ||= "$varname eq $expected";
    my ($sigil, $package, $basename) = ($varname =~ m/^(.)(.+)::([^:]+)$/);
    die "Invalid varname $varname" unless $package && $varname;

    my $got = eval qq[do { package $package; use strict; $sigil$basename }];

    is($@, '', "Accessed $varname");
    is($got, $expected, $msg);
} #eval_var_is

=head2 line_mark_string

Add a C<#line> directive to a string.  Usage:

    my $str = line_mark_string <<EOT ;


    my $str = line_mark_string __FILE__, __LINE__, <<EOT ;

In the first form, information from C<caller> will be used for the filename
and line number.

The C<#line> directive will point to the line after the C<line_mark_string>
invocation, i.e., the first line of <C$contents>.  Generally, C<$contents> will
be source code, although this is not required.

C<$contents> must be defined, but can be empty.


sub line_mark_string {
    my ($contents, $filename, $line);
    if(@_ == 1) {
        $contents = $_[0];
        (undef, $filename, $line) = caller;
    } elsif(@_ == 3) {
        ($filename, $line, $contents) = @_;
    } else {
        croak "Invalid invocation";

    croak "Need text" unless defined $contents;
    die "Couldn't get location information" unless $filename && $line;

    $filename =~ s/"/-/g;

    return <<EOT;
#line $line "$filename"
} #line_mark_string()

=head2 import

Exports the functions using L<Exporter> --- all functions are exported by
default.  Also loads L<Test::More> into the caller.


sub import {
    my $target = caller;
    __PACKAGE__->export_to_level(1, @_);
    my $oldfh = select(STDERR); $| = 1; select($oldfh); $| = 1;