Skip to content

Commit 9b2be86

Browse files
committed
Generalize $$-string tests
1 parent c62ad72 commit 9b2be86

File tree

3 files changed

+25
-39
lines changed

3 files changed

+25
-39
lines changed

test/duckdb.test.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('DuckDBFormatter', () => {
5252
supportsOnConflict(format);
5353
supportsUpdate(format);
5454
supportsTruncateTable(format, { withTable: false, withoutTable: true });
55-
supportsStrings(format, ["''-qq", "X''", "B''", "E''"]);
55+
supportsStrings(format, ["''-qq", "X''", "B''", "E''", '$$']);
5656
supportsIdentifiers(format, [`""-qq`]);
5757
supportsBetween(format);
5858
// Missing: '::' type cast (tested separately)
@@ -137,26 +137,6 @@ describe('DuckDBFormatter', () => {
137137
`);
138138
});
139139

140-
// DuckDB-specific string types
141-
it('supports basic dollar-quoted strings', () => {
142-
expect(format('$$foo JOIN bar$$')).toBe('$$foo JOIN bar$$');
143-
expect(format('$$foo $ JOIN bar$$')).toBe('$$foo $ JOIN bar$$');
144-
expect(format('$$foo \n bar$$')).toBe('$$foo \n bar$$');
145-
expect(format('SELECT $$where$$ FROM $$update$$')).toBe(dedent`
146-
SELECT
147-
$$where$$
148-
FROM
149-
$$update$$
150-
`);
151-
});
152-
153-
// TODO: this conflicts with named parameter syntax: $foo
154-
it.skip('supports tagged dollar-quoted strings', () => {
155-
expect(format('$xxx$foo $$ LEFT JOIN $yyy$ bar$xxx$')).toBe(
156-
'$xxx$foo $$ LEFT JOIN $yyy$ bar$xxx$'
157-
);
158-
});
159-
160140
it('formats type-cast operator without spaces', () => {
161141
expect(format('SELECT 2 :: numeric AS foo;')).toBe(dedent`
162142
SELECT

test/features/strings.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ type StringType =
1818
| 'B""' // no escaping
1919
| "R''" // no escaping
2020
| 'R""' // no escaping
21-
| "E''"; // with backslash escaping
21+
| "E''" // with backslash escaping
22+
| '$$'; // no escaping
2223

2324
export default function supportsStrings(format: FormatFn, stringTypes: StringType[]) {
2425
if (stringTypes.includes('""-qq') || stringTypes.includes('""-bs')) {
@@ -257,4 +258,25 @@ export default function supportsStrings(format: FormatFn, stringTypes: StringTyp
257258
expect(format(`e'a ha'e'hm mm'`)).toBe(`e'a ha' e'hm mm'`);
258259
});
259260
}
261+
262+
if (stringTypes.includes('$$')) {
263+
it('supports dollar-quoted strings', () => {
264+
expect(format('$$foo JOIN bar$$')).toBe('$$foo JOIN bar$$');
265+
expect(format('$$foo $ JOIN bar$$')).toBe('$$foo $ JOIN bar$$');
266+
expect(format('$$foo \n bar$$')).toBe('$$foo \n bar$$');
267+
expect(format('SELECT $$where$$ FROM $$update$$')).toBe(dedent`
268+
SELECT
269+
$$where$$
270+
FROM
271+
$$update$$
272+
`);
273+
});
274+
275+
// TODO: this conflicts with named parameter syntax: $foo
276+
it.skip('supports tagged dollar-quoted strings', () => {
277+
expect(format('$xxx$foo $$ LEFT JOIN $yyy$ bar$xxx$')).toBe(
278+
'$xxx$foo $$ LEFT JOIN $yyy$ bar$xxx$'
279+
);
280+
});
281+
}
260282
}

test/postgresql.test.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('PostgreSqlFormatter', () => {
5555
supportsOnConflict(format);
5656
supportsUpdate(format, { whereCurrentOf: true });
5757
supportsTruncateTable(format, { withoutTable: true });
58-
supportsStrings(format, ["''-qq", "U&''", "X''", "B''", "E''"]);
58+
supportsStrings(format, ["''-qq", "U&''", "X''", "B''", "E''", '$$']);
5959
supportsIdentifiers(format, [`""-qq`, 'U&""']);
6060
supportsBetween(format);
6161
supportsSchema(format);
@@ -172,22 +172,6 @@ describe('PostgreSqlFormatter', () => {
172172
`);
173173
});
174174

175-
// Postgres-specific string types
176-
it('supports dollar-quoted strings', () => {
177-
expect(format('$xxx$foo $$ LEFT JOIN $yyy$ bar$xxx$')).toBe(
178-
'$xxx$foo $$ LEFT JOIN $yyy$ bar$xxx$'
179-
);
180-
expect(format('$$foo JOIN bar$$')).toBe('$$foo JOIN bar$$');
181-
expect(format('$$foo $ JOIN bar$$')).toBe('$$foo $ JOIN bar$$');
182-
expect(format('$$foo \n bar$$')).toBe('$$foo \n bar$$');
183-
expect(format('SELECT $$where$$ FROM $$update$$')).toBe(dedent`
184-
SELECT
185-
$$where$$
186-
FROM
187-
$$update$$
188-
`);
189-
});
190-
191175
it('formats type-cast operator without spaces', () => {
192176
expect(format('SELECT 2 :: numeric AS foo;')).toBe(dedent`
193177
SELECT

0 commit comments

Comments
 (0)