-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
260 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
generators/typescript/codegen/src/ast/__test__/TypeLiteral.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,149 @@ | ||
import { ts } from "../.."; | ||
|
||
describe("TypeLiteral", () => { | ||
describe("emptyArrayToString", () => { | ||
it("Should generate an empty array", () => { | ||
const literal = ts.TypeLiteral.array({ | ||
valueType: ts.Type.string(), | ||
fields: [] | ||
}); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("arrayOfStringsToString", () => { | ||
it("Should generate an array of strings", () => { | ||
const literal = ts.TypeLiteral.array({ | ||
valueType: ts.Type.string(), | ||
fields: [ | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Hello, World!")), | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Goodbye, World!")) | ||
] | ||
}); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
// N.B. If the array is too short prettier is going to print it on a single line | ||
describe("multilineArrayOfStringsToString", () => { | ||
it("Should generate an array of strings", () => { | ||
const literal = ts.TypeLiteral.array({ | ||
valueType: ts.Type.string(), | ||
fields: [ | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Hello, World!")), | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Goodbye, World!")), | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Hello, World!")), | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Goodbye, World!")), | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Hello, World!")), | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Goodbye, World!")), | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Hello, World!")), | ||
ts.TypeLiteral.arrayField(ts.TypeLiteral.string("Goodbye, World!")) | ||
], | ||
multiline: true | ||
}); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("trueBooleanToString", () => { | ||
it("Should generate a true boolean", () => { | ||
const literal = ts.TypeLiteral.boolean(true); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("falseBooleanToString", () => { | ||
it("Should generate a true boolean", () => { | ||
const literal = ts.TypeLiteral.boolean(false); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("numberToString", () => { | ||
it("Should generate a simple number", () => { | ||
const literal = ts.TypeLiteral.number(7); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("stringToString", () => { | ||
it("Should generate a simple string literal", () => { | ||
const literal = ts.TypeLiteral.string("Hello, World!"); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("stringWithDoubleQuotesToString", () => { | ||
it("Should generate a simple string literal with escaped double quotes", () => { | ||
const literal = ts.TypeLiteral.string('"Hello, World!"'); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("manyLinesMultilineStringToString", () => { | ||
it("Should generate a multiline string with backticks", () => { | ||
const literal = ts.TypeLiteral.string(`Hello, | ||
World!`); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("manyLinesMultilineStringWithBackticksToString", () => { | ||
it("Should generate a multiline string with escaped backticks", () => { | ||
const literal = ts.TypeLiteral.string(`\`Hello, | ||
World!\``); | ||
expect(literal.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("simpleObjectToString", () => { | ||
it("Should generate a simple object", () => { | ||
const actual = ts.codeblock((writer) => { | ||
writer.write("let myObj = "); | ||
writer.writeNode( | ||
ts.TypeLiteral.object({ | ||
fields: [ | ||
ts.TypeLiteral.objectField({ | ||
name: "name", | ||
valueType: ts.Type.string(), | ||
value: ts.TypeLiteral.string("John Smith") | ||
}), | ||
ts.TypeLiteral.objectField({ | ||
name: "hometown", | ||
valueType: ts.Type.string(), | ||
value: ts.TypeLiteral.string("New York, New York") | ||
}) | ||
] | ||
}) | ||
); | ||
}); | ||
expect(actual.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
||
describe("multilineObjectToString", () => { | ||
it("Should generate a multiline object", () => { | ||
const actual = ts.codeblock((writer) => { | ||
writer.write("let myObj = "); | ||
writer.writeNode( | ||
ts.TypeLiteral.object({ | ||
fields: [ | ||
ts.TypeLiteral.objectField({ | ||
name: "name", | ||
valueType: ts.Type.string(), | ||
value: ts.TypeLiteral.string("John Smith") | ||
}), | ||
ts.TypeLiteral.objectField({ | ||
name: "hometown", | ||
valueType: ts.Type.string(), | ||
value: ts.TypeLiteral.string("New York, New York") | ||
}) | ||
], | ||
multiline: true | ||
}) | ||
); | ||
}); | ||
expect(actual.toStringFormatted()).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); |
74 changes: 72 additions & 2 deletions
74
generators/typescript/codegen/src/ast/__test__/__snapshots__/TypeLiteral.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,75 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`TypeLiteral > numberToString > Should generate a simple number`] = ` | ||
"7 | ||
exports[`TypeLiteral > arrayOfStringsToString > Should generate an array of strings 1`] = ` | ||
"["Hello, World!", "Goodbye, World!"]; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > emptyArrayToString > Should generate an empty array 1`] = ` | ||
"[]; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > falseBooleanToString > Should generate a true boolean 1`] = ` | ||
"false; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > manyLinesMultilineStringToString > Should generate a multiline string with backticks 1`] = ` | ||
"\`Hello, | ||
World!\`; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > manyLinesMultilineStringWithBackticksToString > Should generate a multiline string with escaped backticks 1`] = ` | ||
"\`\\\`Hello, | ||
World!\\\`\`; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > multilineArrayOfStringsToString > Should generate an array of strings 1`] = ` | ||
"[ | ||
"Hello, World!", | ||
"Goodbye, World!", | ||
"Hello, World!", | ||
"Goodbye, World!", | ||
"Hello, World!", | ||
"Goodbye, World!", | ||
"Hello, World!", | ||
"Goodbye, World!", | ||
]; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > multilineObjectToString > Should generate a multiline object 1`] = ` | ||
"let myObj = { | ||
name: "John Smith", | ||
hometown: "New York, New York", | ||
}; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > numberToString > Should generate a simple number 1`] = ` | ||
"7; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > simpleObjectToString > Should generate a simple object 1`] = ` | ||
"let myObj = { name: "John Smith", hometown: "New York, New York" }; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > stringToString > Should generate a simple string literal 1`] = ` | ||
""Hello, World!"; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > stringWithDoubleQuotesToString > Should generate a simple string literal with escaped double quotes 1`] = ` | ||
""\\"Hello, World!\\""; | ||
" | ||
`; | ||
|
||
exports[`TypeLiteral > trueBooleanToString > Should generate a true boolean 1`] = ` | ||
"true; | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters