Comparing version 1.4.0 to 1.5.0
@@ -44,2 +44,4 @@ 'use strict' | ||
flatValue = function (value, prefix) { | ||
var t1, t16, t18 | ||
if (Array.isArray(value)) { | ||
@@ -57,4 +59,45 @@ // Subarray | ||
}) | ||
} else if (value === Number) { | ||
// Since there are 3 BSON types for a number, we need a hack here | ||
// use $and with $or for each of those types | ||
if (!('$and' in r)) { | ||
r.$and = [] | ||
} | ||
prefix = prefix.substr(0, prefix.length - 1) | ||
t1 = {} | ||
t16 = {} | ||
t18 = {} | ||
t1[prefix] = { | ||
$type: 1 | ||
} | ||
t16[prefix] = { | ||
$type: 16 | ||
} | ||
t18[prefix] = { | ||
$type: 18 | ||
} | ||
r.$and.push({ | ||
$or: [t1, t16, t18] | ||
}) | ||
} else { | ||
// Simple value | ||
if (value === String) { | ||
value = { | ||
$type: 2 | ||
} | ||
} else if (value === Boolean) { | ||
value = { | ||
$type: 8 | ||
} | ||
} else if (value === Date) { | ||
value = { | ||
$type: 9 | ||
} | ||
} else if (value === RegExp) { | ||
value = { | ||
$type: 11 | ||
} | ||
} | ||
r[prefix.substr(0, prefix.length - 1)] = value | ||
@@ -61,0 +104,0 @@ } |
@@ -53,3 +53,5 @@ 'use strict' | ||
Object.keys(additions).forEach(function (path) { | ||
add(base, additions[path], path.split('.')) | ||
add(base, additions[path], path.split('.').map(function (each) { | ||
return /^[0-9]+$/.test(each) ? Number(each) : each | ||
})) | ||
}, this) | ||
@@ -56,0 +58,0 @@ } |
@@ -41,5 +41,5 @@ 'use strict' | ||
this.source.begin++ | ||
return | ||
} else { | ||
this.lines.push(line) | ||
} | ||
this.lines.push(line) | ||
this.source.end++ | ||
@@ -133,3 +133,3 @@ } | ||
if (line[1] !== '\t') { | ||
throw new ParseError('Expected a "\t" after "*"', this) | ||
throw new ParseError('Expected a "\\t" after "*"', this) | ||
} | ||
@@ -145,3 +145,3 @@ if (obj) { | ||
} else { | ||
throw new ParseError('Expected either a "*" or "\t"', this) | ||
throw new ParseError('Expected either a "*" or "\\t"', this) | ||
} | ||
@@ -159,4 +159,6 @@ } | ||
*/ | ||
Obj.prototype._parseObject = function (acceptPath) { | ||
var i, line, obj, match, key, regex | ||
var that = this, | ||
i, line, obj, match, key, regex | ||
@@ -170,4 +172,13 @@ regex = acceptPath ? /^((\d+|[a-z$_][a-z0-9$_]*)(\.(\d+|[a-z$_][a-z0-9$_]*))*):/i : /^([a-z$_][a-z0-9$_]*):/i | ||
this.value = Object.create(null) | ||
var save = function (key, obj) { | ||
if (obj) { | ||
if (key in that.value) { | ||
throw new ParseError('Duplicate key "' + key + '"', that) | ||
} | ||
that.value[key] = obj.parse() | ||
} | ||
} | ||
// Split each object key element | ||
this.value = Object.create(null) | ||
for (i = 0; i < this.lines.length; i++) { | ||
@@ -177,5 +188,3 @@ line = this.lines[i] | ||
// A new key | ||
if (obj) { | ||
this.value[key] = obj.parse() | ||
} | ||
save(key, obj) | ||
key = match[1] | ||
@@ -188,6 +197,6 @@ obj = new Obj(this.source.begin + i) | ||
} else { | ||
throw new ParseError('Expected either "_key_:" or "\t"', this) | ||
throw new ParseError('Expected either "_key_:" or "\\t"', this) | ||
} | ||
} | ||
this.value[key] = obj.parse() | ||
save(key, obj) | ||
return true | ||
@@ -230,3 +239,3 @@ } | ||
} else { | ||
throw new ParseError('Expected either ";" or "," after path ' + path.name, this) | ||
throw new ParseError('Expected either ";" or "," after path "' + path.name + '"', this) | ||
} | ||
@@ -246,3 +255,3 @@ } | ||
} else { | ||
throw new ParseError('Expected the line to start with "\t"', this) | ||
throw new ParseError('Expected the line to start with "\\t"', this) | ||
} | ||
@@ -249,0 +258,0 @@ } |
@@ -22,10 +22,10 @@ 'use strict' | ||
/** | ||
* Populate the error message with the original code region that caused the error | ||
* Log the original code region that caused the error into the console | ||
* @param {string[]} originalLines | ||
*/ | ||
ParseError.prototype.addSourceContext = function (originalLines) { | ||
ParseError.prototype.logSourceContext = function (originalLines) { | ||
var start = Infinity, | ||
end = -Infinity, | ||
str = '\n\n-----', | ||
i, focus, checkElFocus | ||
i, focus, checkElFocus, lineNum | ||
@@ -46,9 +46,14 @@ if (!this.els.length) { | ||
focus = this.els.some(checkElFocus) | ||
str += '\n' + (focus ? '>' : ' ') + ' ' + originalLines[i] | ||
lineNum = String(i + 1) | ||
while (lineNum.length < 3) { | ||
lineNum = ' ' + lineNum | ||
} | ||
str += '\n\x1b[32m' + lineNum + '\x1b[0m ' | ||
str += (focus ? '\x1b[31;1m>' : ' ') + ' ' + originalLines[i] + (focus ? '\x1b[0m' : '') | ||
} | ||
str += '\n-----' | ||
this.message += str | ||
console.log(str) | ||
} | ||
module.exports = ParseError |
{ | ||
"name": "api-test", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"author": "Sitegui <sitegui@sitegui.com.br>", | ||
@@ -5,0 +5,0 @@ "description": "API testing made simple", |
@@ -61,3 +61,3 @@ /** | ||
if (e instanceof ParseError) { | ||
e.addSourceContext(originalLines) | ||
e.logSourceContext(originalLines) | ||
} | ||
@@ -64,0 +64,0 @@ throw e |
@@ -143,2 +143,20 @@ # API Test | ||
## Type checking | ||
If you don't know the exact value for an API response or field in the collection, you can check for its type: | ||
``` | ||
## Testing types | ||
### Post | ||
... | ||
### Out | ||
token: String | ||
### Find in users | ||
password: String | ||
lastLogin: Date | ||
``` | ||
The valid values for `### Out` are: `String`, `Number`, `Boolean`, `Object`, `Array`. | ||
For `### Find in _coll_` are: `Number`, `String`, `Boolean`, `Date`, `RegExp` | ||
## Custom context | ||
@@ -185,3 +203,2 @@ You can use custom context to help writing tests. All default context variables and methods will still be accessible (unless overwritten). | ||
## TODO | ||
* Make keys less restrictive | ||
* Make type check in finds (like `{name: String}`) (covert to `$type` operator) | ||
* Make keys less restrictive |
@@ -38,2 +38,5 @@ # user/signup | ||
user with token: out.token | ||
### Find in users | ||
name: String | ||
password: String | ||
@@ -40,0 +43,0 @@ ## Using the same username again |
@@ -6,2 +6,3 @@ /*globals describe, it*/ | ||
should = require('should'), | ||
ParseError = require('../classes/ParseError'), | ||
context = { | ||
@@ -185,2 +186,15 @@ user: { | ||
}) | ||
it('should work for mixins with array', function () { | ||
check(['order with items.0.name: "c"'], { | ||
items: [{ | ||
name: 'c', | ||
price: 60 | ||
}, { | ||
name: 'b', | ||
price: 63 | ||
}], | ||
price: 123 | ||
}) | ||
}) | ||
}) | ||
@@ -193,3 +207,11 @@ | ||
}) | ||
should(obj.parse().execute(context, '<>')).be.eql(value) | ||
try { | ||
obj.parse() | ||
} catch (e) { | ||
if (e instanceof ParseError) { | ||
e.logSourceContext(lines) | ||
} | ||
throw e | ||
} | ||
should(obj.execute(context, '<>')).be.eql(value) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
0
202
53047
28
1557