Skip to content

Commit

Permalink
code coverage, more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Brady <adam@boxxen.org>
  • Loading branch information
SomeoneWeird committed Jan 10, 2015
1 parent bbbf3e1 commit 5e009ea
Show file tree
Hide file tree
Showing 17 changed files with 277 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ node_js:
- "0.10"
before_install:
- "npm install"
after_success:
- "npm run coverage"
- "npm run push-coverage"
notifications:
irc: "chat.freenode.net#htjs"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ht-schema
=========

[![Build Status](https://travis-ci.org/hudson-taylor/ht-schema.svg?branch=master)](https://travis-ci.org/hudson-taylor/ht-schema)
[![Coverage Status](https://img.shields.io/coveralls/hudson-taylor/ht-schema.svg)](https://coveralls.io/r/hudson-taylor/ht-schema)

Every API function in HT requires a schema that defines the kind of data that
function takes. While this may seem laborious at first, HT schemas are very
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"test": "test"
},
"scripts": {
"test": "mocha -R spec"
"test": "mocha --recursive --reporter spec --bail --check-leaks",
"coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- --ui bdd --recursive -R spec -t 5000",
"push-coverage": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -39,6 +41,8 @@
"isemail": "^1.1.1"
},
"devDependencies": {
"coveralls": "^2.11.2",
"istanbul": "^0.3.5",
"mocha": "^2.1.0"
}
}
12 changes: 12 additions & 0 deletions test/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ var s = require("../index");

describe("Any validator", function() {

it("should require value", function() {
var schema = s.Any();
assert.throws(function() {
schema.validate();
});
});

it("should allow optional value", function() {
var schema = s.Any({ opt: true });
schema.validate();
});

it("should allow any value", function() {
var schema = s.Array([ s.Any() ]);
schema.validate([ true, 5, "hello" ]);
Expand Down
7 changes: 7 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ describe("Array validator", function() {
var mixedArray = [ 1, 2, 3, "bananna" ];
var messyArray = [ 1, 2, 3, "bananna", { name: "cat", colour: "brown" } ];

it("should require value", function() {
var schema = s.Array([ s.Any() ]);
assert.throws(function() {
schema.validate();
});
});

it("should accept a valid simple Array", function() {
var schema = s.Array([s.Number()]);
assert.deepEqual(schema.validate(numArray), numArray);
Expand Down
81 changes: 67 additions & 14 deletions test/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,84 @@ var s = require("../index");

describe("Boolean validator", function() {

it("should allow optional value", function() {
it("should require value", function() {

var schema = s.Boolean();

assert.throws(function() {
schema.validate();
});

});

it("should allow optional value if opt is set", function() {

var schema = s.Boolean({ opt: true });

assert.doesNotThrow(function() {
schema.validate();
schema.validate(undefined);
});

});

it("should accept a valid Bool", function() {
var schema = s.Boolean();
assert.equal(schema.validate(true), true);
it("should coerce given value if coerce is set", function() {

var schema = s.Boolean({ coerce: true });

var result = schema.validate("hello");

assert.equal(result, true);

});

it("should reject non-boolean", function() {
it("should throw if value given is not boolean", function() {

var schema = s.Boolean();
[ 42, "hello", /world/ ].forEach(function(s) {
assert.throws(function() {
schema.validate(s);
});

assert.throws(function() {
schema.validate(42);
});

});

it("should coerce values with option set", function() {
var schema = s.Boolean({ coerce: true });
assert.equal(schema.validate("hello"), true);
assert.equal(schema.validate(""), false);
it("should validate correctly", function() {

var schema = s.Boolean();

assert.doesNotThrow(function() {

schema.validate(true);
schema.validate(false);

});

});

// it("should allow optional value", function() {
// var schema = s.Boolean({ opt: true });
// assert.doesNotThrow(function() {
// schema.validate();
// });
// });

// it("should accept a valid Bool", function() {
// var schema = s.Boolean();
// assert.equal(schema.validate(true), true);
// });

// it("should reject non-boolean", function() {
// var schema = s.Boolean();
// [ 42, "hello", /world/ ].forEach(function(s) {
// assert.throws(function() {
// schema.validate(s);
// });
// });
// });

// it("should coerce values with option set", function() {
// var schema = s.Boolean({ coerce: true });
// assert.equal(schema.validate("hello"), true);
// assert.equal(schema.validate(""), false);
// });

});
20 changes: 20 additions & 0 deletions test/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ describe("Date validator", function() {
var now = new Date();
var notADate = { dinosaur: "rawwwr" };

it("should require value", function() {

var schema = s.Date();

assert.throws(function() {
schema.validate();
});

});

it("should allow optional value if opt is set", function() {

var schema = s.Date({ opt: true });

assert.doesNotThrow(function() {
schema.validate(undefined);
});

});

it("should accept a valid Date", function() {
var schema = s.Date();
assert.equal(schema.validate(old).getTime(), old.getTime());
Expand Down
42 changes: 42 additions & 0 deletions test/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ var s = require("../index");

describe("Email validator", function() {

it("should require value", function() {

var schema = s.Email();

assert.throws(function() {
schema.validate();
});

});

it("should allow optional value if opt is set", function() {

var schema = s.Email({ opt: true });

assert.doesNotThrow(function() {
schema.validate(undefined);
});

});

it("should require string", function() {

var schema = s.Email();

assert.throws(function() {
schema.validate(true);
});

});

it("should accept valid email addresses", function() {
var schema = s.Email();
var valid = [
Expand All @@ -28,4 +58,16 @@ describe("Email validator", function() {
});
});

it("should normalize email if option is set", function() {

var schema = s.Email({ normalize: true });

var email = "TesT@heLLo.CoM";

var result = schema.validate(email);

assert.equal(result, email.toLowerCase());

});

});
14 changes: 14 additions & 0 deletions test/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ describe("Number validator", function() {
var stringNumber = "123";
var notANumber = {};

it("should require value", function() {
var schema = s.Number();
assert.throws(function() {
schema.validate();
});
});

it("should allow optional value if opt is set", function() {
var schema = s.Number({ opt: true });
assert.doesNotThrow(function() {
schema.validate();
});
});

it("should accept a valid Number", function() {
var schema = s.Number();
assert.equal(schema.validate(small), small);
Expand Down
21 changes: 21 additions & 0 deletions test/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,27 @@ describe("Object validator", function() {
var catOwner = { name: "Bea", cat: ceilingCat };
var basementCat = { name: "Penny" };

it("should require value", function() {
var schema = s.Object();
assert.throws(function() {
schema.validate();
});
});

it("should allow optional value if opt is set", function() {
var schema = s.Object({ opt: true });
assert.doesNotThrow(function() {
schema.validate();
});
});

it("should require object as value", function() {
var schema = s.Object();
assert.throws(function() {
schema.validate("notAnObject");
});
});

it("should accept a strictly valid object", function() {
var catSchema = s.Object({
name: s.String(),
Expand Down
9 changes: 9 additions & 0 deletions test/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ describe("String validator", function() {
schema.validate("abcdef");
});

it("should throw if regex is invalid", function() {
var schema = s.String({
regex: "+"
});
assert.throws(function() {
schema.validate("string");
});
});

it("should match a regex", function() {
var schema = s.String({
regex: /h.+d/
Expand Down
16 changes: 12 additions & 4 deletions validators/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
var DELETEKEY = require("../lib/deleteKey");

function anyParser(args, childValidators, data) {
if(data === undefined && args.opt) return DELETEKEY;
return data;

if(data === undefined) {
if(args.opt) {
return DELETEKEY;
}
throw new Error("required Any value");
}

return data;

}

module.exports = {
name: "Any",
fn: anyParser
name: "Any",
fn: anyParser
};
17 changes: 11 additions & 6 deletions validators/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ var DELETEKEY = require("../lib/deleteKey");
function arrayParser(args, childValidators, data, key) {
childValidators = childValidators || [];
var out = [];
if(!data instanceof Array && !args.opt) throw new Error("required Array");
if(!data && args.opt) return DELETEKEY;
for(var i=0; i < data.length; i++) {
var val = data[i];

if(!Array.isArray(data)) {
throw new Error("required Array");
}

for(var i = 0; i < data.length; i++) {

var val = data[i];
var matched = false;
for(var v=0; v < childValidators.length; v++) {

for(var v = 0; v < childValidators.length; v++) {
if(!matched) {
try {
out.push(childValidators[v].parse(val, key + "[" + i + "]"));
Expand All @@ -33,4 +38,4 @@ function arrayParser(args, childValidators, data, key) {
module.exports = {
name: "Array",
fn: arrayParser
};
};
17 changes: 10 additions & 7 deletions validators/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
var DELETEKEY = require("../lib/deleteKey");

function boolParser(args, childValidators, data) {
if(data === null && !args.opt) throw new Error("required Boolean");
if(data === null || data === undefined && args.opt) return DELETEKEY;

if(args.coerce) {
return Boolean(data);
if(data === undefined) {
if(args.opt) {
return DELETEKEY;
}
throw new Error("required Boolean");
}

var type = typeof(data);
if(args.coerce) {
return Boolean(data);
}

if(type != "boolean") {
throw new Error("Expected boolean, got: " + type);
if(typeof data != "boolean") {
throw new Error("expected Boolean, got:" + typeof data);
}

return data;
Expand Down
Loading

0 comments on commit 5e009ea

Please sign in to comment.