|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { EOL } from "os"; |
| 3 | +import * as Factory from "../factory"; |
| 4 | + |
| 5 | +describe("TsGenerator Factory Helpers", () => { |
| 6 | + describe("escapeTemplateText", () => { |
| 7 | + it("テンプレートリテラル内の特殊文字をエスケープできること", () => { |
| 8 | + // バックスラッシュ、バッククォート、${ をエスケープする |
| 9 | + expect(Factory.escapeTemplateText("path\\to\\file")).toBe("path\\\\to\\\\file"); |
| 10 | + expect(Factory.escapeTemplateText("`quoted`")).toBe("\\`quoted\\`"); |
| 11 | + expect(Factory.escapeTemplateText("${variable}")).toBe("\\${variable}"); |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + describe("escapeIdentifier", () => { |
| 16 | + it("識別子に含まれるハイフンをアンダースコアに置換できること", () => { |
| 17 | + expect(Factory.escapeIdentifier("my-variable-name")).toBe("my_variable_name"); |
| 18 | + expect(Factory.escapeIdentifier("nochange")).toBe("nochange"); |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("indentLines", () => { |
| 23 | + it("各行にインデントを付与し、空行はそのままにすること", () => { |
| 24 | + const input = "line1\n\nline2"; |
| 25 | + const expected = " line1\n\n line2"; |
| 26 | + expect(Factory.indentLines(input, " ")).toBe(expected); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("hasTopLevelOp", () => { |
| 31 | + it("トップレベルに | または & がある場合は true を返すこと", () => { |
| 32 | + expect(Factory.hasTopLevelOp("A | B")).toBe(true); |
| 33 | + expect(Factory.hasTopLevelOp("A & B")).toBe(true); |
| 34 | + }); |
| 35 | + |
| 36 | + it("括弧や型引数の中にある演算子は無視されること", () => { |
| 37 | + expect(Factory.hasTopLevelOp("(A | B)")).toBe(false); |
| 38 | + expect(Factory.hasTopLevelOp("Array<A | B>")).toBe(false); |
| 39 | + expect(Factory.hasTopLevelOp("{ prop: A | B }")).toBe(false); |
| 40 | + expect(Factory.hasTopLevelOp("[A | B]")).toBe(false); |
| 41 | + }); |
| 42 | + |
| 43 | + it("ネストが深い場合の演算子も正しく無視されること", () => { |
| 44 | + expect(Factory.hasTopLevelOp("A | Array<{ p: B & C }>")).toBe(true); |
| 45 | + expect(Factory.hasTopLevelOp("Array<{ p: B & C }> | D")).toBe(true); |
| 46 | + expect(Factory.hasTopLevelOp("Map<K, V | T>")).toBe(false); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + describe("buildComment", () => { |
| 51 | + it("単一ラインのコメントを生成できること", () => { |
| 52 | + expect(Factory.buildComment("hello")).toBe(`/** hello */${EOL}`); |
| 53 | + }); |
| 54 | + |
| 55 | + it("複数ラインのコメントを生成できること", () => { |
| 56 | + const input = "line1\nline2"; |
| 57 | + const expected = `/**${EOL} * line1${EOL} * line2${EOL} */${EOL}`; |
| 58 | + expect(Factory.buildComment(input)).toBe(expected); |
| 59 | + }); |
| 60 | + |
| 61 | + it("deprecated フラグがある場合に @deprecated タグを付与すること", () => { |
| 62 | + const expected = `/**${EOL} * @deprecated${EOL} * old feature${EOL} */${EOL}`; |
| 63 | + expect(Factory.buildComment("old feature", true)).toBe(expected); |
| 64 | + }); |
| 65 | + |
| 66 | + it("コメント内の特殊な記号をエスケープすること", () => { |
| 67 | + expect(Factory.buildComment("*/")).toContain("\\*\\\\/"); |
| 68 | + expect(Factory.buildComment("/*")).toContain("/\\\\*"); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe("addComment", () => { |
| 73 | + it("コメントがある場合にコードの前に付与すること", () => { |
| 74 | + const code = "const a = 1;"; |
| 75 | + const comment = "my variable"; |
| 76 | + const result = Factory.addComment(code, comment); |
| 77 | + expect(result).toBe(`/** my variable */${EOL}${code}`); |
| 78 | + }); |
| 79 | + |
| 80 | + it("コメントも deprecated もない場合は元のコードを返すこと", () => { |
| 81 | + const code = "const a = 1;"; |
| 82 | + expect(Factory.addComment(code)).toBe(code); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe("TsGenerator Factory Create API", () => { |
| 88 | + const factory = Factory.create(); |
| 89 | + |
| 90 | + describe("StringLiteral", () => { |
| 91 | + it("文字列リテラルを生成できること(ダブルクォート)", () => { |
| 92 | + expect(factory.StringLiteral.create({ text: 'hello "world"' })).toBe('"hello \\"world\\""'); |
| 93 | + }); |
| 94 | + |
| 95 | + it("文字列リテラルを生成できること(シングルクォート)", () => { |
| 96 | + expect(factory.StringLiteral.create({ text: "hello 'world'", isSingleQuote: true })).toBe("'hello \\'world\\''"); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe("TypeNode", () => { |
| 101 | + it("プリミティブ型を生成できること", () => { |
| 102 | + expect(factory.TypeNode.create({ type: "string" })).toBe("string"); |
| 103 | + expect(factory.TypeNode.create({ type: "number" })).toBe("number"); |
| 104 | + expect(factory.TypeNode.create({ type: "boolean" })).toBe("boolean"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("enum 文字列型を生成できること", () => { |
| 108 | + expect(factory.TypeNode.create({ type: "string", enum: ["a", "b"] })).toBe('"a" | "b"'); |
| 109 | + }); |
| 110 | + |
| 111 | + it("配列型を生成できること", () => { |
| 112 | + expect(factory.TypeNode.create({ type: "array", value: "string" })).toBe("string[]"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("トップレベル演算子を含む型の配列は括弧で囲まれること", () => { |
| 116 | + expect(factory.TypeNode.create({ type: "array", value: "string | number" })).toBe("(string | number)[]"); |
| 117 | + }); |
| 118 | + |
| 119 | + it("オブジェクト型(インライン)を生成できること", () => { |
| 120 | + const result = factory.TypeNode.create({ type: "object", value: ["id: string;", "name: string;"] }); |
| 121 | + expect(result).toBe("{\n id: string;\n name: string;\n}"); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + describe("UnionTypeNode", () => { |
| 126 | + it("ユニオン型を生成できること", () => { |
| 127 | + expect(factory.UnionTypeNode.create({ typeNodes: ["string", "number"] })).toBe("string | number"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("ネストした演算子を持つ型は括弧で囲まれること", () => { |
| 131 | + expect(factory.UnionTypeNode.create({ typeNodes: ["A & B", "C"] })).toBe("(A & B) | C"); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe("InterfaceDeclaration", () => { |
| 136 | + it("インターフェース宣言を生成できること", () => { |
| 137 | + const result = factory.InterfaceDeclaration.create({ |
| 138 | + name: "MyInterface", |
| 139 | + members: ["id: string;", "name?: string;"], |
| 140 | + export: true, |
| 141 | + }); |
| 142 | + expect(result).toBe("export interface MyInterface {\n id: string;\n name?: string;\n}"); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe("Namespace", () => { |
| 147 | + it("名前空間を生成できること", () => { |
| 148 | + const result = factory.Namespace.create({ |
| 149 | + name: "MyNamespace", |
| 150 | + statements: ["export type T = string;"], |
| 151 | + export: true, |
| 152 | + }); |
| 153 | + expect(result).toBe("export namespace MyNamespace {\n export type T = string;\n}"); |
| 154 | + }); |
| 155 | + |
| 156 | + it("ネストした名前空間を一括生成できること", () => { |
| 157 | + const result = factory.Namespace.createMultiple({ |
| 158 | + names: ["A", "B", "C"], |
| 159 | + statements: ["export const v = 1;"], |
| 160 | + export: true, |
| 161 | + }); |
| 162 | + expect(result).toContain("namespace A"); |
| 163 | + expect(result).toContain("namespace B"); |
| 164 | + expect(result).toContain("namespace C"); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments