mirror of
https://github.com/Noratrieb/elven-forest.git
synced 2026-01-16 11:35:00 +01:00
tests
This commit is contained in:
parent
5ef748c2c5
commit
b76d57ee9d
9 changed files with 226 additions and 36 deletions
96
elven-wald/tests/integration/mod.rs
Normal file
96
elven-wald/tests/integration/mod.rs
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
mod simple_asm;
|
||||
|
||||
use std::{
|
||||
ffi::{OsStr, OsString},
|
||||
fmt::Display,
|
||||
path::PathBuf,
|
||||
process::Command,
|
||||
};
|
||||
|
||||
pub fn run(mut cmd: Command) {
|
||||
let out = cmd.output().expect("failed to spawn command");
|
||||
if !out.status.success() {
|
||||
panic!(
|
||||
"FAILED to run {}: {}",
|
||||
cmd.get_program().to_str().unwrap(),
|
||||
String::from_utf8_lossy(&out.stderr)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! elven_wald {
|
||||
($ctx:expr; $($args:expr),*) => {{
|
||||
let ctx = &$ctx;
|
||||
let output = ctx.file_ref("elven-wald-output");
|
||||
let mut cmd = std::process::Command::new("../target/debug/elven-wald");
|
||||
cmd.arg("-o");
|
||||
cmd.arg(&output);
|
||||
$( cmd.arg($args); )*
|
||||
$crate::integration::run(cmd);
|
||||
output
|
||||
}};
|
||||
}
|
||||
pub(crate) use elven_wald;
|
||||
|
||||
pub struct Ctx {
|
||||
_tempdir: tempfile::TempDir,
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
pub struct File(PathBuf);
|
||||
|
||||
impl Display for File {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(self.0.to_str().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<File> for OsString {
|
||||
fn from(value: File) -> Self {
|
||||
value.0.into_os_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<OsStr> for File {
|
||||
fn as_ref(&self) -> &OsStr {
|
||||
self.0.as_os_str()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ctx() -> Ctx {
|
||||
let tempdir = tempfile::tempdir().expect("failed to create tempdir");
|
||||
let path = tempdir.path().to_owned();
|
||||
Ctx {
|
||||
_tempdir: tempdir,
|
||||
path,
|
||||
}
|
||||
}
|
||||
|
||||
impl Ctx {
|
||||
#[allow(dead_code)]
|
||||
pub fn write_to_path(mut self, path: &str) -> Self {
|
||||
self.path = path.into();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn file_ref(&self, filename: &str) -> File {
|
||||
File(self.path.join(filename))
|
||||
}
|
||||
|
||||
pub fn file(&self, filename: &str, content: &str) -> File {
|
||||
let out = self.path.join(filename);
|
||||
std::fs::write(&out, content).expect("failed to write file");
|
||||
File(out)
|
||||
}
|
||||
|
||||
pub fn nasm(&self, filename: &str, content: &str) -> File {
|
||||
let input = self.file(&format!("{filename}.asm"), content);
|
||||
let out = self.path.join(filename);
|
||||
let mut cmd = Command::new("nasm");
|
||||
cmd.args(["-felf64", "-o"]);
|
||||
cmd.arg(&out);
|
||||
cmd.arg(input);
|
||||
run(cmd);
|
||||
File(out)
|
||||
}
|
||||
}
|
||||
56
elven-wald/tests/integration/simple_asm.rs
Normal file
56
elven-wald/tests/integration/simple_asm.rs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
use std::process::Command;
|
||||
|
||||
use crate::prelude::*;
|
||||
|
||||
use super::run;
|
||||
|
||||
#[test]
|
||||
fn single_object_file() {
|
||||
let ctx = ctx();
|
||||
|
||||
let empty = ctx.nasm(
|
||||
"empty",
|
||||
"
|
||||
global _start
|
||||
section .text
|
||||
_start:
|
||||
mov rax, 60
|
||||
mov rdi, 0
|
||||
syscall
|
||||
",
|
||||
);
|
||||
|
||||
let out = elven_wald!(ctx; empty);
|
||||
run(Command::new(out));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_object_files() {
|
||||
let ctx = ctx();
|
||||
|
||||
let start = ctx.nasm(
|
||||
"start",
|
||||
"
|
||||
global _start
|
||||
extern exit
|
||||
|
||||
section .text
|
||||
_start:
|
||||
call exit
|
||||
",
|
||||
);
|
||||
let exit = ctx.nasm(
|
||||
"exit",
|
||||
"
|
||||
global exit
|
||||
section .text
|
||||
exit:
|
||||
mov rax, 60
|
||||
mov rdi, 0
|
||||
syscall
|
||||
",
|
||||
);
|
||||
|
||||
let out = elven_wald!(ctx; start, exit);
|
||||
run(Command::new(out));
|
||||
}
|
||||
5
elven-wald/tests/runtest.rs
Normal file
5
elven-wald/tests/runtest.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
mod integration;
|
||||
|
||||
mod prelude {
|
||||
pub(crate) use crate::integration::{ctx, elven_wald};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue