From 1eb19c35d7f9a63118f4edc043d172af078b2f06 Mon Sep 17 00:00:00 2001 From: shitzuu Date: Tue, 27 Aug 2024 22:27:06 +0200 Subject: [PATCH] Add an option to merge placeholder contexts --- .../evaluator/PlaceholderContext.java | 9 +++++ .../evaluator/PlaceholderContextTests.java | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 honey-common/test/dev/shiza/honey/placeholder/evaluator/PlaceholderContextTests.java diff --git a/honey-common/src/dev/shiza/honey/placeholder/evaluator/PlaceholderContext.java b/honey-common/src/dev/shiza/honey/placeholder/evaluator/PlaceholderContext.java index e1a0707..77a13fc 100644 --- a/honey-common/src/dev/shiza/honey/placeholder/evaluator/PlaceholderContext.java +++ b/honey-common/src/dev/shiza/honey/placeholder/evaluator/PlaceholderContext.java @@ -61,4 +61,13 @@ public Map getValues() { public Map> getPromisedValues() { return promisedValues; } + + public PlaceholderContext merge(final PlaceholderContext context) { + final PlaceholderContext newContext = new PlaceholderContext(); + newContext.withValues(getValues()); + newContext.withValues(context.getValues()); + newContext.withPromisedValues(getPromisedValues()); + newContext.withPromisedValues(context.getPromisedValues()); + return newContext; + } } diff --git a/honey-common/test/dev/shiza/honey/placeholder/evaluator/PlaceholderContextTests.java b/honey-common/test/dev/shiza/honey/placeholder/evaluator/PlaceholderContextTests.java new file mode 100644 index 0000000..06c53a3 --- /dev/null +++ b/honey-common/test/dev/shiza/honey/placeholder/evaluator/PlaceholderContextTests.java @@ -0,0 +1,37 @@ +package dev.shiza.honey.placeholder.evaluator; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class PlaceholderContextTests { + + @Test + void mergeContexts() { + final PlaceholderContext one = + PlaceholderContext.create().withValue("hello", "world").withPromisedValue("hello", "world"); + final PlaceholderContext two = + PlaceholderContext.create().withValue("world", "hello").withPromisedValue("world", "hello"); + final PlaceholderContext mergedContext = one.merge(two); + assertThat(mergedContext.getValues()).hasSize(2); + assertThat(mergedContext.getPromisedValues()).hasSize(2); + } + + @Test + void mergeContextsWithDuplicates() { + final PlaceholderContext one = PlaceholderContext.create().withValue("hello", "world"); + final PlaceholderContext two = PlaceholderContext.create().withValue("hello", "world2"); + final PlaceholderContext mergedContext = one.merge(two); + assertThat(mergedContext.getValues()).hasSize(1); + assertThat(mergedContext.getPromisedValues()).isEmpty(); + } + + @Test + void mergeEmptyContexts() { + final PlaceholderContext one = PlaceholderContext.create(); + final PlaceholderContext two = PlaceholderContext.create(); + final PlaceholderContext mergedContext = one.merge(two); + assertThat(mergedContext.getValues()).isEmpty(); + assertThat(mergedContext.getPromisedValues()).isEmpty(); + } +}