Skip to content

Commit 1188e34

Browse files
committed
Reorganizae test modules
1 parent 42edc6f commit 1188e34

File tree

3 files changed

+138
-187
lines changed

3 files changed

+138
-187
lines changed

tests/formatting/golden.rs

Lines changed: 103 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,123 @@
1-
#[cfg(test)]
2-
mod examples {
3-
use std::fs;
4-
use std::path::Path;
5-
6-
use technique::formatting::*;
7-
use technique::parsing;
8-
9-
/// Golden test for the format command
10-
///
11-
/// This test:
12-
/// 1. Reads all .t files from examples/golden/
13-
/// 2. Runs the equivalent of the `format` command on each file
14-
/// 3. Compares the formatted output with the original input
15-
/// 4. Shows clear diffs when differences are found
16-
///
17-
/// The test expects files to be in their canonical formatted form. If
18-
/// files fail this test, either the parser & formatter is wrong (a bug
19-
/// that needs to be fixed!) or possibly the example file is wrong
20-
/// (perhaps because of a deliberate style change, and they thus might
21-
/// need reformatting)
22-
23-
/// Simple diff function to show line-by-line differences
24-
fn show_diff(original: &str, formatted: &str, file_path: &Path) {
25-
let original_lines: Vec<&str> = original
26-
.lines()
27-
.collect();
28-
let formatted_lines: Vec<&str> = formatted
29-
.lines()
30-
.collect();
31-
32-
let max_lines = original_lines
33-
.len()
34-
.max(formatted_lines.len());
35-
let mut differences_found = false;
36-
37-
println!("\nDifferences found in file: {:?}", file_path);
38-
println!("--- Original");
39-
println!("+++ Formatted");
40-
41-
for i in 0..max_lines {
42-
let orig_line = original_lines
43-
.get(i)
44-
.unwrap_or(&"");
45-
let fmt_line = formatted_lines
46-
.get(i)
47-
.unwrap_or(&"");
48-
49-
if orig_line != fmt_line {
50-
if !differences_found {
51-
differences_found = true;
52-
}
53-
println!("@@ Line {} @@", i + 1);
54-
println!("- {}", orig_line);
55-
println!("+ {}", fmt_line);
1+
use std::fs;
2+
use std::path::Path;
3+
4+
use technique::formatting::*;
5+
use technique::parsing;
6+
7+
/// Golden test for the format command
8+
///
9+
/// This test:
10+
/// 1. Reads all .t files from examples/golden/
11+
/// 2. Runs the equivalent of the `format` command on each file
12+
/// 3. Compares the formatted output with the original input
13+
/// 4. Shows clear diffs when differences are found
14+
///
15+
/// The test expects files to be in their canonical formatted form. If
16+
/// files fail this test, either the parser & formatter is wrong (a bug
17+
/// that needs to be fixed!) or possibly the example file is wrong
18+
/// (perhaps because of a deliberate style change, and they thus might
19+
/// need reformatting)
20+
21+
/// Simple diff function to show line-by-line differences
22+
fn show_diff(original: &str, formatted: &str, file_path: &Path) {
23+
let original_lines: Vec<&str> = original
24+
.lines()
25+
.collect();
26+
let formatted_lines: Vec<&str> = formatted
27+
.lines()
28+
.collect();
29+
30+
let max_lines = original_lines
31+
.len()
32+
.max(formatted_lines.len());
33+
let mut differences_found = false;
34+
35+
println!("\nDifferences found in file: {:?}", file_path);
36+
println!("--- Original");
37+
println!("+++ Formatted");
38+
39+
for i in 0..max_lines {
40+
let orig_line = original_lines
41+
.get(i)
42+
.unwrap_or(&"");
43+
let fmt_line = formatted_lines
44+
.get(i)
45+
.unwrap_or(&"");
46+
47+
if orig_line != fmt_line {
48+
if !differences_found {
49+
differences_found = true;
5650
}
51+
println!("@@ Line {} @@", i + 1);
52+
println!("- {}", orig_line);
53+
println!("+ {}", fmt_line);
5754
}
5855
}
56+
}
5957

60-
#[test]
61-
fn ensure_identical_output() {
62-
// Read all .tq files from examples/prototype/
63-
let dir = Path::new("tests/golden/");
58+
#[test]
59+
fn ensure_identical_output() {
60+
// Read all .tq files from examples/prototype/
61+
let dir = Path::new("tests/golden/");
6462

65-
// Ensure the directory exists
66-
assert!(dir.exists(), "examples directory missing");
63+
// Ensure the directory exists
64+
assert!(dir.exists(), "examples directory missing");
6765

68-
// Get all .tq files in the directory
69-
let entries = fs::read_dir(dir).expect("Failed to read examples directory");
66+
// Get all .tq files in the directory
67+
let entries = fs::read_dir(dir).expect("Failed to read examples directory");
7068

71-
let mut files = Vec::new();
72-
for entry in entries {
73-
let entry = entry.expect("Failed to read directory entry");
74-
let path = entry.path();
69+
let mut files = Vec::new();
70+
for entry in entries {
71+
let entry = entry.expect("Failed to read directory entry");
72+
let path = entry.path();
7573

76-
if path
77-
.extension()
78-
.and_then(|s| s.to_str())
79-
== Some("tq")
80-
{
81-
files.push(path);
82-
}
74+
if path
75+
.extension()
76+
.and_then(|s| s.to_str())
77+
== Some("tq")
78+
{
79+
files.push(path);
8380
}
81+
}
8482

85-
// Ensure we found some test files
86-
assert!(!files.is_empty(), "No .tq files found in examples directory");
83+
// Ensure we found some test files
84+
assert!(
85+
!files.is_empty(),
86+
"No .tq files found in examples directory"
87+
);
8788

88-
let mut failures = Vec::new();
89+
let mut failures = Vec::new();
8990

90-
// Test each file
91-
for file in &files {
92-
// Load the original content
93-
let original = parsing::load(&file)
94-
.unwrap_or_else(|e| panic!("Failed to load file {:?}: {:?}", file, e));
91+
// Test each file
92+
for file in &files {
93+
// Load the original content
94+
let original = parsing::load(&file)
95+
.unwrap_or_else(|e| panic!("Failed to load file {:?}: {:?}", file, e));
9596

96-
// Parse the content into a Document
97-
let document = parsing::parse(&file, &original)
98-
.unwrap_or_else(|e| panic!("Failed to parse file {:?}: {:?}", file, e));
97+
// Parse the content into a Document
98+
let document = parsing::parse(&file, &original)
99+
.unwrap_or_else(|e| panic!("Failed to parse file {:?}: {:?}", file, e));
99100

100-
// Format the document using the Identity renderer (no markup)
101-
// Using width 78 to match the default
102-
let result = render(&Identity, &document, 78);
101+
// Format the document using the Identity renderer (no markup)
102+
// Using width 78 to match the default
103+
let result = render(&Identity, &document, 78);
103104

104-
// Compare the formatted output with the original input
105-
// They should be identical for well-formed files
106-
if result != original {
107-
failures.push(file.clone());
108-
}
105+
// Compare the formatted output with the original input
106+
// They should be identical for well-formed files
107+
if result != original {
108+
failures.push(file.clone());
109109
}
110+
}
110111

111-
// If any files had differences, show detailed diffs and fail
112-
if !failures.is_empty() {
113-
for file_path in &failures {
114-
let content = parsing::load(&file_path).unwrap();
115-
let document = parsing::parse(&file_path, &content).unwrap();
116-
let output = render(&Identity, &document, 78);
117-
show_diff(&content, &output, &file_path);
118-
}
119-
120-
panic!("All examples must format unchanged");
112+
// If any files had differences, show detailed diffs and fail
113+
if !failures.is_empty() {
114+
for file_path in &failures {
115+
let content = parsing::load(&file_path).unwrap();
116+
let document = parsing::parse(&file_path, &content).unwrap();
117+
let output = render(&Identity, &document, 78);
118+
show_diff(&content, &output, &file_path);
121119
}
120+
121+
panic!("All examples must format unchanged");
122122
}
123123
}

tests/parsing/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
mod samples;
2+
mod broken;

tests/parsing/samples.rs

Lines changed: 34 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,51 @@
1-
#[cfg(test)]
2-
mod samples {
3-
use std::fs;
4-
use std::path::Path;
1+
use std::fs;
2+
use std::path::Path;
53

6-
use technique::parsing;
4+
use technique::parsing;
75

8-
#[test]
9-
fn ensure_samples_parse() {
10-
let dir = Path::new("tests/samples/");
6+
#[test]
7+
fn ensure_parse() {
8+
let dir = Path::new("tests/samples/");
119

12-
assert!(dir.exists(), "samples directory missing");
10+
assert!(dir.exists(), "samples directory missing");
1311

14-
let entries = fs::read_dir(dir).expect("Failed to read samples directory");
12+
let entries = fs::read_dir(dir).expect("Failed to read samples directory");
1513

16-
let mut files = Vec::new();
17-
for entry in entries {
18-
let entry = entry.expect("Failed to read directory entry");
19-
let path = entry.path();
14+
let mut files = Vec::new();
15+
for entry in entries {
16+
let entry = entry.expect("Failed to read directory entry");
17+
let path = entry.path();
2018

21-
if path
22-
.extension()
23-
.and_then(|s| s.to_str())
24-
== Some("tq")
25-
{
26-
files.push(path);
27-
}
28-
}
29-
30-
assert!(!files.is_empty(), "No .tq files found in samples directory");
31-
32-
let mut failures = Vec::new();
33-
34-
for file in &files {
35-
let content = parsing::load(&file)
36-
.unwrap_or_else(|e| panic!("Failed to load file {:?}: {:?}", file, e));
37-
38-
match parsing::parse(&file, &content) {
39-
Ok(_) => {}
40-
Err(e) => {
41-
println!("File {:?} failed to parse: {:?}", file, e);
42-
failures.push(file.clone());
43-
}
44-
}
45-
}
46-
47-
if !failures.is_empty() {
48-
panic!(
49-
"Sample files should parse successfully, but {} files failed",
50-
failures.len()
51-
);
19+
if path
20+
.extension()
21+
.and_then(|s| s.to_str())
22+
== Some("tq")
23+
{
24+
files.push(path);
5225
}
5326
}
5427

55-
#[test]
56-
fn ensure_broken_fail() {
57-
let dir = Path::new("tests/broken/");
28+
assert!(!files.is_empty(), "No .tq files found in samples directory");
5829

59-
assert!(dir.exists(), "broken directory missing");
30+
let mut failures = Vec::new();
6031

61-
let entries = fs::read_dir(dir).expect("Failed to read broken directory");
32+
for file in &files {
33+
let content = parsing::load(&file)
34+
.unwrap_or_else(|e| panic!("Failed to load file {:?}: {:?}", file, e));
6235

63-
let mut files = Vec::new();
64-
for entry in entries {
65-
let entry = entry.expect("Failed to read directory entry");
66-
let path = entry.path();
67-
68-
if path
69-
.extension()
70-
.and_then(|s| s.to_str())
71-
== Some("tq")
72-
{
73-
files.push(path);
74-
}
75-
}
76-
77-
assert!(!files.is_empty(), "No .tq files found in broken directory");
78-
79-
let mut unexpected_successes = Vec::new();
80-
81-
for file in &files {
82-
let content = parsing::load(&file)
83-
.unwrap_or_else(|e| panic!("Failed to load file {:?}: {:?}", file, e));
84-
85-
match parsing::parse(&file, &content) {
86-
Ok(_) => {
87-
println!("File {:?} unexpectedly parsed successfully", file);
88-
unexpected_successes.push(file.clone());
89-
}
90-
Err(_) => {}
36+
match parsing::parse(&file, &content) {
37+
Ok(_) => {}
38+
Err(e) => {
39+
println!("File {:?} failed to parse: {:?}", file, e);
40+
failures.push(file.clone());
9141
}
9242
}
43+
}
9344

94-
if !unexpected_successes.is_empty() {
95-
panic!(
96-
"Broken files should not to parse successfully, but {} files passed",
97-
unexpected_successes.len()
98-
);
99-
}
45+
if !failures.is_empty() {
46+
panic!(
47+
"Sample files should parse successfully, but {} files failed",
48+
failures.len()
49+
);
10050
}
10151
}

0 commit comments

Comments
 (0)