Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect } from 'vitest'; |
| 2 | import { QuoteStyle, escapeStringContent } from './QuoteStyle'; |
| 3 | import { stringLiteral } from './StringLiteralType'; |
| 4 | import { Writer } from './Writer'; |
| 5 | |
| 6 | describe('QuoteStyle', () => { |
| 7 | describe('escapeStringContent', () => { |
| 8 | it('should format with double quotes', () => { |
| 9 | expect(escapeStringContent('hello', QuoteStyle.Double)).toBe('"hello"'); |
| 10 | expect(escapeStringContent('hello "world"', QuoteStyle.Double)).toBe('"hello \\"world\\""'); |
| 11 | expect(escapeStringContent("hello 'world'", QuoteStyle.Double)).toBe('"hello \'world\'"'); |
| 12 | }); |
| 13 | |
| 14 | it('should format with single quotes', () => { |
| 15 | expect(escapeStringContent('hello', QuoteStyle.Single)).toBe("'hello'"); |
| 16 | expect(escapeStringContent("hello 'world'", QuoteStyle.Single)).toBe("'hello \\'world\\''"); |
| 17 | expect(escapeStringContent('hello "world"', QuoteStyle.Single)).toBe('\'hello "world"\''); |
| 18 | }); |
| 19 | |
| 20 | it('should format with backticks', () => { |
| 21 | expect(escapeStringContent('hello', QuoteStyle.Backtick)).toBe('`hello`'); |
| 22 | expect(escapeStringContent('hello `world`', QuoteStyle.Backtick)).toBe('`hello \\`world\\``'); |
| 23 | expect(escapeStringContent('hello $world', QuoteStyle.Backtick)).toBe('`hello \\$world`'); |
| 24 | }); |
| 25 | |
| 26 | it('should handle backslashes correctly', () => { |
| 27 | expect(escapeStringContent('hello\\world', QuoteStyle.Double)).toBe('"hello\\\\world"'); |
| 28 | expect(escapeStringContent('hello\\world', QuoteStyle.Single)).toBe("'hello\\\\world'"); |
| 29 | expect(escapeStringContent('hello\\world', QuoteStyle.Backtick)).toBe('`hello\\\\world`'); |
| 30 | }); |
| 31 | }); |
| 32 | |
| 33 | describe('StringLiteralType with different quote styles', () => { |
| 34 | it('should use double quotes by default', () => { |
| 35 | const literal = stringLiteral('hello world'); |
| 36 | const writer = new Writer(0, undefined); |
| 37 | literal.write(writer); |
| 38 | expect(writer.toString()).toBe('"hello world"'); |
| 39 | }); |
| 40 | |
| 41 | it('should use single quotes when configured', () => { |
| 42 | const literal = stringLiteral('hello world'); |
| 43 | const writer = new Writer(0, undefined, { quoteStyle: QuoteStyle.Single }); |
| 44 | literal.write(writer); |
| 45 | expect(writer.toString()).toBe("'hello world'"); |
| 46 | }); |
| 47 | |
| 48 | it('should use backticks when configured', () => { |
| 49 | const literal = stringLiteral('hello world'); |
| 50 | const writer = new Writer(0, undefined, { quoteStyle: QuoteStyle.Backtick }); |
| 51 | literal.write(writer); |
| 52 | expect(writer.toString()).toBe('`hello world`'); |
| 53 | }); |
| 54 | |
| 55 | it('should handle quotes in content correctly', () => { |
| 56 | const literal = stringLiteral('hello "world"'); |
| 57 | |
| 58 | // Double quotes - content quotes are escaped |
| 59 | const doubleWriter = new Writer(0, undefined, { quoteStyle: QuoteStyle.Double }); |
| 60 | literal.write(doubleWriter); |
| 61 | expect(doubleWriter.toString()).toBe('"hello \\"world\\""'); |
| 62 | |
| 63 | // Single quotes - content quotes are not escaped |
| 64 | const singleWriter = new Writer(0, undefined, { quoteStyle: QuoteStyle.Single }); |
| 65 | literal.write(singleWriter); |
| 66 | expect(singleWriter.toString()).toBe('\'hello "world"\''); |
| 67 | }); |
| 68 | |
| 69 | it('should handle single quotes in content correctly', () => { |
| 70 | const literal = stringLiteral("hello 'world'"); |
| 71 | |
| 72 | // Double quotes - content quotes are not escaped |
| 73 | const doubleWriter = new Writer(0, undefined, { quoteStyle: QuoteStyle.Double }); |
| 74 | literal.write(doubleWriter); |
| 75 | expect(doubleWriter.toString()).toBe('"hello \'world\'"'); |
| 76 | |
| 77 | // Single quotes - content quotes are escaped |
| 78 | const singleWriter = new Writer(0, undefined, { quoteStyle: QuoteStyle.Single }); |
| 79 | literal.write(singleWriter); |
| 80 | expect(singleWriter.toString()).toBe("'hello \\'world\\''"); |
| 81 | }); |
| 82 | |
| 83 | it('should handle special characters in backticks', () => { |
| 84 | const literal = stringLiteral('hello `$world`'); |
| 85 | const writer = new Writer(0, undefined, { quoteStyle: QuoteStyle.Backtick }); |
| 86 | literal.write(writer); |
| 87 | expect(writer.toString()).toBe('`hello \\`\\$world\\``'); |
| 88 | }); |
| 89 | }); |
| 90 | }); |