Skip to content

Commit

Permalink
Implemento discrepances.test para testear condiciones en vez de compa…
Browse files Browse the repository at this point in the history
…rar por igualdad

Close #4
  • Loading branch information
emilioplatzer committed Nov 13, 2016
1 parent 647770e commit 3f82ead
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
12 changes: 12 additions & 0 deletions lib/discrepances.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,21 @@ var defaultOpts = {
autoTypeCast:false
};

function DiscrepancesTester(tester){
this.test = tester;
this.message = bestGlobals.functionName(tester);
}

discrepances = function discrepances(a, b, opts){
opts = changing(defaultOpts, opts||{});
if(a === b){ return null; }
// console.log("D", a, b, opts)
/*eslint-disable eqeqeq */
if(opts.autoTypeCast && a == b){ return null; }
/*eslint-enable eqeqeq */
if(b instanceof DiscrepancesTester){
return b.test(a)?null:{fail: b.message};
}
var typeA = getType(a);
var typeB = getType(b);
if(typeA === typeB) {
Expand Down Expand Up @@ -219,6 +227,10 @@ discrepances = function discrepances(a, b, opts){
}
};

discrepances.test = function discrepancesTest(tester){
return new DiscrepancesTester(tester);
}

return discrepances;

});
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "discrepances",
"description": "show discrepances in several flavors",
"version": "0.0.7",
"version": "0.0.8",
"author": "Codenautas <codenautas@googlegroups.com>",
"repository": "codenautas/discrepances",
"license": "MIT",
Expand All @@ -18,14 +18,14 @@
"main": "lib/discrepances.js",
"files": ["lib"],
"dependencies": {
"best-globals": "~0.5.0"
"best-globals": "~0.5.5"
},
"devDependencies": {
"body-parser": "~1.15.2",
"cookie-parser": "~1.4.3",
"expect.js": "~0.3.1",
"express": "~4.14.0",
"express-session": "~1.14.1",
"express-session": "~1.14.2",
"express-useragent": "~1.0.4",
"istanbul": "~0.4.5",
"karma": "~1.3.0",
Expand All @@ -35,15 +35,15 @@
"karma-expect": "~1.1.3",
"karma-firefox-launcher": "~1.0.0",
"karma-ie-launcher": "~1.0.0",
"karma-mocha": "~1.2.0",
"karma-mocha": "~1.3.0",
"karma-phantomjs-launcher": "~1.0.2",
"karma-safari-launcher": "~1.0.0",
"istanbul-middleware": "~0.2.2",
"mocha": "~3.1.0",
"phantomjs-prebuilt": "~2.1.12",
"mocha": "~3.1.2",
"phantomjs-prebuilt": "~2.1.13",

"require-bro": "~0.1.4",
"json4all": "~0.1.4"
"json4all": "~0.2.2"
},
"engines": {
"node": ">= 0.12"
Expand Down
7 changes: 5 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe("discrepances", function(){
{a:4 , b:5 , expect:{difference:-1 , values:[4, 5]} },
{a:"4" , b:4 , expect:{types:['string', 'number'], values:["4", 4]} },
{a:null , b:0 , expect:{types:['null' , 'number'], values:[null, 0]} },
// Emilio: mira este en el que comente el values. Entiendo que no va
{a:fechaActual , b:/a/ , expect:{classes:['Date', 'RegExp']/*, values:[fechaActual, /a/]*/} },
{a:fechaActual , b:/a/ , expect:{classes:['Date', 'RegExp'], values:[fechaActual, /a/]}
,skip: "los values sí van porque ambos son clases conocidas que no son contenedores" },
{a:[1,2,3,4,5] , b:[1,2,33,4,5,6] , expect:{array:{length:discrepances(5,6), 2:discrepances(3,33)}} },
{a:[1,2,3,4,5] , b:[1,2,3,4,5] , expect:null },
{a:{x:1, y:2} , b:{y:3, z:{zz:3}} ,
Expand Down Expand Up @@ -64,6 +64,7 @@ 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']} },
{skip:'#5', a:new Example({uno:1}) , b:[1,2] , expect:{classes:['Example', 'Array']} },
{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:{length:discrepances(1,2), 1:{onlyRight:2}}}
Expand Down Expand Up @@ -134,6 +135,8 @@ describe("discrepances", function(){
{a:f1 , b:f1 , expect:null },
{a:function(a){} , b:function(a){} , expect:null },
{a:f1 , b:f2 , expect:discrepances(f1.toString(), f2.toString()) },
{a:{a:'a', b:[]}, b:{a:'A', b:discrepances.test(Array.isArray)}, expect:{object:{a:discrepances('a', 'A')}} },
{a:{a:'a', b:'b'}, b:{a:'a', b:discrepances.test(Array.isArray)}, expect:{object:{b:{fail:'isArray'}}} },
];
// esto es para evitar que values:[] tenga fechas distintas a 'a' y 'b'
var dateFixtures = [
Expand Down

0 comments on commit 3f82ead

Please sign in to comment.