Comparing version 1.0.1-beta2 to 1.0.1-beta3
@@ -5,3 +5,3 @@ { | ||
"description": "生成随机数据 & 拦截 Ajax 请求", | ||
"version": "1.0.1-beta2", | ||
"version": "1.0.1-beta3", | ||
"homepage": "http://mockjs.com/", | ||
@@ -8,0 +8,0 @@ "keywords": [ |
@@ -32,3 +32,3 @@ /* global require, module, window */ | ||
Mock.version = '1.0.1-beta2' | ||
Mock.version = '1.0.1-beta3' | ||
@@ -35,0 +35,0 @@ // 避免循环依赖 |
@@ -361,5 +361,10 @@ /* | ||
'regexp': function(options) { | ||
// regexp.source | ||
var source = options.template.source | ||
var source = '' | ||
// 'name': /regexp/, | ||
/* jshint -W041 */ | ||
if (options.rule.count == undefined) { | ||
source += options.template.source // regexp.source | ||
} | ||
// 'name|1-5': /regexp/, | ||
@@ -366,0 +371,0 @@ for (var i = 0; i < options.rule.count; i++) { |
@@ -43,4 +43,4 @@ /* | ||
var decimal = parameters && parameters[4] && parameters[4].match(Constant.RE_RANGE) | ||
var dmin = decimal && parseInt(decimal[1], 10) // || 0, | ||
var dmax = decimal && parseInt(decimal[2], 10) // || 0, | ||
var dmin = decimal && decimal[1] && parseInt(decimal[1], 10) // || 0, | ||
var dmax = decimal && decimal[2] && parseInt(decimal[2], 10) // || 0, | ||
// int || dmin-dmax || 0 | ||
@@ -47,0 +47,0 @@ var dcount = decimal ? !decimal[2] && parseInt(decimal[1], 10) || Random.integer(dmin, dmax) : undefined |
@@ -23,2 +23,3 @@ /* | ||
*/ | ||
var Constant = require('../constant') | ||
var Util = require('../util') | ||
@@ -31,3 +32,4 @@ var toJSONSchema = require('../schema') | ||
for (var i = 0; i < result.length; i++) { | ||
// console.log(Assert.message(result[i])) | ||
// console.log(template, data) | ||
// console.warn(Assert.message(result[i])) | ||
} | ||
@@ -95,4 +97,3 @@ return result | ||
if (result.length !== length) return false | ||
return true | ||
return result.length === length | ||
}, | ||
@@ -102,6 +103,26 @@ type: function(schema, data, name, result) { | ||
switch (schema.type) { | ||
case 'string': | ||
// 跳过含有『占位符』的属性值,因为『占位符』返回值的类型可能和模板不一致,例如 '@int' 会返回一个整形值 | ||
if (schema.template.match(Constant.RE_PLACEHOLDER)) return true | ||
break | ||
case 'array': | ||
if (schema.rule.parameters) { | ||
// name|count: array | ||
if (schema.rule.min !== undefined && schema.rule.max === undefined) { | ||
// 跳过 name|1: array,因为最终值的类型(很可能)不是数组,也不一定与 `array` 中的类型一致 | ||
if (schema.rule.count === 1) return true | ||
} | ||
// 跳过 name|+inc: array | ||
if (schema.rule.parameters[2]) return true | ||
} | ||
break | ||
case 'function': | ||
// 跳过 `'name': function`,因为函数可以返回任何类型的值。 | ||
return true | ||
} | ||
Assert.equal('type', schema.path, Util.type(data), schema.type, result) | ||
if (result.length !== length) return false | ||
return true | ||
return result.length === length | ||
}, | ||
@@ -112,12 +133,22 @@ value: function(schema, data, name, result) { | ||
var rule = schema.rule | ||
var templateType = Util.type(schema.template) | ||
if (templateType === 'object' || templateType === 'array') return | ||
var templateType = schema.type | ||
if (templateType === 'object' || templateType === 'array' || templateType === 'function') return true | ||
// 无生成规则 | ||
if (!schema.rule.parameters) { | ||
if (!rule.parameters) { | ||
switch (templateType) { | ||
case 'regexp': | ||
Assert.match('value', schema.path, data, schema.template, result) | ||
return result.length === length | ||
case 'string': | ||
// 同样跳过含有『占位符』的属性值,因为『占位符』的返回值会通常会与模板不一致 | ||
if (schema.template.match(Constant.RE_PLACEHOLDER)) return result.length === length | ||
break | ||
} | ||
Assert.equal('value', schema.path, data, schema.template, result) | ||
return | ||
return result.length === length | ||
} | ||
// 有生成规则 | ||
var actualRepeatCount | ||
switch (templateType) { | ||
@@ -131,5 +162,5 @@ case 'number': | ||
if (rule.min !== undefined && rule.max !== undefined) { | ||
Assert.greaterThanOrEqualTo('value', schema.path, parts[0], rule.min, result) | ||
Assert.greaterThanOrEqualTo('value', schema.path, parts[0], Math.min(rule.min, rule.max), result) | ||
// , 'numeric instance is lower than the required minimum (minimum: {expected}, found: {actual})') | ||
Assert.lessThanOrEqualTo('value', schema.path, parts[0], rule.max, result) | ||
Assert.lessThanOrEqualTo('value', schema.path, parts[0], Math.max(rule.min, rule.max), result) | ||
} | ||
@@ -158,21 +189,37 @@ // |count | ||
break | ||
case 'string': | ||
// 'aaa'.match(/a/g) | ||
var actualRepeatCount = data.match(new RegExp(schema.template, 'g')) | ||
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount | ||
actualRepeatCount = data.match(new RegExp(schema.template, 'g')) | ||
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0 | ||
// |min-max | ||
if (rule.min !== undefined && rule.max !== undefined) { | ||
Assert.greaterThanOrEqualTo('value', schema.path, actualRepeatCount, rule.min, result) | ||
Assert.lessThanOrEqualTo('value', schema.path, actualRepeatCount, rule.max, result) | ||
Assert.greaterThanOrEqualTo('repeat count', schema.path, actualRepeatCount, rule.min, result) | ||
Assert.lessThanOrEqualTo('repeat count', schema.path, actualRepeatCount, rule.max, result) | ||
} | ||
// |count | ||
if (rule.min !== undefined && rule.max === undefined) { | ||
Assert.equal('value', schema.path, actualRepeatCount, rule.min, result) | ||
Assert.equal('repeat count', schema.path, actualRepeatCount, rule.min, result) | ||
} | ||
break | ||
case 'regexp': | ||
actualRepeatCount = data.match(new RegExp(schema.template.source.replace(/^\^|\$$/g, ''), 'g')) | ||
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0 | ||
// |min-max | ||
if (rule.min !== undefined && rule.max !== undefined) { | ||
Assert.greaterThanOrEqualTo('repeat count', schema.path, actualRepeatCount, rule.min, result) | ||
Assert.lessThanOrEqualTo('repeat count', schema.path, actualRepeatCount, rule.max, result) | ||
} | ||
// |count | ||
if (rule.min !== undefined && rule.max === undefined) { | ||
Assert.equal('repeat count', schema.path, actualRepeatCount, rule.min, result) | ||
} | ||
break | ||
} | ||
if (result.length !== length) return false | ||
return true | ||
return result.length === length | ||
}, | ||
@@ -193,8 +240,9 @@ properties: function(schema, data, name, result) { | ||
if (rule.min !== undefined && rule.max !== undefined) { | ||
Assert.greaterThanOrEqualTo('properties length', schema.path, keys.length, rule.min, result) | ||
Assert.lessThanOrEqualTo('properties length', schema.path, keys.length, rule.max, result) | ||
Assert.greaterThanOrEqualTo('properties length', schema.path, keys.length, Math.min(rule.min, rule.max), result) | ||
Assert.lessThanOrEqualTo('properties length', schema.path, keys.length, Math.max(rule.min, rule.max), result) | ||
} | ||
// |count | ||
if (rule.min !== undefined && rule.max === undefined) { | ||
Assert.equal('properties length', schema.path, keys.length, rule.min, result) | ||
// |1, |>1 | ||
if (rule.count !== 1) Assert.equal('properties length', schema.path, keys.length, rule.min, result) | ||
} | ||
@@ -209,3 +257,9 @@ } | ||
this.diff( | ||
schema.properties[i], | ||
function() { | ||
var property | ||
Util.each(schema.properties, function(item /*, index*/ ) { | ||
if (item.name === keys[i]) property = item | ||
}) | ||
return property || schema.properties[i] | ||
}(), | ||
data[keys[i]], | ||
@@ -217,4 +271,3 @@ keys[i] | ||
if (result.length !== length) return false | ||
return true | ||
return result.length === length | ||
}, | ||
@@ -235,5 +288,5 @@ items: function(schema, data, name, result) { | ||
if (rule.min !== undefined && rule.max !== undefined) { | ||
Assert.greaterThanOrEqualTo('items', schema.path, data.length, (rule.min * schema.items.length), result, | ||
Assert.greaterThanOrEqualTo('items', schema.path, data.length, (Math.min(rule.min, rule.max) * schema.items.length), result, | ||
'[{utype}] array is too short: {path} must have at least {expected} elements but instance has {actual} elements') | ||
Assert.lessThanOrEqualTo('items', schema.path, data.length, (rule.max * schema.items.length), result, | ||
Assert.lessThanOrEqualTo('items', schema.path, data.length, (Math.max(rule.min, rule.max) * schema.items.length), result, | ||
'[{utype}] array is too long: {path} must have at most {expected} elements but instance has {actual} elements') | ||
@@ -243,4 +296,8 @@ } | ||
if (rule.min !== undefined && rule.max === undefined) { | ||
Assert.equal('items length', schema.path, data.length, (rule.min * schema.items.length), result) | ||
// |1, |>1 | ||
if (rule.count === 1) return result.length === length | ||
else Assert.equal('items length', schema.path, data.length, (rule.min * schema.items.length), result) | ||
} | ||
// |+inc | ||
if (rule.parameters[2]) return result.length === length | ||
} | ||
@@ -261,4 +318,3 @@ | ||
if (result.length !== length) return false | ||
return true | ||
return result.length === length | ||
} | ||
@@ -282,3 +338,3 @@ } | ||
return (item.message || | ||
'[{utype}] Expect {path}\'{ltype} is {action} {expected}, but is {actual}') | ||
'[{utype}] Expect {path}\'{ltype} {action} {expected}, but is {actual}') | ||
.replace('{utype}', item.type.toUpperCase()) | ||
@@ -293,2 +349,9 @@ .replace('{ltype}', item.type.toLowerCase()) | ||
if (actual === expected) return true | ||
switch (type) { | ||
case 'type': | ||
// 正则模板 === 字符串最终值 | ||
if (expected === 'regexp' && actual === 'string') return true | ||
break | ||
} | ||
var item = { | ||
@@ -299,3 +362,3 @@ path: path, | ||
expected: expected, | ||
action: 'equal to', | ||
action: 'is equal to', | ||
message: message | ||
@@ -307,2 +370,18 @@ } | ||
}, | ||
// actual matches expected | ||
match: function(type, path, actual, expected, result, message) { | ||
if (expected.test(actual)) return true | ||
var item = { | ||
path: path, | ||
type: type, | ||
actual: actual, | ||
expected: expected, | ||
action: 'matches', | ||
message: message | ||
} | ||
item.message = Assert.message(item) | ||
result.push(item) | ||
return false | ||
}, | ||
notEqual: function(type, path, actual, expected, result, message) { | ||
@@ -315,3 +394,3 @@ if (actual !== expected) return true | ||
expected: expected, | ||
action: 'not equal to', | ||
action: 'is not equal to', | ||
message: message | ||
@@ -330,3 +409,3 @@ } | ||
expected: expected, | ||
action: 'greater than', | ||
action: 'is greater than', | ||
message: message | ||
@@ -345,3 +424,3 @@ } | ||
expected: expected, | ||
action: 'less to', | ||
action: 'is less to', | ||
message: message | ||
@@ -360,3 +439,3 @@ } | ||
expected: expected, | ||
action: 'greater than or equal to', | ||
action: 'is greater than or equal to', | ||
message: message | ||
@@ -375,3 +454,3 @@ } | ||
expected: expected, | ||
action: 'less than or equal to', | ||
action: 'is less than or equal to', | ||
message: message | ||
@@ -378,0 +457,0 @@ } |
@@ -360,3 +360,3 @@ /* global window, document, location, Event, setTimeout */ | ||
Util.extend(MockXMLHttpRequest.prototype, { | ||
addEventListene: function addEventListene(type, handle) { | ||
addEventListener: function addEventListener(type, handle) { | ||
var events = this.custom.events | ||
@@ -363,0 +363,0 @@ if (!events[type]) events[type] = [] |
@@ -437,2 +437,10 @@ /* global console, require, chai, describe, before, it */ | ||
}) | ||
describe('#105 addEventListener', function() { | ||
it('addEventListene => addEventListener', function(done) { | ||
var xhr = new Mock.XHR() | ||
expect(xhr.addEventListener).to.not.equal(undefined) | ||
expect(xhr.addEventListene).to.equal(undefined) | ||
done() | ||
}) | ||
}) | ||
}) |
@@ -26,5 +26,7 @@ /* global require, chai, describe, before, it */ | ||
test.title = stringify(tpl) + ' VS ' + stringify(data) + '\n\tresult: ' + stringify(result) | ||
for (var i = 0; i < result.length; i++) { | ||
// test.title += '\n\t' + result[i].message | ||
} | ||
// if (result.length) test.title += '\n\tresult: ' | ||
// for (var i = 0; i < result.length; i++) { | ||
// test.title += '\n\t' + result[i].message // stringify(result) | ||
// } | ||
} | ||
@@ -35,4 +37,4 @@ | ||
var result = Mock.valid(tpl, data) | ||
title(tpl, data, result, this.test) | ||
expect(result).to.be.an('array').with.length(len) | ||
title(tpl, data, result, this.test) | ||
}) | ||
@@ -134,2 +136,34 @@ } | ||
}) | ||
describe('Value - RgeExp', function() { | ||
doit({ | ||
name: /value/ | ||
}, { | ||
name: 'value' | ||
}, 0) | ||
doit({ | ||
name: /value/ | ||
}, { | ||
name: 'vvvvv' | ||
}, 1) | ||
doit({ | ||
'name|1-10': /value/ | ||
}, { | ||
name: 'valuevaluevaluevaluevalue' | ||
}, 0) | ||
doit({ | ||
'name|1-10': /value/ | ||
}, { | ||
name: 'vvvvvvvvvvvvvvvvvvvvvvvvv' | ||
}, 1) | ||
doit({ | ||
'name|1-10': /^value$/ | ||
}, { | ||
name: 'valuevaluevaluevaluevalue' | ||
}, 0) | ||
doit({ | ||
name: /[a-z][A-Z][0-9]/ | ||
}, { | ||
name: 'yL5' | ||
}, 0) | ||
}) | ||
describe('Value - Object', function() { | ||
@@ -182,3 +216,48 @@ doit({ | ||
// 'name|1': array | ||
doit({ | ||
'name|1': [1, 2, 3] | ||
}, { | ||
'name': 1 | ||
}, 0) | ||
doit({ | ||
'name|1': [1, 2, 3] | ||
}, { | ||
'name': 2 | ||
}, 0) | ||
doit({ | ||
'name|1': [1, 2, 3] | ||
}, { | ||
'name': 3 | ||
}, 0) | ||
doit({ // 不检测 | ||
'name|1': [1, 2, 3] | ||
}, { | ||
'name': 4 | ||
}, 0) | ||
// 'name|+1': array | ||
doit({ | ||
'name|+1': [1, 2, 3] | ||
}, { | ||
'name': 1 | ||
}, 0) | ||
doit({ | ||
'name|+1': [1, 2, 3] | ||
}, { | ||
'name': 2 | ||
}, 0) | ||
doit({ | ||
'name|+1': [1, 2, 3] | ||
}, { | ||
'name': 3 | ||
}, 0) | ||
doit({ | ||
'name|+1': [1, 2, 3] | ||
}, { | ||
'name': 4 | ||
}, 0) | ||
// 'name|min-max': array | ||
doit({ | ||
'name|2-3': [1] | ||
@@ -218,3 +297,17 @@ }, { | ||
}, 2) | ||
// 'name|count': array | ||
}) | ||
describe('Value - Placeholder', function() { | ||
doit({ | ||
name: '@email' | ||
}, { | ||
name: 'nuysoft@gmail.com' | ||
}, 0) | ||
doit({ | ||
name: '@int' | ||
}, { | ||
name: 123 | ||
}, 0) | ||
}) | ||
}) |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
1886366
19229