|
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; |
56 | 50 | } |
| 51 | + println!("@@ Line {} @@", i + 1); |
| 52 | + println!("- {}", orig_line); |
| 53 | + println!("+ {}", fmt_line); |
57 | 54 | } |
58 | 55 | } |
| 56 | +} |
59 | 57 |
|
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/"); |
64 | 62 |
|
65 | | - // Ensure the directory exists |
66 | | - assert!(dir.exists(), "examples directory missing"); |
| 63 | + // Ensure the directory exists |
| 64 | + assert!(dir.exists(), "examples directory missing"); |
67 | 65 |
|
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"); |
70 | 68 |
|
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(); |
75 | 73 |
|
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); |
83 | 80 | } |
| 81 | + } |
84 | 82 |
|
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 | + ); |
87 | 88 |
|
88 | | - let mut failures = Vec::new(); |
| 89 | + let mut failures = Vec::new(); |
89 | 90 |
|
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)); |
95 | 96 |
|
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)); |
99 | 100 |
|
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); |
103 | 104 |
|
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()); |
109 | 109 | } |
| 110 | + } |
110 | 111 |
|
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); |
121 | 119 | } |
| 120 | + |
| 121 | + panic!("All examples must format unchanged"); |
122 | 122 | } |
123 | 123 | } |
0 commit comments