From 453cb0cd3a63e5802ef91942dfa50395c561d7ef Mon Sep 17 00:00:00 2001 From: suojae Date: Mon, 25 Nov 2024 21:37:38 +0900 Subject: [PATCH] test(lib): add test for concat method in TokenizedStringFragments Added a test case to verify that the `concat` method correctly merges fragments from another instance. --- .../test/tokens/string-fragments.test.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 packages/cdktf/test/tokens/string-fragments.test.ts diff --git a/packages/cdktf/test/tokens/string-fragments.test.ts b/packages/cdktf/test/tokens/string-fragments.test.ts new file mode 100644 index 0000000000..40783f3a43 --- /dev/null +++ b/packages/cdktf/test/tokens/string-fragments.test.ts @@ -0,0 +1,32 @@ +// string-fragments.test.ts +// SPDX-License-Identifier: MPL-2.0 +import { TokenizedStringFragments } from "../../lib/tokens/string-fragments"; +import { IFragmentConcatenator } from "../../lib/tokens/resolvable"; + +// Helper function to create fragments with literals +function createFragments(...literals: string[]): TokenizedStringFragments { + const fragments = new TokenizedStringFragments(); + literals.forEach((literal) => fragments.addLiteral(literal)); + return fragments; +} + +describe("TokenizedStringFragments", () => { + test("concat method should merge fragments correctly", () => { + // Arrange: Create two instances with literals + const fragments1 = createFragments("Hello", ", "); + const fragments2 = createFragments("World", "!"); + + // Act: Concatenate fragments2 into fragments1 + fragments1.concat(fragments2); + + // Assert: Check the length and combined result + expect(fragments1.length).toBe(4); // Verify total number of fragments + const concatenatedResult = fragments1.join({ + join(left: any, right: any): string { + return `${left}${right}`; + }, + } as IFragmentConcatenator); + + expect(concatenatedResult).toBe("Hello, World!"); // Verify merged content + }); +});