Ensure object diffing works correctly when values are the same

This commit is contained in:
Tobie Morgan Hitchcock 2022-01-31 21:17:28 +00:00
parent dc22d4e40e
commit c3cf0e5e24

View file

@ -7,7 +7,7 @@ impl Value {
pub fn diff(&self, val: &Value, path: Idiom) -> Vec<Operation> { pub fn diff(&self, val: &Value, path: Idiom) -> Vec<Operation> {
let mut ops: Vec<Operation> = vec![]; let mut ops: Vec<Operation> = vec![];
match (self, val) { match (self, val) {
(Value::Object(a), Value::Object(b)) => { (Value::Object(a), Value::Object(b)) if a != b => {
// Loop over old keys // Loop over old keys
for (key, _) in a.value.iter() { for (key, _) in a.value.iter() {
if b.value.contains_key(key) == false { if b.value.contains_key(key) == false {
@ -33,7 +33,7 @@ impl Value {
} }
} }
} }
(Value::Array(a), Value::Array(b)) => { (Value::Array(a), Value::Array(b)) if a != b => {
let mut n = 0; let mut n = 0;
while n < min(a.len(), b.len()) { while n < min(a.len(), b.len()) {
let path = path.add(n.into()); let path = path.add(n.into());
@ -61,7 +61,7 @@ impl Value {
n += 1; n += 1;
} }
} }
(Value::Strand(a), Value::Strand(b)) => ops.push(Operation { (Value::Strand(a), Value::Strand(b)) if a != b => ops.push(Operation {
op: Op::Change, op: Op::Change,
path, path,
value: { value: {
@ -89,6 +89,14 @@ mod tests {
use crate::sql::array::Array; use crate::sql::array::Array;
use crate::sql::test::Parse; use crate::sql::test::Parse;
#[test]
fn diff_none() {
let old = Value::parse("{ test: true, text: 'text', other: { something: true } }");
let now = Value::parse("{ test: true, text: 'text', other: { something: true } }");
let res = Array::parse("[]");
assert_eq!(res.to_operations().unwrap(), old.diff(&now, Idiom::default()));
}
#[test] #[test]
fn diff_add() { fn diff_add() {
let old = Value::parse("{ test: true }"); let old = Value::parse("{ test: true }");