Last active
April 17, 2017 20:33
-
-
Save chromatic/8467580 to your computer and use it in GitHub Desktop.
A very basic, procedural TAP library for the Rust language (hardly idiomatic, but it's my second day)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[crate_id(name = "rusttap")]; | |
#[crate_type = "lib"]; | |
#[feature(globs)]; | |
#[desc = "TAP implementation for rust"]; | |
#[license = "BSD"]; | |
#[allow(dead_code)]; | |
#[allow(unused_variable)]; | |
pub mod rusttap { | |
use std::io; | |
static mut HAS_PLAN: bool = false; | |
static mut PLAN_COUNT: int = 0; | |
static mut TESTS_RUN: int = 0; | |
static mut TESTS_PASS: int = 0; | |
static mut TESTS_FAIL: int = 0; | |
static mut TESTS_SKIP: int = 0; | |
static mut TESTS_TODO: int = 0; | |
fn assert_has_plan() { | |
unsafe { | |
if !HAS_PLAN { fail!( "No plan set!" ) }; | |
}; | |
} | |
pub fn plan_tests( count: int ) { | |
unsafe { | |
if HAS_PLAN { fail!( "Plan already set!" ) }; | |
HAS_PLAN = true; | |
PLAN_COUNT = count; | |
}; | |
write_tap( "1 .. " + count.to_str() + "\n" ); | |
} | |
fn plan_no_plan() { | |
unsafe { | |
if HAS_PLAN { fail!( "Plan already set!" ) }; | |
HAS_PLAN = true; | |
PLAN_COUNT = 0; | |
}; | |
} | |
fn update_tests_run( pass: int, fail: int, todo: int, skip: int ) -> int { | |
assert_has_plan(); | |
let current_test = unsafe { | |
TESTS_RUN = TESTS_RUN + pass + fail + todo + skip; | |
TESTS_PASS = TESTS_PASS + pass; | |
TESTS_FAIL = TESTS_FAIL + fail; | |
TESTS_TODO = TESTS_TODO + todo; | |
TESTS_SKIP = TESTS_SKIP + skip; | |
TESTS_RUN | |
}; | |
return current_test; | |
} | |
pub fn ok( test: bool, description: &str) ->bool { | |
if test { pass( description ) } | |
else { fail( description ) } | |
} | |
pub fn nok( test: bool, description: &str) ->bool { | |
if test { fail( description ) } | |
else { pass( description ) } | |
} | |
pub fn pass(description: &str) ->bool { | |
let count = update_tests_run( 1, 0, 0, 0 ); | |
write_tap_for_test( "ok " + count.to_str(), description.to_str() ); | |
return true; | |
} | |
pub fn fail( description: &str) ->bool { | |
let count = update_tests_run( 0, 1, 0, 0 ); | |
write_tap_for_test( "not ok " + count.to_str(), description ); | |
return false; | |
} | |
pub fn done_testing() {} | |
pub fn plan_skip_all( reason: &str ) { | |
plan_tests( 0 ); | |
write_tap( "1..0\n# " + reason + "\n"); | |
} | |
fn write_tap_for_test( result: &str, description: &str) { | |
let desc = | |
if description == "" { "" } | |
else { " - " }; | |
write_tap( result + desc + description + "\n" ); | |
} | |
fn write_tap( output: &str ) { | |
let mut out = io::stdout(); | |
out.write( output.as_bytes() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment