Test Builder Source

Unit tests for the Builder Pattern implementation.

This file contains unit tests for the Builder Pattern, ensuring that the ConcreteBuilder and Director classes work as expected to construct a Product.

Functions

TEST(BuilderPatternTest, FullProductConstruction)

Test full product construction using ConcreteBuilder and Director.

Verifies that the Director orchestrates the ConcreteBuilder correctly to construct a product with all expected parts (PartA and PartB) built as specified.

Test:

Steps:

  • Create a ConcreteBuilder and pass it to a Director.

  • Invoke Director::construct() to build the product.

  • Retrieve the product from the Director and verify its string representation.

TEST(BuilderPatternTest, PartialProductConstruction)

Test partial product construction directly through the ConcreteBuilder.

Verifies that the ConcreteBuilder can construct a product incrementally without the involvement of the Director.

Test:

Steps:

  • Create a ConcreteBuilder.

  • Call ConcreteBuilder::buildPartA() but skip buildPartB().

  • Retrieve the product and verify its string representation reflects only Part A.

TEST(BuilderPatternTest, BuilderReuse)

Test reusing a builder for multiple products.

Ensures that the ConcreteBuilder can be reused to construct multiple products by resetting or reinitializing its internal state.

Test:

Steps:

  • Create a ConcreteBuilder and use it with a Director to construct a product.

  • Retrieve the product and verify its details.

  • Reuse the same builder to construct another product and verify its details.

TEST(BuilderPatternTest, DirectBuilderUsage)

Test constructing a product without using the Director.

Ensures that the ConcreteBuilder can independently construct a product without orchestration from the Director.

Test:

Steps:

  • Create a ConcreteBuilder.

  • Call both buildPartA() and buildPartB() directly on the builder.

  • Retrieve the product and verify its string representation.