Minor refactor.

This commit is contained in:
Stephen Chung
2022-03-09 09:25:55 +08:00
parent 89426f8b3a
commit 1e4abd012c
11 changed files with 86 additions and 74 deletions

View File

@@ -9,66 +9,64 @@ fn test_blobs() -> Result<(), Box<EvalAltResult>> {
a.push(3);
let engine = Engine::new();
let mut orig_scope = Scope::new();
orig_scope.push("x", a);
let mut scope = orig_scope.clone();
let mut scope = Scope::new();
scope.push("x", a);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[1]")?, 2);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[0]")?, 1);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[-1]")?, 3);
assert_eq!(engine.eval_with_scope::<INT>(&mut scope, "x[-3]")?, 1);
assert_eq!(
engine.eval_with_scope::<INT>(&mut scope, "x += 4; x[3]")?,
engine.eval_with_scope::<INT>(&mut scope.clone(), "x += 4; x[3]")?,
4
);
#[cfg(not(feature = "no_object"))]
{
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.push(4); x")?,
engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.push(4); x")?,
[1, 2, 3, 4]
);
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.insert(0, 4); x")?,
engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.insert(0, 4); x")?,
[4, 1, 2, 3]
);
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.insert(999, 4); x")?,
engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.insert(999, 4); x")?,
[1, 2, 3, 4]
);
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.insert(-2, 4); x")?,
engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.insert(-2, 4); x")?,
[1, 4, 2, 3]
);
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x.insert(-999, 4); x")?,
engine.eval_with_scope::<Blob>(&mut scope.clone(), "x.insert(-999, 4); x")?,
[4, 1, 2, 3]
);
assert_eq!(
engine.eval_with_scope::<INT>(&mut orig_scope.clone(), "let z = [42]; x[z.len]")?,
engine.eval_with_scope::<INT>(&mut scope.clone(), "let z = [42]; x[z.len]")?,
2
);
assert_eq!(
engine.eval_with_scope::<INT>(&mut orig_scope.clone(), "let z = [2]; x[z[0]]")?,
engine.eval_with_scope::<INT>(&mut scope.clone(), "let z = [2]; x[z[0]]")?,
3
);
}
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x += x; x")?,
engine.eval_with_scope::<Blob>(&mut scope.clone(), "x += x; x")?,
[1, 2, 3, 1, 2, 3]
);
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x + x")?,
engine.eval_with_scope::<Blob>(&mut scope.clone(), "x + x")?,
[1, 2, 3, 1, 2, 3]
);
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x += 999; x")?,
engine.eval_with_scope::<Blob>(&mut scope.clone(), "x += 999; x")?,
[1, 2, 3, 0xe7]
);
assert_eq!(
engine.eval_with_scope::<Blob>(&mut orig_scope.clone(), "x[2] = 999; x")?,
engine.eval_with_scope::<Blob>(&mut scope.clone(), "x[2] = 999; x")?,
[1, 2, 0xe7]
);