shell-session-spec.js 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. describe("Shell session grammar", () => {
  2. let grammar = null;
  3. beforeEach(() => {
  4. atom.config.set('core.useTreeSitterParsers', false);
  5. waitsForPromise(() => atom.packages.activatePackage("language-shellscript"));
  6. runs(() => grammar = atom.grammars.grammarForScopeName("text.shell-session"));
  7. });
  8. it("parses the grammar", () => {
  9. expect(grammar).toBeDefined();
  10. expect(grammar.scopeName).toBe("text.shell-session");
  11. });
  12. const prompts = [">", "$", "#", "%", "❯", "➜"];
  13. it("tokenizes prompts", () => (() => {
  14. const result = [];
  15. for (let delim of prompts) {
  16. const {tokens} = grammar.tokenizeLine(delim + ' echo $FOO');
  17. expect(tokens[0]).toEqual({value: delim, scopes: ['text.shell-session', 'punctuation.separator.prompt.shell-session']});
  18. expect(tokens[1]).toEqual({value: ' ', scopes: ['text.shell-session']});
  19. result.push(expect(tokens[2]).toEqual({value: 'echo', scopes: ['text.shell-session', 'source.shell', 'support.function.builtin.shell']}));
  20. }
  21. return result;
  22. })());
  23. it("tokenises prompts with Greek characters", () => {
  24. const sigils = ["λ", "Λ", "Δ", "Σ", "Ω"];
  25. return (() => {
  26. const result = [];
  27. for (let sigil of sigils) {
  28. const lines = grammar.tokenizeLines(`\
  29. ${sigil} echo ${sigil}μμ
  30. O${sigil}tput Ω\
  31. `
  32. );
  33. expect(lines[0][0]).toEqual({value: sigil, scopes: ['text.shell-session', 'punctuation.separator.prompt.shell-session']});
  34. expect(lines[0][2]).toEqual({value: 'echo', scopes: ['text.shell-session', 'source.shell', 'support.function.builtin.shell']});
  35. expect(lines[0][3]).toEqual({value: ` ${sigil}μμ`, scopes: ['text.shell-session', 'source.shell']});
  36. result.push(expect(lines[1][0]).toEqual({value: `O${sigil}tput Ω`, scopes: ['text.shell-session', 'meta.output.shell-session']}));
  37. }
  38. return result;
  39. })();
  40. });
  41. it("does not tokenize prompts with indents", () => (() => {
  42. const result = [];
  43. for (let delim of prompts) {
  44. const {tokens} = grammar.tokenizeLine(' ' + delim + ' echo $FOO');
  45. result.push(expect(tokens[0]).toEqual({value: ' ' + delim + ' echo $FOO', scopes: ['text.shell-session', 'meta.output.shell-session']}));
  46. }
  47. return result;
  48. })());
  49. it("tokenizes prompts with prefixes", () => {
  50. const {tokens} = grammar.tokenizeLine('user@machine $ echo $FOO');
  51. expect(tokens[0]).toEqual({value: 'user@machine', scopes: ['text.shell-session', 'entity.other.prompt-prefix.shell-session']});
  52. expect(tokens[1]).toEqual({value: ' ', scopes: ['text.shell-session']});
  53. expect(tokens[2]).toEqual({value: '$', scopes: ['text.shell-session', 'punctuation.separator.prompt.shell-session']});
  54. expect(tokens[3]).toEqual({value: ' ', scopes: ['text.shell-session']});
  55. expect(tokens[4]).toEqual({value: 'echo', scopes: ['text.shell-session', 'source.shell', 'support.function.builtin.shell']});
  56. });
  57. it("tokenizes prompts with prefixes and a leading parenthetical", () => {
  58. const {tokens} = grammar.tokenizeLine('(venv) machine:pwd user$ echo $FOO');
  59. expect(tokens[0]).toEqual({value: '(venv) machine:pwd user', scopes: ['text.shell-session', 'entity.other.prompt-prefix.shell-session']});
  60. expect(tokens[1]).toEqual({value: '$', scopes: ['text.shell-session', 'punctuation.separator.prompt.shell-session']});
  61. expect(tokens[2]).toEqual({value: ' ', scopes: ['text.shell-session']});
  62. expect(tokens[3]).toEqual({value: 'echo', scopes: ['text.shell-session', 'source.shell', 'support.function.builtin.shell']});
  63. });
  64. it("tokenizes prompts with prefixes with brackets", () => {
  65. const {tokens} = grammar.tokenizeLine('[user@machine pwd]$ echo $FOO');
  66. expect(tokens[0]).toEqual({value: '[user@machine pwd]', scopes: ['text.shell-session', 'entity.other.prompt-prefix.shell-session']});
  67. expect(tokens[1]).toEqual({value: '$', scopes: ['text.shell-session', 'punctuation.separator.prompt.shell-session']});
  68. expect(tokens[2]).toEqual({value: ' ', scopes: ['text.shell-session']});
  69. expect(tokens[3]).toEqual({value: 'echo', scopes: ['text.shell-session', 'source.shell', 'support.function.builtin.shell']});
  70. });
  71. it("tokenizes shell output", () => {
  72. const tokens = grammar.tokenizeLines(`\
  73. $ echo $FOO
  74. foo\
  75. `
  76. );
  77. expect(tokens[1][0]).toEqual({value: 'foo', scopes: ['text.shell-session', 'meta.output.shell-session']});
  78. });
  79. });