Skip to content

Commit 4cc5936

Browse files
alecalvopjvsqzj
authored andcommitted
Refactor lint spacing tests
Use robust check for spacing tests
1 parent 5d00903 commit 4cc5936

File tree

16 files changed

+315
-214
lines changed

16 files changed

+315
-214
lines changed

src/lint/mod.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -189,41 +189,6 @@ pub mod tests {
189189
use std::str::FromStr;
190190
use crate::{analysis::{parsing::{parser::FileInfo, structure::{self, TopAst}}, FileSpec}, vfs::TextFile};
191191

192-
pub static SOURCE: &str = "
193-
dml 1.4;
194-
195-
bank sb_cr {
196-
group monitor {
197-
198-
register MKTME_KEYID_MASK {
199-
method get() -> (uint64) {
200-
local uint64 physical_address_mask = mse.srv10nm_mse_mktme.get_key_addr_mask();
201-
this.Mask.set(physical_address_mask);
202-
this.function_with_args('some_string',
203-
integer,
204-
floater);
205-
return this.val;
206-
}
207-
}
208-
209-
register TDX_KEYID_MASK {
210-
method get() -> (uint64) {
211-
local uint64 tdx_keyid_mask = mse.srv10nm_mse_tdx.get_key_addr_mask();
212-
local uint64 some_uint = (is_this_real) ? then_you_might_like_this_value : or_this_one;
213-
this.Mask.set(tdx_keyid_mask);
214-
return this.val;
215-
}
216-
}
217-
}
218-
}
219-
220-
/*
221-
This is ONEEEE VEEEEEERY LLOOOOOOONG COOOMMMEENTT ON A SINGLEEEE LINEEEEEEEEEEEEEE
222-
and ANOTHEEEER VEEEEEERY LLOOOOOOONG COOOMMMEENTT ON A SINGLEEEE LINEEEEEEEEEEEEEE
223-
*/
224-
225-
";
226-
227192
pub fn create_ast_from_snippet(source: &str) -> TopAst {
228193
use logos::Logos;
229194
use crate::analysis::parsing::lexer::TokenKind;
@@ -251,17 +216,4 @@ pub mod tests {
251216
let example_cfg = parse_lint_cfg(example_path.into()).unwrap();
252217
assert_eq!(example_cfg, LintCfg::default());
253218
}
254-
255-
#[test]
256-
#[ignore]
257-
fn test_main() {
258-
use crate::lint::{begin_style_check, LintCfg};
259-
use crate::lint::rules:: instantiate_rules;
260-
let ast = create_ast_from_snippet(SOURCE);
261-
let cfg = LintCfg::default();
262-
let rules = instantiate_rules(&cfg);
263-
let _lint_errors = begin_style_check(ast, SOURCE.to_string(), &rules);
264-
assert!(_lint_errors.is_ok());
265-
assert!(!_lint_errors.unwrap().is_empty());
266-
}
267219
}

src/lint/rules/tests/common.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ pub fn run_linter(source_code: &str, rules: &CurrentRules)
3737
begin_style_check(ast, source_code.to_string(), rules)
3838
}
3939

40-
pub fn assert_snippet(source_code: &str, expected_errors: usize, rules: &CurrentRules) {
41-
let lint_errors = run_linter(source_code, rules);
42-
assert!(lint_errors.is_ok());
43-
assert_eq!(lint_errors.clone().unwrap().len(), expected_errors,
44-
"{:#?}", lint_errors);
45-
}
46-
47-
pub fn robust_assert_snippet(source_code: &str, expected_errors: Vec<ExpectedDMLStyleError>, rules: &CurrentRules) {
40+
pub fn assert_snippet(source_code: &str, expected_errors: Vec<ExpectedDMLStyleError>, rules: &CurrentRules) {
4841
let lint_errors = run_linter(source_code, rules).unwrap();
4942
assert_eq!(lint_errors.len(), expected_errors.len(), "{:#?}", lint_errors);
5043

src/lint/rules/tests/indentation/closing_brace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
1+
use crate::lint::rules::tests::common::{set_up, assert_snippet};
22
use crate::lint::rules::RuleType;
33

44
static BASIC_COMPOUND_CORRECT: &str = "

src/lint/rules/tests/indentation/code_block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
1+
use crate::lint::rules::tests::common::{set_up, assert_snippet};
22
use crate::lint::rules::RuleType;
33

44
static FUNCTION_CONTENTS_INDENT_CORRECT: &str = "

src/lint/rules/tests/indentation/empty_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
1+
use crate::lint::rules::tests::common::{set_up, assert_snippet};
22
use crate::lint::rules::RuleType;
33

44
static EMPTY_LOOP_INCORRECT: &str = "

src/lint/rules/tests/indentation/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod switch_case;
66
mod empty_loop;
77

88
use crate::lint::rules::tests::common::assert_snippet;
9+
use crate::lint::rules::RuleType;
910
use crate::lint::LintCfg;
1011
use crate::lint::LongLineOptions;
1112
use crate::lint::rules::instantiate_rules;
@@ -20,15 +21,23 @@ param some_parameter_name_in_this_device = some_long_name_bank.some_long_name_gr
2021
fn line_length_incorrect() {
2122
let mut cfg = LintCfg::default();
2223
let mut rules = instantiate_rules(&cfg);
23-
assert_snippet(LINE_LENGTH_INCORRECT, 1, &rules);
24+
let expected_errors = define_expected_errors!(
25+
RuleType::LL1,
26+
(1, 1, 80, 103),
27+
);
28+
assert_snippet(LINE_LENGTH_INCORRECT, expected_errors, &rules);
2429
// Test rule disable
2530
cfg.long_lines = None;
2631
rules = instantiate_rules(&cfg);
27-
assert_snippet(LINE_LENGTH_INCORRECT, 0, &rules);
32+
assert_snippet(LINE_LENGTH_INCORRECT, vec![], &rules);
2833
// Test lower max_length
2934
cfg.long_lines = Some(LongLineOptions{
3035
max_length: (LINE_LENGTH_INCORRECT.len()-3).try_into().unwrap()
3136
});
3237
rules = instantiate_rules(&cfg);
33-
assert_snippet(LINE_LENGTH_INCORRECT, 1, &rules);
38+
let expected_errors = define_expected_errors!(
39+
RuleType::LL1,
40+
(1, 1, 102, 103),
41+
);
42+
assert_snippet(LINE_LENGTH_INCORRECT, expected_errors, &rules);
3443
}

src/lint/rules/tests/indentation/no_tabs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
1+
use crate::lint::rules::tests::common::{set_up, assert_snippet};
22
use crate::lint::rules::RuleType;
33

44
static USING_TAB_INDENT_INCORRECT: &str = "

src/lint/rules/tests/indentation/paren_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
1+
use crate::lint::rules::tests::common::{set_up, assert_snippet};
22
use crate::lint::rules::RuleType;
33

44
// A continuation line that is broken inside a parenthesized expression

src/lint/rules/tests/indentation/switch_case.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::lint::rules::tests::common::{set_up, robust_assert_snippet as assert_snippet};
1+
use crate::lint::rules::tests::common::{set_up, assert_snippet};
22
use crate::lint::rules::RuleType;
33

44
static SWITCH_CASE_INDENT_CORRECT: &str = "
Lines changed: 6 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
use crate::lint::rules::tests::common::assert_snippet;
2-
use crate::lint::rules::instantiate_rules;
3-
use crate::lint::LintCfg;
1+
mod nsp_funpar;
2+
mod nsp_inparen;
3+
mod nsp_trailing;
4+
mod nsp_unary;
5+
mod sp_braces;
6+
mod sp_punct;
47

58
// Put whitespace (space or newline):
69
// SP.reserved around reserved words, such as if, else, default,
@@ -15,51 +18,6 @@ if(this_some_integer == 0x666)
1518
}
1619
";
1720

18-
// SP.braces around braces ({ and })
19-
static SP_BRACES: &str = "
20-
method this_is_some_method() {return 0;}
21-
22-
method this_is_empty_method() { }
23-
24-
bank pcie_config {register command {field mem {
25-
method pcie_write(uint64 value) {
26-
if (value != 0) {value = value + 1;}
27-
default(value);
28-
map_memory_alt();
29-
}
30-
}
31-
}
32-
}
33-
";
34-
#[test]
35-
fn style_check_sp_braces() {
36-
let mut cfg = LintCfg::default();
37-
let mut rules = instantiate_rules(&cfg);
38-
assert_snippet(SP_BRACES, 6, &rules);
39-
// Test rule disable
40-
cfg.sp_brace = None;
41-
rules = instantiate_rules(&cfg);
42-
assert_snippet(SP_BRACES, 0, &rules);
43-
44-
}
45-
46-
static SP_BRACES_02: &str = "
47-
typedef struct {uint16 idx;} hqm_cq_list_release_ctx_t;
48-
49-
typedef layout \"little-endian\" {bitfields 1 {uint1 cq @ [0:0];} byte;} q_t;
50-
";
51-
#[test]
52-
fn style_check_sp_braces_02() {
53-
let mut cfg = LintCfg::default();
54-
let mut rules = instantiate_rules(&cfg);
55-
assert_snippet(SP_BRACES_02, 6, &rules);
56-
// Test rule disable
57-
cfg.sp_brace = None;
58-
rules = instantiate_rules(&cfg);
59-
assert_snippet(SP_BRACES_02, 0, &rules);
60-
61-
}
62-
6321
// SP.binop around binary operators except the dereferencing operators dot
6422
// (a.b) and arrow (a->b)
6523
#[allow(dead_code)]
@@ -79,27 +37,6 @@ local int this_some_integer = (flag?5:7));
7937
}
8038
";
8139

82-
// SP.punct after but not before colon, semicolon and comma
83-
#[allow(dead_code)]
84-
static SP_PUNCT: &str = "
85-
method this_is_some_method(bool flag ,int8 var) {
86-
local int this_some_integer = 0x666 ;
87-
if(this_some_integer == 0x666)
88-
return;
89-
some_func(arg1 ,arg2 ,arg3 ,arg4);
90-
}
91-
";
92-
#[test]
93-
fn style_check_sp_punct_rule() {
94-
let mut cfg = LintCfg::default();
95-
let mut rules = instantiate_rules(&cfg);
96-
assert_snippet(SP_PUNCT, 9, &rules);
97-
// Test rule disable
98-
cfg.sp_punct = None;
99-
rules = instantiate_rules(&cfg);
100-
assert_snippet(SP_PUNCT, 0, &rules);
101-
}
102-
10340
// SP.ptrdecl between a type and the * marking a pointer
10441
#[allow(dead_code)]
10542
static SP_PTRDECL: &str = "
@@ -120,65 +57,6 @@ if(!dummy_obj)//Not null
12057
}
12158
";
12259

123-
// There should be no space:
124-
// NSP.funpar between a function/method name and its opening parenthesis
125-
static NSP_FUNPAR: &str = "
126-
method this_is_some_method (conf_object_t *dummy_obj) {
127-
if(!dummy_obj)
128-
other_method_called ();
129-
}
130-
";
131-
#[test]
132-
fn style_check_nsp_funpar() {
133-
let mut cfg = LintCfg::default();
134-
let mut rules = instantiate_rules(&cfg);
135-
assert_snippet(NSP_FUNPAR, 2, &rules);
136-
// Test rule disable
137-
cfg.nsp_funpar = None;
138-
rules = instantiate_rules(&cfg);
139-
assert_snippet(NSP_FUNPAR, 0, &rules);
140-
}
141-
142-
// NSP.inparen immediately inside parentheses or brackets
143-
static NSP_INPAREN: &str = "
144-
method this_is_some_method( conf_object_t *dummy_obj ) {
145-
if( !dummy_obj[ 0 ] )
146-
return;
147-
}
148-
";
149-
#[test]
150-
fn style_check_nsp_inparen() {
151-
let mut cfg = LintCfg::default();
152-
let mut rules = instantiate_rules(&cfg);
153-
assert_snippet(NSP_INPAREN, 6, &rules);
154-
// Test rule disable
155-
cfg.nsp_inparen = None;
156-
rules = instantiate_rules(&cfg);
157-
assert_snippet(NSP_INPAREN, 0, &rules);
158-
}
159-
160-
// NSP.unary between a unary operator and its operand
161-
static NSP_UNARY: &str = "
162-
method this_is_some_method(conf_object_t *dummy_obj) {
163-
if(! dummy_obj)
164-
return;
165-
local uint64 p = & dummy_obj;
166-
p ++;
167-
-- p;
168-
local int64 neg = - 1;
169-
}
170-
";
171-
#[test]
172-
fn style_check_nsp_unary() {
173-
let mut cfg = LintCfg::default();
174-
let mut rules = instantiate_rules(&cfg);
175-
assert_snippet(NSP_UNARY, 5, &rules);
176-
// Test rule disable
177-
cfg.nsp_unary = None;
178-
rules = instantiate_rules(&cfg);
179-
assert_snippet(NSP_UNARY, 0, &rules);
180-
}
181-
18260
// NSP.ptrdecl after the * marking a pointer in a declaration
18361
#[allow(dead_code)]
18462
static NSP_PTRDECL: &str = "
@@ -187,24 +65,3 @@ if(!dummy_obj)
18765
return;
18866
}
18967
";
190-
191-
// Adding trailing whitespace removal to spacing rules:
192-
// no whitespaces should be left at the end of a line between the last token
193-
// and the newline \n
194-
static NSP_TRAILING: &str = "
195-
method this_is_some_method(int64 num) {
196-
local int this_some_integer = 0x666;
197-
if (this_some_integer == 0x666)
198-
return;
199-
}
200-
";
201-
#[test]
202-
fn style_check_nsp_trailing() {
203-
let mut cfg = LintCfg::default();
204-
let mut rules = instantiate_rules(&cfg);
205-
assert_snippet(NSP_TRAILING, 4, &rules);
206-
// Test rule disable
207-
cfg.nsp_trailing = None;
208-
rules = instantiate_rules(&cfg);
209-
assert_snippet(NSP_TRAILING, 0, &rules);
210-
}

0 commit comments

Comments
 (0)