Skip to content

Commit a222dee

Browse files
authored
Refactoring test folder structure
* Changing file structure for IN5 * Commonizing test functions * Refactoring rules tests * Removing in5 temporary
1 parent bcaed1e commit a222dee

File tree

10 files changed

+402
-412
lines changed

10 files changed

+402
-412
lines changed

src/lint/rules/indentation.rs

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -324,35 +324,3 @@ impl Rule for IN9Rule {
324324
so that they are on the same level as the switch statement"
325325
}
326326
}
327-
328-
#[cfg(test)]
329-
pub mod tests {
330-
331-
use crate::lint::rules::tests::assert_snippet;
332-
use crate::lint::rules::instantiate_rules;
333-
use crate::lint::LintCfg;
334-
use crate::lint::LongLineOptions;
335-
use std::convert::TryInto;
336-
337-
// Line length can be configured to a maximum
338-
//(defaults to 80, feature disabled)
339-
pub static LONG_LINE: &str = "
340-
param some_parameter_name_in_this_device = some_long_name_bank.some_long_name_group.SOME_REGISTER_NAME;
341-
";
342-
#[test]
343-
fn style_check_long_line() {
344-
let mut cfg = LintCfg::default();
345-
let mut rules = instantiate_rules(&cfg);
346-
assert_snippet(LONG_LINE, 1, &rules);
347-
// Test rule disable
348-
cfg.long_lines = None;
349-
rules = instantiate_rules(&cfg);
350-
assert_snippet(LONG_LINE, 0, &rules);
351-
// Test lower max_length
352-
cfg.long_lines = Some(LongLineOptions{
353-
max_length: (LONG_LINE.len()-3).try_into().unwrap()
354-
});
355-
rules = instantiate_rules(&cfg);
356-
assert_snippet(LONG_LINE, 1, &rules);
357-
}
358-
}

src/lint/rules/mod.rs

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pub mod spacing;
22
pub mod indentation;
3-
pub mod test;
3+
4+
#[cfg(test)]
5+
pub mod tests;
46

57
use spacing::{SpBracesRule,
68
SpPunctRule, NspFunparRule, NspInparenRule,
@@ -41,29 +43,3 @@ pub trait Rule {
4143
fn name() -> &'static str;
4244
fn description() -> &'static str;
4345
}
44-
45-
pub mod tests {
46-
47-
use crate::lint::tests::create_ast_from_snippet;
48-
use crate::lint::begin_style_check;
49-
use crate::lint::rules::CurrentRules;
50-
use crate::analysis::LocalDMLError;
51-
use crate::vfs::Error;
52-
53-
pub fn run_linter(source_code: &str, rules: &CurrentRules)
54-
-> Result<Vec<LocalDMLError>, Error>
55-
{
56-
print!("\nSnippet to test on:\n{}\n", source_code);
57-
let ast = create_ast_from_snippet(source_code);
58-
print!("Resulting AST:\n{:#?}\n", ast);
59-
begin_style_check(ast, source_code.to_string(), rules)
60-
}
61-
62-
pub fn assert_snippet(source_code: &str, expected_errors: usize, rules: &CurrentRules) {
63-
let lint_errors = run_linter(source_code, rules);
64-
assert!(lint_errors.is_ok());
65-
assert_eq!(lint_errors.clone().unwrap().len(), expected_errors,
66-
"{:#?}", lint_errors);
67-
}
68-
69-
}

src/lint/rules/spacing.rs

Lines changed: 0 additions & 212 deletions
Original file line numberDiff line numberDiff line change
@@ -519,215 +519,3 @@ impl Rule for NspTrailingRule {
519519
"Found trailing whitespace on row"
520520
}
521521
}
522-
523-
#[cfg(test)]
524-
pub mod tests {
525-
526-
use crate::lint::rules::tests::assert_snippet;
527-
use crate::lint::rules::instantiate_rules;
528-
use crate::lint::LintCfg;
529-
530-
// Put whitespace (space or newline):
531-
// SP.reserved around reserved words, such as if, else, default,
532-
// size, const and in, except when a reserved word is used as an identifier
533-
// (e.g., local uint8 *data;)
534-
pub static SP_RESERVED: &str = "
535-
method this_is_some_method() {
536-
local int this_some_integer = 0x666;
537-
if(this_some_integer == 0x666)
538-
return;
539-
}
540-
";
541-
542-
// SP.braces around braces ({ and })
543-
pub static SP_BRACES: &str = "
544-
method this_is_some_method() {return 0;}
545-
546-
method this_is_empty_method() { }
547-
548-
bank pcie_config {register command {field mem {
549-
method pcie_write(uint64 value) {
550-
if (value != 0) {value = value + 1;
551-
}
552-
default(value);
553-
map_memory_alt();}
554-
}}}
555-
";
556-
#[test]
557-
fn style_check_sp_braces() {
558-
let mut cfg = LintCfg::default();
559-
let mut rules = instantiate_rules(&cfg);
560-
assert_snippet(SP_BRACES, 8, &rules);
561-
// Test rule disable
562-
cfg.sp_brace = None;
563-
rules = instantiate_rules(&cfg);
564-
assert_snippet(SP_BRACES, 0, &rules);
565-
566-
}
567-
568-
pub static SP_BRACES_02: &str = "
569-
typedef struct {uint16 idx;} hqm_cq_list_release_ctx_t;
570-
571-
typedef layout \"little-endian\" {bitfields 8 {uint2 rsvd @ [7:6];
572-
uint1 error_f @ [5:5];
573-
uint1 int_arm @ [4:4];
574-
uint1 qe_valid @ [3:3];
575-
uint1 qe_frag @ [2:2];
576-
uint1 qe_comp @ [1:1];
577-
uint1 cq_token @ [0:0];} byte;} prod_qe_cmd_t;
578-
";
579-
#[test]
580-
fn style_check_sp_braces_02() {
581-
let mut cfg = LintCfg::default();
582-
let mut rules = instantiate_rules(&cfg);
583-
assert_snippet(SP_BRACES_02, 6, &rules);
584-
// Test rule disable
585-
cfg.sp_brace = None;
586-
rules = instantiate_rules(&cfg);
587-
assert_snippet(SP_BRACES_02, 0, &rules);
588-
589-
}
590-
591-
// SP.binop around binary operators except the dereferencing operators dot
592-
// (a.b) and arrow (a->b)
593-
pub static SP_BINOP: &str = "
594-
method this_is_some_method() {
595-
local int this_some_integer = 5+6;
596-
if (this_some_integer == 0x666)
597-
this_some_integer = this.val;
598-
}
599-
";
600-
601-
// SP.ternary around ? and : in the ternary ?: operator
602-
pub static SP_TERNARY: &str = "
603-
method this_is_some_method(bool flag) {
604-
local int this_some_integer = (flag?5:7));
605-
}
606-
";
607-
608-
// SP.punct after but not before colon, semicolon and comma
609-
pub static SP_PUNCT: &str = "
610-
method this_is_some_method(bool flag ,int8 var) {
611-
local int this_some_integer = 0x666 ;
612-
if(this_some_integer == 0x666)
613-
return;
614-
some_func(arg1 ,arg2 ,arg3 ,arg4);
615-
}
616-
";
617-
#[test]
618-
fn style_check_sp_punct_rule() {
619-
let mut cfg = LintCfg::default();
620-
let mut rules = instantiate_rules(&cfg);
621-
assert_snippet(SP_PUNCT, 9, &rules);
622-
// Test rule disable
623-
cfg.sp_punct = None;
624-
rules = instantiate_rules(&cfg);
625-
assert_snippet(SP_PUNCT, 0, &rules);
626-
}
627-
628-
// SP.ptrdecl between a type and the * marking a pointer
629-
pub static SP_PTRDECL: &str = "
630-
method this_is_some_method(conf_object_t* dummy_obj) {
631-
if(!dummy_obj)
632-
return;
633-
}
634-
";
635-
636-
// SP.comment around the comment delimiters //, /* and **/
637-
pub static SP_COMMENT: &str = "
638-
/*Function
639-
documentation*/
640-
method this_is_some_method(conf_object_t *dummy_obj) {
641-
if(!dummy_obj)//Not null
642-
return;
643-
}
644-
";
645-
646-
// There should be no space:
647-
// NSP.funpar between a function/method name and its opening parenthesis
648-
pub static NSP_FUNPAR: &str = "
649-
method this_is_some_method (conf_object_t *dummy_obj) {
650-
if(!dummy_obj)
651-
other_method_called ();
652-
}
653-
";
654-
#[test]
655-
fn style_check_nsp_funpar() {
656-
let mut cfg = LintCfg::default();
657-
let mut rules = instantiate_rules(&cfg);
658-
assert_snippet(NSP_FUNPAR, 2, &rules);
659-
// Test rule disable
660-
cfg.nsp_funpar = None;
661-
rules = instantiate_rules(&cfg);
662-
assert_snippet(NSP_FUNPAR, 0, &rules);
663-
}
664-
665-
// NSP.inparen immediately inside parentheses or brackets
666-
pub static NSP_INPAREN: &str = "
667-
method this_is_some_method( conf_object_t *dummy_obj ) {
668-
if( !dummy_obj[ 0 ] )
669-
return;
670-
}
671-
";
672-
#[test]
673-
fn style_check_nsp_inparen() {
674-
let mut cfg = LintCfg::default();
675-
let mut rules = instantiate_rules(&cfg);
676-
assert_snippet(NSP_INPAREN, 6, &rules);
677-
// Test rule disable
678-
cfg.nsp_inparen = None;
679-
rules = instantiate_rules(&cfg);
680-
assert_snippet(NSP_INPAREN, 0, &rules);
681-
}
682-
683-
// NSP.unary between a unary operator and its operand
684-
pub static NSP_UNARY: &str = "
685-
method this_is_some_method(conf_object_t *dummy_obj) {
686-
if(! dummy_obj)
687-
return;
688-
local uint64 p = & dummy_obj;
689-
p ++;
690-
-- p;
691-
local int64 neg = - 1;
692-
}
693-
";
694-
#[test]
695-
fn style_check_nsp_unary() {
696-
let mut cfg = LintCfg::default();
697-
let mut rules = instantiate_rules(&cfg);
698-
assert_snippet(NSP_UNARY, 5, &rules);
699-
// Test rule disable
700-
cfg.nsp_unary = None;
701-
rules = instantiate_rules(&cfg);
702-
assert_snippet(NSP_UNARY, 0, &rules);
703-
}
704-
705-
// NSP.ptrdecl after the * marking a pointer in a declaration
706-
pub static NSP_PTRDECL: &str = "
707-
method this_is_some_method(conf_object_t * dummy_obj) {
708-
if(!dummy_obj)
709-
return;
710-
}
711-
";
712-
713-
// Adding trailing whitespace removal to spacing rules:
714-
// no whitespaces should be left at the end of a line between the last token
715-
// and the newline \n
716-
pub static NSP_TRAILING: &str = "
717-
method this_is_some_method(int64 num) {
718-
local int this_some_integer = 0x666;
719-
if (this_some_integer == 0x666)
720-
return;
721-
}
722-
";
723-
#[test]
724-
fn style_check_nsp_trailing() {
725-
let mut cfg = LintCfg::default();
726-
let mut rules = instantiate_rules(&cfg);
727-
assert_snippet(NSP_TRAILING, 4, &rules);
728-
// Test rule disable
729-
cfg.nsp_trailing = None;
730-
rules = instantiate_rules(&cfg);
731-
assert_snippet(NSP_TRAILING, 0, &rules);
732-
}
733-
}

src/lint/rules/tests/common.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::lint::{LintCfg, begin_style_check};
2+
use crate::lint::rules::{CurrentRules, instantiate_rules};
3+
use crate::lint::tests::create_ast_from_snippet;
4+
use crate::analysis::LocalDMLError;
5+
use crate::vfs::Error;
6+
7+
pub fn run_linter(source_code: &str, rules: &CurrentRules)
8+
-> Result<Vec<LocalDMLError>, Error>
9+
{
10+
print!("\nSnippet to test on:\n{}\n", source_code);
11+
let ast = create_ast_from_snippet(source_code);
12+
print!("Resulting AST:\n{:#?}\n", ast);
13+
begin_style_check(ast, source_code.to_string(), rules)
14+
}
15+
16+
pub fn assert_snippet(source_code: &str, expected_errors: usize, rules: &CurrentRules) {
17+
let lint_errors = run_linter(source_code, rules);
18+
assert!(lint_errors.is_ok());
19+
assert_eq!(lint_errors.clone().unwrap().len(), expected_errors,
20+
"{:#?}", lint_errors);
21+
}
22+
23+
pub fn set_up() -> CurrentRules {
24+
let cfg = LintCfg::default();
25+
instantiate_rules(&cfg)
26+
}

0 commit comments

Comments
 (0)