util.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import {
  2. hasCR,
  3. isSpace,
  4. seedTokens,
  5. seedBlock,
  6. splitLines,
  7. splitSpace,
  8. seedSpec,
  9. } from '../../src/util.js';
  10. test.each([
  11. ['beginning', '\r to end', false],
  12. ['middle', 'has \r in middle', false],
  13. ['ending', 'only at end \r', true],
  14. ['none', 'no carriage returns', false],
  15. ])('carriage returns - %s', (name, source, boolResult) => {
  16. expect(hasCR(source)).toEqual(boolResult);
  17. });
  18. test.each([
  19. ['win', 'a\r\nb\r\nc', ['a\r', 'b\r', 'c']],
  20. ['unix', 'a\nb\nc', ['a', 'b', 'c']],
  21. ['mixed', 'a\nb\r\nc', ['a', 'b\r', 'c']],
  22. ['none', 'abc', ['abc']],
  23. ])('spliLines - %s', (name, source, parsed) =>
  24. expect(splitLines(source)).toEqual(parsed)
  25. );
  26. test.each([
  27. ['pre', ' abc', [' ', 'abc']],
  28. ['pre', 'abc ', ['', 'abc ']],
  29. ['pre+post', ' abc ', [' ', 'abc ']],
  30. ['none', 'abc', ['', 'abc']],
  31. ])('spliSpace - %s', (name, source, parsed) =>
  32. expect(splitSpace(source)).toEqual(parsed)
  33. );
  34. test.each([
  35. ['space', ' ', true],
  36. ['spaces', ' ', true],
  37. ['tab', '\t', true],
  38. ['tabs', '\t\t', true],
  39. ['line end', '\n', true],
  40. ['line ends', '\n\n', true],
  41. ['line return', '\r', true],
  42. ['line returns', '\r\r', true],
  43. ['mixed space', '\n\r\t', true],
  44. ['mixed', '\naba', false],
  45. ['alpahnumeric', '1abcd34', false],
  46. ['symbols', '*', false],
  47. ['empty', '', false],
  48. ])('isSpace - %s', (name, source, result) =>
  49. expect(isSpace(source)).toBe(result)
  50. );
  51. test('seedTokens defaults', () => {
  52. expect(seedTokens()).toEqual({
  53. start: '',
  54. delimiter: '',
  55. postDelimiter: '',
  56. tag: '',
  57. postTag: '',
  58. name: '',
  59. postName: '',
  60. type: '',
  61. postType: '',
  62. description: '',
  63. end: '',
  64. lineEnd: '',
  65. });
  66. });
  67. test('seedTokens overrides', () => {
  68. expect(seedTokens({ description: 'abc' })).toEqual({
  69. start: '',
  70. delimiter: '',
  71. postDelimiter: '',
  72. tag: '',
  73. postTag: '',
  74. name: '',
  75. postName: '',
  76. type: '',
  77. postType: '',
  78. description: 'abc',
  79. end: '',
  80. lineEnd: '',
  81. });
  82. });
  83. test('seedBlock defaults', () => {
  84. expect(seedBlock()).toEqual({
  85. description: '',
  86. tags: [],
  87. source: [],
  88. problems: [],
  89. });
  90. });
  91. test('seedBlock overrides', () => {
  92. expect(seedBlock({ description: 'abc' })).toEqual({
  93. description: 'abc',
  94. tags: [],
  95. source: [],
  96. problems: [],
  97. });
  98. });
  99. test('seedSpec defaults', () => {
  100. expect(seedSpec()).toEqual({
  101. tag: '',
  102. name: '',
  103. type: '',
  104. optional: false,
  105. description: '',
  106. problems: [],
  107. source: [],
  108. });
  109. });
  110. test('seedSpec overrides', () => {
  111. expect(seedSpec({ description: 'abc' })).toEqual({
  112. tag: '',
  113. name: '',
  114. type: '',
  115. optional: false,
  116. description: 'abc',
  117. problems: [],
  118. source: [],
  119. });
  120. });