Skip to content

Commit

Permalink
quick cleanups of the testsuite code
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiteknight committed Feb 19, 2011
1 parent 1f28bf8 commit f6be7b0
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 131 deletions.
1 change: 1 addition & 0 deletions setup.nqp
Expand Up @@ -61,6 +61,7 @@ sub MAIN(@argv) {
my @xunit_files := <
include/Core
xunit/Assertions
xunit/Failure
xunit/Listener
xunit/Listener/TAP
xunit/Loader
Expand Down
2 changes: 0 additions & 2 deletions xunit/Assertions.nqp
Expand Up @@ -3,8 +3,6 @@

module Assert;

our sub _initload() { }

our sub fail($why = "Unknown failure") {
my $ex := Exception::RosellaFailure.new();
$ex.message($why);
Expand Down
18 changes: 18 additions & 0 deletions xunit/Failure.nqp
@@ -0,0 +1,18 @@
class Rosella::Failure {
has $!fault;
has $!test_case;

our method fault($value?) {
if pir::defined__IP($value) {
$!fault := $value;
}
$!fault;
}

our method test_case($value?) {
if pir::defined__IP($value) {
$!test_case := $value;
}
$!test_case;
}
}
29 changes: 10 additions & 19 deletions xunit/Loader.nqp
Expand Up @@ -6,6 +6,10 @@ class Rosella::Loader {
has %!seen_methods;
has $!test_prefix;

method BUILD() {
$!test_prefix := 'test';
}

sub compare_methods($a, $b) {
pir::cmp__ISS(~$a, ~$b);
}
Expand All @@ -24,8 +28,7 @@ class Rosella::Loader {
}

our method default_suite() {
my $suite := Rosella::Suite.new();
$suite.BUILD();
my $suite := Rosella::build(Rosella::Suite);
return $suite;
}

Expand All @@ -44,6 +47,7 @@ class Rosella::Loader {
my $temp;
return @array;

# TODO: Reimplement this.
#while $bound > 0 {
# $swap := pir::rand__iiii(0, $bound);
# $swap-- if $swap > $bound; # Rare but possible
Expand Down Expand Up @@ -76,27 +80,14 @@ class Rosella::Loader {
self.order_tests(@test_methods);
}

our method _init_obj(*@pos, *%named) {
$!test_prefix := 'test';

self._init_args(|@pos, |%named);
}

# Returns true for "test_foo" and "testFoo" names
our method is_test_method($name) {
my $result := 0;
my $prefixlength := pir::length($!test_prefix);

if pir::length($name) > 4 && pir::substr($name, 0, 4) eq 'test' {

my $ch4 := $name[4];

# TODO: Translate this out. I don't think is_cclass is a method
# on String
if $ch4 eq '_'
|| $ch4.is_cclass(String::CharacterClass::UPPERCASE)
|| $ch4.is_cclass(String::CharacterClass::NUMERIC) {
$result := 1;
}
if pir::length($name) > $prefixlength &&
pir::substr($name, 0, $prefixlength) eq $!test_prefix {
$result := 1;
}

$result;
Expand Down
195 changes: 88 additions & 107 deletions xunit/Result.nqp
@@ -1,142 +1,123 @@
# Copyright (C) 2010, Austin Hastings. See accompanying LICENSE file, or
# http://www.opensource.org/licenses/artistic-license-2.0.php for license.

class Rosella::Failure {
has $!fault;
has $!test_case;

our method fault($value?) {
if pir::defined__IP($value) {
$!fault := $value;
class Rosella::Result {
has @!errors;
has @!failures;
has @!listeners;
has $!should_stop;
has $!num_tests;
has $!planned_tests;

our method should_stop($value?) {
if pir::defined($value) {
$!should_stop := $value;
}
$!fault;
$!should_stop;
}

our method test_case($value?) {
if pir::defined__IP($value) {
$!test_case := $value;
our method num_tests($value?) {
if pir::defined($value) {
$!num_tests := $value;
}
$!test_case;
$!num_tests;
}
}

class Rosella::Result;

has @!errors;
has @!failures;
has @!listeners;
has $!should_stop;
has $!num_tests;
has $!planned_tests;

our method should_stop($value?) {
if pir::defined($value) {
$!should_stop := $value;
our method add_error($test, $error) {
self.add_fault($test, $error, :notify('add_error'), :queue(@!errors));
}
$!should_stop;
}

our method num_tests($value?) {
if pir::defined($value) {
$!num_tests := $value;
our method add_failure($test, $failure) {
self.add_fault($test, $failure, :notify('add_failure'), :queue(@!failures));
}
$!num_tests;
}

our method add_error($test, $error) {
self.add_fault($test, $error, :notify('add_error'), :queue(@!errors));
}
my method add_fault($test, $exception, :$notify, :$queue) {
my $failure := Rosella::build(Rosella::Failure);
$failure.fault($exception);
$failure.test_case($test);

our method add_failure($test, $failure) {
self.add_fault($test, $failure, :notify('add_failure'), :queue(@!failures));
}
$queue.push($failure);
self.notify_listeners($notify, $failure);
}

my method add_fault($test, $exception, :$notify, :$queue) {
my $failure := Rosella::Failure.new();
$failure.fault($exception);
$failure.test_case($test);
our method add_listener($listener) {
@!listeners := @!listeners.push($listener);
self;
}

$queue.push($failure);
self.notify_listeners($notify, $failure);
}
our method end_test($test) {
self.notify_listeners('end_test', $test);
}

our method add_listener($listener) {
@!listeners := @!listeners.push($listener);
self;
}
our method error_count() {
pir::elements(@!errors);
}

our method end_test($test) {
self.notify_listeners('end_test', $test);
}
our method failure_count() {
pir::elements(@!failures);
}

our method error_count() {
pir::elements(@!errors);
}
my method notify_listeners($method, $arg) {
for @!listeners {
my $object := $_;
Q:PIR {
.local pmc object, meth, arg
object = find_lex '$object'
meth = find_lex '$method'
arg = find_lex '$arg'
our method failure_count() {
pir::elements(@!failures);
}
$I0 = isa meth, 'Sub'
unless $I0 goto call_string
my method notify_listeners($method, $arg) {
for @!listeners {
my $object := $_;
Q:PIR {
.local pmc object, meth, arg
object = find_lex '$object'
meth = find_lex '$method'
arg = find_lex '$arg'
object.meth(arg)
$I0 = isa meth, 'Sub'
unless $I0 goto call_string
object.meth(arg)
call_string:
$S0 = meth
object.$S0(arg)
};
}

call_string:
$S0 = meth
object.$S0(arg)
};
self;
}

self;
}

our method plan_tests($num_tests) {
# Ignore repeats in hierarchy of suites.
unless $!planned_tests {
$!planned_tests := $num_tests;
self.notify_listeners: 'plan_tests', $num_tests;
our method plan_tests($num_tests) {
# Ignore repeats in hierarchy of suites.
unless $!planned_tests {
$!planned_tests := $num_tests;
self.notify_listeners: 'plan_tests', $num_tests;
}
}
}

our method remove_listener($listener) {
my $index := 0;
our method remove_listener($listener) {
my $index := 0;

while $index < self.listeners.elems {
if self.listeners[$index] =:= $listener {
self.listeners.delete($index);
}
else {
$index++;
while $index < self.listeners.elems {
if self.listeners[$index] =:= $listener {
self.listeners.delete($index);
}
else {
$index++;
}
}
}

self;
}
self;
}

our method run_count() {
self.num_tests;
}
our method run_count() {
self.num_tests;
}

our method start_test($test) {
self.num_tests(self.num_tests + $test.num_tests);
self.notify_listeners('start_test', $test);
}
our method start_test($test) {
self.num_tests(self.num_tests + $test.num_tests);
self.notify_listeners('start_test', $test);
}

our method stop() {
self.should_stop(1);
self;
}
our method stop() {
self.should_stop(1);
self;
}

our method was_successful() {
self.error_count == 0 && self.failure_count == 0;
our method was_successful() {
self.error_count == 0 && self.failure_count == 0;
}
}
8 changes: 5 additions & 3 deletions xunit/Testcase.nqp
Expand Up @@ -7,7 +7,7 @@ class Rosella::Testcase {
has $!name;

my method default_loader() {
Rosella::Loader.new;
return Rosella::build(Rosella::Loader);
}

our method name($value?) { pir::defined__IP($value) ?? ($!name := $value) !! $!name; }
Expand All @@ -27,8 +27,8 @@ class Rosella::Testcase {
}

my method default_result() {
my $result := Rosella::Result.new;
$result.add_listener: Rosella::Listener::TAP.new;
my $result := Rosella::build(Rosella::Result);
$result.add_listener: Rosella::build(Rosella::Listener::TAP);
return $result;
}

Expand Down Expand Up @@ -100,10 +100,12 @@ class Rosella::Testcase {

our method tear_down() { }

# TODO: Figure out what this is and implement it
method todo_test( *@text ) {
$!todo := @text.join;
}

# TODO: Figure this out
method verify_that(*@text) {
$!verify := @text.join;
}
Expand Down

0 comments on commit f6be7b0

Please sign in to comment.