Skip to content

Commit

Permalink
test: add tests related to struct in C files or in other scopes that …
Browse files Browse the repository at this point in the history
…is not global
  • Loading branch information
NiumXp committed Nov 23, 2023
1 parent e0de65f commit 5f95c2a
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/rules/samples/check_utype_declaration_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
struct
{
int a;
};

void main(void)
{
struct
{
int b;
};
}

// https://github.com/42School/norminette/issues/437
enum e_endian
{
LITTLE,
BIG
};

enum e_endian which_endian(void)
{
union
{
unsigned char var2[2];
unsigned short var1;
} u_endian;// This should be the correct indentation
// } u_endian;
u_endian.var1 = 1;
if (u_endian.var1 == u_endian.var2[0])
return (LITTLE);
else
return (BIG);
}
79 changes: 79 additions & 0 deletions tests/rules/samples/check_utype_declaration_1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
check_utype_declaration_1.c - IsUserDefinedType In "GlobalScope" from "None" line 1":
<STRUCT> <NEWLINE>
check_utype_declaration_1.c - IsBlockStart In "UserDefinedType" from "GlobalScope" line 2":
<LBRACE> <NEWLINE>
check_utype_declaration_1.c - IsVarDeclaration In "UserDefinedType" from "GlobalScope" line 3":
<TAB> <INT> <TAB> <IDENTIFIER=a> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsBlockEnd In "UserDefinedType" from "GlobalScope" line 4":
<RBRACE> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsEmptyLine In "GlobalScope" from "None" line 5":
<NEWLINE>
check_utype_declaration_1.c - IsFuncDeclaration In "GlobalScope" from "None" line 6":
<VOID> <TAB> <IDENTIFIER=main> <LPARENTHESIS> <VOID> <RPARENTHESIS> <NEWLINE>
check_utype_declaration_1.c - IsBlockStart In "Function" from "GlobalScope" line 7":
<LBRACE> <NEWLINE>
check_utype_declaration_1.c - IsUserDefinedType In "Function" from "GlobalScope" line 8":
<TAB> <STRUCT> <NEWLINE>
check_utype_declaration_1.c - IsBlockStart In "UserDefinedType" from "Function" line 9":
<TAB> <LBRACE> <NEWLINE>
check_utype_declaration_1.c - IsVarDeclaration In "UserDefinedType" from "Function" line 10":
<TAB> <TAB> <INT> <TAB> <IDENTIFIER=b> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsBlockEnd In "UserDefinedType" from "Function" line 11":
<TAB> <RBRACE> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsBlockEnd In "Function" from "GlobalScope" line 12":
<RBRACE> <NEWLINE>
check_utype_declaration_1.c - IsEmptyLine In "GlobalScope" from "None" line 13":
<NEWLINE>
check_utype_declaration_1.c - IsComment In "GlobalScope" from "None" line 14":
<COMMENT=// https://github.com/42School/norminette/issues/437> <NEWLINE>
check_utype_declaration_1.c - IsUserDefinedType In "GlobalScope" from "None" line 15":
<ENUM> <SPACE> <IDENTIFIER=e_endian> <NEWLINE>
check_utype_declaration_1.c - IsBlockStart In "UserDefinedEnum" from "GlobalScope" line 16":
<LBRACE> <NEWLINE>
check_utype_declaration_1.c - IsEnumVarDecl In "UserDefinedEnum" from "GlobalScope" line 17":
<TAB> <IDENTIFIER=LITTLE> <COMMA> <NEWLINE>
check_utype_declaration_1.c - IsEnumVarDecl In "UserDefinedEnum" from "GlobalScope" line 18":
<TAB> <IDENTIFIER=BIG> <NEWLINE>
check_utype_declaration_1.c - IsBlockEnd In "UserDefinedEnum" from "GlobalScope" line 19":
<RBRACE> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsEmptyLine In "GlobalScope" from "None" line 20":
<NEWLINE>
check_utype_declaration_1.c - IsFuncDeclaration In "GlobalScope" from "None" line 21":
<ENUM> <SPACE> <IDENTIFIER=e_endian> <TAB> <IDENTIFIER=which_endian> <LPARENTHESIS> <VOID> <RPARENTHESIS> <NEWLINE>
check_utype_declaration_1.c - IsBlockStart In "Function" from "GlobalScope" line 22":
<LBRACE> <NEWLINE>
check_utype_declaration_1.c - IsUserDefinedType In "Function" from "GlobalScope" line 23":
<TAB> <UNION> <NEWLINE>
check_utype_declaration_1.c - IsBlockStart In "UserDefinedType" from "Function" line 24":
<TAB> <LBRACE> <NEWLINE>
check_utype_declaration_1.c - IsVarDeclaration In "UserDefinedType" from "Function" line 25":
<TAB> <TAB> <UNSIGNED> <SPACE> <CHAR> <TAB> <IDENTIFIER=var2> <LBRACKET> <CONSTANT=2> <RBRACKET> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsVarDeclaration In "UserDefinedType" from "Function" line 26":
<TAB> <TAB> <UNSIGNED> <SPACE> <SHORT> <TAB> <IDENTIFIER=var1> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsBlockEnd In "UserDefinedType" from "Function" line 27":
<TAB> <RBRACE> <TAB> <IDENTIFIER=u_endian> <SEMI_COLON>
check_utype_declaration_1.c - IsComment In "Function" from "GlobalScope" line 27":
<COMMENT=// This should be the correct indentation> <NEWLINE>
check_utype_declaration_1.c - IsComment In "Function" from "GlobalScope" line 28":
<COMMENT=// } u_endian; > <NEWLINE>
check_utype_declaration_1.c - IsAssignation In "Function" from "GlobalScope" line 29":
<TAB> <IDENTIFIER=u_endian> <DOT> <IDENTIFIER=var1> <SPACE> <ASSIGN> <SPACE> <CONSTANT=1> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsControlStatement In "Function" from "GlobalScope" line 30":
<TAB> <IF> <SPACE> <LPARENTHESIS> <IDENTIFIER=u_endian> <DOT> <IDENTIFIER=var1> <SPACE> <EQUALS> <SPACE> <IDENTIFIER=u_endian> <DOT> <IDENTIFIER=var2> <LBRACKET> <CONSTANT=0> <RBRACKET> <RPARENTHESIS> <NEWLINE>
check_utype_declaration_1.c - IsExpressionStatement In "ControlStructure" from "Function" line 31":
<TAB> <TAB> <RETURN> <SPACE> <LPARENTHESIS> <IDENTIFIER=LITTLE> <RPARENTHESIS> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsControlStatement In "Function" from "GlobalScope" line 32":
<TAB> <ELSE> <NEWLINE>
check_utype_declaration_1.c - IsExpressionStatement In "ControlStructure" from "Function" line 33":
<TAB> <TAB> <RETURN> <SPACE> <LPARENTHESIS> <IDENTIFIER=BIG> <RPARENTHESIS> <SEMI_COLON> <NEWLINE>
check_utype_declaration_1.c - IsBlockEnd In "Function" from "GlobalScope" line 34":
<RBRACE> <NEWLINE>
check_utype_declaration_1.c: Error!
Error: FORBIDDEN_STRUCT (line: 1, col: 1): Struct declaration are not allowed in .c files
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: FORBIDDEN_STRUCT (line: 8, col: 5): Struct declaration are not allowed in .c files
Error: TYPE_NOT_GLOBAL (line: 8, col: 5): Enums, structs and unions need to be defined only in global scope
Error: TYPE_NOT_GLOBAL (line: 23, col: 5): Enums, structs and unions need to be defined only in global scope
Error: WRONG_SCOPE_COMMENT (line: 27, col: 18): Comment is invalid in this scope
Error: WRONG_SCOPE_COMMENT (line: 27, col: 18): Comment is invalid in this scope
Error: WRONG_SCOPE_COMMENT (line: 28, col: 1): Comment is invalid in this scope
12 changes: 12 additions & 0 deletions tests/rules/samples/check_utype_declaration_2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
struct
{
int a;
};

struct
{
struct
{
int b;
} s_a;
};
26 changes: 26 additions & 0 deletions tests/rules/samples/check_utype_declaration_2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
check_utype_declaration_2.h - IsUserDefinedType In "GlobalScope" from "None" line 1":
<STRUCT> <NEWLINE>
check_utype_declaration_2.h - IsBlockStart In "UserDefinedType" from "GlobalScope" line 2":
<LBRACE> <NEWLINE>
check_utype_declaration_2.h - IsVarDeclaration In "UserDefinedType" from "GlobalScope" line 3":
<TAB> <INT> <TAB> <IDENTIFIER=a> <SEMI_COLON> <NEWLINE>
check_utype_declaration_2.h - IsBlockEnd In "UserDefinedType" from "GlobalScope" line 4":
<RBRACE> <SEMI_COLON> <NEWLINE>
check_utype_declaration_2.h - IsEmptyLine In "GlobalScope" from "None" line 5":
<NEWLINE>
check_utype_declaration_2.h - IsUserDefinedType In "GlobalScope" from "None" line 6":
<STRUCT> <NEWLINE>
check_utype_declaration_2.h - IsBlockStart In "UserDefinedType" from "GlobalScope" line 7":
<LBRACE> <NEWLINE>
check_utype_declaration_2.h - IsUserDefinedType In "UserDefinedType" from "GlobalScope" line 8":
<TAB> <STRUCT> <NEWLINE>
check_utype_declaration_2.h - IsBlockStart In "UserDefinedType" from "UserDefinedType" line 9":
<TAB> <LBRACE> <NEWLINE>
check_utype_declaration_2.h - IsVarDeclaration In "UserDefinedType" from "UserDefinedType" line 10":
<TAB> <TAB> <INT> <TAB> <IDENTIFIER=b> <SEMI_COLON> <NEWLINE>
check_utype_declaration_2.h - IsBlockEnd In "UserDefinedType" from "UserDefinedType" line 11":
<TAB> <RBRACE> <TAB> <IDENTIFIER=s_a> <SEMI_COLON> <NEWLINE>
check_utype_declaration_2.h - IsBlockEnd In "UserDefinedType" from "GlobalScope" line 12":
<RBRACE> <SEMI_COLON> <NEWLINE>
check_utype_declaration_2.h: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
1 change: 1 addition & 0 deletions tests/rules/samples/ko_struct_indent.out
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Error: MISALIGNED_VAR_DECL (line: 2, col: 29): Misaligned variable declarati
Notice: GLOBAL_VAR_DETECTED (line: 3, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: MISALIGNED_VAR_DECL (line: 3, col: 29): Misaligned variable declaration
Error: MULT_DECL_LINE (line: 3, col: 34): Multiple declarations on a single line
Error: FORBIDDEN_STRUCT (line: 5, col: 9): Struct declaration are not allowed in .c files
Error: BRACE_NEWLINE (line: 5, col: 23): Expected newline before brace
Error: MISALIGNED_VAR_DECL (line: 7, col: 29): Misaligned variable declaration
Error: MISALIGNED_VAR_DECL (line: 8, col: 1): Misaligned variable declaration
Expand Down
1 change: 1 addition & 0 deletions tests/rules/samples/ko_struct_name.out
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Notice: GLOBAL_VAR_DETECTED (line: 2, col: 1): Global variable present in f
Error: GLOBAL_VAR_NAMING (line: 2, col: 25): Global variable must start with g_
Notice: GLOBAL_VAR_DETECTED (line: 3, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Error: GLOBAL_VAR_NAMING (line: 3, col: 25): Global variable must start with g_
Error: FORBIDDEN_STRUCT (line: 5, col: 9): Struct declaration are not allowed in .c files
Error: BRACE_NEWLINE (line: 5, col: 21): Expected newline before brace
Error: USER_DEFINED_TYPEDEF (line: 8, col: 25): User defined typedef must start with t_
Error: USER_DEFINED_TYPEDEF (line: 10, col: 5): User defined typedef must start with t_
2 changes: 2 additions & 0 deletions tests/rules/samples/ok_struct_name.out
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@
<RBRACE> <NEWLINE>
ok_struct_name.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: FORBIDDEN_STRUCT (line: 1, col: 9): Struct declaration are not allowed in .c files
Error: FORBIDDEN_STRUCT (line: 5, col: 1): Struct declaration are not allowed in .c files
Notice: GLOBAL_VAR_DETECTED (line: 11, col: 1): Global variable present in file. Make sure it is a reasonable choice.
Notice: GLOBAL_VAR_DETECTED (line: 12, col: 1): Global variable present in file. Make sure it is a reasonable choice.
2 changes: 2 additions & 0 deletions tests/rules/samples/ok_typedef.out
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@
<RBRACE> <NEWLINE>
ok_typedef.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: FORBIDDEN_STRUCT (line: 1, col: 9): Struct declaration are not allowed in .c files
Error: FORBIDDEN_STRUCT (line: 9, col: 1): Struct declaration are not allowed in .c files
1 change: 1 addition & 0 deletions tests/rules/samples/test_comments.out
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
test_comments.c - IsBlockEnd In "Function" from "GlobalScope" line 21":
<RBRACE> <NEWLINE>
test_comments.c: Error!
Error: FORBIDDEN_STRUCT (line: 1, col: 1): Struct declaration are not allowed in .c files
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: BRACE_NEWLINE (line: 1, col: 8): Expected newline before brace
Error: COMMENT_ON_INSTR (line: 6, col: 9): Comment must be on its own line or at end of a line
Expand Down
1 change: 1 addition & 0 deletions tests/rules/samples/test_file_1019.out
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
test_file_1019.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: RETURN_PARENTHESIS (line: 8, col: 12): Return value must be in parenthesis
Error: FORBIDDEN_STRUCT (line: 11, col: 1): Struct declaration are not allowed in .c files
Error: FORBIDDEN_CHAR_NAME (line: 28, col: 9): user defined identifiers should contain only lowercase characters, digits or '_'
Error: COMMENT_ON_INSTR (line: 35, col: 6): Comment must be on its own line or at end of a line
Error: COMMENT_ON_INSTR (line: 41, col: 10): Comment must be on its own line or at end of a line
Expand Down
1 change: 1 addition & 0 deletions tests/rules/samples/test_file_1019_1.out
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
test_file_1019_1.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: PREPROC_BAD_IFNDEF (line: 1, col: 1): Ifndef preprocessor statement without endif
Error: FORBIDDEN_STRUCT (line: 5, col: 1): Struct declaration are not allowed in .c files
2 changes: 2 additions & 0 deletions tests/rules/samples/test_file_1022.out
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
test_file_1022.c - IsBlockEnd In "UserDefinedType" from "GlobalScope" line 14":
<RBRACE> <SEMI_COLON>
test_file_1022.c: Error!
Error: FORBIDDEN_STRUCT (line: 1, col: 1): Struct declaration are not allowed in .c files
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: SPACE_REPLACE_TAB (line: 9, col: 4): Found space when expecting tab
Error: FORBIDDEN_STRUCT (line: 11, col: 1): Struct declaration are not allowed in .c files
3 changes: 3 additions & 0 deletions tests/rules/samples/test_file_210304.out
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@
<RBRACE> <NEWLINE>
test_file_210304.c: Error!
Error: INVALID_HEADER (line: 1, col: 1): Missing or invalid 42 header
Error: FORBIDDEN_STRUCT (line: 1, col: 9): Struct declaration are not allowed in .c files
Error: FORBIDDEN_STRUCT (line: 6, col: 9): Struct declaration are not allowed in .c files
Error: FORBIDDEN_STRUCT (line: 14, col: 9): Struct declaration are not allowed in .c files

0 comments on commit 5f95c2a

Please sign in to comment.