test.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* eslint-env mocha */
  2. /* eslint-disable max-nested-callbacks */
  3. 'use strict'
  4. const { assert } = require('chai')
  5. const Hoopy = require('.')
  6. test('interface is correct', () => {
  7. assert.isFunction(Hoopy)
  8. assert.lengthOf(Hoopy, 1)
  9. assert.throws(() => new Hoopy(0))
  10. assert.doesNotThrow(() => new Hoopy(1))
  11. assert.throws(() => new Hoopy(-1))
  12. assert.throws(() => new Hoopy(1).push())
  13. assert.throws(() => new Hoopy(1).pop())
  14. assert.throws(() => new Hoopy(1).shift())
  15. assert.throws(() => new Hoopy(1).unshift())
  16. })
  17. suite('instantiate, size=1:', () => {
  18. let hoopy
  19. setup(() => {
  20. hoopy = new Hoopy(1)
  21. })
  22. test('instance is array', () => {
  23. assert.isTrue(Array.isArray(hoopy))
  24. })
  25. test('length is correct', () => {
  26. assert.equal(hoopy.length, 1)
  27. })
  28. test('[0] is undefined', () => {
  29. assert.isUndefined(hoopy[0])
  30. })
  31. test('[1] is undefined', () => {
  32. assert.isUndefined(hoopy[1])
  33. })
  34. test('[-1] is undefined', () => {
  35. assert.isUndefined(hoopy[-1])
  36. })
  37. test('grow method is implemented', () => {
  38. assert.isFunction(hoopy.grow)
  39. assert.lengthOf(hoopy.grow, 1)
  40. })
  41. test('grow throws if by=0', () => {
  42. assert.throws(() => hoopy.grow(0))
  43. })
  44. suite('assign to [0]:', () => {
  45. setup(() => {
  46. hoopy[0] = 'foo'
  47. })
  48. test('[0] is set correctly', () => {
  49. assert.equal(hoopy[0], 'foo')
  50. })
  51. test('[1] is set correctly', () => {
  52. assert.equal(hoopy[1], 'foo')
  53. })
  54. test('[-1] is set correctly', () => {
  55. assert.equal(hoopy[-1], 'foo')
  56. })
  57. suite('assign to [1]:', () => {
  58. setup(() => {
  59. hoopy[1] = 'bar'
  60. })
  61. test('[0] is set correctly', () => {
  62. assert.equal(hoopy[0], 'bar')
  63. })
  64. test('[1] is set correctly', () => {
  65. assert.equal(hoopy[1], 'bar')
  66. })
  67. test('[-1] is set correctly', () => {
  68. assert.equal(hoopy[-1], 'bar')
  69. })
  70. })
  71. suite('grow, by=1:', () => {
  72. setup(() => {
  73. hoopy.grow(1)
  74. })
  75. test('length is correct', () => {
  76. assert.equal(hoopy.length, 2)
  77. })
  78. test('[0] is set correctly', () => {
  79. assert.equal(hoopy[0], 'foo')
  80. })
  81. test('[1] is undefined', () => {
  82. assert.isUndefined(hoopy[1])
  83. })
  84. test('[-1] is undefined', () => {
  85. assert.isUndefined(hoopy[-1])
  86. })
  87. })
  88. })
  89. })
  90. suite('instantiate, size=2:', () => {
  91. let hoopy
  92. setup(() => {
  93. hoopy = new Hoopy(2)
  94. })
  95. test('length is correct', () => {
  96. assert.equal(hoopy.length, 2)
  97. })
  98. suite('assign to [0]:', () => {
  99. setup(() => {
  100. hoopy[0] = 'foo'
  101. })
  102. test('[0] is set correctly', () => {
  103. assert.equal(hoopy[0], 'foo')
  104. })
  105. test('[1] is undefined', () => {
  106. assert.isUndefined(hoopy[1])
  107. })
  108. test('[2] is set correctly', () => {
  109. assert.equal(hoopy[2], 'foo')
  110. })
  111. test('[3] is undefined', () => {
  112. assert.isUndefined(hoopy[3])
  113. })
  114. test('[-1] is undefined', () => {
  115. assert.isUndefined(hoopy[-1])
  116. })
  117. suite('assign to [1]:', () => {
  118. setup(() => {
  119. hoopy[1] = 'bar'
  120. })
  121. test('[0] is set correctly', () => {
  122. assert.equal(hoopy[0], 'foo')
  123. })
  124. test('[1] is set correctly', () => {
  125. assert.equal(hoopy[1], 'bar')
  126. })
  127. test('[2] is set correctly', () => {
  128. assert.equal(hoopy[2], 'foo')
  129. })
  130. test('[-1] is set correctly', () => {
  131. assert.equal(hoopy[-1], 'bar')
  132. })
  133. suite('assign to [2]:', () => {
  134. setup(() => {
  135. hoopy[2] = 'baz'
  136. })
  137. test('[0] is set correctly', () => {
  138. assert.equal(hoopy[0], 'baz')
  139. })
  140. test('[1] is set correctly', () => {
  141. assert.equal(hoopy[1], 'bar')
  142. })
  143. test('[2] is set correctly', () => {
  144. assert.equal(hoopy[2], 'baz')
  145. })
  146. test('[-1] is set correctly', () => {
  147. assert.equal(hoopy[-1], 'bar')
  148. })
  149. suite('grow, by=1:', () => {
  150. setup(() => {
  151. hoopy.grow(1)
  152. })
  153. test('length is correct', () => {
  154. assert.equal(hoopy.length, 3)
  155. })
  156. test('[0] is undefined', () => {
  157. assert.isUndefined(hoopy[0])
  158. })
  159. test('[1] is set correctly', () => {
  160. assert.equal(hoopy[1], 'bar')
  161. })
  162. test('[2] is set correctly', () => {
  163. assert.equal(hoopy[2], 'baz')
  164. })
  165. test('[3] is undefined', () => {
  166. assert.isUndefined(hoopy[3])
  167. })
  168. })
  169. suite('grow, by=2:', () => {
  170. setup(() => {
  171. hoopy.grow(2)
  172. })
  173. test('length is correct', () => {
  174. assert.equal(hoopy.length, 4)
  175. })
  176. test('[0] is undefined', () => {
  177. assert.isUndefined(hoopy[0])
  178. })
  179. test('[1] is set correctly', () => {
  180. assert.equal(hoopy[1], 'bar')
  181. })
  182. test('[2] is set correctly', () => {
  183. assert.equal(hoopy[2], 'baz')
  184. })
  185. test('[3] is undefined', () => {
  186. assert.isUndefined(hoopy[3])
  187. })
  188. test('[4] is undefined', () => {
  189. assert.isUndefined(hoopy[4])
  190. })
  191. test('[5] is set correctly', () => {
  192. assert.equal(hoopy[5], 'bar')
  193. })
  194. })
  195. })
  196. })
  197. })
  198. })
  199. suite('instantiate and overflow, size=3:', () => {
  200. let hoopy
  201. setup(() => {
  202. hoopy = new Hoopy(3)
  203. hoopy[2] = 'foo'
  204. hoopy[3] = 'bar'
  205. hoopy[4] = 'baz'
  206. })
  207. test('data is correct', () => {
  208. assert.equal(hoopy.length, 3)
  209. assert.equal(hoopy[2], 'foo')
  210. assert.equal(hoopy[3], 'bar')
  211. assert.equal(hoopy[4], 'baz')
  212. assert.equal(hoopy[0], hoopy[3])
  213. assert.equal(hoopy[1], hoopy[4])
  214. })
  215. test('slice works correctly', () => {
  216. assert.equal(hoopy.slice(0, 3)[2], hoopy[2])
  217. })
  218. suite('grow, by=1:', () => {
  219. setup(() => {
  220. hoopy.grow(1)
  221. })
  222. test('data is correct', () => {
  223. assert.equal(hoopy.length, 4)
  224. assert.equal(hoopy[2], 'foo')
  225. assert.equal(hoopy[3], 'bar')
  226. assert.equal(hoopy[4], 'baz')
  227. assert.equal(hoopy[0], hoopy[4])
  228. assert.isUndefined(hoopy[1])
  229. })
  230. })
  231. suite('grow, by=2:', () => {
  232. setup(() => {
  233. hoopy.grow(2)
  234. })
  235. test('data is correct', () => {
  236. assert.equal(hoopy.length, 5)
  237. assert.equal(hoopy[2], 'foo')
  238. assert.equal(hoopy[3], 'bar')
  239. assert.equal(hoopy[4], 'baz')
  240. assert.isUndefined(hoopy[0])
  241. assert.isUndefined(hoopy[1])
  242. })
  243. })
  244. })