Skip to content

Commit

Permalink
Add test for invalid bytecode and add invalid version m0
Browse files Browse the repository at this point in the history
  • Loading branch information
leto committed May 6, 2011
1 parent 71edc13 commit 0137f68
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/m0/m0_assembler.pl
Expand Up @@ -50,6 +50,13 @@ sub generate_bytecode {

# iterate over textual representation of bytecode
# use variable table to generate binary bytecode
my @lines = split /\n/, $b;
for my $line (@lines) {
if ($line =~ m/^(?<opname>\S+)\s+(?<arg1>\S+),(?<arg2>\S+),(?<arg3>\S+)\s+$/) {
} else {
say "Invalid M0 bytecode segment: $line";
}
}
}

sub parse_version {
Expand Down
9 changes: 9 additions & 0 deletions t/m0/invalid_bytecode.m0
@@ -0,0 +1,9 @@
# THIS IS INVALID M0 FOR TESTING PURPOSES ONLY
.version 0
.chunk
.variables
0 1
1 "hello world"
.metadata
.bytecode
set_var S0, 0, 1, 42, 42, A, JUNK
17 changes: 17 additions & 0 deletions t/m0/invalid_version.m0
@@ -0,0 +1,17 @@
# THIS IS INVALID M0 FOR TESTING PURPOSES ONLY
.version FOOBAR

.chunk

# This defines our variable table
.variables
0 1
1 "hello world"

.metadata

.bytecode

set_var S0, 0, 1
set_var I0, 0, 0
print_s I0, S0
8 changes: 7 additions & 1 deletion t/m0/m0_assembler.t
Expand Up @@ -21,7 +21,7 @@ use warnings;
use Test::More;
use File::Spec::Functions;

plan tests => 5;
plan tests => 6;

my $exefile = catfile( ".", qw/src m0 m0_assembler.pl/ );

Expand Down Expand Up @@ -55,6 +55,12 @@ output_like(
'detect invalid version'
);

output_like(
catfile(qw/t m0 invalid_bytecode.m0/),
qr/Invalid M0 bytecode/ms,
'detect invalid bytecode'
);

sub output_like {
my ($options, $snippet, $desc) = @_;

Expand Down

0 comments on commit 0137f68

Please sign in to comment.