value-to-string.spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {expect} from 'chai';
  2. import {valueToString} from './value-to-string.js';
  3. describe('valueToString', function () {
  4. it('returns a string representation of the given string', function () {
  5. const res = valueToString('foo');
  6. expect(res).to.be.eq('"foo"');
  7. });
  8. it('returns a string representation of the given empty string', function () {
  9. const res = valueToString('');
  10. expect(res).to.be.eq('""');
  11. });
  12. it('returns a string representation of the given number', function () {
  13. const res = valueToString(10);
  14. expect(res).to.be.eq('10');
  15. });
  16. it('returns a string representation of the given zero', function () {
  17. const res = valueToString(0);
  18. expect(res).to.be.eq('0');
  19. });
  20. it('returns a string representation of the given NaN', function () {
  21. const res = valueToString(NaN);
  22. expect(res).to.be.eq('NaN');
  23. });
  24. it('returns a string representation of the given Infinity', function () {
  25. const res = valueToString(Infinity);
  26. expect(res).to.be.eq('Infinity');
  27. });
  28. it('returns a string representation of the given true', function () {
  29. const res = valueToString(true);
  30. expect(res).to.be.eq('true');
  31. });
  32. it('returns a string representation of the given false', function () {
  33. const res = valueToString(false);
  34. expect(res).to.be.eq('false');
  35. });
  36. it('returns a string representation of the given array', function () {
  37. const res = valueToString([1, 2, 3]);
  38. expect(res).to.be.eq('Array');
  39. });
  40. it('returns a string representation of the given empty array', function () {
  41. const res = valueToString([]);
  42. expect(res).to.be.eq('Array');
  43. });
  44. it('returns a string representation of the given key-value object', function () {
  45. const res = valueToString({foo: 'bar'});
  46. expect(res).to.be.eq('Object');
  47. });
  48. it('returns a string representation of the given object without keys', function () {
  49. const res = valueToString({});
  50. expect(res).to.be.eq('Object');
  51. });
  52. it('returns a string representation of the given object without prototype', function () {
  53. const res = valueToString(Object.create(null));
  54. expect(res).to.be.eq('Object');
  55. });
  56. it('returns a string representation of the given date instance', function () {
  57. const res = valueToString(new Date());
  58. expect(res).to.be.eq('Date');
  59. });
  60. it('returns a string representation of the given map instance', function () {
  61. const res = valueToString(new Map());
  62. expect(res).to.be.eq('Map');
  63. });
  64. it('returns a string representation of the given class instance', function () {
  65. class MyClass {}
  66. const res = valueToString(new MyClass());
  67. expect(res).to.be.eq('MyClass');
  68. });
  69. it('returns a string representation of the given function', function () {
  70. const res = valueToString(function () {});
  71. expect(res).to.be.eq('Function');
  72. });
  73. it('returns a string representation of the given anonymous function', function () {
  74. const res = valueToString(() => undefined);
  75. expect(res).to.be.eq('Function');
  76. });
  77. it('returns a string representation of the given named function', function () {
  78. function foo() {}
  79. const res = valueToString(foo);
  80. expect(res).to.be.eq('Function');
  81. });
  82. it('returns a string representation of the given anonymous class', function () {
  83. const res = valueToString(class {});
  84. expect(res).to.be.eq('Class');
  85. });
  86. it('returns a string representation of the given named class', function () {
  87. class MyClass {}
  88. const res = valueToString(MyClass);
  89. expect(res).to.be.eq('MyClass');
  90. });
  91. it('returns a string representation of the given class constructor', function () {
  92. class MyClass {}
  93. const res = valueToString(MyClass.constructor);
  94. expect(res).to.be.eq('Function');
  95. });
  96. it('returns a string representation of the given symbol', function () {
  97. const res = valueToString(Symbol());
  98. expect(res).to.be.eq('Symbol');
  99. });
  100. it('returns a string representation of the given named symbol', function () {
  101. const res = valueToString(Symbol('foo'));
  102. expect(res).to.be.eq('Symbol');
  103. });
  104. it('returns a string representation of the given undefined', function () {
  105. const res = valueToString(undefined);
  106. expect(res).to.be.eq('undefined');
  107. });
  108. it('returns a string representation of the given null', function () {
  109. const res = valueToString(null);
  110. expect(res).to.be.eq('null');
  111. });
  112. });