add spec tests

This commit is contained in:
Noah Hellman 2022-11-12 18:46:13 +01:00
parent 40a612df95
commit 8c0abca85f
7 changed files with 143 additions and 0 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "jgm/djot"]
path = modules/djot
url = git@github.com:jgm/djot.git

View file

@ -11,5 +11,11 @@ homepage = "https://hllmn.net/projects/jotdown"
repository = "https://github.com/hellux/jotdown"
documentation = "https://docs.rs/jotdown"
exclude = [
".gitmodules",
"Makefile",
"modules",
"tests",
]
[features]
suite = [] # test suite

16
Makefile Normal file
View file

@ -0,0 +1,16 @@
.POSIX:
.PHONY: suite
suite:
git submodule update --init modules/djot
for f in $$(find modules/djot/test -name '*.test' | xargs basename -a); do \
ln -fs ../../modules/djot/test/$$f tests/suite/$$f; \
done
(cd tests/suite && make)
cargo test --features suite
clean:
cargo clean
git submodule deinit -f --all
rm -f tests/suite/*.test
(cd tests/suite && make clean)

1
modules/djot Submodule

@ -0,0 +1 @@
Subproject commit 4b3bf26dde0fa46a5d22b536154e3634619ef3a4

48
tests/lib.rs Normal file
View file

@ -0,0 +1,48 @@
#[cfg(feature = "suite")]
mod suite;
#[cfg(feature = "suite")]
#[macro_export]
macro_rules! suite_test {
($src:expr, $expected:expr) => {
let src = $src;
let expected = $expected;
let p = jotdown::Parser::new(src);
let mut actual = String::new();
jotdown::html::push(p, &mut actual);
assert_eq!(
actual.trim(),
expected.trim(),
concat!(
"\n",
"\x1b[0;1m========================= INPUT ============================\x1b[0m\n",
"\x1b[2m{}",
"\x1b[0;1m=================== ACTUAL vs EXPECTED =====================\x1b[0m\n",
"{}",
"\x1b[0;1m============================================================\x1b[0m\n",
),
$src,
{
let a = actual.trim().split('\n');
let b = expected.trim().split('\n');
let max = a.clone().count().max(b.clone().count());
let a_width = a.clone().map(|a| a.len()).max().unwrap_or(0);
a.chain(std::iter::repeat(""))
.zip(b.chain(std::iter::repeat("")))
.take(max)
.map(|(a, b)| {
format!(
"\x1b[{}m{:a_width$}\x1b[0m {}= \x1b[{}m{}\x1b[0m\n",
if a == b { "2" } else { "31" },
a,
if a == b { '=' } else { '!' },
if a == b { "2" } else { "32" },
b,
a_width = a_width,
)
})
.collect::<String>()
},
);
};
}

22
tests/suite/Makefile Normal file
View file

@ -0,0 +1,22 @@
.POSIX:
.SUFFIXES: .test .rs
TEST=$(shell find . -name '*.test' | sort)
TEST_RS=${TEST:.test=.rs}
.PHONY: suite
suite: mod.rs
mod.rs: ${TEST_RS}
printf "" > $@
for f in ${TEST}; do \
name=$$(basename -s .test $$f); \
echo "mod $$name;" >> $@; \
done
.test.rs:
awk -fgen.awk $< > $@
clean:
rm -f *.rs

47
tests/suite/gen.awk Normal file
View file

@ -0,0 +1,47 @@
BEGIN {
print "use crate::suite_test;"
print ""
}
$0 ~ "^`{3,}$" {
l=length($0)
if (fence == 0) { # enter fence
print "#[test]"
printf "fn test%02d() {\n", count
printf " let src = r##\""
fence=l
count+=1
} else if (fence == l) { # exit fence
if (ignore) {
ignore=0
} else {
print "\"##;"
print " suite_test!(src, expected);"
print "}"
print ""
}
fence=0
} else {
print $0 # md/html
}
next
}
fence == 0 && $0 ~ "^`{3,} .*$" {
ignore=1
fence=match($0, "[^`]")-1
next
}
$0 ~ "^\\.$" && !ignore { # enter html
print "\"##;"
printf " let expected = r##\""
next
}
!ignore {
if (fence==0 && $0 != "") { # comment
printf "// "
}
print $0 # comment/md/html
}