1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
use 5.008001; use strict; use warnings;
package TestML::Tiny;
; # original $VERSION removed by Doppelgaenger
use Carp();
use Test::More 0.99 ();
# use XXX;
sub import {
strict->import;
warnings->import;
}
sub new {
my $self = bless { @_[1..$#_] }, $_[0];
my $testml = $self->_get_testml;
my $bridge = $self->_get_bridge;
$self->{runtime} ||= TestML::Tiny::Runtime->new(
bridge => $bridge,
);
my $compiler = TestML::Tiny::Compiler->new(
$self->{version} ? (version => $self->{version}) : (),
);
$self->{function} = $compiler->compile($testml);
return $self;
}
sub run {
my ($self) = @_;
my $runtime = $self->{runtime} || '';
Carp::croak "Missing or invalid runtime object for TestML::Tiny::run()"
unless defined($runtime) and ref($runtime) eq 'TestML::Tiny::Runtime';
$runtime->run;
}
sub _get_testml {
my ($self) = @_;
my $testml = $self->{testml}
or Carp::croak "TestML object requires a testml attribute";
$testml = $self->_slurp($testml)
if $testml !~ /\n/;
return $testml;
}
sub _get_bridge {
my ($self) = @_;
my $bridge = $self->{bridge} || 'main';
return $bridge if ref $bridge;
eval "require $bridge";
Carp::croak $@ if $@ and $@ !~ /^Can't locate /;
return (
defined(&{"${bridge}::new"})
? $bridge->new
: bless {}, $bridge
);
}
sub _slurp {
open my $fh, "<:raw:encoding(UTF-8)", $_[1]
or die "Can't open $_[1] for input";
local $/;
<$fh>;
}
#------------------------------------------------------------------------------
package TestML::Tiny::Runtime;
# use XXX;
sub new {
my $self = $TestML::Tiny::Runtime::Singleton =
bless { @_[1..$#_] }, $_[0];
};
sub run {
Test::More::fail 'not done yet!';
Test::More::done_testing;
}
#------------------------------------------------------------------------------
package TestML::Tiny::Compiler;
# use XXX;
my $ID = qr/\w+/;
my $SP = qr/[\ \t]/;
my $LINE = qr/.*$/m;
my $DIRECTIVE = qr/^%($ID)$SP+($LINE)/m;
sub new {
my $self = bless { @_[1..$#_] }, $_[0];
}
sub runtime {
$TestML::Tiny::Runtime::Singleton;
}
sub compile {
my ($self, $testml) = @_;
my $function = $self->{function} = TestML::Tiny::Function->new;
$self->{testml} = $testml;
$self->preprocess;
my $version = $self->check_version;
my ($code_syntax, $data_syntax) =
@{$self}{qw(code_syntax data_syntax)};
my $code_method = "compile_code_${code_syntax}_$version";
Carp::croak "Don't know how to compile TestML '$code_syntax' code"
unless $self->can($code_method);
my $data_method = "compile_data_${data_syntax}_$version";
Carp::croak "Don't know how to compile TestML '$data_syntax' data"
unless $self->can($data_method);
$function->{statements} = $self->$code_method;
$function->{data} = $self->$data_method;
return $function;
}
my %directives = (
code_syntax => 'tiny',
data_syntax => 'testml',
data_marker => '===',
block_marker => '===',
point_marker => '---',
);
sub preprocess {
my ($self) = @_;
my $version = $self->{version} || undef;
my $testml = $self->{testml};
my $directives = [ $testml =~ /$DIRECTIVE/gm ];
$testml =~ s/($DIRECTIVE)/#$1/g;
while (@$directives) {
my ($key, $value) = splice(@$directives, 0, 2);
if ($key eq "TestML") {
$self->check_not_set_and_set($key, $value, 'version');
}
elsif ($key eq "BlockMarker") {
$self->check_not_set_and_set(
'BlockMarker', $value, 'block_marker'
);
($self->{block_marker} = $value) =~
s/([\*\^\$\+\?\(\)\.])/\\$1/g;
}
elsif ($key eq "PointMarker") {
$self->check_not_set_and_set(
'PointMarker', $value, 'point_marker'
);
($self->{point_marker} = $value) =~
s/([\*\^\$\+\?\(\)\.])/\\$1/g;
}
elsif ($key eq "CodeSyntax") {
die "Untested";
$self->check_not_set_and_set(
'CodeSyntax', $value, 'code_syntax'
);
$self->{code_syntax} = $value;
}
elsif ($key eq "DataSyntax") {
die "Untested";
$self->check_not_set_and_set(
'DataSyntax', $value, 'data_syntax'
);
$self->{data_syntax} = $value;
}
else {
Carp::croak "Unknown TestML directive: '%$key'";
}
}
$self->{data_marker} = $self->{block_marker}
if not($self->{data_marker}) and $self->{block_marker};
for my $directive (keys %directives) {
$self->{$directive} ||= $directives{$directive};
}
($self->{code}, $self->{data}) =
($testml =~ /(.*?)(^$self->{data_marker}.*)/msg);
$self->{code} ||= '';
$self->{data} ||= '';
}
sub check_not_set_and_set {
my ($self, $key, $value, $attr) = @_;
if (defined $self->{$attr} and $self->{$attr} ne $value) {
Carp::croak "Can't set TestML '$key' directive to '$value'. " .
"Already set to '$self->{$attr}'";
}
$self->{$attr} = $value;
}
sub check_version {
my ($self) = @_;
my $version = $self->{version} || undef;
Carp::croak "TestML syntax version not defined. Cannot continue"
unless defined $version;
Carp::croak "Invalid value for TestML version '$version'. Must be 0.1.0"
unless $version eq '0.1.0';
$version =~ s/\./_/g;
return $version;
}
sub compile_code_tiny_0_1_0 {
my ($self) = @_;
my $num = 1;
[ grep { not /(^#|^\s*$)/ } split /\n/, $self->{code} ];
}
sub compile_data_testml_0_1_0 {
my ($self) = @_;
my $lines = [ grep { ! /^#/ } split /\n/, $self->{data} ];
my $blocks = [];
my $parse = [];
push @$lines, undef; # sentinel
while (@$lines) {
push @$parse, shift @$lines;
if (!defined($lines->[0]) or
$lines->[0] =~ /^$self->{block_marker}/
) {
my $block = $self->_parse_testml_block($parse);
push @$blocks, $block
unless exists $block->{SKIP};
last if exists $block->{LAST};
$parse = []; # clear for next parse
}
last if !defined($lines->[0]);
}
my $only = [ grep { exists $_->{ONLY} } @$blocks ];
return @$only ? $only : $blocks;
}
sub _parse_testml_block {
my ($self, $lines) = @_;
my ($label) = $lines->[0] =~ /^$self->{block_marker}(?:\s+(.*))?$/;
shift @$lines until not(@$lines) or
$lines->[0] =~ /^$self->{point_marker} +\w+/;
my $block = $self->_parse_testml_points($lines);
$block->{Label} = $label || '';
return $block;
}
sub _parse_testml_points {
my ($self, $lines) = @_;
my $block = {};
while (@$lines) {
my $line = shift @$lines;
$line =~ /^$self->{point_marker} +(\w+)/
or die "Invalid TestML line:\n'$line'";
my $point_name = $1;
die "$block repeats $point_name"
if exists $block->{$point_name};
$block->{$point_name} = '';
if ($line =~ /^$self->{point_marker} +(\w+): +(.*?) *$/) {
($block->{$1} = $2) =~ s/^ *(.*?) *$/$1/;
shift @$lines while @$lines and
$lines->[0] !~ /^$self->{point_marker} +(\w)/;
}
elsif ($line =~ /^$self->{point_marker} +(\w+)$/) {
$point_name = $1;
while ( @$lines ) {
$line = shift @$lines;
if ($line =~ /^$self->{point_marker} \w+/) {
unshift @$lines, $line;
last;
}
$block->{$point_name} .= "$line\n";
}
$block->{$point_name} =~ s/\n\s*\z/\n/;
$block->{$point_name} =~ s/^\\//gm;
}
else {
die "Invalid TestML line:\n'$line'";
}
}
return $block;
}
#------------------------------------------------------------------------------
package TestML::Tiny::Function;
sub new {
my $self = bless {
statements => [],
data => [],
namespace => {},
}, $_[0];
}
#------------------------------------------------------------------------------
package TestML::Tiny::Bridge;
sub new {
my $self = bless { @_[1..$#_] }, $_[0];
}
#------------------------------------------------------------------------------
package TestML::Tiny::Library::Standard;
sub new {
my $self = bless { @_[1..$#_] }, $_[0];
}
1;
|