extend_test.coffee 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. ###
  2. Test suite for node AND browser in one file
  3. So, we are need some data from global
  4. Its so wrong, but its OK for test
  5. ###
  6. # resolve require from [window] or by require()
  7. # _ = @_ ? require 'lodash'
  8. lib_path = GLOBAL?.lib_path || ''
  9. extend = require "#{lib_path}whet.extend"
  10. describe 'whet.extend:', ->
  11. str = int = arr = date = obj = deep = null
  12. beforeEach ->
  13. str = 'me a test'
  14. int = 10
  15. arr = [ 1, 'what', new Date( 81, 8, 4 )];
  16. date = new Date( 81, 4, 13 );
  17. obj =
  18. str : str
  19. int : int
  20. arr : arr
  21. date : date
  22. deep =
  23. ori : obj
  24. layer :
  25. int : 10
  26. str : 'str'
  27. date : new Date( 84, 5, 12 )
  28. arr : [ 101, 'dude', new Date( 82, 10, 4 )]
  29. deep :
  30. str : obj.str
  31. int : int
  32. arr : obj.arr
  33. date : new Date( 81, 7, 4 )
  34. describe 'should merge string with:', ->
  35. it 'string', ->
  36. ori = 'what u gonna say';
  37. target = extend ori, str
  38. ori.should.eql 'what u gonna say'
  39. str.should.eql 'me a test'
  40. target.should.eql
  41. '0' : 'm',
  42. '1' : 'e',
  43. '2' : ' ',
  44. '3' : 'a',
  45. '4' : ' ',
  46. '5' : 't',
  47. '6' : 'e',
  48. '7' : 's',
  49. '8' : 't'
  50. it 'number', ->
  51. ori = 'what u gonna say'
  52. target = extend ori, int
  53. ori.should.eql 'what u gonna say'
  54. int.should.eql 10
  55. target.should.eql {}
  56. it 'array', ->
  57. ori = 'what u gonna say'
  58. target = extend ori, arr
  59. ori.should.eql 'what u gonna say'
  60. arr.should.eql [ 1, 'what', new Date( 81, 8, 4 )]
  61. target.should.eql
  62. '0' : 1,
  63. '1' : 'what',
  64. '2' : new Date( 81, 8, 4 )
  65. it 'date', ->
  66. ori = 'what u gonna say'
  67. target = extend ori, date
  68. ori.should.eql 'what u gonna say'
  69. date.should.eql new Date( 81, 4, 13 )
  70. target.should.eql new Date( 81, 4, 13 )
  71. it 'object', ->
  72. ori = 'what u gonna say'
  73. target = extend ori, obj
  74. ori.should.eql 'what u gonna say'
  75. obj.should.eql
  76. str : 'me a test',
  77. int : 10,
  78. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  79. date : new Date( 81, 4, 13 )
  80. target.should.eql
  81. str : 'me a test',
  82. int : 10,
  83. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  84. date : new Date( 81, 4, 13 )
  85. describe 'should merge number with:', ->
  86. it 'string', ->
  87. ori = 20
  88. target = extend ori, str
  89. ori.should.eql 20
  90. str.should.eql 'me a test'
  91. target.should.eql
  92. '0' : 'm',
  93. '1' : 'e',
  94. '2' : ' ',
  95. '3' : 'a',
  96. '4' : ' ',
  97. '5' : 't',
  98. '6' : 'e',
  99. '7' : 's',
  100. '8' : 't'
  101. it 'number', ->
  102. ori = 20
  103. target = extend ori, int
  104. ori.should.eql 20
  105. int.should.eql 10
  106. target.should.eql {}
  107. it 'array', ->
  108. ori = 20
  109. target = extend ori, arr
  110. ori.should.eql 20
  111. arr.should.eql [ 1, 'what', new Date( 81, 8, 4 )]
  112. target.should.eql
  113. '0' : 1,
  114. '1' : 'what',
  115. '2' : new Date( 81, 8, 4 )
  116. it 'date', ->
  117. ori = 20
  118. target = extend ori, date
  119. ori.should.eql 20
  120. date.should.eql new Date( 81, 4, 13 )
  121. target.should.eql new Date( 81, 4, 13 )
  122. it 'object', ->
  123. ori = 20
  124. target = extend ori, obj
  125. ori.should.eql 20
  126. obj.should.eql
  127. str : 'me a test',
  128. int : 10,
  129. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  130. date : new Date( 81, 4, 13 )
  131. target.should.eql
  132. str : 'me a test',
  133. int : 10,
  134. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  135. date : new Date( 81, 4, 13 )
  136. describe 'should merge array with:', ->
  137. it 'string', ->
  138. ori = [ 1, 2, 3, 4, 5, 6 ]
  139. target = extend ori, str
  140. ori.should.eql [ 'm', 'e', ' ', 'a', ' ', 't', 'e', 's', 't' ]
  141. str.should.eql 'me a test'
  142. target.should.eql
  143. '0' : 'm',
  144. '1' : 'e',
  145. '2' : ' ',
  146. '3' : 'a',
  147. '4' : ' ',
  148. '5' : 't',
  149. '6' : 'e',
  150. '7' : 's',
  151. '8' : 't'
  152. it 'number', ->
  153. ori = [ 1, 2, 3, 4, 5, 6 ]
  154. target = extend ori, int
  155. ori.should.eql [ 1, 2, 3, 4, 5, 6 ]
  156. int.should.eql 10
  157. target.should.eql [ 1, 2, 3, 4, 5, 6 ]
  158. it 'array', ->
  159. ori = [ 1, 2, 3, 4, 5, 6 ]
  160. target = extend ori, arr
  161. ori.should.eql [ 1, 'what', new Date( 81, 8, 4 ), 4, 5, 6 ]
  162. arr.should.eql [ 1, 'what', new Date( 81, 8, 4 )]
  163. target.should.eql [ 1, 'what', new Date( 81, 8, 4 ), 4, 5, 6 ]
  164. it 'date', ->
  165. ori = [ 1, 2, 3, 4, 5, 6 ]
  166. target = extend ori, date
  167. ori.should.eql [ 1, 2, 3, 4, 5, 6 ]
  168. date.should.eql new Date( 81, 4, 13 )
  169. target.should.eql [ 1, 2, 3, 4, 5, 6 ]
  170. it 'object', ->
  171. ori = [ 1, 2, 3, 4, 5, 6 ]
  172. target = extend ori, obj
  173. ori.length.should.equal 6
  174. ori[ 'str' ].should.eql 'me a test'
  175. ori[ 'int' ].should.eql 10
  176. ori[ 'arr' ].should.eql [ 1, 'what', new Date( 81, 8, 4 )]
  177. ori[ 'date' ].should.eql new Date( 81, 4, 13 )
  178. obj.should.eql
  179. str : 'me a test',
  180. int : 10,
  181. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  182. date : new Date( 81, 4, 13 )
  183. target.length.should.equal 6
  184. target[ 'str' ].should.eql 'me a test'
  185. target[ 'int' ].should.eql 10
  186. target[ 'arr' ].should.eql [ 1, 'what', new Date( 81, 8, 4 )]
  187. target[ 'date' ].should.eql new Date( 81, 4, 13 )
  188. describe 'should merge date with:', ->
  189. it 'string', ->
  190. ori = new Date( 81, 9, 20 )
  191. target = extend ori, str
  192. ori.should.eql
  193. '0' : 'm',
  194. '1' : 'e',
  195. '2' : ' ',
  196. '3' : 'a',
  197. '4' : ' ',
  198. '5' : 't',
  199. '6' : 'e',
  200. '7' : 's',
  201. '8' : 't'
  202. str.should.eql 'me a test'
  203. target.should.eql
  204. '0' : 'm',
  205. '1' : 'e',
  206. '2' : ' ',
  207. '3' : 'a',
  208. '4' : ' ',
  209. '5' : 't',
  210. '6' : 'e',
  211. '7' : 's',
  212. '8' : 't'
  213. it 'number', ->
  214. ori = new Date( 81, 9, 20 )
  215. target = extend ori, int
  216. ori.should.eql {}
  217. int.should.eql 10
  218. target.should.eql {}
  219. it 'array', ->
  220. ori = new Date( 81, 9, 20 )
  221. target = extend ori, arr
  222. ori.should.eql [ 1, 'what', new Date( 81, 8, 4 )]
  223. int.should.eql 10
  224. target.should.eql [ 1, 'what', new Date( 81, 8, 4 )]
  225. it 'date', ->
  226. ori = new Date( 81, 9, 20 )
  227. target = extend ori, date
  228. ori.should.eql {}
  229. date.should.eql new Date( 81, 4, 13 )
  230. target.should.eql {}
  231. it 'object', ->
  232. ori = new Date( 81, 9, 20 )
  233. target = extend ori, obj
  234. ori.should.eql
  235. str : 'me a test',
  236. int : 10,
  237. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  238. date : new Date( 81, 4, 13 )
  239. obj.should.eql
  240. str : 'me a test',
  241. int : 10,
  242. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  243. date : new Date( 81, 4, 13 )
  244. target.should.eql
  245. str : 'me a test',
  246. int : 10,
  247. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  248. date : new Date( 81, 4, 13 )
  249. describe 'should merge object with:', ->
  250. it 'string', ->
  251. ori =
  252. str : 'no shit'
  253. int : 76
  254. arr : [ 1, 2, 3, 4 ]
  255. date : new Date( 81, 7, 26 )
  256. target = extend ori, str
  257. ori.should.eql
  258. '0' : 'm',
  259. '1' : 'e',
  260. '2' : ' ',
  261. '3' : 'a',
  262. '4' : ' ',
  263. '5' : 't',
  264. '6' : 'e',
  265. '7' : 's',
  266. '8' : 't',
  267. str: 'no shit',
  268. int: 76,
  269. arr: [ 1, 2, 3, 4 ],
  270. date: new Date( 81, 7, 26 )
  271. str.should.eql 'me a test'
  272. target.should.eql
  273. '0' : 'm',
  274. '1' : 'e',
  275. '2' : ' ',
  276. '3' : 'a',
  277. '4' : ' ',
  278. '5' : 't',
  279. '6' : 'e',
  280. '7' : 's',
  281. '8' : 't',
  282. str: 'no shit',
  283. int: 76,
  284. arr: [ 1, 2, 3, 4 ],
  285. date: new Date( 81, 7, 26 )
  286. it 'number', ->
  287. ori =
  288. str : 'no shit',
  289. int : 76,
  290. arr : [ 1, 2, 3, 4 ],
  291. date : new Date( 81, 7, 26 )
  292. target = extend ori, int
  293. ori.should.eql
  294. str : 'no shit',
  295. int : 76,
  296. arr : [ 1, 2, 3, 4 ],
  297. date : new Date( 81, 7, 26 )
  298. int.should.eql 10
  299. target.should.eql
  300. str : 'no shit',
  301. int : 76,
  302. arr : [ 1, 2, 3, 4 ],
  303. date : new Date( 81, 7, 26 )
  304. it 'array', ->
  305. ori =
  306. str : 'no shit',
  307. int : 76,
  308. arr : [ 1, 2, 3, 4 ],
  309. date : new Date( 81, 7, 26 )
  310. target = extend ori, arr
  311. ori.should.eql
  312. '0' : 1,
  313. '1' : 'what',
  314. '2' : new Date( 81, 8, 4 ),
  315. str : 'no shit',
  316. int : 76,
  317. arr : [ 1, 2, 3, 4 ],
  318. date : new Date( 81, 7, 26 )
  319. arr.should.eql [ 1, 'what', new Date( 81, 8, 4 )]
  320. target.should.eql
  321. '0' : 1,
  322. '1' : 'what',
  323. '2' : new Date( 81, 8, 4 ),
  324. str : 'no shit',
  325. int : 76,
  326. arr : [ 1, 2, 3, 4 ],
  327. date : new Date( 81, 7, 26 )
  328. it 'date', ->
  329. ori =
  330. str : 'no shit',
  331. int : 76,
  332. arr : [ 1, 2, 3, 4 ],
  333. date : new Date( 81, 7, 26 )
  334. target = extend ori, date
  335. ori.should.eql
  336. str : 'no shit',
  337. int : 76,
  338. arr : [ 1, 2, 3, 4 ],
  339. date : new Date( 81, 7, 26 )
  340. date.should.eql new Date( 81, 4, 13 )
  341. target.should.eql
  342. str : 'no shit',
  343. int : 76,
  344. arr : [ 1, 2, 3, 4 ],
  345. date : new Date( 81, 7, 26 )
  346. it 'object', ->
  347. ori =
  348. str : 'no shit',
  349. int : 76,
  350. arr : [ 1, 2, 3, 4 ],
  351. date : new Date( 81, 7, 26 )
  352. target = extend ori, obj
  353. ori.should.eql
  354. str : 'me a test',
  355. int : 10,
  356. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  357. date : new Date( 81, 4, 13 )
  358. obj.should.eql
  359. str : 'me a test',
  360. int : 10,
  361. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  362. date : new Date( 81, 4, 13 )
  363. target.should.eql
  364. str : 'me a test',
  365. int : 10,
  366. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  367. date : new Date( 81, 4, 13 )
  368. describe 'should make deep clone: ', ->
  369. it 'object with object', ->
  370. ori =
  371. str : 'no shit',
  372. int : 76,
  373. arr : [ 1, 2, 3, 4 ],
  374. date : new Date( 81, 7, 26 )
  375. target = extend true, ori, deep
  376. ori.should.eql
  377. str : 'no shit',
  378. int : 76,
  379. arr : [ 1, 2, 3, 4 ],
  380. date : new Date( 81, 7, 26 ),
  381. ori :
  382. str : 'me a test',
  383. int : 10,
  384. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  385. date : new Date( 81, 4, 13 )
  386. layer :
  387. int : 10,
  388. str : 'str',
  389. date : new Date( 84, 5, 12 ),
  390. arr : [ 101, 'dude', new Date( 82, 10, 4 )],
  391. deep :
  392. str : 'me a test',
  393. int : 10,
  394. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  395. date : new Date( 81, 7, 4 )
  396. deep.should.eql
  397. ori :
  398. str : 'me a test',
  399. int : 10,
  400. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  401. date : new Date( 81, 4, 13 )
  402. layer :
  403. int : 10,
  404. str : 'str',
  405. date : new Date( 84, 5, 12 ),
  406. arr : [ 101, 'dude', new Date( 82, 10, 4 )],
  407. deep :
  408. str : 'me a test',
  409. int : 10,
  410. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  411. date : new Date( 81, 7, 4 )
  412. target.should.eql
  413. str : 'no shit',
  414. int : 76,
  415. arr : [ 1, 2, 3, 4 ],
  416. date : new Date( 81, 7, 26 ),
  417. ori :
  418. str : 'me a test',
  419. int : 10,
  420. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  421. date : new Date( 81, 4, 13 )
  422. layer :
  423. int : 10,
  424. str : 'str',
  425. date : new Date( 84, 5, 12 ),
  426. arr : [ 101, 'dude', new Date( 82, 10, 4 )],
  427. deep :
  428. str : 'me a test',
  429. int : 10,
  430. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  431. date : new Date( 81, 7, 4 )
  432. target.layer.deep = 339;
  433. deep.should.eql
  434. ori :
  435. str : 'me a test',
  436. int : 10,
  437. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  438. date : new Date( 81, 4, 13 )
  439. layer :
  440. int : 10,
  441. str : 'str',
  442. date : new Date( 84, 5, 12 ),
  443. arr : [ 101, 'dude', new Date( 82, 10, 4 )],
  444. deep :
  445. str : 'me a test',
  446. int : 10,
  447. arr : [ 1, 'what', new Date( 81, 8, 4 )],
  448. date : new Date( 81, 7, 4 )
  449. ###
  450. NEVER USE EXTEND WITH THE ABOVE SITUATION
  451. ###
  452. describe 'must pass additional test: ', ->
  453. it 'should merge objects with \'null\' and \'undefined\'', ->
  454. ori =
  455. a : 10
  456. b : null
  457. c : 'test data'
  458. d : undefined
  459. additional =
  460. x : 'googol'
  461. y : 8939843
  462. z : null
  463. az : undefined
  464. target = extend ori, additional
  465. target.should.to.be.eql
  466. a : 10
  467. b : null
  468. c : 'test data'
  469. d : undefined
  470. x : 'googol'
  471. y : 8939843
  472. z : null
  473. az : undefined