FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
kernelscript/tests/test_comment_positions.ml at main · multikernel/kernelscript · GitHub
multikernel
/
kernelscript
Public
Notifications
You must be signed in to change notification settings
Fork
24
Star
508
Code
Issues
2
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
kernelscript
/
tests
/
test_comment_positions.ml
Copy path
More file actions
More file actions
Latest commit
History
History
History
136 lines (126 loc) · 4.73 KB
Breadcrumbs
kernelscript
/
tests
/
test_comment_positions.ml
Copy path
File metadata and controls
136 lines (126 loc) · 4.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
133
134
135
136
(*
* Copyright 2025 Multikernel Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*)
open
Kernelscript.Parse
open
Kernelscript.Ast
open
Alcotest
(*
* Test that comments at line 1, column 1 don't cause parse errors
*)
let
test_comment_at_start
()
=
let
program_text
=
{
|//
This
is a comment at line
1
, column
1
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
return
XDP_PASS
}
|
}
in
try
let
ast
=
parse_string program_text
in
check
int
"
AST declarations count
"
1
(
List.
length ast);
()
with
|
Parse_error
(
msg
,
pos
) ->
fail (
"
Parse error:
"
^
msg
^
"
at
"
^
string_of_position pos)
(*
* Test that comments with whitespace before them work
*)
let
test_comment_with_whitespace
()
=
let
program_text
=
{
|
//
Comment
with
whitespace before it
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
return
XDP_PASS
}
|
}
in
try
let
ast
=
parse_string program_text
in
check
int
"
AST declarations count
"
1
(
List.
length ast);
()
with
|
Parse_error
(
msg
,
pos
) ->
fail (
"
Parse error:
"
^
msg
^
"
at
"
^
string_of_position pos)
(*
* Test that error positions are correctly reported when there's a comment at the start
*)
let
test_error_position_after_comment
()
=
let
program_text
=
{
|//
Comment
at start
@
xdp fn test_invalid_syntax_here
|
}
in
try
let
_
=
parse_string program_text
in
fail
"
Expected parse error but parsing succeeded
"
with
|
Parse_error
(
msg
,
pos
) ->
check
int
"
error line
"
2
pos.line;
check
bool
"
error column reasonable
"
true
(pos.column
>
=
1
);
(*
Parser reports actual error position
*)
check
string
"
error message
"
"
Syntax error
"
msg
(*
* Test that error positions are correctly reported without comments
*)
let
test_error_position_no_comment
()
=
let
program_text
=
{
|@
xdp fn test_invalid_syntax_here
|
}
in
try
let
_
=
parse_string program_text
in
fail
"
Expected parse error but parsing succeeded
"
with
|
Parse_error
(
msg
,
pos
) ->
check
int
"
error line
"
1
pos.line;
check
bool
"
error column reasonable
"
true
(pos.column
>
=
1
);
(*
Parser reports actual error position
*)
check
string
"
error message
"
"
Syntax error
"
msg
(*
* Test multiple lines with comments
*)
let
test_multiple_line_comments
()
=
let
program_text
=
{
|//
First
comment
//
Second
comment
//
Third
comment
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
//
Comment
inside
function
return
XDP_PASS
}|}
in
try
let
ast
=
parse_string
program_text
in
check
int
"AST declarations count"
1
(
List.
length
ast
);
()
with
|
Parse_error
(
msg
,
pos
) ->
fail (
"
Parse error:
"
^
msg
^
"
at
"
^
string_of_position pos)
(*
* Test that inline comments work
*)
let
test_inline_comments
()
=
let
program_text
=
{
|@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
//
Inline
comment,
Another
inline comment
return
XDP_PASS
//
Final
comment
}
|
}
in
try
let
ast
=
parse_string program_text
in
check
int
"
AST declarations count
"
1
(
List.
length ast);
()
with
|
Parse_error
(
msg
,
pos
) ->
fail (
"
Parse error:
"
^
msg
^
"
at
"
^
string_of_position pos)
(*
* Test error position in a multi-line file with comments
*)
let
test_error_position_multiline
()
=
let
program_text
=
{
|//
Comment
line
1
//
Comment
line
2
@
xdp fn test(ctx:
*xdp_md
) -> xdp_action {
let
x
=
if
(missing_condition_error) {
return
XDP_PASS
}
return
XDP_PASS
}
|
}
in
try
let
_
=
parse_string program_text
in
fail
"
Expected parse error but parsing succeeded
"
with
|
Parse_error
(
msg
,
pos
) ->
check
int
"
error line
"
4
pos.line;
(*
Error is on line 4 where the syntax error occurs
*)
check
bool
"
error column reasonable
"
true
(pos.column
>
=
1
);
(*
Parser reports actual error position
*)
check
string
"
error message
"
"
Syntax error
"
msg
let
comment_position_tests
=
[
"
comment_at_start
"
,
`Quick
, test_comment_at_start;
"
comment_with_whitespace
"
,
`Quick
, test_comment_with_whitespace;
"
error_position_after_comment
"
,
`Quick
, test_error_position_after_comment;
"
error_position_no_comment
"
,
`Quick
, test_error_position_no_comment;
"
multiple_line_comments
"
,
`Quick
, test_multiple_line_comments;
"
inline_comments
"
,
`Quick
, test_inline_comments;
"
error_position_multiline
"
,
`Quick
, test_error_position_multiline;
]
let
()
=
run
"
KernelScript Comment Position Tests
"
[
"
comment_positions
"
, comment_position_tests;
]
Back
|
FazBrowse Home
|
New Git URL