forked from plantuml/plantuml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add builtin function
%boolval
(plantuml#1873)
This change adds the `%boolval` function to improve usability of boolean values and conditionals. This is especially helpful when the diagram data is generated in a previous workflow step, using a conventional programming or scripting language. In this first implementation, the boolean strings are defined as: * True values: `true`, `1` * False values: `false`, `0` These values are case-sensitive, meaning `True` `TRUE` are also treated as true values (likewise for false values). This builtin function helps mitigate some user confusion with the use of boolean values, specifically that all strings including `"0"` and `"false"` are treated as true values. Existing behaviour (integer `0` is false, all other numbers and strings are true) is not affected, and can be used by simply not calling the new `%boolval` function. Other minor improvements: 1. Parsing error in `%intval` will now raise an exception. This improves tool behaviour consistency, for example, when `-noerror` flag is used from the CLI. 2. Functions in `TContext.java` are arranged in sorted order. This code readability improvement does not change the tool behaviour from the user's perspective.
- Loading branch information
1 parent
dac564a
commit 425db1d
Showing
5 changed files
with
179 additions
and
47 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* ======================================================================== | ||
* PlantUML : a free UML diagram generator | ||
* ======================================================================== | ||
* | ||
* (C) Copyright 2009-2024, Arnaud Roques | ||
* | ||
* Project Info: https://plantuml.com | ||
* | ||
* If you like this project or if you find it useful, you can support us at: | ||
* | ||
* https://plantuml.com/patreon (only 1$ per month!) | ||
* https://plantuml.com/paypal | ||
* | ||
* This file is part of PlantUML. | ||
* | ||
* PlantUML is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* PlantUML distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | ||
* USA. | ||
* | ||
* | ||
* Original Author: Arnaud Roques | ||
* Contribution: Aravind Pai | ||
* | ||
*/ | ||
package net.sourceforge.plantuml.tim.stdlib; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import net.sourceforge.plantuml.text.StringLocated; | ||
import net.sourceforge.plantuml.tim.EaterException; | ||
import net.sourceforge.plantuml.tim.TContext; | ||
import net.sourceforge.plantuml.tim.TFunctionSignature; | ||
import net.sourceforge.plantuml.tim.TMemory; | ||
import net.sourceforge.plantuml.tim.expression.TValue; | ||
|
||
public class BoolVal extends SimpleReturnFunction { | ||
|
||
public TFunctionSignature getSignature() { | ||
return new TFunctionSignature("%boolval", 1); | ||
} | ||
|
||
@Override | ||
public boolean canCover(int nbArg, Set<String> namedArgument) { | ||
return nbArg == 1; | ||
} | ||
|
||
@Override | ||
public TValue executeReturnFunction(TContext context, TMemory memory, StringLocated location, List<TValue> values, | ||
Map<String, TValue> named) throws EaterException { | ||
final String s = values.get(0).toString().toLowerCase(); | ||
if (trueValues.contains(s)) { | ||
return TValue.fromBoolean(true); | ||
} else if (falseValues.contains(s)) { | ||
return TValue.fromBoolean(false); | ||
} | ||
|
||
throw new EaterException("Cannot convert " + s + " to boolean.", location); | ||
} | ||
|
||
private final List<String> trueValues = Arrays.asList("true", "1"); | ||
private final List<String> falseValues = Arrays.asList("false", "0"); | ||
} |
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
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 @@ | ||
package net.sourceforge.plantuml.tim.stdlib; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import net.sourceforge.plantuml.text.StringLocated; | ||
import net.sourceforge.plantuml.tim.EaterException; | ||
import net.sourceforge.plantuml.tim.expression.TValue; | ||
|
||
class BoolValTest { | ||
/** | ||
* Tests boolval according to a list of input / expected output | ||
* | ||
* @throws EaterException should not | ||
*/ | ||
@ParameterizedTest | ||
@CsvSource(nullValues = "null", value = { | ||
"True, true", | ||
"true, true", | ||
"1, true", | ||
"False, false", | ||
"false, false", | ||
"0, false", | ||
}) | ||
void executeReturnFunctionWithValidBooleanValueStringTest(String input, Boolean expected) throws EaterException { | ||
BoolVal cut = new BoolVal(); | ||
List<TValue> values = Arrays.asList(TValue.fromString(input)); | ||
|
||
TValue tValue = cut.executeReturnFunction(null, null, null, values, null); | ||
assertEquals(expected, tValue.toBoolean()); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource(nullValues = "null", value = { | ||
"2", | ||
"hello", | ||
}) | ||
void executeReturnFunctionWithInvalidBooleanValueStringTest(String input) throws EaterException { | ||
BoolVal cut = new BoolVal(); | ||
List<TValue> values = Arrays.asList(TValue.fromString(input)); | ||
|
||
StringLocated stringLocated = new StringLocated("test location", null); | ||
EaterException exception = assertThrows(EaterException.class, () -> { | ||
cut.executeReturnFunction(null, null, stringLocated, values, null); | ||
}); | ||
|
||
assertEquals("Cannot convert " + input + " to boolean.", exception.getMessage()); | ||
} | ||
} |