yeet broken tests

This commit is contained in:
phyto 2023-05-06 10:55:27 +00:00
parent dac63b0fe2
commit 13b9d53d81
Signed by: phyto
SSH Key Fingerprint: SHA256:FJdIUDW+Q/c/oLlaNI7vrxCrv0VMxMgT6usJ+7A/wo0
10 changed files with 4 additions and 427 deletions

140
Cargo.lock generated
View File

@ -2,27 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "anyhow"
version = "1.0.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61604a8f862e1d5c3229fdd78f8b02c68dcf73a4c4b05fd636d12240aaa242c1"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "codespan-reporting"
version = "0.11.1"
@ -33,138 +12,27 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "ctor"
version = "0.1.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccc0a48a9b826acdf4028595adc9db92caea352f7af011a3034acd172a52a0aa"
dependencies = [
"quote",
"syn",
]
[[package]]
name = "diff"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499"
[[package]]
name = "generator"
version = "0.0.0"
dependencies = [
"anyhow",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "output_vt100"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9"
dependencies = [
"winapi",
]
[[package]]
name = "pretty-hex"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5c99d529f0d30937f6f4b8a86d988047327bb88d04d2c4afc356de74722131"
[[package]]
name = "pretty_assertions"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0cfe1b2403f172ba0f234e500906ee0a3e493fb81092dac23ebefe129301cc"
dependencies = [
"ansi_term",
"ctor",
"diff",
"output_vt100",
]
[[package]]
name = "proc-macro2"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ruxnasm"
version = "0.2.0"
dependencies = [
"codespan-reporting",
"generator",
"pretty-hex",
"pretty_assertions",
"test-case",
]
[[package]]
name = "syn"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "termcolor"
version = "1.1.2"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]]
name = "test-case"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "956044ef122917dde830c19dec5f76d0670329fde4104836d62ebcb14f4865f1"
dependencies = [
"cfg-if",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "unicode-width"
version = "0.1.8"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "winapi"

View File

@ -11,9 +11,6 @@ keywords = ["assembler", "uxn", "uxntal"]
categories = ["command-line-utilities", "compilers"]
exclude = [".github", ".vscode", "docs"]
[workspace]
members = ["tests/generator"]
[[bin]]
name = "ruxnasm"
required-features = ["bin"]
@ -26,9 +23,3 @@ bin = ["codespan-reporting"]
[dependencies]
codespan-reporting = { version = "0.11.1", optional = true }
[dev-dependencies]
generator = { path = "tests/generator" }
pretty_assertions = "1.0.0"
pretty-hex = "0.2"
test-case = "1.1"

View File

@ -1,14 +0,0 @@
[package]
name = "generator"
version = "0.0.0"
authors = ["Karol Belina <karolbelina@gmail.com>"]
edition = "2018"
[lib]
proc-macro = true
[dependencies]
anyhow = "1.0"
proc-macro2 = "1.0"
quote = "1.0"
syn = { version = "1.0", features = ["full"] }

View File

@ -1,27 +0,0 @@
mod test;
mod tests;
mod tests_mod;
mod utils;
use proc_macro::TokenStream;
use quote::quote;
use test::Test;
use tests::Tests;
use tests_mod::TestsMod;
#[proc_macro]
pub fn generate_tests(_: TokenStream) -> TokenStream {
let tests = match Tests::discover("tests/suite") {
Ok(tests) => tests,
Err(err) => {
panic!("\n{:?}", err);
}
};
let tests = tests.expand();
(quote! {
#tests
})
.into()
}

View File

@ -1,83 +0,0 @@
use crate::utils::escape_name;
use anyhow::{bail, Result};
use proc_macro2::TokenStream;
use quote::quote;
use std::path::{Component, Path, PathBuf};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Test {
name: String,
dirs: Vec<String>,
path: PathBuf,
}
impl Test {
pub fn load(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref();
let mut components = human_readable_components(path).collect::<Vec<_>>();
let name = components.pop().unwrap();
let relative_path = Path::new("tests/suite").join(path);
if !relative_path.join("input.tal").exists() {
bail!("Test is missing the `input.tal` file");
}
Ok(Self {
name,
dirs: components,
path: relative_path,
})
}
pub fn dirs(&self) -> impl Iterator<Item = String> + '_ {
self.dirs.iter().cloned()
}
pub fn expand(&self) -> TokenStream {
let name = escape_name(&self.name);
let input_path_string = self.path.join("input.tal").display().to_string();
let output_path_string = self.path.join("output.rom").display().to_string();
quote! {
#[test]
fn #name() {
let input_path = ::std::path::Path::new(#input_path_string);
let output_path = ::std::path::Path::new(#output_path_string);
let input = ::std::fs::read(input_path).unwrap();
let expected_output = if output_path.exists() {
Some(::std::fs::read(output_path).unwrap())
} else {
None
};
match (assemble(&input), expected_output) {
(Ok((actual_binary, _)), Some(expected_binary)) => {
if actual_binary != expected_binary {
let actual_hex_dump = HexDump::new(&actual_binary);
let expected_hex_dump = HexDump::new(&expected_binary);
pretty_assertions::assert_eq!(expected_hex_dump, actual_hex_dump);
}
},
(Ok((actual_binary, _)), None) => {
panic!("expected no output but received some");
}
(Err((errors, _)), Some(_)) => {
panic!("expected some output but received none");
}
(Err(_), None) => (),
}
}
}
}
}
fn human_readable_components<'a>(path: &'a Path) -> impl Iterator<Item = String> + 'a {
path.components().flat_map(|component| {
if let Component::Normal(dir) = component {
Some(dir.to_string_lossy().into_owned())
} else {
None
}
})
}

View File

@ -1,36 +0,0 @@
use crate::{Test, TestsMod};
use anyhow::{bail, Context as _, Result};
use proc_macro2::TokenStream;
use std::{
fs,
path::{Path, PathBuf},
};
#[derive(Debug)]
pub struct Tests {
tests: Vec<Test>,
}
impl Tests {
pub fn discover(dir: impl AsRef<Path>) -> Result<Self> {
if !dir.as_ref().exists() {
bail!("Cound't find the test suite");
}
let tests = fs::read_to_string(dir.as_ref().join("index"))
.with_context(|| format!("Couldn't find the test suite's index"))?
.lines()
.map(|line| line.into())
.map(|path: PathBuf| {
Test::load(&path)
.with_context(|| format!("Couldn't load test: {}", path.display(),))
})
.collect::<Result<Vec<_>>>()?;
Ok(Self { tests })
}
pub fn expand(&self) -> TokenStream {
TestsMod::build(&self.tests).expand()
}
}

View File

@ -1,59 +0,0 @@
use crate::utils::escape_name;
use crate::Test;
use proc_macro2::TokenStream;
use quote::quote;
use std::collections::{BTreeMap, BTreeSet};
#[derive(Default, Debug)]
pub struct TestsMod<'a> {
tests: BTreeSet<&'a Test>,
children: BTreeMap<String, Self>,
}
impl<'a> TestsMod<'a> {
pub fn build(tests: &'a [Test]) -> Self {
tests.iter().fold(Self::default(), |mut this, test| {
this.add(test);
this
})
}
fn add(&mut self, test: &'a Test) {
let mut this = self;
for test_dir in test.dirs() {
this = this.children.entry(test_dir).or_default();
}
this.tests.insert(test);
}
pub fn expand(&self) -> TokenStream {
let tests = self.expand_tests();
let children = self.expand_children();
quote! {
#(#tests)*
#(#children)*
}
}
fn expand_tests(&self) -> impl Iterator<Item = TokenStream> + '_ {
self.tests.iter().map(|test| test.expand())
}
fn expand_children(&self) -> impl Iterator<Item = TokenStream> + '_ {
self.children.iter().map(|(name, children)| {
let name = escape_name(name);
let children = children.expand();
quote! {
mod #name {
use super::*;
#children
}
}
})
}
}

View File

@ -1,43 +0,0 @@
use proc_macro2::{Ident, Span};
pub fn escape_name(name: &str) -> Ident {
const KEYWORDS: &[&str] = &[
"as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum",
"extern", "false", "fn", "for", "if", "impl", "in", "let", "loop", "match", "mod",
"move", "mut", "pub", "ref", "return", "Self", "self", "static", "struct", "super",
"trait", "true", "type", "union", "unsafe", "use", "where", "while", "abstract",
"become", "box", "do", "final", "macro", "override", "priv", "try", "typeof",
"unsized", "virtual", "yield",
];
if name.is_empty() {
return Ident::new("_empty", Span::call_site());
}
if KEYWORDS.contains(&name) {
return Ident::new(&format!("_{}", name), Span::call_site());
}
let mut last_under = false;
let mut ident: String = name
.to_ascii_lowercase()
.chars()
.filter_map(|c| match c {
c if c.is_alphanumeric() => {
last_under = false;
Some(c.to_ascii_lowercase())
}
_ if !last_under => {
last_under = true;
Some('_')
}
_ => None,
})
.collect();
if !ident.starts_with(|c: char| c == '_' || c.is_ascii_alphabetic()) {
ident = format!("_{}", ident);
}
Ident::new(&ident, Span::call_site())
}

View File

@ -1,19 +0,0 @@
use ruxnasm::assemble;
#[derive(PartialEq, Eq)]
struct HexDump<'a>(&'a [u8]);
impl<'a> HexDump<'a> {
pub fn new(binary: &'a [u8]) -> Self {
Self(binary)
}
}
impl<'a> std::fmt::Debug for HexDump<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let hex_dump = pretty_hex::pretty_hex(&self.0);
write!(f, "{}", hex_dump)
}
}
generator::generate_tests!();

@ -1 +0,0 @@
Subproject commit 270f196212c087d0aa76d4d890f4bf892ac92602