Skip to content

Commit

Permalink
xunit: remove the Standalone class. It wasn't used in the current scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
Whiteknight committed Feb 17, 2011
1 parent 5ca5b3c commit ddbe2e5
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 282 deletions.
173 changes: 87 additions & 86 deletions xunit/Loader.nqp
@@ -1,117 +1,118 @@
# Copyright (C) 2010, Austin Hastings. See accompanying LICENSE file, or
# http://www.opensource.org/licenses/artistic-license-2.0.php for license.

class UnitTest::Loader;
class UnitTest::Loader {

has $!class;
has %!seen_methods;
has $!test_prefix;
has $!class;
has %!seen_methods;
has $!test_prefix;

sub compare_methods($a, $b) {
pir::cmp__ISS(~$a, ~$b);
}
sub compare_methods($a, $b) {
pir::cmp__ISS(~$a, ~$b);
}

our method configure_suite(@tests, :$suite = self.default_suite, *%named) {
my $metaclass := pir::getprop__psp('metaclass', $!class);
my $protoobject := pir::getattribute__pps($metaclass, 'protoobject');
our method configure_suite(@tests, :$suite = self.default_suite, *%named) {
my $metaclass := pir::getprop__psp('metaclass', $!class);
my $protoobject := pir::getattribute__pps($metaclass, 'protoobject');

for @tests -> $test {
my $test_obj := $protoobject.new();
$test_obj.name($test);
$suite.add_test: $test_obj
}
for @tests -> $test {
my $test_obj := $protoobject.new();
$test_obj.name($test);
$suite.add_test: $test_obj
}

$suite;
}
$suite;
}

our method default_suite() {
UnitTest::Suite.new();
}
our method default_suite() {
UnitTest::Suite.new();
}

sub hash_contains(%hash, $key) {
return Q:PIR {
$P0 = find_lex '%hash'
$P1 = find_lex '$key'
$I0 = exists $P0[$P1]
%r = box $I0
};
}
sub hash_contains(%hash, $key) {
return Q:PIR {
$P0 = find_lex '%hash'
$P1 = find_lex '$key'
$I0 = exists $P0[$P1]
%r = box $I0
};
}
sub array_unsort(@array) {
my $bound := pir::elements(@array) - 1;
my $swap;
my $temp;
return @array;

#while $bound > 0 {
# $swap := pir::rand__iiii(0, $bound);
# $swap-- if $swap > $bound; # Rare but possible
# $temp := @array[$bound];
# @array[$bound] := @array[$swap];
# @array[$swap] := $temp;
# $bound--;
#}

#@array;
}
sub array_unsort(@array) {
my $bound := pir::elements(@array) - 1;
my $swap;
my $temp;
return @array;

#while $bound > 0 {
# $swap := pir::rand__iiii(0, $bound);
# $swap-- if $swap > $bound; # Rare but possible
# $temp := @array[$bound];
# @array[$bound] := @array[$swap];
# @array[$swap] := $temp;
# $bound--;
#}

#@array;
}

our method get_test_methods() {
my @mro := $!class.inspect('all_parents');
my @test_methods := [ ];
our method get_test_methods() {
my @mro := $!class.inspect('all_parents');
my @test_methods := [ ];

for @mro {
my %methods := $_.inspect('methods');
for @mro {
my %methods := $_.inspect('methods');

for %methods {
my $name := ~ $_;
for %methods {
my $name := ~ $_;

if self.is_test_method($name) && !hash_contains(%!seen_methods, $name) {
%!seen_methods{$name} := 1;
@test_methods.push($name);
if self.is_test_method($name) && !hash_contains(%!seen_methods, $name) {
%!seen_methods{$name} := 1;
@test_methods.push($name);
}
}
}
}

self.order_tests(@test_methods);
}
self.order_tests(@test_methods);
}

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

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

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

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

my $ch4 := $name[4];
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;
# 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;
}
}
}

$result;
}
$result;
}

our method load_tests_from_testcase($class, *%named) {
$!class := P6metaclass.get_parrotclass($class);
my @tests := self.get_test_methods;
our method load_tests_from_testcase($class, *%named) {
$!class := P6metaclass.get_parrotclass($class);
my @tests := self.get_test_methods;

self.configure_suite(@tests, |%named);
}
self.configure_suite(@tests, |%named);
}

our method order_tests(@tests) {
array_unsort(@tests);
}
our method order_tests(@tests) {
array_unsort(@tests);
}

our method test_prefix($value?) {
$value ?? ($!test_prefix := $value) !! $!test_prefix;
our method test_prefix($value?) {
$value ?? ($!test_prefix := $value) !! $!test_prefix;
}
}
53 changes: 0 additions & 53 deletions xunit/Standalone.nqp
@@ -1,53 +0,0 @@
# Copyright (C) 2010, Austin Hastings. See accompanying LICENSE file, or
# http://www.opensource.org/licenses/artistic-license-2.0.php for license.

# Parent class of both Testcase and Suite, this class has a name, and a MAIN/main routine
# to invoke when running as a standalone test program.
class UnitTest::Standalone;

has $!name;

our method MAIN() {
unless our $_Already_running {
# Prevent loaded testcases from running themselves automatically.
$_Already_running := 1;
self.main();
}
}

our method main() {
# TODO: Should be a Program here. Need to process args, in case I want to run just
# a few of the test cases.
self.suite.run();
}

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

our method run($result?) {
unless pir::defined__IP($result) {
$result := self.default_result;
}

$result.plan_tests(self.num_tests);

for self.members {
unless $result.should_stop {
$_.run($result);
}
}

$result;
}

sub sort_cmp($a, $b) {
$a.name lt $b.name ?? -1 !! 1;
}

our method sort() {
self.members.sort(UnitTest::Suite::sort_cmp);
self;
}

our method suite() {
self;
}
83 changes: 42 additions & 41 deletions xunit/Suite.nqp
@@ -1,58 +1,59 @@
# Copyright (C) 2010, Austin Hastings. See accompanying LICENSE file, or
# http://www.opensource.org/licenses/artistic-license-2.0.php for license.

class UnitTest::Suite is UnitTest::Standalone;

has @!members;
has $!num_tests;

our method add_test($test) {
@!members := @!members.push($test);
$!num_tests := $!num_tests + $test.num_tests;
self;
}
class UnitTest::Suite {
has @!members;
has $!num_tests;

our method add_test($test) {
@!members := @!members.push($test);
$!num_tests := $!num_tests + $test.num_tests;
self;
}

our method add_tests(*@tests) {
for @tests {
self.add_test($_);
our method add_tests(*@tests) {
for @tests {
self.add_test($_);
}
self;
}
self;
}

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

our method _init_obj(*@pos, *%named) {
$!num_tests := 0;
@!members := [ ];
our method _init_obj(*@pos, *%named) {
$!num_tests := 0;
@!members := [ ];

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

our method run($result = self.default_result) {
$result.plan_tests($!num_tests);
our method run($result = self.default_result) {
$result.plan_tests($!num_tests);

for @!members {
unless $result.should_stop {
$_.run($result);
for @!members {
unless $result.should_stop {
$_.run($result);
}
}

$result;
}

$result;
}
sub sort_cmp($a, $b) {
$a.name lt $b.name ?? -1 !! 1;
}

sub sort_cmp($a, $b) {
$a.name lt $b.name ?? -1 !! 1;
}
our method sort() {
@!members.sort(UnitTest::Suite::sort_cmp);
self;
}

our method sort() {
@!members.sort(UnitTest::Suite::sort_cmp);
self;
our method suite() {
self;
}
}

our method suite() {
self;
}

0 comments on commit ddbe2e5

Please sign in to comment.