Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swernli committed Oct 17, 2023
1 parent 6b177a3 commit 51c4514
Showing 1 changed file with 358 additions and 0 deletions.
358 changes: 358 additions & 0 deletions compiler/qsc_frontend/src/compile/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,3 +783,361 @@ fn two_files_error_eof() {
Namespace (Ident 1 [26-29] "Bar"): <empty>"#]]
.assert_eq(&unit.package.to_string());
}

#[test]
fn deprecated_call_from_dependency_produces_error() {
let lib_sources = SourceMap::new(
[(
"lib".into(),
indoc! {"
namespace Foo {
@Deprecated()
operation Bar() : Unit {}
}
"}
.into(),
)],
None,
);
let mut store = PackageStore::new(super::core());
let lib = compile(&store, &[], lib_sources, TargetProfile::Full);
assert!(lib.errors.is_empty(), "{:#?}", lib.errors);
let lib = store.insert(lib);

let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Test {
open Foo;
operation Main() : Unit {
Bar();
}
}
"}
.into(),
)],
None,
);
let unit = compile(&store, &[lib], sources, TargetProfile::Full);
expect![[r#"
[
Error(
Resolve(
Deprecated(
"Bar",
Span {
lo: 69,
hi: 72,
},
),
),
),
]
"#]]
.assert_debug_eq(&unit.errors);
}

#[test]
fn deprecated_attribute_with_callable_name_allowed() {
let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Foo {
@Deprecated(Foo.Baz)
operation Bar() : Unit {}
operation Baz() : Unit {}
}
"}
.into(),
)],
None,
);
let store = PackageStore::new(super::core());
let unit = compile(&store, &[], sources, TargetProfile::Full);
assert!(unit.errors.is_empty(), "{:#?}", unit.errors);
}

#[test]
fn deprecated_attribute_with_lambda_allowed() {
let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Foo {
@Deprecated(() => {})
operation Bar() : Unit {}
}
"}
.into(),
)],
None,
);
let store = PackageStore::new(super::core());
let unit = compile(&store, &[], sources, TargetProfile::Full);
assert!(unit.errors.is_empty(), "{:#?}", unit.errors);
}

#[test]
fn deprecated_attribute_call_within_unit_allowed() {
let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Foo {
@Deprecated(() => {})
operation Bar() : Unit {}
operation Baz() : Unit {
Bar();
}
}
"}
.into(),
)],
None,
);
let store = PackageStore::new(super::core());
let unit = compile(&store, &[], sources, TargetProfile::Full);
assert!(unit.errors.is_empty(), "{:#?}", unit.errors);
}

#[test]
fn deprecated_attribute_with_non_callable_expr_error() {
let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Foo {
@Deprecated(1)
operation Bar() : Unit {}
}
"}
.into(),
)],
None,
);
let store = PackageStore::new(super::core());
let unit = compile(&store, &[], sources, TargetProfile::Full);
expect![[r#"
[
Error(
Lower(
InvalidAttrArgs(
"callable expression or ()",
Span {
lo: 31,
hi: 34,
},
),
),
),
]
"#]]
.assert_debug_eq(&unit.errors);
}

#[test]
fn deprecated_attribute_avoids_ambiguous_error_with_duplicate_names_in_scope() {
let lib_sources = SourceMap::new(
[(
"lib".into(),
indoc! {"
namespace Foo {
@Deprecated()
operation Bar() : Unit {}
}
"}
.into(),
)],
None,
);
let mut store = PackageStore::new(super::core());
let lib = compile(&store, &[], lib_sources, TargetProfile::Full);
assert!(lib.errors.is_empty(), "{:#?}", lib.errors);
let lib = store.insert(lib);

let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Dependency {
operation Bar() : Unit {}
}
namespace Test {
open Foo;
open Dependency;
operation Main() : Unit {
Bar();
}
}
"}
.into(),
)],
None,
);
let unit = compile(&store, &[lib], sources, TargetProfile::Full);
expect![[r#"
[]
"#]]
.assert_debug_eq(&unit.errors);
}

#[test]
fn unimplemented_call_from_dependency_produces_error() {
let lib_sources = SourceMap::new(
[(
"lib".into(),
indoc! {"
namespace Foo {
@Unimplemented()
operation Bar() : Unit {}
}
"}
.into(),
)],
None,
);
let mut store = PackageStore::new(super::core());
let lib = compile(&store, &[], lib_sources, TargetProfile::Full);
assert!(lib.errors.is_empty(), "{:#?}", lib.errors);
let lib = store.insert(lib);

let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Test {
open Foo;
operation Main() : Unit {
Bar();
}
}
"}
.into(),
)],
None,
);
let unit = compile(&store, &[lib], sources, TargetProfile::Full);
expect![[r#"
[
Error(
Resolve(
Unimplemented(
"Bar",
Span {
lo: 69,
hi: 72,
},
),
),
),
]
"#]]
.assert_debug_eq(&unit.errors);
}

#[test]
fn unimplemented_attribute_call_within_unit_allowed() {
let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Foo {
@Unimplemented()
operation Bar() : Unit {}
operation Baz() : Unit {
Bar();
}
}
"}
.into(),
)],
None,
);
let store = PackageStore::new(super::core());
let unit = compile(&store, &[], sources, TargetProfile::Full);
assert!(unit.errors.is_empty(), "{:#?}", unit.errors);
}

#[test]
fn unimplemented_attribute_with_non_unit_expr_error() {
let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Foo {
@Unimplemented(1)
operation Bar() : Unit {}
}
"}
.into(),
)],
None,
);
let store = PackageStore::new(super::core());
let unit = compile(&store, &[], sources, TargetProfile::Full);
expect![[r#"
[
Error(
Lower(
InvalidAttrArgs(
"()",
Span {
lo: 34,
hi: 37,
},
),
),
),
]
"#]]
.assert_debug_eq(&unit.errors);
}

#[test]
fn unimplemented_attribute_avoids_ambiguous_error_with_duplicate_names_in_scope() {
let lib_sources = SourceMap::new(
[(
"lib".into(),
indoc! {"
namespace Foo {
@Unimplemented()
operation Bar() : Unit {}
}
"}
.into(),
)],
None,
);
let mut store = PackageStore::new(super::core());
let lib = compile(&store, &[], lib_sources, TargetProfile::Full);
assert!(lib.errors.is_empty(), "{:#?}", lib.errors);
let lib = store.insert(lib);

let sources = SourceMap::new(
[(
"test".into(),
indoc! {"
namespace Dependency {
operation Bar() : Unit {}
}
namespace Test {
open Foo;
open Dependency;
operation Main() : Unit {
Bar();
}
}
"}
.into(),
)],
None,
);
let unit = compile(&store, &[lib], sources, TargetProfile::Full);
expect![[r#"
[]
"#]]
.assert_debug_eq(&unit.errors);
}

0 comments on commit 51c4514

Please sign in to comment.