array-to-list.spec.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import {expect} from 'chai';
  2. import {arrayToList} from './array-to-list.js';
  3. describe('arrayToList', function () {
  4. describe('non-array values', function () {
  5. it('returns a string representation of the given string', function () {
  6. const res = arrayToList('foo');
  7. expect(res).to.be.eq('"foo"');
  8. });
  9. it('returns a string representation of the given empty string', function () {
  10. const res = arrayToList('');
  11. expect(res).to.be.eq('""');
  12. });
  13. it('returns a string representation of the given number', function () {
  14. const res = arrayToList(10);
  15. expect(res).to.be.eq('10');
  16. });
  17. it('returns a string representation of the given zero', function () {
  18. const res = arrayToList(0);
  19. expect(res).to.be.eq('0');
  20. });
  21. it('returns a string representation of the given NaN', function () {
  22. const res = arrayToList(NaN);
  23. expect(res).to.be.eq('NaN');
  24. });
  25. it('returns a string representation of the given Infinity', function () {
  26. const res = arrayToList(Infinity);
  27. expect(res).to.be.eq('Infinity');
  28. });
  29. it('returns a string representation of the given true', function () {
  30. const res = arrayToList(true);
  31. expect(res).to.be.eq('true');
  32. });
  33. it('returns a string representation of the given false', function () {
  34. const res = arrayToList(false);
  35. expect(res).to.be.eq('false');
  36. });
  37. it('returns a string representation of the given key-value object', function () {
  38. const res = arrayToList({foo: 'bar'});
  39. expect(res).to.be.eq('Object');
  40. });
  41. it('returns a string representation of the given object without keys', function () {
  42. const res = arrayToList({});
  43. expect(res).to.be.eq('Object');
  44. });
  45. it('returns a string representation of the given object without prototype', function () {
  46. const res = arrayToList(Object.create(null));
  47. expect(res).to.be.eq('Object');
  48. });
  49. it('returns a string representation of the given date instance', function () {
  50. const res = arrayToList(new Date());
  51. expect(res).to.be.eq('Date');
  52. });
  53. it('returns a string representation of the given map instance', function () {
  54. const res = arrayToList(new Map());
  55. expect(res).to.be.eq('Map');
  56. });
  57. it('returns a string representation of the given class instance', function () {
  58. class MyClass {}
  59. const res = arrayToList(new MyClass());
  60. expect(res).to.be.eq('MyClass (instance)');
  61. });
  62. it('returns a string representation of the given function', function () {
  63. const res = arrayToList(function () {});
  64. expect(res).to.be.eq('Function');
  65. });
  66. it('returns a string representation of the given anonymous function', function () {
  67. const res = arrayToList(() => undefined);
  68. expect(res).to.be.eq('Function');
  69. });
  70. it('returns a string representation of the given named function', function () {
  71. function foo() {}
  72. const res = arrayToList(foo);
  73. expect(res).to.be.eq('Function');
  74. });
  75. it('returns a string representation of the given anonymous class', function () {
  76. const res = arrayToList(class {});
  77. expect(res).to.be.eq('Class');
  78. });
  79. it('returns a string representation of the given named class', function () {
  80. class MyClass {}
  81. const res = arrayToList(MyClass);
  82. expect(res).to.be.eq('MyClass');
  83. });
  84. it('returns a string representation of the given class constructor', function () {
  85. class MyClass {}
  86. const res = arrayToList(MyClass.constructor);
  87. expect(res).to.be.eq('Function');
  88. });
  89. it('returns a string representation of the given symbol', function () {
  90. const res = arrayToList(Symbol());
  91. expect(res).to.be.eq('Symbol');
  92. });
  93. it('returns a string representation of the given named symbol', function () {
  94. const res = arrayToList(Symbol('foo'));
  95. expect(res).to.be.eq('Symbol');
  96. });
  97. it('returns a string representation of the given undefined', function () {
  98. const res = arrayToList(undefined);
  99. expect(res).to.be.eq('undefined');
  100. });
  101. it('returns a string representation of the given null', function () {
  102. const res = arrayToList(null);
  103. expect(res).to.be.eq('null');
  104. });
  105. });
  106. describe('array values', function () {
  107. it('adds a separator between the given elements', function () {
  108. const res = arrayToList(['foo', 1, true]);
  109. expect(res).to.be.eq('"foo", 1, true');
  110. });
  111. it('returns a string representation of the given empty array', function () {
  112. const res = arrayToList([]);
  113. expect(res).to.be.eq('Array');
  114. });
  115. it('returns an element representation of the given string', function () {
  116. const res = arrayToList(['foo']);
  117. expect(res).to.be.eq('"foo"');
  118. });
  119. it('returns an element representation of the given empty string', function () {
  120. const res = arrayToList(['']);
  121. expect(res).to.be.eq('""');
  122. });
  123. it('returns an element representation of the given number', function () {
  124. const res = arrayToList([10]);
  125. expect(res).to.be.eq('10');
  126. });
  127. it('returns an element representation of the given zero', function () {
  128. const res = arrayToList([0]);
  129. expect(res).to.be.eq('0');
  130. });
  131. it('returns an element representation of the given NaN', function () {
  132. const res = arrayToList([NaN]);
  133. expect(res).to.be.eq('NaN');
  134. });
  135. it('returns an element representation of the given Infinity', function () {
  136. const res = arrayToList([Infinity]);
  137. expect(res).to.be.eq('Infinity');
  138. });
  139. it('returns an element representation of the given true', function () {
  140. const res = arrayToList([true]);
  141. expect(res).to.be.eq('true');
  142. });
  143. it('returns an element representation of the given false', function () {
  144. const res = arrayToList([false]);
  145. expect(res).to.be.eq('false');
  146. });
  147. it('returns an element representation of the given key-value object', function () {
  148. const res = arrayToList([{foo: 'bar'}]);
  149. expect(res).to.be.eq('Object');
  150. });
  151. it('returns an element representation of the given object without keys', function () {
  152. const res = arrayToList([{}]);
  153. expect(res).to.be.eq('Object');
  154. });
  155. it('returns an element representation of the given object without prototype', function () {
  156. const res = arrayToList([Object.create(null)]);
  157. expect(res).to.be.eq('Object');
  158. });
  159. it('returns an element representation of the given date instance', function () {
  160. const res = arrayToList([new Date()]);
  161. expect(res).to.be.eq('Date');
  162. });
  163. it('returns an element representation of the given map instance', function () {
  164. const res = arrayToList([new Map()]);
  165. expect(res).to.be.eq('Map');
  166. });
  167. it('returns an element representation of the given class instance', function () {
  168. class MyClass {}
  169. const res = arrayToList([new MyClass()]);
  170. expect(res).to.be.eq('MyClass (instance)');
  171. });
  172. it('returns an element representation of the given function', function () {
  173. const res = arrayToList([function () {}]);
  174. expect(res).to.be.eq('Function');
  175. });
  176. it('returns an element representation of the given anonymous function', function () {
  177. const res = arrayToList([() => undefined]);
  178. expect(res).to.be.eq('Function');
  179. });
  180. it('returns an element representation of the given named function', function () {
  181. function foo() {}
  182. const res = arrayToList([foo]);
  183. expect(res).to.be.eq('Function');
  184. });
  185. it('returns an element representation of the given anonymous class', function () {
  186. const res = arrayToList([class {}]);
  187. expect(res).to.be.eq('Class');
  188. });
  189. it('returns an element representation of the given named class', function () {
  190. class MyClass {}
  191. const res = arrayToList([MyClass]);
  192. expect(res).to.be.eq('MyClass');
  193. });
  194. it('returns an element representation of the given class constructor', function () {
  195. class MyClass {}
  196. const res = arrayToList([MyClass.constructor]);
  197. expect(res).to.be.eq('Function');
  198. });
  199. it('returns an element representation of the given symbol', function () {
  200. const res = arrayToList([Symbol()]);
  201. expect(res).to.be.eq('Symbol');
  202. });
  203. it('returns an element representation of the given named symbol', function () {
  204. const res = arrayToList([Symbol('foo')]);
  205. expect(res).to.be.eq('Symbol');
  206. });
  207. it('returns an element representation of the given undefined', function () {
  208. const res = arrayToList([undefined]);
  209. expect(res).to.be.eq('undefined');
  210. });
  211. it('returns an element representation of the given null', function () {
  212. const res = arrayToList([null]);
  213. expect(res).to.be.eq('null');
  214. });
  215. });
  216. });