From e805e8c1d5bf26e9716fb855f97d950395129c20 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 29 Jun 2020 14:21:57 +0200 Subject: (T): make typification tests more data driven --- crates/ra_hir_ty/src/tests/never_type.rs | 118 ++++++++++++------------------- crates/ra_hir_ty/src/tests/simple.rs | 89 ++++++++--------------- 2 files changed, 74 insertions(+), 133 deletions(-) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/never_type.rs b/crates/ra_hir_ty/src/tests/never_type.rs index ab9a990f5..64d421d40 100644 --- a/crates/ra_hir_ty/src/tests/never_type.rs +++ b/crates/ra_hir_ty/src/tests/never_type.rs @@ -1,99 +1,91 @@ use insta::assert_snapshot; -use super::{infer_with_mismatches, type_at}; +use super::{check_types, infer_with_mismatches}; #[test] fn infer_never1() { - let t = type_at( + check_types( r#" -//- /main.rs fn test() { let t = return; - t<|>; -} + t; +} //^ ! "#, ); - assert_eq!(t, "!"); } #[test] fn infer_never2() { - let t = type_at( + check_types( r#" -//- /main.rs fn gen() -> T { loop {} } fn test() { let a = gen(); if false { a } else { loop {} }; - a<|>; -} + a; +} //^ ! "#, ); - assert_eq!(t, "!"); } #[test] fn infer_never3() { - let t = type_at( + check_types( r#" -//- /main.rs fn gen() -> T { loop {} } fn test() { let a = gen(); if false { loop {} } else { a }; - a<|>; + a; + //^ ! } "#, ); - assert_eq!(t, "!"); } #[test] fn never_type_in_generic_args() { - let t = type_at( + check_types( r#" -//- /main.rs enum Option { None, Some(T) } fn test() { let a = if true { Option::None } else { Option::Some(return) }; - a<|>; -} + a; +} //^ Option "#, ); - assert_eq!(t, "Option"); } #[test] fn never_type_can_be_reinferred1() { - let t = type_at( + check_types( r#" -//- /main.rs fn gen() -> T { loop {} } fn test() { let a = gen(); if false { loop {} } else { a }; - a<|>; + a; + //^ () if false { a }; } "#, ); - assert_eq!(t, "()"); } #[test] fn never_type_can_be_reinferred2() { - let t = type_at( + check_types( r#" -//- /main.rs enum Option { None, Some(T) } fn test() { let a = if true { Option::None } else { Option::Some(return) }; - a<|>; + a; + //^ Option match 42 { 42 => a, _ => Option::Some(42), @@ -101,19 +93,18 @@ fn test() { } "#, ); - assert_eq!(t, "Option"); } #[test] fn never_type_can_be_reinferred3() { - let t = type_at( + check_types( r#" -//- /main.rs enum Option { None, Some(T) } fn test() { let a = if true { Option::None } else { Option::Some(return) }; - a<|>; + a; + //^ Option<&str> match 42 { 42 => a, _ => Option::Some("str"), @@ -121,82 +112,72 @@ fn test() { } "#, ); - assert_eq!(t, "Option<&str>"); } #[test] fn match_no_arm() { - let t = type_at( + check_types( r#" -//- /main.rs enum Void {} fn test(a: Void) { let t = match a {}; - t<|>; -} + t; +} //^ ! "#, ); - assert_eq!(t, "!"); } #[test] fn match_unknown_arm() { - let t = type_at( + check_types( r#" -//- /main.rs fn test(a: Option) { let t = match 0 { _ => unknown, }; - t<|>; -} + t; +} //^ {unknown} "#, ); - assert_eq!(t, "{unknown}"); } #[test] fn if_never() { - let t = type_at( + check_types( r#" -//- /main.rs fn test() { let i = if true { loop {} } else { 3.0 }; - i<|>; -} + i; +} //^ f64 "#, ); - assert_eq!(t, "f64"); } #[test] fn if_else_never() { - let t = type_at( + check_types( r#" -//- /main.rs fn test(input: bool) { let i = if input { 2.0 } else { return }; - i<|>; -} + i; +} //^ f64 "#, ); - assert_eq!(t, "f64"); } #[test] fn match_first_arm_never() { - let t = type_at( + check_types( r#" -//- /main.rs fn test(a: i32) { let i = match a { 1 => return, @@ -204,18 +185,16 @@ fn test(a: i32) { 3 => loop {}, _ => 3.0, }; - i<|>; -} + i; +} //^ f64 "#, ); - assert_eq!(t, "f64"); } #[test] fn match_second_arm_never() { - let t = type_at( + check_types( r#" -//- /main.rs fn test(a: i32) { let i = match a { 1 => 3.0, @@ -223,45 +202,40 @@ fn test(a: i32) { 3 => 3.0, _ => return, }; - i<|>; -} + i; +} //^ f64 "#, ); - assert_eq!(t, "f64"); } #[test] fn match_all_arms_never() { - let t = type_at( + check_types( r#" -//- /main.rs fn test(a: i32) { let i = match a { 2 => return, _ => loop {}, }; - i<|>; -} + i; +} //^ ! "#, ); - assert_eq!(t, "!"); } #[test] fn match_no_never_arms() { - let t = type_at( + check_types( r#" -//- /main.rs fn test(a: i32) { let i = match a { 2 => 2.0, _ => 3.0, }; - i<|>; -} + i; +} //^ f64 "#, ); - assert_eq!(t, "f64"); } #[test] diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs index 5e3f2bd3c..de63f4cce 100644 --- a/crates/ra_hir_ty/src/tests/simple.rs +++ b/crates/ra_hir_ty/src/tests/simple.rs @@ -1,19 +1,17 @@ -use super::{infer, type_at, type_at_pos}; -use crate::test_db::TestDB; use insta::assert_snapshot; -use ra_db::fixture::WithFixture; + +use super::{check_types, infer}; #[test] fn infer_box() { - let (db, pos) = TestDB::with_position( + check_types( r#" //- /main.rs crate:main deps:std - fn test() { let x = box 1; let t = (x, box x, box &1, box [1]); - t<|>; -} + t; +} //^ (Box, Box>, Box<&i32>, Box<[i32; _]>) //- /std.rs crate:std #[prelude_import] use prelude::*; @@ -25,29 +23,24 @@ mod boxed { inner: *mut T, } } - "#, ); - assert_eq!("(Box, Box>, Box<&i32>, Box<[i32; _]>)", type_at_pos(&db, pos)); } #[test] fn infer_adt_self() { - let (db, pos) = TestDB::with_position( + check_types( r#" -//- /main.rs enum Nat { Succ(Self), Demo(Nat), Zero } fn test() { let foo: Nat = Nat::Zero; if let Nat::Succ(x) = foo { - x<|> - } + x + } //^ Nat } - "#, ); - assert_eq!("Nat", type_at_pos(&db, pos)); } #[test] @@ -93,7 +86,7 @@ fn foo() { #[test] fn infer_ranges() { - let (db, pos) = TestDB::with_position( + check_types( r#" //- /main.rs crate:main deps:core fn test() { @@ -105,8 +98,8 @@ fn test() { let f = 'a'..='z'; let t = (a, b, c, d, e, f); - t<|>; -} + t; +} //^ (RangeFull, RangeFrom, RangeTo, Range, RangeToInclusive, RangeInclusive) //- /core.rs crate:core #[prelude_import] use prelude::*; @@ -135,29 +128,22 @@ pub mod ops { } "#, ); - assert_eq!( - "(RangeFull, RangeFrom, RangeTo, Range, RangeToInclusive, RangeInclusive)", - type_at_pos(&db, pos), - ); } #[test] fn infer_while_let() { - let (db, pos) = TestDB::with_position( + check_types( r#" -//- /main.rs enum Option { Some(T), None } fn test() { let foo: Option = None; while let Option::Some(x) = foo { - <|>x - } + x + } //^ f32 } - "#, ); - assert_eq!("f32", type_at_pos(&db, pos)); } #[test] @@ -1687,9 +1673,8 @@ fn test() { #[test] fn shadowing_primitive() { - let t = type_at( + check_types( r#" -//- /main.rs struct i32; struct Foo; @@ -1697,15 +1682,15 @@ impl i32 { fn foo(&self) -> Foo { Foo } } fn main() { let x: i32 = i32; - x.foo()<|>; + x.foo(); + //^ Foo }"#, ); - assert_eq!(t, "Foo"); } #[test] fn not_shadowing_primitive_by_module() { - let t = type_at( + check_types( r#" //- /str.rs fn foo() {} @@ -1715,15 +1700,15 @@ mod str; fn foo() -> &'static str { "" } fn main() { - foo()<|>; + foo(); + //^ &str }"#, ); - assert_eq!(t, "&str"); } #[test] fn not_shadowing_module_by_primitive() { - let t = type_at( + check_types( r#" //- /str.rs fn foo() -> u32 {0} @@ -1733,10 +1718,10 @@ mod str; fn foo() -> &'static str { "" } fn main() { - str::foo()<|>; + str::foo(); + //^ u32 }"#, ); - assert_eq!(t, "u32"); } // This test is actually testing the shadowing behavior within ra_hir_def. It @@ -1744,27 +1729,7 @@ fn main() { // capable of asserting the necessary conditions. #[test] fn should_be_shadowing_imports() { - let t = type_at( - r#" -mod a { - pub fn foo() -> i8 {0} - pub struct foo { a: i8 } -} -mod b { pub fn foo () -> u8 {0} } -mod c { pub struct foo { a: u8 } } -mod d { - pub use super::a::*; - pub use super::c::foo; - pub use super::b::foo; -} - -fn main() { - d::foo()<|>; -}"#, - ); - assert_eq!(t, "u8"); - - let t = type_at( + check_types( r#" mod a { pub fn foo() -> i8 {0} @@ -1779,10 +1744,12 @@ mod d { } fn main() { - d::foo{a:0<|>}; + d::foo(); + //^ u8 + d::foo{a:0}; + //^ u8 }"#, ); - assert_eq!(t, "u8"); } #[test] -- cgit v1.2.3