test.mock.random.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* global require, chai, describe, before, it */
  2. /* global window */
  3. // 数据占位符定义(Data Placeholder Definition,DPD)
  4. var expect = chai.expect
  5. var Mock, Random, $, _, Random
  6. /* jshint -W061 */
  7. describe('Random', function() {
  8. before(function(done) {
  9. require(['mock', 'underscore', 'jquery'], function() {
  10. Mock = arguments[0]
  11. window.Random = Random = Mock.Random
  12. _ = arguments[1]
  13. $ = arguments[2]
  14. expect(Mock).to.not.equal(undefined)
  15. expect(_).to.not.equal(undefined)
  16. expect($).to.not.equal(undefined)
  17. done()
  18. })
  19. })
  20. function stringify(json) {
  21. return JSON.stringify(json /*, null, 4*/ )
  22. }
  23. function doit(expression, validator) {
  24. it('', function() {
  25. // for (var i = 0; i < 1; i++) {}
  26. var data = eval(expression)
  27. validator(data)
  28. this.test.title = stringify(expression) + ' => ' + stringify(data)
  29. })
  30. }
  31. describe('Basic', function() {
  32. doit('Random.boolean()', function(data) {
  33. expect(data).to.be.a('boolean')
  34. })
  35. doit('Random.natural()', function(data) {
  36. expect(data).to.be.a('number').within(0, 9007199254740992)
  37. })
  38. doit('Random.natural(1, 3)', function(data) {
  39. expect(data).to.be.a('number').within(1, 3)
  40. })
  41. doit('Random.natural(1)', function(data) {
  42. expect(data).to.be.a('number').least(1)
  43. })
  44. doit('Random.integer()', function(data) {
  45. expect(data).to.be.a('number').within(-9007199254740992, 9007199254740992)
  46. })
  47. doit('Random.integer(-10, 10)', function(data) {
  48. expect(data).to.be.a('number').within(-10, 10)
  49. })
  50. // 1 整数部分 2 小数部分
  51. var RE_FLOAT = /(\-?\d+)\.?(\d+)?/
  52. function validFloat(float, min, max, dmin, dmax) {
  53. RE_FLOAT.lastIndex = 0
  54. var parts = RE_FLOAT.exec(float + '')
  55. expect(+parts[1]).to.be.a('number').within(min, max)
  56. /* jshint -W041 */
  57. if (parts[2] != undefined) {
  58. expect(parts[2]).to.have.length.within(dmin, dmax)
  59. }
  60. }
  61. doit('Random.float()', function(data) {
  62. validFloat(data, -9007199254740992, 9007199254740992, 0, 17)
  63. })
  64. doit('Random.float(0)', function(data) {
  65. validFloat(data, 0, 9007199254740992, 0, 17)
  66. })
  67. doit('Random.float(60, 100)', function(data) {
  68. validFloat(data, 60, 100, 0, 17)
  69. })
  70. doit('Random.float(60, 100, 3)', function(data) {
  71. validFloat(data, 60, 100, 3, 17)
  72. })
  73. doit('Random.float(60, 100, 3, 5)', function(data) {
  74. validFloat(data, 60, 100, 3, 5)
  75. })
  76. var CHARACTER_LOWER = 'abcdefghijklmnopqrstuvwxyz'
  77. var CHARACTER_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  78. var CHARACTER_NUMBER = '0123456789'
  79. var CHARACTER_SYMBOL = '!@#$%^&*()[]'
  80. doit('Random.character()', function(data) {
  81. expect(data).to.be.a('string').with.length(1)
  82. expect(
  83. CHARACTER_LOWER +
  84. CHARACTER_UPPER +
  85. CHARACTER_NUMBER +
  86. CHARACTER_SYMBOL
  87. ).to.include(data)
  88. })
  89. doit('Random.character("lower")', function(data) {
  90. expect(data).to.be.a('string').with.length(1)
  91. expect(CHARACTER_LOWER).to.include(data)
  92. })
  93. doit('Random.character("upper")', function(data) {
  94. expect(data).to.be.a('string').with.length(1)
  95. expect(CHARACTER_UPPER).to.include(data)
  96. })
  97. doit('Random.character("number")', function(data) {
  98. expect(data).to.be.a('string').with.length(1)
  99. expect(CHARACTER_NUMBER).to.include(data)
  100. })
  101. doit('Random.character("symbol")', function(data) {
  102. expect(data).to.be.a('string').with.length(1)
  103. expect(CHARACTER_SYMBOL).to.include(data)
  104. })
  105. doit('Random.character("aeiou")', function(data) {
  106. expect(data).to.be.a('string').with.length(1)
  107. expect('aeiou').to.include(data)
  108. })
  109. doit('Random.string()', function(data) {
  110. expect(data).to.be.a('string').with.length.within(3, 7)
  111. })
  112. doit('Random.string(5)', function(data) {
  113. expect(data).to.be.a('string').with.length(5)
  114. })
  115. doit('Random.string("lower", 5)', function(data) {
  116. expect(data).to.be.a('string').with.length(5)
  117. for (var i = 0; i < data.length; i++) {
  118. expect(CHARACTER_LOWER).to.include(data[i])
  119. }
  120. })
  121. doit('Random.string(7, 10)', function(data) {
  122. expect(data).to.be.a('string').with.length.within(7, 10)
  123. })
  124. doit('Random.string("aeiou", 1, 3)', function(data) {
  125. expect(data).to.be.a('string').with.length.within(1, 3)
  126. for (var i = 0; i < data.length; i++) {
  127. expect('aeiou').to.include(data[i])
  128. }
  129. })
  130. doit('Random.range(10)', function(data) {
  131. expect(data).to.be.an('array').with.length(10)
  132. })
  133. doit('Random.range(3, 7)', function(data) {
  134. expect(data).to.be.an('array').deep.equal([3, 4, 5, 6])
  135. })
  136. doit('Random.range(1, 10, 2)', function(data) {
  137. expect(data).to.be.an('array').deep.equal([1, 3, 5, 7, 9])
  138. })
  139. doit('Random.range(1, 10, 3)', function(data) {
  140. expect(data).to.be.an('array').deep.equal([1, 4, 7])
  141. })
  142. var RE_DATE = /\d{4}-\d{2}-\d{2}/
  143. var RE_TIME = /\d{2}:\d{2}:\d{2}/
  144. var RE_DATETIME = new RegExp(RE_DATE.source + ' ' + RE_TIME.source)
  145. doit('Random.date()', function(data) {
  146. expect(RE_DATE.test(data)).to.be.true
  147. })
  148. doit('Random.time()', function(data) {
  149. expect(RE_TIME.test(data)).to.be.true
  150. })
  151. doit('Random.datetime()', function(data) {
  152. expect(RE_DATETIME.test(data)).to.be.true
  153. })
  154. doit('Random.datetime("yyyy-MM-dd A HH:mm:ss")', function(data) {
  155. expect(data).to.be.ok
  156. })
  157. doit('Random.datetime("yyyy-MM-dd a HH:mm:ss")', function(data) {
  158. expect(data).to.be.ok
  159. })
  160. doit('Random.datetime("yy-MM-dd HH:mm:ss")', function(data) {
  161. expect(data).to.be.ok
  162. })
  163. doit('Random.datetime("y-MM-dd HH:mm:ss")', function(data) {
  164. expect(data).to.be.ok
  165. })
  166. doit('Random.datetime("y-M-d H:m:s")', function(data) {
  167. expect(data).to.be.ok
  168. })
  169. doit('Random.datetime("yyyy yy y MM M dd d HH H hh h mm m ss s SS S A a T")', function(data) {
  170. expect(data).to.be.ok
  171. })
  172. doit('Random.now()', function(data) {
  173. expect(data).to.be.ok
  174. })
  175. doit('Random.now("year")', function(data) {
  176. expect(data).to.be.ok
  177. })
  178. doit('Random.now("month")', function(data) {
  179. expect(data).to.be.ok
  180. })
  181. doit('Random.now("day")', function(data) {
  182. expect(data).to.be.ok
  183. })
  184. doit('Random.now("hour")', function(data) {
  185. expect(data).to.be.ok
  186. })
  187. doit('Random.now("minute")', function(data) {
  188. expect(data).to.be.ok
  189. })
  190. doit('Random.now("second")', function(data) {
  191. expect(data).to.be.ok
  192. })
  193. doit('Random.now("week")', function(data) {
  194. expect(data).to.be.ok
  195. })
  196. doit('Random.now("yyyy-MM-dd HH:mm:ss SS")', function(data) {
  197. expect(data).to.be.ok
  198. })
  199. })
  200. describe('Image', function() {
  201. doit('Random.image()', function(data) {
  202. expect(data).to.be.ok
  203. })
  204. it('Random.dataImage()', function() {
  205. var data = eval(this.test.title)
  206. expect(data).to.be.ok
  207. this.test.title = stringify(this.test.title) + ' => '
  208. })
  209. it('Random.dataImage("200x100")', function() {
  210. var data = eval(this.test.title)
  211. expect(data).to.be.ok
  212. this.test.title = stringify(this.test.title) + ' => '
  213. })
  214. it('Random.dataImage("200x100", "Hello Mock.js!")', function() {
  215. var data = eval(this.test.title)
  216. expect(data).to.be.ok
  217. this.test.title = stringify(this.test.title) + ' => '
  218. })
  219. })
  220. var RE_COLOR = /^#[0-9a-fA-F]{6}$/
  221. var RE_COLOR_RGB = /^rgb\(\d{1,3}, \d{1,3}, \d{1,3}\)$/
  222. var RE_COLOR_RGBA = /^rgba\(\d{1,3}, \d{1,3}, \d{1,3}, 0\.\d{1,2}\)$/
  223. var RE_COLOR_HSL = /^hsl\(\d{1,3}, \d{1,3}, \d{1,3}\)$/
  224. describe('Color', function() {
  225. doit('Random.color()', function(data) {
  226. expect(RE_COLOR.test(data)).to.true
  227. })
  228. doit('Random.hex()', function(data) {
  229. expect(RE_COLOR.test(data)).to.true
  230. })
  231. doit('Random.rgb()', function(data) {
  232. expect(RE_COLOR_RGB.test(data)).to.true
  233. })
  234. doit('Random.rgba()', function(data) {
  235. expect(RE_COLOR_RGBA.test(data)).to.true
  236. })
  237. doit('Random.hsl()', function(data) {
  238. expect(RE_COLOR_HSL.test(data)).to.true
  239. })
  240. })
  241. describe('Text', function() {
  242. doit('Random.paragraph()', function(data) {
  243. expect(data.split('.').length - 1).to.within(3, 7)
  244. })
  245. doit('Random.paragraph(2)', function(data) {
  246. expect(data.split('.').length - 1).to.equal(2)
  247. })
  248. doit('Random.paragraph(1, 3)', function(data) {
  249. expect(data.split('.').length - 1).to.within(1, 3)
  250. })
  251. doit('Random.sentence()', function(data) {
  252. expect(data[0]).to.equal(data.toUpperCase()[0])
  253. expect(data.split(' ').length).to.within(12, 18)
  254. })
  255. doit('Random.sentence(4)', function(data) {
  256. expect(data[0]).to.equal(data.toUpperCase()[0])
  257. expect(data.split(' ').length).to.equal(4)
  258. })
  259. doit('Random.sentence(3, 5)', function(data) {
  260. expect(data[0]).to.equal(data.toUpperCase()[0])
  261. expect(data.split(' ').length).to.within(3, 5)
  262. })
  263. doit('Random.word()', function(data) {
  264. expect(data).to.have.length.within(3, 10)
  265. })
  266. doit('Random.word(4)', function(data) {
  267. expect(data).to.have.length(4)
  268. })
  269. doit('Random.word(3, 5)', function(data) {
  270. expect(data).to.have.length.within(3, 5)
  271. })
  272. doit('Random.title()', function(data) {
  273. var words = data.split(' ')
  274. _.each(words, function(word) {
  275. expect(word[0]).to.equal(word[0].toUpperCase())
  276. })
  277. expect(words).to.have.length.within(3, 7)
  278. })
  279. doit('Random.title(4)', function(data) {
  280. var words = data.split(' ')
  281. _.each(words, function(word) {
  282. expect(word[0]).to.equal(word[0].toUpperCase())
  283. })
  284. expect(words).to.have.length(4)
  285. })
  286. doit('Random.title(3, 5)', function(data) {
  287. var words = data.split(' ')
  288. _.each(words, function(word) {
  289. expect(word[0]).to.equal(word[0].toUpperCase())
  290. })
  291. expect(words).to.have.length.within(3, 5)
  292. })
  293. })
  294. describe('Name', function() {
  295. doit('Random.first()', function(data) {
  296. expect(data[0]).to.equal(data[0].toUpperCase())
  297. })
  298. doit('Random.last()', function(data) {
  299. expect(data[0]).to.equal(data[0].toUpperCase())
  300. })
  301. doit('Random.name()', function(data) {
  302. var words = data.split(' ')
  303. expect(words).to.have.length(2)
  304. expect(words[0][0]).to.equal(words[0][0].toUpperCase())
  305. expect(words[1][0]).to.equal(words[1][0].toUpperCase())
  306. })
  307. doit('Random.name(true)', function(data) {
  308. var words = data.split(' ')
  309. expect(words).to.have.length(3)
  310. expect(words[0][0]).to.equal(words[0][0].toUpperCase())
  311. expect(words[1][0]).to.equal(words[1][0].toUpperCase())
  312. expect(words[2][0]).to.equal(words[2][0].toUpperCase())
  313. })
  314. doit('Random.cfirst()', function(data) {
  315. expect(data).to.be.ok
  316. })
  317. doit('Random.clast()', function(data) {
  318. expect(data).to.be.ok
  319. })
  320. doit('Random.cname()', function(data) {
  321. expect(data).to.be.ok
  322. })
  323. })
  324. var RE_URL = /^([\w.+-]+:)(?:\/\/([^\/?#:]*)(?::(\d+)|)|)/
  325. var RE_IP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
  326. describe('Web', function() {
  327. doit('Random.url()', function(data) {
  328. expect(RE_URL.test(data)).to.be.true
  329. })
  330. doit('Random.domain()', function(data) {
  331. expect(data).to.be.ok
  332. })
  333. doit('Random.domain("com")', function(data) {
  334. expect(data).to.include('.com')
  335. })
  336. doit('Random.tld()', function(data) {
  337. expect(data).to.be.ok
  338. })
  339. doit('Random.email()', function(data) {
  340. expect(data).to.be.ok
  341. })
  342. doit('Random.email("nuysoft.com")', function(data) {
  343. expect(data).to.include('@nuysoft.com')
  344. })
  345. doit('Random.ip()', function(data) {
  346. expect(RE_IP.test(data)).to.be.true
  347. })
  348. })
  349. describe('Address', function() {
  350. doit('Random.region()', function(data) {
  351. expect(data).to.be.ok
  352. })
  353. doit('Random.province()', function(data) {
  354. expect(data).to.be.ok
  355. })
  356. doit('Random.city()', function(data) {
  357. expect(data).to.be.ok
  358. })
  359. doit('Random.city(true)', function(data) {
  360. expect(data).to.be.ok
  361. })
  362. doit('Random.county()', function(data) {
  363. expect(data).to.be.ok
  364. })
  365. doit('Random.county(true)', function(data) {
  366. expect(data).to.be.ok
  367. })
  368. doit('Random.zip()', function(data) {
  369. expect(data).to.be.ok
  370. })
  371. })
  372. describe('Helpers', function() {
  373. doit('Random.capitalize()', function(data) {
  374. expect(data).to.equal('Undefined')
  375. })
  376. doit('Random.capitalize("hello")', function(data) {
  377. expect(data).to.equal('Hello')
  378. })
  379. doit('Random.upper()', function(data) {
  380. expect(data).to.equal('UNDEFINED')
  381. })
  382. doit('Random.upper("hello")', function(data) {
  383. expect(data).to.equal('HELLO')
  384. })
  385. doit('Random.lower()', function(data) {
  386. expect(data).to.equal('undefined')
  387. })
  388. doit('Random.lower("HELLO")', function(data) {
  389. expect(data).to.equal('hello')
  390. })
  391. doit('Random.pick()', function(data) {
  392. expect(data).to.be.undefined
  393. })
  394. doit('Random.pick("a", "e", "i", "o", "u")', function(data) {
  395. expect(["a", "e", "i", "o", "u"]).to.include(data)
  396. })
  397. doit('Random.pick(["a", "e", "i", "o", "u"])', function(data) {
  398. expect(["a", "e", "i", "o", "u"]).to.include(data)
  399. })
  400. doit('Random.pick(["a", "e", "i", "o", "u"], 3)', function(data) {
  401. expect(data).to.be.an('array').with.length(3)
  402. })
  403. doit('Random.pick(["a", "e", "i", "o", "u"], 1, 5)', function(data) {
  404. expect(data).to.be.an('array').with.length.within(1, 5)
  405. })
  406. doit('Random.shuffle()', function(data) {
  407. expect(data).to.deep.equal([])
  408. })
  409. doit('Random.shuffle(["a", "e", "i", "o", "u"])', function(data) {
  410. expect(data.join('')).to.not.equal('aeiou')
  411. expect(data.sort().join('')).to.equal('aeiou')
  412. })
  413. doit('Random.shuffle(["a", "e", "i", "o", "u"], 3)', function(data) {
  414. expect(data).to.be.an('array').with.length(3)
  415. })
  416. doit('Random.shuffle(["a", "e", "i", "o", "u"], 1, 5)', function(data) {
  417. expect(data).to.be.an('array').with.length.within(1, 5)
  418. })
  419. })
  420. var RE_GUID = /[a-fA-F0-9]{8}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{4}\-[a-fA-F0-9]{12}/
  421. describe('Miscellaneous', function() {
  422. doit('Random.guid()', function(data) {
  423. expect(data).to.be.a('string').with.length(36)
  424. expect(RE_GUID.test(data)).to.be.true
  425. })
  426. doit('Random.id()', function(data) {
  427. expect(data).to.be.a('string').with.length(18)
  428. })
  429. })
  430. })