diff --git a/src/def/lower.rs b/src/def/lower.rs index b169aa1..6e15b4a 100644 --- a/src/def/lower.rs +++ b/src/def/lower.rs @@ -247,3 +247,132 @@ impl LowerCtx { self.alloc_expr(Expr::StringInterpolation(parts), ptr) } } + +#[cfg(test)] +mod tests { + use super::lower; + use crate::base::{FileId, InFile}; + use expect_test::{expect, Expect}; + use std::fmt::Write; + use syntax::parse_file; + + fn check_lower(src: &str, expect: Expect) { + let parse = parse_file(src); + let (module, _source_map) = lower(InFile::new(FileId(0), parse.root())); + let mut got = String::new(); + for (i, e) in module.exprs.iter() { + writeln!(got, "{}: {:?}", i.into_raw(), e).unwrap(); + } + expect.assert_eq(&got); + } + + #[test] + fn literal() { + check_lower( + "42", + expect![[r#" + 0: Literal(Int(42)) + "#]], + ); + check_lower( + "1.2e3", + expect![[r#" + 0: Literal(Float(OrderedFloat(1200.0))) + "#]], + ); + check_lower( + "a:b", + expect![[r#" + 0: Literal(String("a:b")) + "#]], + ); + } + + #[test] + fn path() { + check_lower( + "./.", + expect![[r#" + 0: Literal(Path(Path { anchor: Relative(FileId(0)), supers: 0, raw_segments: "" })) + "#]], + ); + check_lower( + "../.", + expect![[r#" + 0: Literal(Path(Path { anchor: Relative(FileId(0)), supers: 1, raw_segments: "" })) + "#]], + ); + check_lower( + "../a/../../.b/./c", + expect![[r#" + 0: Literal(Path(Path { anchor: Relative(FileId(0)), supers: 2, raw_segments: ".b/c" })) + "#]], + ); + check_lower( + "/../a/../../.b/./c", + expect![[r#" + 0: Literal(Path(Path { anchor: Absolute, supers: 0, raw_segments: ".b/c" })) + "#]], + ); + check_lower( + "~/../a/../../.b/./c", + expect![[r#" + 0: Literal(Path(Path { anchor: Home, supers: 2, raw_segments: ".b/c" })) + "#]], + ); + check_lower( + "

", + expect![[r#" + 0: Literal(Path(Path { anchor: Search("p"), supers: 2, raw_segments: ".b/c" })) + "#]], + ); + } + + #[test] + fn lambda() { + check_lower( + "a: 0", + expect![[r#" + 0: Literal(Int(0)) + 1: Lambda(Some(Idx::(0)), None, Idx::(0)) + "#]], + ); + check_lower( + "{ }: 0", + expect![[r#" + 0: Literal(Int(0)) + 1: Lambda(None, Some(Pat { fields: [], ellipsis: false }), Idx::(0)) + "#]], + ); + check_lower( + "a@{ ... }: 0", + expect![[r#" + 0: Literal(Int(0)) + 1: Lambda(Some(Idx::(0)), Some(Pat { fields: [], ellipsis: true }), Idx::(0)) + "#]], + ); + check_lower( + "{ }@a: 0", + expect![[r#" + 0: Literal(Int(0)) + 1: Lambda(Some(Idx::(0)), Some(Pat { fields: [], ellipsis: false }), Idx::(0)) + "#]], + ); + check_lower( + "{ a, b ? 0, ... }: 0", + expect![[r#" + 0: Literal(Int(0)) + 1: Literal(Int(0)) + 2: Lambda(None, Some(Pat { fields: [(Some(Idx::(0)), None), (Some(Idx::(1)), Some(Idx::(0)))], ellipsis: true }), Idx::(1)) + "#]], + ); + check_lower( + "{ a ? 0, b }@c: 0", + expect![[r#" + 0: Literal(Int(0)) + 1: Literal(Int(0)) + 2: Lambda(Some(Idx::(0)), Some(Pat { fields: [(Some(Idx::(1)), Some(Idx::(0))), (Some(Idx::(2)), None)], ellipsis: false }), Idx::(1)) + "#]], + ); + } +} diff --git a/src/def/tests/mod.rs b/src/def/tests.rs similarity index 98% rename from src/def/tests/mod.rs rename to src/def/tests.rs index 511b2f4..a93e872 100644 --- a/src/def/tests/mod.rs +++ b/src/def/tests.rs @@ -2,8 +2,6 @@ use super::DefDatabase; use crate::tests::TestDB; use expect_test::expect; -mod lower; - #[test] fn source_map() { let (db, root_id) = TestDB::from_file("foo 123"); diff --git a/src/def/tests/lower.rs b/src/def/tests/lower.rs deleted file mode 100644 index a13b854..0000000 --- a/src/def/tests/lower.rs +++ /dev/null @@ -1,125 +0,0 @@ -use crate::base::{FileId, InFile}; -use crate::def::lower::lower; -use expect_test::{expect, Expect}; -use std::fmt::Write; -use syntax::parse_file; - -fn check_lower(src: &str, expect: Expect) { - let parse = parse_file(src); - let (module, _source_map) = lower(InFile::new(FileId(0), parse.root())); - let mut got = String::new(); - for (i, e) in module.exprs.iter() { - writeln!(got, "{}: {:?}", i.into_raw(), e).unwrap(); - } - expect.assert_eq(&got); -} - -#[test] -fn literal() { - check_lower( - "42", - expect![[r#" - 0: Literal(Int(42)) - "#]], - ); - check_lower( - "1.2e3", - expect![[r#" - 0: Literal(Float(OrderedFloat(1200.0))) - "#]], - ); - check_lower( - "a:b", - expect![[r#" - 0: Literal(String("a:b")) - "#]], - ); -} - -#[test] -fn path() { - check_lower( - "./.", - expect![[r#" - 0: Literal(Path(Path { anchor: Relative(FileId(0)), supers: 0, raw_segments: "" })) - "#]], - ); - check_lower( - "../.", - expect![[r#" - 0: Literal(Path(Path { anchor: Relative(FileId(0)), supers: 1, raw_segments: "" })) - "#]], - ); - check_lower( - "../a/../../.b/./c", - expect![[r#" - 0: Literal(Path(Path { anchor: Relative(FileId(0)), supers: 2, raw_segments: ".b/c" })) - "#]], - ); - check_lower( - "/../a/../../.b/./c", - expect![[r#" - 0: Literal(Path(Path { anchor: Absolute, supers: 0, raw_segments: ".b/c" })) - "#]], - ); - check_lower( - "~/../a/../../.b/./c", - expect![[r#" - 0: Literal(Path(Path { anchor: Home, supers: 2, raw_segments: ".b/c" })) - "#]], - ); - check_lower( - "

", - expect![[r#" - 0: Literal(Path(Path { anchor: Search("p"), supers: 2, raw_segments: ".b/c" })) - "#]], - ); -} - -#[test] -fn lambda() { - check_lower( - "a: 0", - expect![[r#" - 0: Literal(Int(0)) - 1: Lambda(Some(Idx::(0)), None, Idx::(0)) - "#]], - ); - check_lower( - "{ }: 0", - expect![[r#" - 0: Literal(Int(0)) - 1: Lambda(None, Some(Pat { fields: [], ellipsis: false }), Idx::(0)) - "#]], - ); - check_lower( - "a@{ ... }: 0", - expect![[r#" - 0: Literal(Int(0)) - 1: Lambda(Some(Idx::(0)), Some(Pat { fields: [], ellipsis: true }), Idx::(0)) - "#]], - ); - check_lower( - "{ }@a: 0", - expect![[r#" - 0: Literal(Int(0)) - 1: Lambda(Some(Idx::(0)), Some(Pat { fields: [], ellipsis: false }), Idx::(0)) - "#]], - ); - check_lower( - "{ a, b ? 0, ... }: 0", - expect![[r#" - 0: Literal(Int(0)) - 1: Literal(Int(0)) - 2: Lambda(None, Some(Pat { fields: [(Some(Idx::(0)), None), (Some(Idx::(1)), Some(Idx::(0)))], ellipsis: true }), Idx::(1)) - "#]], - ); - check_lower( - "{ a ? 0, b }@c: 0", - expect![[r#" - 0: Literal(Int(0)) - 1: Literal(Int(0)) - 2: Lambda(Some(Idx::(0)), Some(Pat { fields: [(Some(Idx::(1)), Some(Idx::(0))), (Some(Idx::(2)), None)], ellipsis: false }), Idx::(1)) - "#]], - ); -}