Skip to content

Commit dc157e4

Browse files
test: add UnexpectedToken aac test, #6627
1 parent 1ea97d3 commit dc157e4

File tree

2 files changed

+309
-15
lines changed

2 files changed

+309
-15
lines changed

stackslib/src/chainstate/tests/parse_tests.rs

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// along with this program. If not, see <http://www.gnu.org/licenses/>.
1515

1616
/// This module contains consensus tests related to Clarity Parse errors.
17-
use clarity::vm::ast::parser::v2::{MAX_CONTRACT_NAME_LEN, MAX_NESTING_DEPTH};
17+
use clarity::vm::ast::parser::v2::MAX_NESTING_DEPTH;
1818
use clarity::vm::ast::stack_depth_checker::AST_CALL_STACK_DEPTH_BUFFER;
1919
use clarity::vm::MAX_CALL_STACK_DEPTH;
2020
#[allow(unused_imports)] // Just used for documentation purpose
@@ -211,20 +211,6 @@ fn test_lexer_unknown_symbol() {
211211
);
212212
}
213213

214-
/// ParserError: [`ParseErrors::ContractNameTooLong`]
215-
/// Caused by: contract name longer than [`MAX_CONTRACT_NAME_LEN`]
216-
/// Outcome: Panic
217-
/// Due to [`ContractName::consensus_serialize`] when creating the transaction,
218-
/// this error variant is unreachable.
219-
#[test]
220-
#[should_panic]
221-
fn test_contract_name_too_long() {
222-
contract_deploy_consensus_test!(
223-
contract_name: &{"n".repeat(MAX_CONTRACT_NAME_LEN + 1)},
224-
contract_code: "()",
225-
);
226-
}
227-
228214
/// ParserError: [`ParseErrors::ExpectedClosing`]
229215
/// Caused by: missing closing parenthesis
230216
/// Outcome: block accepted
@@ -258,3 +244,95 @@ fn test_expected_white_space() {
258244
contract_code: "(define-public (get-one)(ok u1))",
259245
);
260246
}
247+
248+
/// ParserError: [`ParseErrors::UnexpectedToken`]
249+
/// Caused by: unexpected token in the expression (rightest paranthesis)
250+
/// Outcome: block accepted
251+
#[test]
252+
fn test_unexpected_token() {
253+
contract_deploy_consensus_test!(
254+
contract_name: "my-contract",
255+
contract_code: "(define-public (get-one) (ok u1)) )",
256+
);
257+
}
258+
259+
fn variant_coverage_report(variant: ParseErrors) {
260+
enum VariantCoverage {
261+
Unreachable_Functionally,
262+
Unreachable_ExpectLike,
263+
Unreachable_NotUsed,
264+
Skipped,
265+
Tested,
266+
267+
TODO,
268+
}
269+
270+
use ParseErrors::*;
271+
use VariantCoverage::*;
272+
273+
_ = match variant {
274+
// Costs
275+
CostOverflow => Unreachable_ExpectLike,
276+
CostBalanceExceeded(_, _) => Unreachable_Functionally,
277+
MemoryBalanceExceeded(_, _) => Unreachable_NotUsed,
278+
CostComputationFailed(_) => Unreachable_ExpectLike,
279+
ExecutionTimeExpired => Unreachable_NotUsed, // To re-check
280+
281+
TooManyExpressions => Unreachable_ExpectLike,
282+
ExpressionStackDepthTooDeep => Tested,
283+
VaryExpressionStackDepthTooDeep => Tested,
284+
FailedParsingIntValue(_) => Tested,
285+
CircularReference(_) => Tested,
286+
NameAlreadyUsed(_) => Tested,
287+
TraitReferenceNotAllowed => Tested,
288+
ImportTraitBadSignature => Tested,
289+
DefineTraitBadSignature => Tested,
290+
ImplTraitBadSignature => Tested,
291+
TraitReferenceUnknown(_) => Tested,
292+
293+
Lexer(LexerError) => Tested,
294+
ContractNameTooLong(String) => Unreachable_Functionally,
295+
ExpectedClosing(Token) => Tested,
296+
ExpectedContractIdentifier => TODO,
297+
ExpectedTraitIdentifier => TODO,
298+
ExpectedWhitespace => Tested,
299+
FailedParsingUIntValue(_) => Tested,
300+
IllegalTraitName(_) => TODO,
301+
InvalidPrincipalLiteral => TODO,
302+
InvalidBuffer => TODO,
303+
NameTooLong(_) => TODO,
304+
UnexpectedToken(_) => Tested,
305+
TupleColonExpectedv2 => TODO,
306+
TupleCommaExpectedv2 => TODO,
307+
TupleValueExpected => TODO,
308+
IllegalClarityName(_) => TODO,
309+
IllegalASCIIString(_) => TODO,
310+
IllegalContractName(_) => TODO,
311+
NoteToMatchThis(_) => Tested,
312+
UnexpectedParserFailure => Unreachable_ExpectLike,
313+
InterpreterFailure => Unreachable_ExpectLike, // currently cause block rejection
314+
315+
// V1
316+
FailedCapturingInput
317+
| SeparatorExpected(_)
318+
| SeparatorExpectedAfterColon(_)
319+
| ProgramTooLarge
320+
| IllegalVariableName(_)
321+
| FailedParsingBuffer(_)
322+
| FailedParsingHexValue(_, _)
323+
| FailedParsingPrincipal(_)
324+
| FailedParsingField(_)
325+
| FailedParsingRemainder(_)
326+
| ClosingParenthesisUnexpected
327+
| ClosingParenthesisExpected
328+
| ClosingTupleLiteralUnexpected
329+
| ClosingTupleLiteralExpected
330+
| TupleColonExpected(_)
331+
| TupleCommaExpected(_)
332+
| TupleItemExpected(_)
333+
| CommaSeparatorUnexpected
334+
| ColonSeparatorUnexpected
335+
| InvalidCharactersDetected
336+
| InvalidEscaping => Skipped, //parser v1 should be removed?!
337+
}
338+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
---
2+
source: stackslib/src/chainstate/tests/parse_tests.rs
3+
expression: result
4+
---
5+
[
6+
Success(ExpectedBlockOutput(
7+
marf_hash: "43bd53a10611e249b013aee865a9d9d482d7e58b8c6f713fbeee24c9805507fd",
8+
evaluated_epoch: Epoch32,
9+
transactions: [
10+
ExpectedTransactionOutput(
11+
tx: "SmartContract(name: my-contract-Epoch3_2-Clarity1, code_body: [..], clarity_version: Some(Clarity1))",
12+
vm_error: "Some(unexpected \')\') [NON-CONSENSUS BREAKING]",
13+
return_type: Response(ResponseData(
14+
committed: false,
15+
data: Optional(OptionalData(
16+
data: None,
17+
)),
18+
)),
19+
cost: ExecutionCost(
20+
write_length: 0,
21+
write_count: 0,
22+
read_length: 0,
23+
read_count: 0,
24+
runtime: 1026,
25+
),
26+
),
27+
],
28+
total_block_cost: ExecutionCost(
29+
write_length: 0,
30+
write_count: 0,
31+
read_length: 0,
32+
read_count: 0,
33+
runtime: 1026,
34+
),
35+
)),
36+
Success(ExpectedBlockOutput(
37+
marf_hash: "5bcafed522353ec7f7884380ead701ee55346e396518ba2052481db08641ee28",
38+
evaluated_epoch: Epoch32,
39+
transactions: [
40+
ExpectedTransactionOutput(
41+
tx: "SmartContract(name: my-contract-Epoch3_2-Clarity2, code_body: [..], clarity_version: Some(Clarity2))",
42+
vm_error: "Some(unexpected \')\') [NON-CONSENSUS BREAKING]",
43+
return_type: Response(ResponseData(
44+
committed: false,
45+
data: Optional(OptionalData(
46+
data: None,
47+
)),
48+
)),
49+
cost: ExecutionCost(
50+
write_length: 0,
51+
write_count: 0,
52+
read_length: 0,
53+
read_count: 0,
54+
runtime: 1026,
55+
),
56+
),
57+
],
58+
total_block_cost: ExecutionCost(
59+
write_length: 0,
60+
write_count: 0,
61+
read_length: 0,
62+
read_count: 0,
63+
runtime: 1026,
64+
),
65+
)),
66+
Success(ExpectedBlockOutput(
67+
marf_hash: "533348112048f63dcb3f1b7974ec41a6530e48e2c3e5f8695844d0c52b677c6f",
68+
evaluated_epoch: Epoch32,
69+
transactions: [
70+
ExpectedTransactionOutput(
71+
tx: "SmartContract(name: my-contract-Epoch3_2-Clarity3, code_body: [..], clarity_version: Some(Clarity3))",
72+
vm_error: "Some(unexpected \')\') [NON-CONSENSUS BREAKING]",
73+
return_type: Response(ResponseData(
74+
committed: false,
75+
data: Optional(OptionalData(
76+
data: None,
77+
)),
78+
)),
79+
cost: ExecutionCost(
80+
write_length: 0,
81+
write_count: 0,
82+
read_length: 0,
83+
read_count: 0,
84+
runtime: 1026,
85+
),
86+
),
87+
],
88+
total_block_cost: ExecutionCost(
89+
write_length: 0,
90+
write_count: 0,
91+
read_length: 0,
92+
read_count: 0,
93+
runtime: 1026,
94+
),
95+
)),
96+
Success(ExpectedBlockOutput(
97+
marf_hash: "79b61578b821fef1af5352522afb62f9f5f0c19013fa308570b5bbcb6d813acd",
98+
evaluated_epoch: Epoch33,
99+
transactions: [
100+
ExpectedTransactionOutput(
101+
tx: "SmartContract(name: my-contract-Epoch3_3-Clarity1, code_body: [..], clarity_version: Some(Clarity1))",
102+
vm_error: "Some(unexpected \')\') [NON-CONSENSUS BREAKING]",
103+
return_type: Response(ResponseData(
104+
committed: false,
105+
data: Optional(OptionalData(
106+
data: None,
107+
)),
108+
)),
109+
cost: ExecutionCost(
110+
write_length: 0,
111+
write_count: 0,
112+
read_length: 0,
113+
read_count: 0,
114+
runtime: 1026,
115+
),
116+
),
117+
],
118+
total_block_cost: ExecutionCost(
119+
write_length: 0,
120+
write_count: 0,
121+
read_length: 0,
122+
read_count: 0,
123+
runtime: 1026,
124+
),
125+
)),
126+
Success(ExpectedBlockOutput(
127+
marf_hash: "bf79944f45d80e3dbf193a6429811e3d9d6b1a738086fb7faaabdfad77fcd17b",
128+
evaluated_epoch: Epoch33,
129+
transactions: [
130+
ExpectedTransactionOutput(
131+
tx: "SmartContract(name: my-contract-Epoch3_3-Clarity2, code_body: [..], clarity_version: Some(Clarity2))",
132+
vm_error: "Some(unexpected \')\') [NON-CONSENSUS BREAKING]",
133+
return_type: Response(ResponseData(
134+
committed: false,
135+
data: Optional(OptionalData(
136+
data: None,
137+
)),
138+
)),
139+
cost: ExecutionCost(
140+
write_length: 0,
141+
write_count: 0,
142+
read_length: 0,
143+
read_count: 0,
144+
runtime: 1026,
145+
),
146+
),
147+
],
148+
total_block_cost: ExecutionCost(
149+
write_length: 0,
150+
write_count: 0,
151+
read_length: 0,
152+
read_count: 0,
153+
runtime: 1026,
154+
),
155+
)),
156+
Success(ExpectedBlockOutput(
157+
marf_hash: "0dee2823d39e8349caeb535c163515101ba74666b2e2f1e75d02bfcd7c18760d",
158+
evaluated_epoch: Epoch33,
159+
transactions: [
160+
ExpectedTransactionOutput(
161+
tx: "SmartContract(name: my-contract-Epoch3_3-Clarity3, code_body: [..], clarity_version: Some(Clarity3))",
162+
vm_error: "Some(unexpected \')\') [NON-CONSENSUS BREAKING]",
163+
return_type: Response(ResponseData(
164+
committed: false,
165+
data: Optional(OptionalData(
166+
data: None,
167+
)),
168+
)),
169+
cost: ExecutionCost(
170+
write_length: 0,
171+
write_count: 0,
172+
read_length: 0,
173+
read_count: 0,
174+
runtime: 1026,
175+
),
176+
),
177+
],
178+
total_block_cost: ExecutionCost(
179+
write_length: 0,
180+
write_count: 0,
181+
read_length: 0,
182+
read_count: 0,
183+
runtime: 1026,
184+
),
185+
)),
186+
Success(ExpectedBlockOutput(
187+
marf_hash: "b1c6f63facb9b84d1fcb59872ec91a9ca1322f98cb9575fdbb73e973493d62eb",
188+
evaluated_epoch: Epoch33,
189+
transactions: [
190+
ExpectedTransactionOutput(
191+
tx: "SmartContract(name: my-contract-Epoch3_3-Clarity4, code_body: [..], clarity_version: Some(Clarity4))",
192+
vm_error: "Some(unexpected \')\') [NON-CONSENSUS BREAKING]",
193+
return_type: Response(ResponseData(
194+
committed: false,
195+
data: Optional(OptionalData(
196+
data: None,
197+
)),
198+
)),
199+
cost: ExecutionCost(
200+
write_length: 0,
201+
write_count: 0,
202+
read_length: 0,
203+
read_count: 0,
204+
runtime: 1026,
205+
),
206+
),
207+
],
208+
total_block_cost: ExecutionCost(
209+
write_length: 0,
210+
write_count: 0,
211+
read_length: 0,
212+
read_count: 0,
213+
runtime: 1026,
214+
),
215+
)),
216+
]

0 commit comments

Comments
 (0)