Skip to content

Commit 2fd57cd

Browse files
committed
Unify tests for tokenizer
1 parent c77413d commit 2fd57cd

File tree

5 files changed

+161
-167
lines changed

5 files changed

+161
-167
lines changed

packages/css-property-parser/lib/Parser.re

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,9 @@ and property_animation_fill_mode = [%value.rec
830830
and property_animation_iteration_count = [%value.rec
831831
"[ <single-animation-iteration-count> ]#"
832832
]
833-
and property_animation_name = [%value.rec "[ <keyframes-name> | 'none' | <interpolation> ]#"]
833+
and property_animation_name = [%value.rec
834+
"[ <keyframes-name> | 'none' | <interpolation> ]#"
835+
]
834836
and property_animation_play_state = [%value.rec
835837
"[ <single-animation-play-state> ]#"
836838
]

packages/parser/lib/Ast.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* We have records with identical field names. Type inference can sometimes struggle to determine which field you're referring to. This ambiguity can lead to compiler warnings or errors, we disable it with -30 because we always construct this with annotations in those cases. */
1+
/* We have records with identical field names. Type inference can sometimes struggle to determine which field you're referring to. This ambiguity can lead to compiler warnings or errors, we disable it with -30 because we always construct this with annotations. */
22
[@warning "-30"];
33

44
[@deriving show({with_path: false})]

packages/parser/test/Lexer_test.re

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
open Alcotest;
22

3+
module Tokens = Styled_ppx_css_parser.Tokens;
34
module Lexer = Styled_ppx_css_parser.Lexer;
45
module Parser = Styled_ppx_css_parser.Parser;
56

@@ -158,4 +159,158 @@ let error_tests =
158159
);
159160
});
160161

161-
let tests = List.append(success_tests, error_tests);
162+
let parse = input => {
163+
let values =
164+
switch (Lexer.from_string(input)) {
165+
| Ok(values) => values
166+
| Error(`Frozen) => failwith("Lexer got frozen")
167+
};
168+
169+
let Lexer.{loc, _} = List.hd(values);
170+
let values = values |> List.map((Lexer.{txt, _}) => txt);
171+
(loc, values);
172+
};
173+
174+
let render_token =
175+
fun
176+
| Tokens.EOF => ""
177+
| t => Tokens.show_token(t);
178+
179+
let list_parse_tokens_to_string = tokens =>
180+
tokens
181+
|> List.rev
182+
|> List.map(
183+
fun
184+
| Ok(token) => render_token(token)
185+
| Error((token, err)) =>
186+
"Error("
187+
++ Tokens.show_error(err)
188+
++ ") "
189+
++ Tokens.show_token(token),
190+
)
191+
|> String.concat(" ")
192+
|> String.trim;
193+
194+
let list_tokens_to_string = tokens =>
195+
tokens |> List.map(render_token) |> String.concat(" ") |> String.trim;
196+
197+
let test_with_location =
198+
[
199+
({||}, [EOF], 0),
200+
(" \n\t ", [Tokens.WS], 4),
201+
({|"something"|}, [STRING("something")], 11),
202+
// TODO: is that right?
203+
({|#2|}, [HASH("2", `UNRESTRICTED)], 2),
204+
({|#abc|}, [HASH("abc", `ID)], 4),
205+
({|#|}, [DELIM("#")], 1),
206+
({|'tuturu'|}, [STRING("tuturu")], 8),
207+
({|(|}, [LEFT_PAREN], 1),
208+
({|)|}, [RIGHT_PAREN], 1),
209+
({|+12.3|}, [NUMBER(12.3)], 5),
210+
({|+|}, [DELIM("+")], 1),
211+
({|,|}, [COMMA], 1),
212+
({|-45.6|}, [NUMBER(-45.6)], 5),
213+
({|--potato|}, [IDENT("--potato")], 8),
214+
({|-|}, [DELIM("-")], 1),
215+
({|.7|}, [NUMBER(0.7)], 2),
216+
({|.|}, [DELIM(".")], 1),
217+
({|:|}, [COLON], 1),
218+
({|;|}, [SEMI_COLON], 1),
219+
({|<|}, [DELIM("<")], 1),
220+
({|@mayushii|}, [AT_KEYWORD("mayushii")], 9),
221+
({|@|}, [DELIM("@")], 1),
222+
({|[|}, [LEFT_BRACKET], 1),
223+
("\\@desu", [IDENT("@desu")], 6),
224+
({|]|}, [RIGHT_BRACKET], 1),
225+
({|12345678.9|}, [NUMBER(12345678.9)], 10),
226+
({|bar|}, [IDENT("bar")], 3),
227+
({|!|}, [DELIM("!")], 1),
228+
("1 / 1", [NUMBER(1.), WS, DELIM("/"), WS, NUMBER(1.)], 5),
229+
(
230+
{|calc(10px + 10px)|},
231+
[
232+
FUNCTION("calc"),
233+
DIMENSION(10., "px"),
234+
WS,
235+
DELIM("+"),
236+
WS,
237+
DIMENSION(10., "px"),
238+
RIGHT_PAREN,
239+
],
240+
17,
241+
),
242+
(
243+
{|background-image:url('img_tree.gif' )|},
244+
[
245+
IDENT("background-image"),
246+
COLON,
247+
FUNCTION("url"),
248+
STRING("img_tree.gif"),
249+
WS,
250+
RIGHT_PAREN,
251+
],
252+
37,
253+
),
254+
(
255+
{|calc(10px+ 10px)|},
256+
[
257+
FUNCTION("calc"),
258+
DIMENSION(10., "px"),
259+
DELIM("+"),
260+
WS,
261+
DIMENSION(10., "px"),
262+
RIGHT_PAREN,
263+
],
264+
16,
265+
),
266+
({|calc(10%)|}, [FUNCTION("calc"), PERCENTAGE(10.), RIGHT_PAREN], 9),
267+
(
268+
{|$(Module.variable)|},
269+
[
270+
DELIM("$"),
271+
LEFT_PAREN,
272+
IDENT("Module"),
273+
DELIM("."),
274+
IDENT("variable"),
275+
RIGHT_PAREN,
276+
],
277+
18,
278+
),
279+
(
280+
{|$(Module.variable')|},
281+
[
282+
DELIM("$"),
283+
LEFT_PAREN,
284+
IDENT("Module"),
285+
DELIM("."),
286+
IDENT("variable'"),
287+
RIGHT_PAREN,
288+
],
289+
19,
290+
),
291+
({|--color-main|}, [IDENT("--color-main")], 12),
292+
]
293+
|> List.mapi((_index, (input, output, last_position)) => {
294+
let (loc, values) = parse(input);
295+
296+
loc.loc_end.pos_cnum == last_position
297+
? ()
298+
: Alcotest.fail(
299+
"position should be "
300+
++ string_of_int(last_position)
301+
++ " received "
302+
++ string_of_int(loc.loc_end.pos_cnum),
303+
);
304+
305+
let assertion = () =>
306+
Alcotest.check(
307+
Alcotest.string,
308+
"should succeed lexing: " ++ input,
309+
list_parse_tokens_to_string(values),
310+
list_tokens_to_string(output),
311+
);
312+
313+
Alcotest.test_case(input, `Quick, assertion);
314+
});
315+
316+
let tests = success_tests @ error_tests @ test_with_location;

packages/parser/test/Runner.re

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
Alcotest.run(
22
"Lexer and Parser",
3-
[
4-
("Lexer", Lexer_test.tests),
5-
("Parser", Parser_test.tests),
6-
("Tokenizer", Tokenizer_test.tests),
7-
],
3+
[("Lexer", Lexer_test.tests), ("Parser", Parser_test.tests)],
84
);

packages/parser/test/Tokenizer_test.re

Lines changed: 0 additions & 159 deletions
This file was deleted.

0 commit comments

Comments
 (0)