Skip to content

Commit

Permalink
Merge pull request #5528 from mP1/feature/SpreadsheetMetadataEnvironm…
Browse files Browse the repository at this point in the history
…entContext-ignores-wrapped-EnvironmentContext-values

SpreadsheetMetadataEnvironmentContext ignores wrapped EnvironmentCont…
  • Loading branch information
mP1 authored Jan 6, 2025
2 parents 89eb531 + 689ed6b commit a00d1f6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import walkingkooka.datetime.DateTimeContexts;
import walkingkooka.datetime.HasNow;
import walkingkooka.environment.EnvironmentContext;
import walkingkooka.environment.EnvironmentContexts;
import walkingkooka.locale.HasLocale;
import walkingkooka.math.DecimalNumberContext;
import walkingkooka.math.HasDecimalNumberContext;
Expand Down Expand Up @@ -611,28 +610,16 @@ final DecimalNumberContext decimalNumberContext0() {
// EnvironmentContext...............................................................................................

/**
* Getter that returns a {@link EnvironmentContext} view o this {@link SpreadsheetMetadata} combined with the given {@link EnvironmentContext}.
* Getter that returns a {@link EnvironmentContext} view o this {@link SpreadsheetMetadata} using the given {@link EnvironmentContext}
* for the time and user.
*/
public final EnvironmentContext environmentContext(final EnvironmentContext context) {
Objects.requireNonNull(context, "context");

if (null == this.environmentContext) {
this.environmentContext = SpreadsheetMetadataEnvironmentContext.with(this);
}
return EnvironmentContexts.collection(
Lists.of(
this.environmentContext,
context
),
context
return SpreadsheetMetadataEnvironmentContext.with(
this,
context
);
}

/**
* Cached {@link SpreadsheetMetadataEnvironmentContext}.
*/
private EnvironmentContext environmentContext;

// ExpressionFunctionProvider.......................................................................................

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@
*/
final class SpreadsheetMetadataEnvironmentContext implements EnvironmentContext {

static SpreadsheetMetadataEnvironmentContext with(final SpreadsheetMetadata metadata) {
static SpreadsheetMetadataEnvironmentContext with(final SpreadsheetMetadata metadata,
final EnvironmentContext context) {
return new SpreadsheetMetadataEnvironmentContext(
Objects.requireNonNull(metadata, "metadata")
Objects.requireNonNull(metadata, "metadata"),
Objects.requireNonNull(context, "context")
);
}

private SpreadsheetMetadataEnvironmentContext(final SpreadsheetMetadata metadata) {
private SpreadsheetMetadataEnvironmentContext(final SpreadsheetMetadata metadata,
final EnvironmentContext context) {
this.metadata = metadata;
this.context = context;
}

@Override
Expand Down Expand Up @@ -98,14 +102,16 @@ public Set<EnvironmentValueName<?>> environmentValueNames() {

@Override
public LocalDateTime now() {
throw new UnsupportedOperationException();
return this.context.now();
}

@Override
public Optional<EmailAddress> user() {
throw new UnsupportedOperationException();
return this.context.user();
}

private final EnvironmentContext context;

@Override
public String toString() {
return this.metadata.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,54 @@
import walkingkooka.environment.EnvironmentContextTesting2;
import walkingkooka.environment.EnvironmentContexts;
import walkingkooka.environment.EnvironmentValueName;
import walkingkooka.net.email.EmailAddress;
import walkingkooka.spreadsheet.SpreadsheetId;
import walkingkooka.spreadsheet.SpreadsheetName;

import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.util.Locale;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertThrows;

public final class SpreadsheetMetadataEnvironmentContextTest implements EnvironmentContextTesting2<SpreadsheetMetadataEnvironmentContext>,
ToStringTesting<SpreadsheetMetadataEnvironmentContext> {

private final static LocalDateTime NOW = LocalDateTime.of(
1999,
12,
31,
12,
58
);

private final static EmailAddress USER = EmailAddress.parse("[email protected]");

private final static EnvironmentContext CONTEXT = EnvironmentContexts.empty(
() -> NOW,
Optional.of(USER)
);

@Test
public void testWithNullFails() {
public void testWithNullMetadataFails() {
assertThrows(
NullPointerException.class,
() -> SpreadsheetMetadataEnvironmentContext.with(null)
() -> SpreadsheetMetadataEnvironmentContext.with(
null,
CONTEXT
)
);
}

@Test
public void testWithNullEnvironmentContextFails() {
assertThrows(
NullPointerException.class,
() -> SpreadsheetMetadataEnvironmentContext.with(
SpreadsheetMetadata.EMPTY,
null
)
);
}

Expand Down Expand Up @@ -97,13 +128,22 @@ public void testEnvironmentValueNames() {

@Override
public void testUser() {
throw new UnsupportedOperationException();
this.userAndCheck(USER);
}

@Test
public void testNow() {
this.checkEquals(
this.createContext().now(),
NOW
);
}

@Override
public SpreadsheetMetadataEnvironmentContext createContext() {
return SpreadsheetMetadataEnvironmentContext.with(
SpreadsheetMetadataTesting.METADATA_EN_AU
SpreadsheetMetadataTesting.METADATA_EN_AU,
CONTEXT
);
}

Expand Down

0 comments on commit a00d1f6

Please sign in to comment.