Skip to content

Commit dbcf122

Browse files
committed
PR #38 mv test suites from main crate
Merge branch 'test_crates'
2 parents e8f2002 + 4105bc8 commit dbcf122

19 files changed

Lines changed: 191 additions & 59 deletions

File tree

.github/workflows/ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ jobs:
3434
RUSTDOCFLAGS: -D warnings
3535
run: |
3636
make check
37-
suite:
38-
name: Build and run external tests
37+
test_html:
38+
name: Build and run HTML tests
3939
runs-on: ubuntu-latest
4040
steps:
4141
- name: "Checkout repo"
@@ -44,14 +44,14 @@ jobs:
4444
run: |
4545
rustup update 1.56
4646
rustup default 1.56
47-
- name: "Run unit tests"
48-
run: make suite
47+
- name: "Run HTML unit tests"
48+
run: make test_html_ut
4949
- name: "Setup node"
5050
uses: actions/setup-node@v3
5151
with:
5252
node-version: 18
53-
- name: "Compare benchmark files"
54-
run: make suite_bench
53+
- name: "Compare HTML with reference implementation"
54+
run: make test_html_ref
5555
lint:
5656
name: Lint
5757
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ members = [
2626
"bench/iai",
2727
"bench/input",
2828
"examples/jotdown_wasm",
29+
"tests/html-ref",
30+
"tests/html-ut",
2931
]
3032
exclude = [
3133
"tests/afl",
@@ -39,6 +41,4 @@ doc = false
3941
[features]
4042
default = ["html"]
4143
html = [] # html renderer and minimal cli binary
42-
suite = [] # test suite
43-
suite_bench = [] # bench test suite
4444
deterministic = [] # for stable fuzzing

Makefile

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,34 @@ docs:
1515

1616
.PHONY: lint
1717
lint:
18+
cargo clippy --workspace -- -D warnings
19+
cargo clippy --workspace --no-default-features -- -D warnings
20+
cargo clippy --workspace --all-features -- -D warnings
1821
cargo fmt --all -- --check
19-
cargo clippy -- -D warnings
20-
cargo clippy --no-default-features -- -D warnings
21-
cargo clippy --all-features -- -D warnings
2222

2323
.PHONY: check
2424
check:
2525
cargo test --workspace
2626
cargo test --workspace --no-default-features
2727

28-
.PHONY: suite
29-
suite:
28+
.PHONY: test_html_ut
29+
test_html_ut:
3030
git submodule update --init modules/djot.js
3131
for f in $$(find modules/djot.js/test -name '*.test' | xargs basename -a); do \
32-
ln -fs ../../modules/djot.js/test/$$f tests/suite/djot_js_$$f; \
32+
ln -fs ../../../modules/djot.js/test/$$f tests/html-ut/ut/djot_js_$$f; \
3333
done
34-
(cd tests/suite && make)
35-
cargo test --features suite suite::
34+
cargo test -p test-html-ut
35+
cargo test -p test-html-ut -- --ignored 2>/dev/null | grep -q 'test result: FAILED. 0 passed'
3636

37-
.PHONY: suite_bench
38-
suite_bench:
37+
.PHONY: test_html_ref
38+
test_html_ref:
3939
git submodule update --init modules/djot.js
4040
for f in $$(find modules/djot.js/bench -name '*.dj' | xargs basename -a); do \
4141
dst=$$(echo $$f | sed 's/-/_/g'); \
42-
ln -fs ../../modules/djot.js/bench/$$f tests/bench/$$dst; \
42+
ln -fs ../../modules/djot.js/bench/$$f tests/html-ref/$$dst; \
4343
done
44-
(cd tests/bench && make)
45-
cargo test --features suite_bench bench::
44+
cargo test -p test-html-ref
45+
cargo test -p test-html-ref -- --ignored 2>/dev/null | grep -q 'test result: FAILED. 0 passed'
4646

4747
.PHONY: bench
4848
bench:
@@ -52,9 +52,6 @@ bench:
5252
ln -fs ../../modules/djot.js/bench/$$f bench/input/$$dst; \
5353
done
5454

55-
cov: suite suite_bench
56-
LLVM_COV=llvm-cov LLVM_PROFDATA=llvm-profdata cargo llvm-cov --features=suite,suite_bench --workspace --html --ignore-run-fail
57-
5855
AFL_TARGET?=parse
5956
AFL_JOBS?=1
6057
AFL_TARGET_CRASH?=crashes
@@ -101,11 +98,12 @@ afl_tmin:
10198

10299
clean:
103100
cargo clean
101+
rm -rf bench/iai/target
104102
git submodule deinit -f --all
105-
find tests -type l -path 'tests/suite/*.test' -print0 | xargs -0 rm -f
106-
(cd tests/suite && make clean)
107-
rm -f tests/bench/*.dj
108-
(cd tests/bench && make clean)
103+
find tests -type l -path 'tests/html-ut/ut/*.test' -print0 | xargs -0 rm -f
104+
(cd tests/html-ut && make clean)
105+
rm -f tests/html-ref/*.dj
106+
(cd tests/html-ref && make clean)
109107
find bench -type l -path 'bench/*.dj' -print0 | xargs -0 rm -f
110108
rm -rf tests/afl/out
111109
(cd examples/jotdown_wasm && make clean)

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,19 @@ including:
121121
- footnotes.
122122

123123
The HTML output is in some cases not exactly identical to the [reference
124-
implementation][djot-js]. There are two test suites that compares Jotdown with
125-
the reference implementation. One uses the unit tests of the reference
126-
implementation and runs them with Jotdown. It can be run with:
124+
implementation][djot-js]. There are two test suites that compares Jotdown's
125+
HTML output with that of the reference implementation. One uses the unit tests
126+
of the reference implementation and runs them with Jotdown. It can be run with:
127127

128128
```
129-
$ make suite
129+
$ make test_html_ut
130130
```
131131

132132
Another target uses the reference implementation to generate html output for
133133
its benchmark files and compares it to the output of Jotdown:
134134

135135
```
136-
$ make suite_bench
136+
$ make test_html_ref
137137
```
138138

139139
Note that it requires node in order to run the reference implementation.

src/html.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,10 @@ impl<'s> Writer<'s> {
392392
Event::Softbreak => out.write_char('\n')?,
393393
Event::Escape | Event::Blankline => {}
394394
Event::ThematicBreak(attrs) => {
395-
out.write_str("\n<hr")?;
395+
if self.not_first_line {
396+
out.write_char('\n')?;
397+
}
398+
out.write_str("<hr")?;
396399
for (a, v) in attrs.iter() {
397400
write!(out, r#" {}=""#, a)?;
398401
v.parts().try_for_each(|part| write_attr(part, &mut out))?;

tests/html-ref/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "test-html-ref"
3+
description = "Reference implementation HTML output comparison tests"
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
[dependencies]
8+
jotdown = { path = "../.." }
9+
10+
[lib]
11+
name = "test_html_ref"
12+
path = "lib.rs"
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ TEST=${TEST_DJ:.dj=}
66
DJOT_JS=../../modules/djot.js
77
DJOT_JS_SRC=$(shell find ${DJOT_JS}/src -name '.ts')
88

9-
mod.rs: ${TEST_DJ} html
10-
echo "use crate::suite_test;" > $@
9+
ref.rs: ${TEST_DJ} html
10+
echo "use crate::compare;" > $@
1111
for name in ${TEST}; do \
1212
name_snake=$$(basename -a $$name); \
1313
skip_reason=$$(grep -E "^$${name_snake}:" skip | cut -d: -f2); \
@@ -17,10 +17,8 @@ mod.rs: ${TEST_DJ} html
1717
printf ' let src = r###"'; \
1818
cat $$name.dj; \
1919
echo '"###;'; \
20-
printf ' let expected = r###"'; \
21-
cat $$name.html; \
22-
echo '"###;'; \
23-
echo " suite_test!(src, expected);"; \
20+
printf ' let expected = "%s";' "$$name.html"; \
21+
echo " compare!(src, expected);"; \
2422
echo "}"; \
2523
done >> $@
2624

@@ -36,6 +34,7 @@ djot-js: ${DJOT_JS_SRC}
3634
chmod +x $@
3735

3836
clean:
39-
rm -f *.rs *.html
37+
rm -f ref.rs
38+
rm -f *.html
4039
rm -f html
4140
rm -f djot-js

tests/html-ref/build.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
fn main() {
2+
let has_dj = std::fs::read_dir(".").unwrap().any(|e| {
3+
e.map_or(false, |e| {
4+
e.path()
5+
.extension()
6+
.map_or(false, |ext| ext.to_str() == Some("dj"))
7+
})
8+
});
9+
if has_dj {
10+
let status = std::process::Command::new("make")
11+
.status()
12+
.expect("failed to execute make");
13+
assert!(status.success());
14+
} else {
15+
std::fs::write("ref.rs", &[b'\n']).unwrap();
16+
}
17+
}

tests/html-ref/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#[cfg(test)]
2+
mod r#ref;
3+
4+
#[macro_export]
5+
macro_rules! compare {
6+
($src:expr, $expected:expr) => {
7+
use jotdown::Render;
8+
let src = $src;
9+
let expected = std::fs::read_to_string($expected).expect("read failed");
10+
let p = jotdown::Parser::new(src);
11+
let mut actual = String::new();
12+
jotdown::html::Renderer::default()
13+
.push(p, &mut actual)
14+
.unwrap();
15+
assert_eq!(actual, expected, "\n{}", {
16+
use std::io::Write;
17+
let mut child = std::process::Command::new("diff")
18+
.arg("--color=always")
19+
.arg("-")
20+
.arg($expected)
21+
.stdin(std::process::Stdio::piped())
22+
.stdout(std::process::Stdio::piped())
23+
.spawn()
24+
.expect("spawn diff failed");
25+
let mut stdin = child.stdin.take().unwrap();
26+
let actual = actual.clone();
27+
std::thread::spawn(move || stdin.write_all(actual.as_bytes()).unwrap());
28+
let stdout = child.wait_with_output().unwrap().stdout;
29+
String::from_utf8(stdout).unwrap()
30+
});
31+
};
32+
}

0 commit comments

Comments
 (0)