Skip to content

Commit

Permalink
add tests for built-in function str
Browse files Browse the repository at this point in the history
  • Loading branch information
binoyjayan committed Sep 17, 2023
1 parent bb9d4a0 commit 9a1fe18
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/common/object.rs
Expand Up @@ -265,10 +265,10 @@ impl fmt::Display for HMap {
let pairs_str = self
.pairs
.iter()
.map(|(k, v)| format!("{}: {}, ", k, v))
.map(|(k, v)| format!(r#""{}": {}, "#, k, v))
.collect::<String>();
let pairs_str = pairs_str.trim_end_matches(|c| c == ' ' || c == ',');
write!(f, "{{ {} }}", pairs_str)
write!(f, "{{{}}}", pairs_str)
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/vm/tests.rs
Expand Up @@ -828,6 +828,39 @@ fn test_builtin_functions() {
"#,
expected: Object::Number(2.0),
},
VmTestCase {
// rest([]) is Nil
input: "str(rest([]))",
expected: Object::Str(String::from("nil")),
},
VmTestCase {
input: r#"str("hello")"#,
expected: Object::Str(String::from("hello")),
},
VmTestCase {
input: "str(999)",
expected: Object::Str(String::from("999")),
},
VmTestCase {
input: "str(str(999))",
expected: Object::Str(String::from("999")),
},
VmTestCase {
input: "str(true)",
expected: Object::Str(String::from("true")),
},
VmTestCase {
input: "str([1, 2, 3333, 4, 5])",
expected: Object::Str(String::from("[1, 2, 3333, 4, 5]")),
},
VmTestCase {
input: r#"str({"a": 1})"#,
expected: Object::Str(String::from(r#"{"a": 1}"#)),
},
VmTestCase {
input: "str({})",
expected: Object::Str(String::from("{}")),
},
];
run_vm_tests(&tests);
}
Expand Down Expand Up @@ -855,6 +888,10 @@ fn test_builtin_function_failures() {
input: r#"push(1, 1)"#,
expected: "argument to 'push' not supported",
},
VmTestCaseErr {
input: "str(fn() {})",
expected: "argument to 'str' not supported",
},
];

run_vm_negative_tests(&tests);
Expand Down

0 comments on commit 9a1fe18

Please sign in to comment.