array-to-string.spec.js 8.9 KB

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