#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "analyzer_tests",
))]
#[path = "common/test_utils.rs"]
mod test_utils;
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_trait_keyword_param_basic() {
let source = r#"
trait Describable {
fn describe(self) -> string
}
struct Point {
x: f32,
y: f32,
}
impl Describable for Point {
fn describe(self) -> string {
format!("({}, {})", self.x, self.y)
}
}
fn print_item(item: trait Describable) {
println!("{}", item.describe())
}
fn main() {
let p = Point { x: 1.0, y: 2.0 }
print_item(p)
}
"#;
let rust_code = test_utils::compile_single(source);
assert!(
rust_code.contains("impl Describable"),
"Should generate 'impl Describable' for static dispatch\nGenerated:\n{}",
rust_code
);
assert!(
rust_code.contains("item: impl Describable"),
"Should generate 'item: impl Describable' for trait keyword param\nGenerated:\n{}",
rust_code
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_trait_keyword_return_position() {
let source = r#"
trait Greeter {
fn greet(self) -> string
}
struct English {}
impl Greeter for English {
fn greet(self) -> string {
"Hello!".to_string()
}
}
fn make_greeter() -> trait Greeter {
English {}
}
fn main() {
let g = make_greeter()
println!("{}", g.greet())
}
"#;
let rust_code = test_utils::compile_single(source);
assert!(
rust_code.contains("-> impl Greeter"),
"Should generate '-> impl Greeter' in return type\nGenerated:\n{}",
rust_code
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_trait_keyword_in_vec() {
let source = r#"
trait Describable {
fn describe(self) -> string
}
struct Point { x: f32, y: f32 }
struct Color { r: u8, g: u8, b: u8 }
impl Describable for Point {
fn describe(self) -> string {
format!("Point({}, {})", self.x, self.y)
}
}
impl Describable for Color {
fn describe(self) -> string {
format!("Color({}, {}, {})", self.r, self.g, self.b)
}
}
struct ItemList {
items: Vec<trait Describable>,
}
fn main() {
println!("hello")
}
"#;
let rust_code = test_utils::compile_single(source);
assert!(
rust_code.contains("Vec<Box<dyn Describable>>"),
"Vec<trait T> should generate Vec<Box<dyn T>>\nGenerated:\n{}",
rust_code
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_trait_keyword_behind_reference() {
let source = r#"
trait Describable {
fn describe(self) -> string
}
struct Point { x: f32, y: f32 }
impl Describable for Point {
fn describe(self) -> string {
format!("({}, {})", self.x, self.y)
}
}
struct Wrapper {
item: &trait Describable,
}
fn main() {
println!("hello")
}
"#;
let rust_code = test_utils::compile_single(source);
assert!(
rust_code.contains("&dyn Describable"),
"&trait T should generate &dyn T\nGenerated:\n{}",
rust_code
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_trait_keyword_in_box() {
let source = r#"
trait Describable {
fn describe(self) -> string
}
struct Point { x: f32, y: f32 }
impl Describable for Point {
fn describe(self) -> string {
format!("({}, {})", self.x, self.y)
}
}
struct Wrapper {
item: Box<trait Describable>,
}
fn main() {
println!("hello")
}
"#;
let rust_code = test_utils::compile_single(source);
assert!(
rust_code.contains("Box<dyn Describable>"),
"Box<trait T> should generate Box<dyn T>\nGenerated:\n{}",
rust_code
);
}
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn test_trait_keyword_ownership_inference() {
let source = r#"
trait Resettable {
fn reset(self)
}
struct Counter {
count: int,
}
impl Resettable for Counter {
fn reset(self) {
self.count = 0
}
}
fn reset_item(item: trait Resettable) {
item.reset()
}
fn main() {
let mut c = Counter { count: 5 }
reset_item(c)
}
"#;
let rust_code = test_utils::compile_single(source);
assert!(
rust_code.contains("impl Resettable"),
"Should generate impl Resettable for static dispatch\nGenerated:\n{}",
rust_code
);
}