Skip to content

Commit

Permalink
Add test cases that better indicate null behavior
Browse files Browse the repository at this point in the history
Signed-off-by: Luc Perkins <[email protected]>
  • Loading branch information
Luc Perkins committed Dec 29, 2020
1 parent 09f7aec commit a7fe062
Showing 1 changed file with 46 additions and 16 deletions.
62 changes: 46 additions & 16 deletions lib/remap-functions/src/del.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ pub struct DelFn {
field: Path,
}

impl DelFn {
#[cfg(test)]
fn new(field: Path) -> Self {
Self { field }
}
}

impl Expression for DelFn {
fn execute(&self, _: &mut state::Program, object: &mut dyn Object) -> Result<Value> {
match object.remove(self.field.as_ref(), false) {
Expand All @@ -50,24 +57,47 @@ impl Expression for DelFn {
#[cfg(test)]
mod tests {
use super::*;
use crate::map;

test_type_def![static_type_def {
expr: |_| DelFn {
field: Path::from("foo"),
},
def: TypeDef {
fallible: true,
kind: value::Kind::Null,
..Default::default()
},
}];
test_type_def![
static_type_def {
expr: |_| DelFn {
field: Path::from("foo"),
},
def: TypeDef {
fallible: true,
kind: value::Kind::Null,
..Default::default()
},
}
];

#[test]
fn del() {
let cases = vec![
(
// Delete existing field
map!["exists": "value"],
Ok(Value::Null),
DelFn::new(Path::from("exists")),
),
(
// Delete non-existing field
map!["exists": "value"],
Ok(Value::Null),
DelFn::new(Path::from("does_not_exist")),
)
];

let mut state = state::Program::default();

test_function![
del => Del;
for (object, exp, func) in cases {
let mut object: Value = object.into();
let got = func
.execute(&mut state, &mut object)
.map_err(|e| format!("{:#}", anyhow::anyhow!(e)));

array {
args: func_args![field: Path::from("foo")],
want: Ok(value!(null)),
assert_eq!(got, exp);
}
];
}
}

0 comments on commit a7fe062

Please sign in to comment.