forked from bastibe/MatlabCodeAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_check.m
132 lines (95 loc) · 4.03 KB
/
test_check.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
%% Tokenizing a text should not change the content
text = fileread('check.m');
tokens = tokenize_code(text);
reconstructed_text = horzcat(tokens.text);
assert(strcmp(reconstructed_text, text))
%% Function names should be extracted
report = analyze_file('', tokenize_code('function foo(); end'));
assert(strcmp(report.name.text, 'foo'))
report = analyze_file('', tokenize_code('function x = foo(); end'));
assert(strcmp(report.name.text, 'foo'))
report = analyze_file('', tokenize_code('function [x, y] = foo(); end'));
assert(strcmp(report.name.text, 'foo'))
%% Function return names should be extracted
report = analyze_file('', tokenize_code('function foo(); end'));
assert(isempty(report.returns))
report = analyze_file('', tokenize_code('function x = foo(); end'));
assert(strcmp(report.returns(1).text, 'x'))
assert(length(report.returns) == 1)
report = analyze_file('', tokenize_code('function [x, y] = foo(); end'));
assert(strcmp(report.returns(1).text, 'x'))
assert(strcmp(report.returns(2).text, 'y'))
assert(length(report.returns) == 2)
%% Function arguments should be extracted
report = analyze_file('', tokenize_code('function foo(); end'));
assert(isempty(report.arguments))
report = analyze_file('', tokenize_code('function foo(x); end'));
assert(strcmp(report.arguments(1).text, 'x'))
assert(length(report.arguments) == 1)
report = analyze_file('', tokenize_code('function foo(x, y); end'));
assert(strcmp(report.arguments(1).text, 'x'))
assert(strcmp(report.arguments(2).text, 'y'))
assert(length(report.arguments) == 2)
%% Operators should be parsed correctly
tokens = tokenize_code('a>=-b');
assert(tokens(2).hasText('>='))
assert(tokens(3).hasText('-'))
%% Transpose Operators should not be strings
tokens = tokenize_code('a''');
assert(tokens(2).isEqual('punctuation', ''''))
tokens = tokenize_code('a.''');
assert(tokens(2).isEqual('punctuation', '.'''))
tokens = tokenize_code('a''+''a''.''');
assert(tokens(2).isEqual('punctuation', ''''))
assert(tokens(4).isEqual('string', '''a'''))
assert(tokens(5).isEqual('punctuation', '.'''))
%% differentiate commands from expressions
tokens = tokenize_code('help me please % test');
assert(tokens(1).isEqual('identifier', 'help'))
assert(tokens(3).isEqual('string', 'me'))
assert(tokens(5).isEqual('string', 'please'))
assert(tokens(7).isEqual('comment', '% test'))
%% differentiate keyword end from variable end
tokens = tokenize_code('if a(end); end');
assert(tokens(5).isEqual('identifier', 'end'))
assert(tokens(9).isEqual('keyword', 'end'))
%% differentiate semicolons from linebreaks
tokens = tokenize_code('[1;2];3');
assert(tokens(3).isEqual('punctuation', ';'))
assert(tokens(6).isEqual('linebreak', ';'))
%% Identify block comments
comment = sprintf('%%{ \n foo bar \n %%}');
tokens = tokenize_code(comment);
assert(length(tokens) == 1)
assert(tokens.isEqual('comment', comment))
tokens = tokenize_code(sprintf('x\n%s\nx', comment));
assert(length(tokens) == 5)
assert(tokens(3).isEqual('comment', comment))
%% line breaks should break lines
tokens = tokenize_code(',foo bar');
assert(tokens(1).hasType('linebreak'))
assert(tokens(4).hasType('string'))
tokens = tokenize_code(';foo bar');
assert(tokens(1).hasType('linebreak'))
assert(tokens(4).hasType('string'))
%% line breaks should not break lines within brackets
tokens = tokenize_code('[a;b];');
assert(tokens(3).hasType('punctuation'))
assert(tokens(6).hasType('linebreak'))
tokens = tokenize_code('[a,b],');
assert(tokens(3).hasType('punctuation'))
assert(tokens(6).hasType('linebreak'))
%% comments follow continuation operator
tokens = tokenize_code('... % this is a comment');
assert(tokens(1).hasType('punctuation'));
assert(tokens(3).hasType('comment'));
tokens = tokenize_code('... this is a comment');
assert(tokens(1).hasType('punctuation'));
assert(tokens(2).hasType('space'));
assert(tokens(3).hasType('comment'));
tokens = tokenize_code('....');
assert(tokens(1).hasType('punctuation'));
assert(tokens(2).hasType('comment'));
tokens = tokenize_code('.*...');
assert(tokens(1).hasType('punctuation'));
assert(tokens(2).hasType('punctuation'));