Skip to content

Commit

Permalink
Primer test para ordered (objects)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoefe committed Oct 31, 2016
1 parent dd00f29 commit e9b0600
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
37 changes: 28 additions & 9 deletions lib/discrepances.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var bestGlobals = require('best-globals');
var datetime = bestGlobals.datetime;
var timeInterval = bestGlobals.timeInterval;
var constructorName = bestGlobals.constructorName;
var changing = bestGlobals.changing;

function getType(variable) {
if(null === variable) { return 'null'; }
Expand All @@ -49,8 +50,6 @@ function compare(resultArray, a, b, index) {
resultArray.push(diff || null);
}

function setIfDiff(obj, key, val) { if(val) { obj[key] = val; } }

function timeStr(dt) { return datetime(dt).toYmdHmsM().substr(11); }

function compareStrings(a,b) {
Expand Down Expand Up @@ -120,16 +119,36 @@ function compareDates(a, b) {
return {difference:res.join(''), values:[a,b]};
}

function setIfDiff(obj, key, val) { if(val) { obj[key] = val; } }

// function compareOrdered(aKeys, )
function compareObjects(a, b, opts) {
var rv = {};
var aKeys = Object.keys(a);
var bKeys = Object.keys(b);
aKeys.forEach(function(key) {
setIfDiff(rv, key, !(key in b) ? {onlyLeft:a[key]} : discrepances(a[key], b[key]));
});
bKeys.forEach(function(key) {
setIfDiff(rv, key, !(key in a) ? {onlyRight:b[key]} : discrepances(a[key], b[key]));
});
if(opts.unordered) {
aKeys.forEach(function(key) {
setIfDiff(rv, key, !(key in b) ? {onlyLeft:a[key]} : discrepances(a[key], b[key]));
});
bKeys.forEach(function(key) {
setIfDiff(rv, key, !(key in a) ? {onlyRight:b[key]} : discrepances(a[key], b[key]));
});
} else {
var diffs = [], keys=[], values=[];
aKeys.forEach(function(key,index) {
var bKey = bKeys[index];
if(key !== bKey) {
diffs.push(index);
keys.push([key, bKey]);
values.push(discrepances(a[key], b[bKey]));
}
});
if(diffs.length) {
rv.differences = diffs;
rv.keys = keys;
rv.values = values;
}
}
return Object.keys(rv).length ? {object:rv} : null;
}

Expand All @@ -138,7 +157,7 @@ function isSimpleValue(classType) {
}

discrepances = function discrepances(a, b, opts){
var opts = opts || {};
var opts = changing({unordered:true}, opts||{});
if(a === b){ return null; }
var typeA = getType(a);
var typeB = getType(b);
Expand Down
22 changes: 16 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,23 @@ describe("discrepances", function(){
{a: undefined , b:false , expect:{types:['undefined' , 'boolean'], values:[undefined, false] }},
{a: new Example({uno:1}) , b: new Example({uno:1}) , expect: null },
{a: new Example({uno:1}) , b: {uno:1} , expect: {classes:['Example', 'Object'] } },
{a: new Example({uno:1}) , b: new Example({uno:2}) , expect: {"object":{"uno":discrepances(1,2)}} },
{a: new Example({uno:1}) , b: new Example({uno:2}) , expect: {object:{"uno":discrepances(1,2)}} },
{a: {0:1, length:1} , b: {0:1,1:2,length:2} , expect:{object:{1:{onlyRight:2}, length:discrepances(1,2)}}},
{a: {last:'Simpson', name:'Bart'} , b:{last:'Simpson', name:'Lisa'} , expect:{"object":{"name":discrepances("Bart","Lisa")}} },
{a: {name:'Hommer', last:'Simpson'} , b:{last:'Simpson', name:'Hommer'} , expect:null, opts:{unordered:true}},
//{a: {name:'Hommer', last:'Simpson'} , b:{last:'Simpson', name:'Hommer'} , expect:{differences:} },
{a: {name:'Hommer', age:40} , b:{name:'Hommer'} , expect:{"object":{"age":{"onlyLeft":40}}}},
{a: {name:'Hommer'} , b:{name:'Hommer', age:40} , expect:{"object":{"age":{"onlyRight":40}}}},
{a: {last:'Simpson', name:'Bart'} , b:{last:'Simpson', name:'Lisa'} , expect:{object:{"name":discrepances("Bart","Lisa")}} },
{a: {name:'Hommer', last:'Simpson'} , b:{last:'Simpson', name:'Hommer'} , expect:null},
{ a:{one:'un', two:'dos'},
b:{two:'dos', one:'un'},
expect:{
object:{
differences:[0,1],
keys:[['one','two'] , ['two','one']],
values:[discrepances('un','dos'), discrepances('dos','un')]
}
},
opts:{unordered:false}
},
{a: {name:'Hommer', age:40} , b:{name:'Hommer'} , expect:{object:{"age":{"onlyLeft":40}}}},
{a: {name:'Hommer'} , b:{name:'Hommer', age:40} , expect:{object:{"age":{"onlyRight":40}}}},
];
// esto es para evitar que values:[] tenga fechas distintas a 'a' y 'b'
var dateFixtures = [
Expand Down

0 comments on commit e9b0600

Please sign in to comment.