wc.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Phoenix Shell.
  5. *
  6. * Phoenix Shell is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import assert from 'assert';
  20. import { MakeTestContext } from './harness.js'
  21. import builtins from '../../src/puter-shell/coreutils/__exports__.js';
  22. export const runWcTests = () => {
  23. describe('wc', function () {
  24. const testCases = [
  25. {
  26. description: 'can read from stdin when given `-`',
  27. positionals: ['-'],
  28. stdin: 'Well hello friends!',
  29. expectedStdout: '0 3 19 -\n',
  30. },
  31. {
  32. description: 'reads from stdin when given no arguments',
  33. positionals: [],
  34. stdin: 'Well hello friends!',
  35. expectedStdout: '0 3 19\n',
  36. },
  37. {
  38. description: 'handles empty stdin',
  39. positionals: ['-'],
  40. stdin: '',
  41. expectedStdout: '0 0 0 -\n',
  42. },
  43. {
  44. description: 'counts newlines',
  45. positionals: ['-'],
  46. stdin: 'Well\nhello\nfriends!\n',
  47. expectedStdout: '3 3 20 -\n',
  48. },
  49. {
  50. description: '-lwc produces the default output',
  51. options: { bytes: true, lines: true, words: true },
  52. positionals: ['-'],
  53. stdin: 'Well\nhello\nfriends!\n',
  54. expectedStdout: '3 3 20 -\n',
  55. },
  56. {
  57. description: '-l outputs only lines',
  58. options: { lines: true },
  59. positionals: ['-'],
  60. stdin: 'Well\nhello\nmy friends!\n',
  61. expectedStdout: '3 -\n',
  62. },
  63. {
  64. description: '-w outputs only words',
  65. options: { words: true },
  66. positionals: ['-'],
  67. stdin: 'Well\nhello\nmy friends!\n',
  68. expectedStdout: '4 -\n',
  69. },
  70. {
  71. description: '-c outputs only bytes',
  72. options: { bytes: true },
  73. positionals: ['-'],
  74. stdin: '🖥️ Well\nhello\nmy friends!\n',
  75. expectedStdout: '31 -\n',
  76. },
  77. {
  78. description: '-m outputs only characters',
  79. options: { chars: true },
  80. positionals: ['-'],
  81. stdin: '🖥️ Well\nhello\nmy friends!\n',
  82. expectedStdout: '27 -\n',
  83. },
  84. {
  85. description: '-L outputs the maximum line length',
  86. options: { 'max-line-length': true },
  87. positionals: ['-'],
  88. stdin: '🖥️ Well\nhello\nmy friends!\n',
  89. expectedStdout: '11 -\n',
  90. },
  91. {
  92. description: '-L treats tabs as jumping to the next multiple of 8 columns',
  93. options: { 'max-line-length': true },
  94. positionals: ['-'],
  95. stdin: 'hi\tmum\t!\n',
  96. expectedStdout: '17 -\n',
  97. },
  98. {
  99. description: '-lwmcL outputs everything',
  100. options: { bytes: true, chars: true, lines: true, 'max-line-length': true, words: true },
  101. positionals: ['-'],
  102. stdin: '🖥️ Well\nhello\nmy friends!\n',
  103. expectedStdout: '3 5 27 31 11 -\n',
  104. },
  105. // TODO: Test with files once the harness supports that.
  106. ];
  107. for (const { description, options, positionals, stdin, expectedStdout } of testCases) {
  108. it(description, async () => {
  109. let ctx = MakeTestContext(builtins.wc, { positionals, values: options, stdinInputs: [stdin] });
  110. try {
  111. const result = await builtins.wc.execute(ctx);
  112. assert.equal(result, undefined, 'should exit successfully, returning nothing');
  113. } catch (e) {
  114. assert.fail(e);
  115. }
  116. assert.equal(ctx.externs.out.output, expectedStdout, 'wrong output written to stdout');
  117. assert.equal(ctx.externs.err.output, '', 'nothing should be written to stderr');
  118. });
  119. }
  120. });
  121. }