-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SpreadsheetExpressionFunctionObjectCellFormattedValue
- Loading branch information
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
...preadsheet/expression/function/SpreadsheetExpressionFunctionObjectCellFormattedValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2022 Miroslav Pokorny (github.com/mP1) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package walkingkooka.spreadsheet.expression.function; | ||
|
||
import walkingkooka.spreadsheet.expression.SpreadsheetExpressionEvaluationContext; | ||
import walkingkooka.tree.expression.function.ExpressionFunctionParameter; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* A custom function that retrieves the current cell formatted value. | ||
* This is intended to be used to create a query expression and not actual cell formulas. | ||
*/ | ||
final class SpreadsheetExpressionFunctionObjectCellFormattedValue extends SpreadsheetExpressionFunctionObject { | ||
|
||
/** | ||
* Singleton | ||
*/ | ||
final static SpreadsheetExpressionFunctionObjectCellFormattedValue INSTANCE = new SpreadsheetExpressionFunctionObjectCellFormattedValue(); | ||
|
||
private SpreadsheetExpressionFunctionObjectCellFormattedValue() { | ||
super("cellFormattedValue"); | ||
} | ||
|
||
@Override | ||
public List<ExpressionFunctionParameter<?>> parameters(final int count) { | ||
return ExpressionFunctionParameter.EMPTY; | ||
} | ||
|
||
@Override | ||
public Object apply(final List<Object> parameters, | ||
final SpreadsheetExpressionEvaluationContext context) { | ||
this.checkParameterCount(parameters); | ||
|
||
return context.cellOrFail() | ||
.formattedValue() | ||
.orElse(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...dsheet/expression/function/SpreadsheetExpressionFunctionObjectCellFormattedValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2022 Miroslav Pokorny (github.com/mP1) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package walkingkooka.spreadsheet.expression.function; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import walkingkooka.collect.list.Lists; | ||
import walkingkooka.color.Color; | ||
import walkingkooka.spreadsheet.SpreadsheetCell; | ||
import walkingkooka.spreadsheet.SpreadsheetFormula; | ||
import walkingkooka.spreadsheet.expression.FakeSpreadsheetExpressionEvaluationContext; | ||
import walkingkooka.spreadsheet.format.SpreadsheetText; | ||
import walkingkooka.spreadsheet.reference.SpreadsheetSelection; | ||
import walkingkooka.tree.text.TextNode; | ||
|
||
import java.util.Optional; | ||
|
||
public final class SpreadsheetExpressionFunctionObjectCellFormattedValueTest extends SpreadsheetExpressionFunctionObjectTestCase<SpreadsheetExpressionFunctionObjectCellFormattedValue> { | ||
|
||
@Test | ||
public void testApplyWhenFormattedValuePresent() { | ||
this.applyAndCheck2( | ||
SpreadsheetText.with( | ||
"Hello123" | ||
).setColor( | ||
Optional.of(Color.parse("#456789")) | ||
).toTextNode() | ||
); | ||
} | ||
|
||
@Test | ||
public void testApplyWhenFormattedValueAbsent() { | ||
this.applyAndCheck2(null); | ||
} | ||
|
||
private void applyAndCheck2(final TextNode formattedValue) { | ||
this.applyAndCheck( | ||
Lists.empty(), | ||
new FakeSpreadsheetExpressionEvaluationContext() { | ||
@Override | ||
public Optional<SpreadsheetCell> cell() { | ||
return Optional.of( | ||
SpreadsheetSelection.A1.setFormula(SpreadsheetFormula.EMPTY) | ||
.setFormattedValue( | ||
Optional.ofNullable(formattedValue) | ||
) | ||
); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "cell: " + this.cell(); | ||
} | ||
}, | ||
formattedValue | ||
); | ||
} | ||
|
||
@Override | ||
public SpreadsheetExpressionFunctionObjectCellFormattedValue createBiFunction() { | ||
return SpreadsheetExpressionFunctionObjectCellFormattedValue.INSTANCE; | ||
} | ||
|
||
@Override | ||
public int minimumParameterCount() { | ||
return 0; | ||
} | ||
|
||
// toString......................................................................................................... | ||
|
||
@Test | ||
public void testToString() { | ||
this.toStringAndCheck( | ||
this.createBiFunction(), | ||
"cellFormattedValue" | ||
); | ||
} | ||
|
||
// class............................................................................................................ | ||
|
||
@Override | ||
public Class<SpreadsheetExpressionFunctionObjectCellFormattedValue> type() { | ||
return SpreadsheetExpressionFunctionObjectCellFormattedValue.class; | ||
} | ||
} |