2022-03-10 08:21:57 +03:00
|
|
|
// === Non-Standard Linter Configuration ===
|
2022-03-10 07:57:59 +03:00
|
|
|
#![deny(non_ascii_idents)]
|
|
|
|
#![warn(unsafe_code)]
|
|
|
|
|
2022-10-11 00:38:48 +03:00
|
|
|
use parser_scala::prelude::*;
|
2020-04-29 20:29:37 +03:00
|
|
|
|
2022-10-11 00:38:48 +03:00
|
|
|
use parser_scala::Parser;
|
2020-04-29 20:29:37 +03:00
|
|
|
use wasm_bindgen_test::wasm_bindgen_test;
|
|
|
|
use wasm_bindgen_test::wasm_bindgen_test_configure;
|
|
|
|
|
2022-03-10 07:32:33 +03:00
|
|
|
|
|
|
|
|
2020-04-29 20:29:37 +03:00
|
|
|
wasm_bindgen_test_configure!(run_in_browser);
|
|
|
|
|
2020-07-13 14:58:22 +03:00
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn import_utilities() {
|
|
|
|
use ast::macros::ast_as_import_match;
|
|
|
|
use ast::macros::is_ast_import;
|
|
|
|
use ast::macros::is_match_import;
|
|
|
|
|
|
|
|
let parser = Parser::new_or_panic();
|
2021-11-02 16:05:43 +03:00
|
|
|
let expect_import = |code: &str| {
|
2021-08-05 17:28:26 +03:00
|
|
|
let ast = parser.parse_line_ast(code).unwrap();
|
2021-07-21 10:57:08 +03:00
|
|
|
assert!(is_ast_import(&ast), "Not Ast import: {:?}", ast);
|
2020-07-13 14:58:22 +03:00
|
|
|
let ast_match = ast_as_import_match(&ast).unwrap();
|
2021-11-02 16:05:43 +03:00
|
|
|
assert_eq!(&ast, ast_match.ast());
|
2020-07-13 14:58:22 +03:00
|
|
|
assert!(is_match_import(&ast_match));
|
|
|
|
};
|
|
|
|
|
2021-11-02 16:05:43 +03:00
|
|
|
let expect_not_import = |code: &str| {
|
2021-08-05 17:28:26 +03:00
|
|
|
let ast = parser.parse_line_ast(code).unwrap();
|
2020-07-13 14:58:22 +03:00
|
|
|
assert!(!is_ast_import(&ast));
|
|
|
|
assert!(ast_as_import_match(&ast).is_none());
|
|
|
|
};
|
|
|
|
|
2021-03-02 16:21:50 +03:00
|
|
|
expect_import("import");
|
2020-07-13 14:58:22 +03:00
|
|
|
expect_import("import Foo");
|
2021-07-21 10:57:08 +03:00
|
|
|
expect_import("import foo.Foo.Bar");
|
|
|
|
expect_import("import foo.Foo.Bar");
|
2020-07-13 14:58:22 +03:00
|
|
|
expect_import("import Foo.Bar");
|
|
|
|
expect_import("import Foo.Bar.Baz");
|
2021-02-15 19:48:41 +03:00
|
|
|
expect_import("from Foo import Bar");
|
2021-07-21 10:57:08 +03:00
|
|
|
expect_import("from foo.Foo import all hiding Bar");
|
2021-02-15 19:48:41 +03:00
|
|
|
expect_import("from Base.Data.List import all hiding Cons, Nil");
|
2020-07-13 14:58:22 +03:00
|
|
|
|
|
|
|
expect_not_import("type Foo");
|
|
|
|
expect_not_import("type Foo as Bar");
|
|
|
|
expect_not_import("if Foo then Bar else Baz");
|
|
|
|
expect_not_import("Foo.Bar.Baz");
|
2021-03-02 16:21:50 +03:00
|
|
|
expect_not_import("->");
|
|
|
|
expect_not_import("export");
|
2021-02-15 19:48:41 +03:00
|
|
|
expect_not_import("export Foo");
|
|
|
|
expect_not_import("from Foo export all hiding Bar");
|
2020-07-13 14:58:22 +03:00
|
|
|
}
|
|
|
|
|
2020-04-29 20:29:37 +03:00
|
|
|
#[wasm_bindgen_test]
|
|
|
|
fn recognizing_lambdas() {
|
|
|
|
let parser = Parser::new_or_panic();
|
|
|
|
|
2021-11-02 16:05:43 +03:00
|
|
|
let expect_lambda = |code: &str, arg: &str, body: &str| {
|
2021-08-05 17:28:26 +03:00
|
|
|
let ast = parser.parse_line_ast(code).unwrap();
|
2020-04-29 20:29:37 +03:00
|
|
|
let lambda = ast::macros::as_lambda(&ast).expect("failed to recognize lambda");
|
|
|
|
assert_eq!(lambda.arg.repr(), arg);
|
|
|
|
assert_eq!(lambda.body.repr(), body);
|
|
|
|
assert_eq!(*lambda.arg, ast.get_traversing(&lambda.arg.crumbs).unwrap());
|
|
|
|
assert_eq!(*lambda.body, ast.get_traversing(&lambda.body.crumbs).unwrap());
|
|
|
|
};
|
2021-11-02 16:05:43 +03:00
|
|
|
let expect_not_lambda = |code: &str| {
|
2021-08-05 17:28:26 +03:00
|
|
|
let ast = parser.parse_line_ast(code).unwrap();
|
2020-04-29 20:29:37 +03:00
|
|
|
assert!(ast::macros::as_lambda_match(&ast).is_none(), "wrongly recognized a lambda");
|
|
|
|
};
|
|
|
|
|
2021-11-02 16:05:43 +03:00
|
|
|
expect_lambda("a->b", "a", "b");
|
|
|
|
expect_lambda("foo->4+(4)", "foo", "4+(4)");
|
|
|
|
expect_lambda("a->b->c", "a", "b->c");
|
|
|
|
expect_lambda("(a->b)->c", "(a->b)", "c");
|
2020-04-29 20:29:37 +03:00
|
|
|
|
|
|
|
expect_not_lambda("(a->b)");
|
|
|
|
expect_not_lambda("a+b");
|
|
|
|
expect_not_lambda("'a+b'");
|
|
|
|
expect_not_lambda("497");
|
|
|
|
}
|