Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nuysoft committed Feb 2, 2016
1 parent 5f4d246 commit 4376639
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 59 deletions.
10 changes: 5 additions & 5 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.

107 changes: 82 additions & 25 deletions dist/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,13 @@ return /******/ (function(modules) { // webpackBootstrap
return options.template.call(options.context.currentContext, options)
},
'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/,
for (var i = 0; i < options.rule.count; i++) {
Expand Down Expand Up @@ -7651,6 +7656,7 @@ return /******/ (function(modules) { // webpackBootstrap
[JSON-Schama validator](http://json-schema-validator.herokuapp.com/)
[Regexp Demo](http://demos.forbeslindesay.co.uk/regexp/)
*/
var Constant = __webpack_require__(2)
var Util = __webpack_require__(3)
var toJSONSchema = __webpack_require__(23)

Expand Down Expand Up @@ -7721,28 +7727,42 @@ return /******/ (function(modules) { // webpackBootstrap

Assert.equal('name', schema.path, name + '', schema.name + '', result)

if (result.length !== length) return false
return true
return result.length === length
},
type: function(schema, data, name, result) {
var length = result.length

switch (schema.type) {
// 跳过含有『占位符』的属性值,因为『占位符』返回值的类型可能和模板不一致,例如 '@int' 会返回一个整形值
case 'string':
if (schema.template.match(Constant.RE_PLACEHOLDER)) return true
break
}

Assert.equal('type', schema.path, Util.type(data), schema.type, result)

if (result.length !== length) return false
return true
return result.length === length
},
value: function(schema, data, name, result) {
var length = result.length

var rule = schema.rule
var templateType = Util.type(schema.template)
var templateType = schema.type
if (templateType === 'object' || templateType === 'array') return

// 无生成规则
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
}

// 有生成规则
Expand Down Expand Up @@ -7780,25 +7800,41 @@ return /******/ (function(modules) { // webpackBootstrap

case 'boolean':
break

case 'string':
// 'aaa'.match(/a/g)
var actualRepeatCount = data.match(new RegExp(schema.template, 'g'))
actualRepeatCount = actualRepeatCount ? actualRepeatCount.length : actualRepeatCount

// |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('repeat count', schema.path, actualRepeatCount, rule.min, result)
}

break

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

// |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('value', schema.path, actualRepeatCount, rule.min, result)
Assert.equal('repeat count', schema.path, actualRepeatCount, rule.min, result)
}
break
}

if (result.length !== length) return false
return true
return result.length === length
},
properties: function(schema, data, name, result) {
var length = result.length
Expand Down Expand Up @@ -7836,8 +7872,7 @@ return /******/ (function(modules) { // webpackBootstrap
)
}

if (result.length !== length) return false
return true
return result.length === length
},
items: function(schema, data, name, result) {
var length = result.length
Expand Down Expand Up @@ -7877,8 +7912,7 @@ return /******/ (function(modules) { // webpackBootstrap
)
}

if (result.length !== length) return false
return true
return result.length === length
}
}

Expand All @@ -7897,7 +7931,7 @@ return /******/ (function(modules) { // webpackBootstrap
var Assert = {
message: function(item) {
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())
.replace('{ltype}', item.type.toLowerCase())
.replace('{path}', Util.isArray(item.path) && item.path.join('.') || item.path)
Expand All @@ -7907,12 +7941,35 @@ return /******/ (function(modules) { // webpackBootstrap
},
equal: function(type, path, actual, expected, result, message) {
if (actual === expected) return true
switch (type) {
case 'type':
// 正则模板 === 字符串最终值
if (expected === 'regexp' && actual === 'string') return true
break
}

var item = {
path: path,
type: type,
actual: actual,
expected: expected,
action: 'is equal to',
message: message
}
item.message = Assert.message(item)
result.push(item)
return false
},
// 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: 'equal to',
action: 'matches',
message: message
}
item.message = Assert.message(item)
Expand All @@ -7926,7 +7983,7 @@ return /******/ (function(modules) { // webpackBootstrap
type: type,
actual: actual,
expected: expected,
action: 'not equal to',
action: 'is not equal to',
message: message
}
item.message = Assert.message(item)
Expand All @@ -7940,7 +7997,7 @@ return /******/ (function(modules) { // webpackBootstrap
type: type,
actual: actual,
expected: expected,
action: 'greater than',
action: 'is greater than',
message: message
}
item.message = Assert.message(item)
Expand All @@ -7954,7 +8011,7 @@ return /******/ (function(modules) { // webpackBootstrap
type: type,
actual: actual,
expected: expected,
action: 'less to',
action: 'is less to',
message: message
}
item.message = Assert.message(item)
Expand All @@ -7968,7 +8025,7 @@ return /******/ (function(modules) { // webpackBootstrap
type: type,
actual: actual,
expected: expected,
action: 'greater than or equal to',
action: 'is greater than or equal to',
message: message
}
item.message = Assert.message(item)
Expand All @@ -7982,7 +8039,7 @@ return /******/ (function(modules) { // webpackBootstrap
type: type,
actual: actual,
expected: expected,
action: 'less than or equal to',
action: 'is less than or equal to',
message: message
}
item.message = Assert.message(item)
Expand Down
Binary file modified src/dependencies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 7 additions & 2 deletions src/mock/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,13 @@ Handler.extend({
return options.template.call(options.context.currentContext, options)
},
'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/,
for (var i = 0; i < options.rule.count; i++) {
Expand Down
Loading

0 comments on commit 4376639

Please sign in to comment.