Disallow hiding the same def twice; Add tests

Tests got removed after rebase.
This commit is contained in:
Jakub Žádník 2021-10-02 00:12:30 +03:00
parent 891d79d2aa
commit fb0f83e574
2 changed files with 75 additions and 2 deletions

View file

@ -337,8 +337,12 @@ impl<'a> StateWorkingSet<'a> {
}
pub fn hide_decl(&mut self, name: &[u8]) -> Option<DeclId> {
let mut hiding: HashSet<DeclId> = HashSet::new();
// Since we can mutate scope frames in delta, remove the id directly
for scope in self.delta.scope.iter_mut().rev() {
hiding.extend(&scope.hiding);
if let Some(decl_id) = scope.decls.remove(name) {
return Some(decl_id);
}
@ -352,11 +356,16 @@ impl<'a> StateWorkingSet<'a> {
.expect("internal error: missing required scope frame");
for scope in self.permanent_state.scope.iter().rev() {
hiding.extend(&scope.hiding);
if let Some(decl_id) = scope.decls.get(name) {
if !hiding.contains(decl_id) {
// Do not hide already hidden decl
last_scope_frame.hiding.insert(*decl_id);
return Some(*decl_id);
}
}
}
None
}

View file

@ -383,6 +383,70 @@ fn module_imports_5() -> TestResult {
)
}
#[test]
fn module_import_uses_internal_command() -> TestResult {
run_test(
r#"module foo { def b [] { 2 }; export def a [] { b } }; use foo; foo.a"#,
"2",
)
}
#[test]
fn hides_def() -> TestResult {
fail_test(
r#"def foo [] { "foo" }; hide foo; foo"#,
"command not found",
)
}
#[test]
fn hides_def_then_redefines() -> TestResult {
fail_test(
r#"def foo [] { "foo" }; hide foo; def foo [] { "bar" }; foo"#,
"defined more than once",
)
}
#[test]
fn hides_def_in_scope_1() -> TestResult {
fail_test(
r#"def foo [] { "foo" }; do { hide foo; foo }"#,
"command not found",
)
}
#[test]
fn hides_def_in_scope_2() -> TestResult {
run_test(
r#"def foo [] { "foo" }; do { def foo [] { "bar" }; hide foo; foo }"#,
"foo",
)
}
#[test]
fn hides_def_in_scope_3() -> TestResult {
fail_test(
r#"def foo [] { "foo" }; do { hide foo; def foo [] { "bar" }; hide foo; foo }"#,
"command not found",
)
}
#[test]
fn hides_def_in_scope_4() -> TestResult {
fail_test(
r#"def foo [] { "foo" }; do { def foo [] { "bar" }; hide foo; hide foo; foo }"#,
"command not found",
)
}
#[test]
fn hide_twice_not_allowed() -> TestResult {
fail_test(
r#"def foo [] { "foo" }; hide foo; hide foo"#,
"unknown command",
)
}
#[test]
fn from_json_1() -> TestResult {
run_test(r#"('{"name": "Fred"}' | from json).name"#, "Fred")