|
1 | 1 | open Alcotest;
|
2 | 2 |
|
| 3 | +module Tokens = Styled_ppx_css_parser.Tokens; |
3 | 4 | module Lexer = Styled_ppx_css_parser.Lexer;
|
4 | 5 | module Parser = Styled_ppx_css_parser.Parser;
|
5 | 6 |
|
@@ -158,4 +159,158 @@ let error_tests =
|
158 | 159 | );
|
159 | 160 | });
|
160 | 161 |
|
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; |
0 commit comments