From 4e09ea2c299bad6afd1923561d5ebfef5651410c Mon Sep 17 00:00:00 2001 From: suojae Date: Mon, 18 Nov 2024 01:26:04 +0900 Subject: [PATCH 1/2] fix(lib): correct the concat method in TokenizedStringFragments The concat method was not properly updating `this.fragments` because `Array.prototype.concat` does not modify the original array in place. Changed the implementation to use `push` with the spread operator to correctly merge fragments. --- packages/cdktf/lib/tokens/string-fragments.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cdktf/lib/tokens/string-fragments.ts b/packages/cdktf/lib/tokens/string-fragments.ts index a0a5369889..bfaf03ccc5 100644 --- a/packages/cdktf/lib/tokens/string-fragments.ts +++ b/packages/cdktf/lib/tokens/string-fragments.ts @@ -87,7 +87,7 @@ export class TokenizedStringFragments { } public concat(other: TokenizedStringFragments): void { - this.fragments.concat(other.fragments); + this.fragments.push(...other.fragments); } /** From f0c8272a4559c9cb6804827c23e0a15062f5e12a Mon Sep 17 00:00:00 2001 From: suojae Date: Mon, 25 Nov 2024 21:37:38 +0900 Subject: [PATCH 2/2] 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 | 31 +++++++++++++++++++ 1 file changed, 31 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..8ba4a19f63 --- /dev/null +++ b/packages/cdktf/test/tokens/string-fragments.test.ts @@ -0,0 +1,31 @@ +// Copyright (c) HashiCorp, Inc +// SPDX-License-Identifier: MPL-2.0 +import { TokenizedStringFragments } from "../../lib/tokens/string-fragments"; +import { IFragmentConcatenator } from "../../lib/tokens/resolvable"; + +describe("TokenizedStringFragments", () => { + test("concat method should merge fragments correctly", () => { + // Arrange: Create two instances with literals + const fragments1 = new TokenizedStringFragments(); + fragments1.addLiteral("Hello"); + fragments1.addLiteral(", "); + + const fragments2 = new TokenizedStringFragments(); + fragments2.addLiteral("World"); + fragments2.addLiteral("!"); + + // 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 result = fragments1.join({ + join(left: any, right: any): string { + return `${left}${right}`; + }, + } as IFragmentConcatenator); + + expect(result).toBe("Hello, World!"); // Verify merged content + }); +});