Skip to content

Commit

Permalink
Add an option to merge placeholder contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
shitzuu committed Aug 27, 2024
1 parent ac504e1 commit 1eb19c3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ public Map<String, Object> getValues() {
public Map<String, CompletableFuture<?>> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 1eb19c3

Please sign in to comment.