test.mock.schema.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* global require, chai, describe, before, it */
  2. /* global window */
  3. // 数据占位符定义(Data Placeholder Definition,DPD)
  4. var expect = chai.expect
  5. var Mock, $, _
  6. describe('Schema', function() {
  7. before(function(done) {
  8. require(['mock', 'underscore', 'jquery'], function() {
  9. Mock = arguments[0]
  10. window.XMLHttpRequest = Mock.XHR
  11. _ = arguments[1]
  12. $ = arguments[2]
  13. expect(Mock).to.not.equal(undefined)
  14. expect(_).to.not.equal(undefined)
  15. expect($).to.not.equal(undefined)
  16. done()
  17. })
  18. })
  19. function stringify(json) {
  20. return JSON.stringify(json /*, null, 4*/ )
  21. }
  22. function doit(template, validator) {
  23. it('', function() {
  24. var schema = Mock.toJSONSchema(template)
  25. this.test.title = (stringify(template) || template.toString()) + ' => ' + stringify(schema)
  26. validator(schema)
  27. })
  28. }
  29. describe('Type', function() {
  30. doit(1, function(schema) {
  31. expect(schema.name).to.be.an('undefined')
  32. // expect(schema).to.not.have.property('name')
  33. expect(schema).to.have.property('type', 'number')
  34. for (var n in schema.rule) {
  35. expect(schema.rule[n]).to.be.null()
  36. }
  37. })
  38. doit(true, function(schema) {
  39. expect(schema.name).to.be.an('undefined')
  40. // expect(schema).to.not.have.property('name')
  41. expect(schema).to.have.property('type', 'boolean')
  42. for (var n in schema.rule) {
  43. expect(schema.rule[n]).to.be.null()
  44. }
  45. })
  46. doit('', function(schema) {
  47. expect(schema.name).to.be.an('undefined')
  48. // expect(schema).to.not.have.property('name')
  49. expect(schema).to.have.property('type', 'string')
  50. for (var n in schema.rule) {
  51. expect(schema.rule[n]).to.be.null()
  52. }
  53. })
  54. doit(function() {}, function(schema) {
  55. expect(schema.name).to.be.an('undefined')
  56. // expect(schema).to.not.have.property('name')
  57. expect(schema).to.have.property('type', 'function')
  58. for (var n in schema.rule) {
  59. expect(schema.rule[n]).to.be.null()
  60. }
  61. })
  62. doit(/\d/, function(schema) {
  63. expect(schema.name).to.be.an('undefined')
  64. // expect(schema).to.not.have.property('name')
  65. expect(schema).to.have.property('type', 'regexp')
  66. for (var n in schema.rule) {
  67. expect(schema.rule[n]).to.be.null()
  68. }
  69. })
  70. doit([], function(schema) {
  71. expect(schema.name).to.be.an('undefined')
  72. // expect(schema).to.not.have.property('name')
  73. expect(schema).to.have.property('type', 'array')
  74. for (var n in schema.rule) {
  75. expect(schema.rule[n]).to.be.null()
  76. }
  77. expect(schema).to.have.property('items').with.length(0)
  78. })
  79. doit({}, function(schema) {
  80. expect(schema.name).to.be.an('undefined')
  81. // expect(schema).to.not.have.property('name')
  82. expect(schema).to.have.property('type', 'object')
  83. for (var n in schema.rule) {
  84. expect(schema.rule[n]).to.be.null()
  85. }
  86. expect(schema).to.have.property('properties').with.length(0)
  87. })
  88. })
  89. describe('Object', function() {
  90. doit({
  91. a: {
  92. b: {
  93. c: {
  94. d: {}
  95. }
  96. }
  97. }
  98. }, function(schema) {
  99. expect(schema.name).to.be.an('undefined')
  100. // expect(schema).to.not.have.property('name')
  101. expect(schema).to.have.property('type', 'object')
  102. var properties;
  103. // root.properties
  104. properties = schema.properties
  105. expect(properties).to.with.length(1)
  106. expect(properties[0]).to.have.property('name', 'a')
  107. expect(properties[0]).to.have.property('type', 'object')
  108. // root.a.properties
  109. properties = properties[0].properties
  110. expect(properties).to.with.length(1)
  111. expect(properties[0]).to.have.property('name', 'b')
  112. expect(properties[0]).to.have.property('type', 'object')
  113. // root.a.b.properties
  114. properties = properties[0].properties
  115. expect(properties).to.with.length(1)
  116. expect(properties[0]).to.have.property('name', 'c')
  117. expect(properties[0]).to.have.property('type', 'object')
  118. // root.a.b.c.properties
  119. properties = properties[0].properties
  120. expect(properties).to.with.length(1)
  121. expect(properties[0]).to.have.property('name', 'd')
  122. expect(properties[0]).to.have.property('type', 'object')
  123. // root.a.b.c.d.properties
  124. properties = properties[0].properties
  125. expect(properties).to.with.length(0)
  126. })
  127. })
  128. describe('Array', function() {
  129. doit([
  130. [
  131. ['foo', 'bar']
  132. ]
  133. ], function(schema) {
  134. expect(schema.name).to.be.an('undefined')
  135. // expect(schema).to.not.have.property('name')
  136. expect(schema).to.have.property('type', 'array')
  137. var items;
  138. // root.items
  139. items = schema.items
  140. expect(items).to.with.length(1)
  141. expect(items[0]).to.have.property('type', 'array')
  142. // root[0].items
  143. items = items[0].items
  144. expect(items).to.with.length(1)
  145. expect(items[0]).to.have.property('type', 'array')
  146. // root[0][0].items
  147. items = items[0].items
  148. expect(items).to.with.length(2)
  149. expect(items[0]).to.have.property('type', 'string')
  150. expect(items[1]).to.have.property('type', 'string')
  151. })
  152. })
  153. describe('String Rule', function() {
  154. doit({
  155. 'string|1-10': '★'
  156. }, function(schema) {
  157. expect(schema.name).to.be.an('undefined')
  158. // expect(schema).to.not.have.property('name')
  159. expect(schema).to.have.property('type', 'object')
  160. var properties;
  161. // root.properties
  162. properties = schema.properties
  163. expect(properties).to.with.length(1)
  164. expect(properties[0]).to.have.property('type', 'string')
  165. expect(properties[0].rule).to.have.property('min', 1)
  166. expect(properties[0].rule).to.have.property('max', 10)
  167. })
  168. doit({
  169. 'string|3': 'value',
  170. }, function(schema) {
  171. expect(schema.name).to.be.an('undefined')
  172. // expect(schema).to.not.have.property('name')
  173. expect(schema).to.have.property('type', 'object')
  174. var properties;
  175. // root.properties
  176. properties = schema.properties
  177. expect(properties).to.with.length(1)
  178. expect(properties[0]).to.have.property('type', 'string')
  179. expect(properties[0].rule).to.have.property('min', 3)
  180. expect(properties[0].rule.max).to.be.an('undefined')
  181. })
  182. })
  183. })