clean up tests/

give helper function a more descriptive name
This commit is contained in:
Skgland
2021-03-11 01:21:20 +01:00
parent d4d47182b4
commit e1c681fffe
7 changed files with 245 additions and 66 deletions

View File

@@ -1,66 +0,0 @@
/// Loads the file and if some expected output is given checks that it matches
fn test_file(file: &str, expected: Option<&[u8]>) {
use scryer_prolog::*;
let input = machine::Stream::from("");
let output = machine::Stream::from(String::new());
let mut wam = machine::Machine::new(input, output.clone());
wam.load_file(
file.into(),
machine::Stream::from(
std::fs::read_to_string(AsRef::<std::path::Path>::as_ref(file)).unwrap(),
),
);
if let Some(expected) = expected {
let output = output.bytes().unwrap();
assert_eq!(output.as_slice(), expected);
}
}
#[test]
fn builtins() {
test_file("src/tests/builtins.pl", Some(b""));
}
#[test]
fn call_with_inference_limit() {
test_file("src/tests/call_with_inference_limit.pl", Some(b""));
}
#[test]
fn facts() {
test_file("src/tests/facts.pl", Some(b""));
}
#[test]
fn hello_world() {
test_file(
"src/tests/hello_world.pl",
Some("Hello World!\n".as_bytes()),
);
}
#[test]
fn predicates() {
test_file("src/tests/predicates.pl", Some(b""));
}
#[test]
fn rules() {
test_file("src/tests/rules.pl", Some(b""));
}
#[test]
#[ignore] // ignored as this does not appear to terminate
fn setup_call_cleanup() {
test_file("src/tests/setup_call_cleanup.pl", Some(b""));
}
#[test]
#[ignore] // ignored as this does not terminate
fn clpz() {
test_file("src/tests/clpz/test_clpz.pl", Some(b""));
}

84
tests/scryer/helper.rs Normal file
View File

@@ -0,0 +1,84 @@
use assert_cmd::Command;
use std::ffi::OsStr;
pub(crate) trait Expectable {
#[track_caller]
fn assert_eq(self, other: &[u8]);
}
impl Expectable for &str {
#[track_caller]
fn assert_eq(self, other: &[u8]) {
if let Ok(other_str) = std::str::from_utf8(other) {
assert_eq!(other_str, self)
} else {
// should always fail as other is not valid utf-8 but self is
// just for consistent assert error message
assert_eq!(other, self.as_bytes())
}
}
}
impl Expectable for &[u8] {
#[track_caller]
fn assert_eq(self, other: &[u8]) {
assert_eq!(other, self)
}
}
/// Tests whether the file can be successfully loaded
/// and produces the expected output during it
pub(crate) fn load_module_test<T: Expectable>(file: &str, expected: T) {
use scryer_prolog::*;
let input = machine::Stream::from("");
let output = machine::Stream::from(String::new());
let mut wam = machine::Machine::new(input, output.clone());
wam.load_file(
file.into(),
machine::Stream::from(
std::fs::read_to_string(AsRef::<std::path::Path>::as_ref(file)).unwrap(),
),
);
let output = output.bytes().unwrap();
expected.assert_eq(output.as_slice());
}
pub const SCRYER_PROLOG: &str = "scryer-prolog";
pub fn run_top_level_test_no_args<
S: Into<Vec<u8>>,
O: assert_cmd::assert::IntoOutputPredicate<P>,
P: predicates_core::Predicate<[u8]>,
>(
stdin: S,
expected_stdout: O,
) {
run_top_level_test_with_args::<&[&str], _, _, _, _>(&[], stdin, expected_stdout)
}
/// Test whether scryr-prolog
/// produces the expected output when called with the supplied
/// arguments and fed the supplied input
pub fn run_top_level_test_with_args<
A: IntoIterator<Item = AS>,
S: Into<Vec<u8>>,
O: assert_cmd::assert::IntoOutputPredicate<P>,
AS: AsRef<OsStr>,
P: predicates_core::Predicate<[u8]>,
>(
args: A,
stdin: S,
expected_stdout: O,
) {
Command::cargo_bin(SCRYER_PROLOG)
.unwrap()
.args(args)
.write_stdin(stdin)
.assert()
.stdout(expected_stdout.into_output())
.success();
}

3
tests/scryer/main.rs Normal file
View File

@@ -0,0 +1,3 @@
mod helper;
mod src_tests;

59
tests/scryer/src_tests.rs Normal file
View File

@@ -0,0 +1,59 @@
use crate::helper::{load_module_test, run_top_level_test_with_args};
#[test]
fn builtins() {
load_module_test("src/tests/builtins.pl", "");
}
#[test]
fn call_with_inference_limit() {
load_module_test("src/tests/call_with_inference_limit.pl", "");
}
#[test]
fn facts() {
load_module_test("src/tests/facts.pl", "");
}
#[test]
fn hello_world() {
load_module_test("src/tests/hello_world.pl", "Hello World!\n");
}
#[test]
fn syntax_error() {
load_module_test(
"tests-pl/syntax_error.pl",
"caught: error(syntax_error(incomplete_reduction),read_term/3:5)\n",
);
}
#[test]
fn predicates() {
load_module_test("src/tests/predicates.pl", "");
}
#[test]
fn rules() {
load_module_test("src/tests/rules.pl", "");
}
#[test]
fn setup_call_cleanup_load() {
load_module_test("src/tests/setup_call_cleanup.pl", "caught: unthrown\n");
}
#[test]
fn setup_call_cleanup_process() {
run_top_level_test_with_args(
&["src/tests/setup_call_cleanup.pl"],
"",
"caught: unthrown\n",
);
}
#[test]
#[ignore] // ignored as this does not terminate
fn clpz_load() {
load_module_test("src/tests/clpz/test_clpz.pl", "");
}