Skip to content

Commit 42edc6f

Browse files
committed
Move tests under parsing module to access internals
1 parent 5fb8b1c commit 42edc6f

File tree

8 files changed

+1637
-1652
lines changed

8 files changed

+1637
-1652
lines changed

src/language/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Types representing an Abstract Syntax Tree for the Technique language
22
3-
use crate::{language::quantity::parse_quantity, regex::*};
3+
use crate::regex::*;
44

55
#[derive(Eq, Debug, PartialEq)]
66
pub struct Document<'i> {

src/parsing/checks/errors.rs

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
use super::*;
2+
use std::path::Path;
3+
4+
/// Helper function to check if parsing produces the expected error type
5+
fn expect_error(content: &str, expected: ParsingError) {
6+
let result = parse_with_recovery(Path::new("test.tq"), content);
7+
match result {
8+
Ok(_) => panic!(
9+
"Expected parsing to fail, but it succeeded for input: {}",
10+
content
11+
),
12+
Err(errors) => {
13+
// Check if any error matches the expected type
14+
let found_expected = errors
15+
.iter()
16+
.any(|error| std::mem::discriminant(error) == std::mem::discriminant(&expected));
17+
18+
if !found_expected {
19+
panic!(
20+
"Expected error type like {:?} but got: {:?} for input '{}'",
21+
expected, errors, content
22+
);
23+
}
24+
}
25+
}
26+
}
27+
28+
#[test]
29+
fn invalid_identifier_uppercase_start() {
30+
expect_error(
31+
r#"
32+
Making_Coffee : Ingredients -> Coffee
33+
"#
34+
.trim_ascii(),
35+
ParsingError::InvalidIdentifier(0, "".to_string()),
36+
);
37+
}
38+
39+
#[test]
40+
fn invalid_identifier_mixed_case() {
41+
expect_error(
42+
r#"
43+
makeCoffee : Ingredients -> Coffee
44+
"#
45+
.trim_ascii(),
46+
ParsingError::InvalidIdentifier(0, "".to_string()),
47+
);
48+
}
49+
50+
#[test]
51+
fn invalid_identifier_with_dashes() {
52+
expect_error(
53+
r#"
54+
make-coffee : Ingredients -> Coffee
55+
"#
56+
.trim_ascii(),
57+
ParsingError::InvalidIdentifier(0, "".to_string()),
58+
);
59+
}
60+
61+
#[test]
62+
fn invalid_identifier_with_spaces() {
63+
expect_error(
64+
r#"
65+
make coffee : Ingredients -> Coffee
66+
"#
67+
.trim_ascii(),
68+
ParsingError::InvalidParameters(0),
69+
);
70+
}
71+
72+
#[test]
73+
fn invalid_signature_wrong_arrow() {
74+
expect_error(
75+
r#"
76+
making_coffee : Ingredients => Coffee
77+
"#
78+
.trim_ascii(),
79+
ParsingError::InvalidSignature(0),
80+
);
81+
}
82+
83+
#[test]
84+
fn invalid_genus_lowercase_forma() {
85+
expect_error(
86+
r#"
87+
making_coffee : ingredients -> Coffee
88+
"#
89+
.trim_ascii(),
90+
ParsingError::InvalidGenus(16),
91+
);
92+
}
93+
94+
#[test]
95+
fn invalid_genus_both_lowercase() {
96+
expect_error(
97+
r#"
98+
making_coffee : ingredients -> coffee
99+
"#
100+
.trim_ascii(),
101+
ParsingError::InvalidGenus(16),
102+
);
103+
}
104+
105+
#[test]
106+
fn invalid_signature_missing_arrow() {
107+
expect_error(
108+
r#"
109+
making_coffee : Ingredients Coffee
110+
"#
111+
.trim_ascii(),
112+
ParsingError::InvalidSignature(16),
113+
);
114+
}
115+
116+
#[test]
117+
fn invalid_declaration_missing_colon() {
118+
expect_error(
119+
r#"
120+
making_coffee Ingredients -> Coffee
121+
"#
122+
.trim_ascii(),
123+
ParsingError::Unrecognized(0),
124+
);
125+
}
126+
127+
#[test]
128+
fn invalid_identifier_in_parameters() {
129+
expect_error(
130+
r#"
131+
making_coffee(BadParam) : Ingredients -> Coffee
132+
"#
133+
.trim_ascii(),
134+
ParsingError::InvalidIdentifier(14, "".to_string()),
135+
);
136+
}
137+
138+
#[test]
139+
fn invalid_identifier_empty() {
140+
expect_error(
141+
r#"
142+
: Ingredients -> Coffee
143+
"#
144+
.trim_ascii(),
145+
ParsingError::InvalidDeclaration(0),
146+
);
147+
}
148+
149+
#[test]
150+
fn invalid_step_format() {
151+
expect_error(
152+
r#"
153+
making_coffee :
154+
155+
A. First step (should be lowercase 'a.')
156+
"#
157+
.trim_ascii(),
158+
ParsingError::InvalidStep(21),
159+
);
160+
}
161+
162+
#[test]
163+
fn invalid_response_wrong_quotes() {
164+
expect_error(
165+
r#"
166+
making_coffee :
167+
168+
1. Do you want coffee?
169+
"Yes" | "No"
170+
"#
171+
.trim_ascii(),
172+
ParsingError::InvalidResponse(52),
173+
);
174+
}
175+
176+
#[test]
177+
fn invalid_multiline_missing_closing() {
178+
expect_error(
179+
r#"
180+
making_coffee :
181+
182+
1. Do something with ```
183+
This is missing closing backticks
184+
"#
185+
.trim_ascii(),
186+
ParsingError::InvalidMultiline(41),
187+
);
188+
}
189+
190+
#[test]
191+
fn invalid_code_block_missing_closing_brace() {
192+
expect_error(
193+
r#"
194+
making_coffee :
195+
196+
1. Do something { exec("command"
197+
"#
198+
.trim_ascii(),
199+
ParsingError::ExpectedMatchingChar(38, "a code block", '{', '}'),
200+
);
201+
}
202+
203+
#[test]
204+
fn invalid_step_wrong_ordinal() {
205+
expect_error(
206+
r#"
207+
making_coffee :
208+
209+
i. Wrong case section
210+
"#
211+
.trim_ascii(),
212+
ParsingError::InvalidStep(21),
213+
);
214+
}
215+
216+
#[test]
217+
fn invalid_invocation_malformed() {
218+
expect_error(
219+
r#"
220+
making_coffee :
221+
222+
1. Do <something_without_closing
223+
"#
224+
.trim_ascii(),
225+
ParsingError::ExpectedMatchingChar(27, "an invocation", '<', '>'),
226+
);
227+
}
228+
229+
#[test]
230+
fn invalid_execution_malformed() {
231+
expect_error(
232+
r#"
233+
making_coffee :
234+
235+
1. Do something { exec("command" }
236+
"#
237+
.trim_ascii(),
238+
ParsingError::ExpectedMatchingChar(43, "a function call", '(', ')'),
239+
);
240+
}
241+
242+
#[test]
243+
fn invalid_invocation_in_repeat() {
244+
expect_error(
245+
r#"
246+
making_coffee :
247+
248+
1. { repeat <making_coffee }
249+
"#
250+
.trim_ascii(),
251+
ParsingError::ExpectedMatchingChar(29, "an invocation", '<', '>'),
252+
);
253+
}
254+
255+
#[test]
256+
fn invalid_substep_uppercase() {
257+
expect_error(
258+
r#"
259+
making_coffee :
260+
261+
1. First step
262+
A. This should be lowercase
263+
"#
264+
.trim_ascii(),
265+
ParsingError::InvalidSubstep(37),
266+
);
267+
}

0 commit comments

Comments
 (0)