Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nuysoft committed Apr 7, 2016
1 parent 9aa7e25 commit c4d7cba
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 38 deletions.
8 changes: 4 additions & 4 deletions dist/mock-min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mock-min.js.map

Large diffs are not rendered by default.

58 changes: 42 additions & 16 deletions dist/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -882,8 +882,8 @@ return /******/ (function(modules) { // webpackBootstrap
var count = range ? !range[2] ? parseInt(range[1], 10) : Random.integer(min, max) : undefined

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
var dcount = decimal ? !decimal[2] && parseInt(decimal[1], 10) || Random.integer(dmin, dmax) : undefined

Expand Down Expand Up @@ -7664,7 +7664,8 @@ return /******/ (function(modules) { // webpackBootstrap
var schema = toJSONSchema(template)
var result = Diff.diff(schema, data)
for (var i = 0; i < result.length; i++) {
// console.log(Assert.message(result[i]))
// console.log(template, data)
// console.warn(Assert.message(result[i]))
}
return result
}
Expand Down Expand Up @@ -7733,10 +7734,24 @@ return /******/ (function(modules) { // webpackBootstrap
var length = result.length

switch (schema.type) {
// 跳过含有『占位符』的属性值,因为『占位符』返回值的类型可能和模板不一致,例如 '@int' 会返回一个整形值
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)
Expand All @@ -7748,7 +7763,7 @@ return /******/ (function(modules) { // webpackBootstrap

var rule = schema.rule
var templateType = schema.type
if (templateType === 'object' || templateType === 'array') return
if (templateType === 'object' || templateType === 'array' || templateType === 'function') return true

// 无生成规则
if (!rule.parameters) {
Expand All @@ -7775,9 +7790,9 @@ return /******/ (function(modules) { // webpackBootstrap
// 整数部分
// |min-max
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)
}
// |count
if (rule.min !== undefined && rule.max === undefined) {
Expand Down Expand Up @@ -7805,7 +7820,7 @@ return /******/ (function(modules) { // webpackBootstrap
case 'string':
// 'aaa'.match(/a/g)
actualRepeatCount = data.match(new RegExp(schema.template, 'g'))
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0

// |min-max
if (rule.min !== undefined && rule.max !== undefined) {
Expand All @@ -7821,7 +7836,7 @@ return /******/ (function(modules) { // webpackBootstrap

case 'regexp':
actualRepeatCount = data.match(new RegExp(schema.template.source.replace(/^\^|\$$/g, ''), 'g'))
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0

// |min-max
if (rule.min !== undefined && rule.max !== undefined) {
Expand Down Expand Up @@ -7851,12 +7866,13 @@ return /******/ (function(modules) { // webpackBootstrap
// 有生成规则
// |min-max
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)
}
}

Expand All @@ -7866,7 +7882,13 @@ return /******/ (function(modules) { // webpackBootstrap
result.push.apply(
result,
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]],
keys[i]
)
Expand All @@ -7889,15 +7911,19 @@ return /******/ (function(modules) { // webpackBootstrap
// 有生成规则
// |min-max
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')
}
// |count
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
}

if (result.length !== length) return false
Expand Down
4 changes: 2 additions & 2 deletions src/mock/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ module.exports = {
var count = range ? !range[2] ? parseInt(range[1], 10) : Random.integer(min, max) : undefined

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
var dcount = decimal ? !decimal[2] && parseInt(decimal[1], 10) || Random.integer(dmin, dmax) : undefined

Expand Down
54 changes: 40 additions & 14 deletions src/mock/valid/valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ function valid(template, data) {
var schema = toJSONSchema(template)
var result = Diff.diff(schema, data)
for (var i = 0; i < result.length; i++) {
// console.log(Assert.message(result[i]))
// console.log(template, data)
// console.warn(Assert.message(result[i]))
}
return result
}
Expand Down Expand Up @@ -97,10 +98,24 @@ var Diff = {
var length = result.length

switch (schema.type) {
// 跳过含有『占位符』的属性值,因为『占位符』返回值的类型可能和模板不一致,例如 '@int' 会返回一个整形值
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)
Expand All @@ -112,7 +127,7 @@ var Diff = {

var rule = schema.rule
var templateType = schema.type
if (templateType === 'object' || templateType === 'array') return
if (templateType === 'object' || templateType === 'array' || templateType === 'function') return true

// 无生成规则
if (!rule.parameters) {
Expand All @@ -139,9 +154,9 @@ var Diff = {
// 整数部分
// |min-max
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)
}
// |count
if (rule.min !== undefined && rule.max === undefined) {
Expand Down Expand Up @@ -169,7 +184,7 @@ var Diff = {
case 'string':
// 'aaa'.match(/a/g)
actualRepeatCount = data.match(new RegExp(schema.template, 'g'))
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0

// |min-max
if (rule.min !== undefined && rule.max !== undefined) {
Expand All @@ -185,7 +200,7 @@ var Diff = {

case 'regexp':
actualRepeatCount = data.match(new RegExp(schema.template.source.replace(/^\^|\$$/g, ''), 'g'))
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : 0

// |min-max
if (rule.min !== undefined && rule.max !== undefined) {
Expand Down Expand Up @@ -215,12 +230,13 @@ var Diff = {
// 有生成规则
// |min-max
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)
}
}

Expand All @@ -230,7 +246,13 @@ var Diff = {
result.push.apply(
result,
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]],
keys[i]
)
Expand All @@ -253,15 +275,19 @@ var Diff = {
// 有生成规则
// |min-max
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')
}
// |count
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
}

if (result.length !== length) return false
Expand Down
50 changes: 49 additions & 1 deletion test/test.mock.valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('Mock.valid', function() {

function title(tpl, data, result, test) {
test.title = stringify(tpl) + ' VS ' + stringify(data) + '\n\tresult: ' + stringify(result)

// if (result.length) test.title += '\n\tresult: '
// for (var i = 0; i < result.length; i++) {
// test.title += '\n\t' + result[i].message // stringify(result)
Expand All @@ -32,8 +33,8 @@ describe('Mock.valid', function() {
function doit(tpl, data, len) {
it('', function() {
var result = Mock.valid(tpl, data)
expect(result).to.be.an('array').with.length(len)
title(tpl, data, result, this.test)
expect(result).to.be.an('array').with.length(len)
})
}

Expand Down Expand Up @@ -209,6 +210,51 @@ describe('Mock.valid', function() {

doit([1, 2, 3], [1, 2, 3, 4], 1)

// '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]
}, {
Expand Down Expand Up @@ -244,6 +290,8 @@ describe('Mock.valid', function() {
}, {
'name': [1, 2, 3]
}, 2)

// 'name|count': array
})
describe('Value - Placeholder', function() {
doit({
Expand Down

0 comments on commit c4d7cba

Please sign in to comment.