Skip to content

Commit

Permalink
diffDeepEquals - Make protobuf differences more clear
Browse files Browse the repository at this point in the history
This helper uses `json-diff` to provide a unified diff style output
when two objects don't match.  This is way faster than manually diff a
large nested object.
  • Loading branch information
jlisee committed Jun 2, 2019
1 parent ddc6d1b commit 409fae7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -35,6 +35,7 @@
"clone": "^2.1.2",
"coveralls": "^3.0.0",
"cross-spawn": "^6.0.5",
"json-diff": "^0.5.4",
"jsonlint": "^1.6.3",
"ocular-dev-tools": "0.0.22",
"pre-commit": "^1.2.2",
Expand Down
3 changes: 2 additions & 1 deletion test/modules/schema/proto-validation.spec.js
Expand Up @@ -16,6 +16,7 @@
import {loadProtos} from '@xviz/schema';
import {XVIZValidator, protoEnumsToInts, getXVIZProtoTypes, EXTENSION_PROPERTY} from '@xviz/schema';

import {diffDeepEquals} from '../../util/diff-deep-equals';
import {loadJSON} from '../../../scripts/file-utils';
import EXAMPLES from './examples.json';

Expand Down Expand Up @@ -138,7 +139,7 @@ function validateAgainstExample(t, validator, protoType, examplePath, jsonExampl

// Now lets make sure we handled all fields
if (!exampleHasExtraFields(examplePath)) {
t.deepEquals(originalJsonExample, fromProtoObject, `Full round trip equivalent`);
diffDeepEquals(t, originalJsonExample, fromProtoObject, `Full round trip equivalent`);
}
}

Expand Down
33 changes: 33 additions & 0 deletions test/util/diff-deep-equals.js
@@ -0,0 +1,33 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import {default as deepEquals} from 'deep-equal';
import {diffString} from 'json-diff';

/**
* Run a deep comparison on the given objects, returning a clear
* unified diff style difference. Helps highlight small changes in
* large, nested, objects.
* @param {Tape} t Tape test object
* @param {Object} a First object
* @param {Object} b Second object, compared to the first
*/
export function diffDeepEquals(t, a, b, msg) {
if (deepEquals(a, b, msg)) {
t.pass(msg);
} else {
const diff = diffString(a, b);
t.fail(`Objects not equal: ${msg}: ${diff}`);
}
}
50 changes: 45 additions & 5 deletions yarn.lock
Expand Up @@ -3023,6 +3023,13 @@ clean-css@4.2.x:
dependencies:
source-map "~0.6.0"

cli-color@~0.1.6:
version "0.1.7"
resolved "https://registry.yarnpkg.com/cli-color/-/cli-color-0.1.7.tgz#adc3200fa471cc211b0da7f566b71e98b9d67347"
integrity sha1-rcMgD6RxzCEbDaf1ZrcemLnWc0c=
dependencies:
es5-ext "0.8.x"

cli-cursor@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-2.1.0.tgz#b35dac376479facc3e94747d41d0d0f5238ffcb5"
Expand Down Expand Up @@ -3874,6 +3881,13 @@ diffie-hellman@^5.0.0:
miller-rabin "^4.0.0"
randombytes "^2.0.0"

difflib@~0.2.1:
version "0.2.4"
resolved "https://registry.yarnpkg.com/difflib/-/difflib-0.2.4.tgz#b5e30361a6db023176d562892db85940a718f47e"
integrity sha1-teMDYabbAjF21WKJLbhZQKcY9H4=
dependencies:
heap ">= 0.2.0"

dns-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/dns-equal/-/dns-equal-1.0.0.tgz#b39e7f1da6eb0a75ba9c17324b34753c47e0654d"
Expand Down Expand Up @@ -3966,6 +3980,13 @@ draco3d@^1.3.4:
resolved "https://registry.yarnpkg.com/draco3d/-/draco3d-1.3.4.tgz#b17e4fedd4b4b946c9eb8df2e1d2f470a953cdb8"
integrity sha512-I7kAKyiSIb++K5VdWGeIC9xXWkoQ1HZ8wSCQhaRMf41KKxYX5jYmy+rRna/o7o3CrunxvCGbZBl6d6bdWVcJXw==

dreamopt@~0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/dreamopt/-/dreamopt-0.6.0.tgz#d813ccdac8d39d8ad526775514a13dda664d6b4b"
integrity sha1-2BPM2sjTnYrVJndVFKE92mZNa0s=
dependencies:
wordwrap ">=0.0.2"

duplexer3@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2"
Expand Down Expand Up @@ -4103,6 +4124,11 @@ es-to-primitive@^1.2.0:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

es5-ext@0.8.x:
version "0.8.2"
resolved "https://registry.yarnpkg.com/es5-ext/-/es5-ext-0.8.2.tgz#aba8d9e1943a895ac96837a62a39b3f55ecd94ab"
integrity sha1-q6jZ4ZQ6iVrJaDemKjmz9V7NlKs=

es6-error@^4.0.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/es6-error/-/es6-error-4.1.1.tgz#9e3af407459deed47e9a91f9b885a84eb05c561d"
Expand Down Expand Up @@ -5112,6 +5138,11 @@ he@1.2.x:
resolved "https://registry.yarnpkg.com/he/-/he-1.2.0.tgz#84ae65fa7eafb165fddb61566ae14baf05664f0f"
integrity sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==

"heap@>= 0.2.0":
version "0.2.6"
resolved "https://registry.yarnpkg.com/heap/-/heap-0.2.6.tgz#087e1f10b046932fc8594dd9e6d378afc9d1e5ac"
integrity sha1-CH4fELBGky/IWU3Z5tN4r8nR5aw=

hmac-drbg@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1"
Expand Down Expand Up @@ -5808,6 +5839,15 @@ jsesc@~0.5.0:
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-0.5.0.tgz#e7dee66e35d6fc16f710fe91d5cf69f70f08911d"
integrity sha1-597mbjXW/Bb3EP6R1c9p9w8IkR0=

json-diff@^0.5.4:
version "0.5.4"
resolved "https://registry.yarnpkg.com/json-diff/-/json-diff-0.5.4.tgz#7bc8198c441756632aab66c7d9189d365a7a035a"
integrity sha512-q5Xmx9QXNOzOzIlMoYtLrLiu4Jl/Ce2bn0CNcv54PhyH89CI4GWlGVDye8ei2Ijt9R3U+vsWPsXpLUNob8bs8Q==
dependencies:
cli-color "~0.1.6"
difflib "~0.2.1"
dreamopt "~0.6.0"

json-parse-better-errors@^1.0.1, json-parse-better-errors@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9"
Expand Down Expand Up @@ -9432,16 +9472,16 @@ wide-align@^1.1.0:
dependencies:
string-width "^1.0.2 || 2"

wordwrap@>=0.0.2, wordwrap@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus=

wordwrap@~0.0.2:
version "0.0.3"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"
integrity sha1-o9XabNXAvAAI03I0u68b7WMFkQc=

wordwrap@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
integrity sha1-J1hIEIkUVqQXHI0CJkQa3pDLyus=

worker-farm@^1.5.2:
version "1.6.0"
resolved "https://registry.yarnpkg.com/worker-farm/-/worker-farm-1.6.0.tgz#aecc405976fab5a95526180846f0dba288f3a4a0"
Expand Down

0 comments on commit 409fae7

Please sign in to comment.