From 3bd3edb7402e09a4a33b9a12b267faf05d838c3f Mon Sep 17 00:00:00 2001 From: The-Lum <86879521+The-Lum@users.noreply.github.com> Date: Sat, 3 Feb 2024 14:06:49 +0000 Subject: [PATCH] feat: `Random` function: add test and private declaration Add: - `RandomFunctionTest` test file - private declaration for random lib. Ref.: - #1667 Improve: - https://github.com/plantuml/plantuml/commit/dbaf8ac2cd50f04ac4ec4309e7dadae234ea8375 - #1668 --- .../plantuml/tim/stdlib/RandomFunction.java | 2 +- .../tim/stdlib/RandomFuntionTest.java | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 test/net/sourceforge/plantuml/tim/stdlib/RandomFuntionTest.java diff --git a/src/net/sourceforge/plantuml/tim/stdlib/RandomFunction.java b/src/net/sourceforge/plantuml/tim/stdlib/RandomFunction.java index 915779c9c..d33a0af09 100644 --- a/src/net/sourceforge/plantuml/tim/stdlib/RandomFunction.java +++ b/src/net/sourceforge/plantuml/tim/stdlib/RandomFunction.java @@ -57,7 +57,7 @@ public class RandomFunction extends SimpleReturnFunction { return nbArg == 0 || nbArg == 1 || nbArg == 2; } - final Random random = new Random(); + private final Random random = new Random(); public TValue executeReturnFunction(TContext context, TMemory memory, LineLocation location, List values, Map named) throws EaterException, EaterExceptionLocated { diff --git a/test/net/sourceforge/plantuml/tim/stdlib/RandomFuntionTest.java b/test/net/sourceforge/plantuml/tim/stdlib/RandomFuntionTest.java new file mode 100644 index 000000000..c8505449e --- /dev/null +++ b/test/net/sourceforge/plantuml/tim/stdlib/RandomFuntionTest.java @@ -0,0 +1,50 @@ +package net.sourceforge.plantuml.tim.stdlib; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Arrays; +import java.util.Collections; + +import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; +import org.junit.jupiter.api.IndicativeSentencesGeneration; +import org.junit.jupiter.api.RepeatedTest; + +import net.sourceforge.plantuml.tim.EaterException; +import net.sourceforge.plantuml.tim.EaterExceptionLocated; +import net.sourceforge.plantuml.tim.TFunction; +import net.sourceforge.plantuml.tim.expression.TValue; + +/** + * Tests the builtin function. + */ +@IndicativeSentencesGeneration(separator = ": ", generator = ReplaceUnderscores.class) + +class RandomFunctionTest { + TFunction cut = new RandomFunction(); + final String cutName = "Random"; + final String repetitionLabel = "[{currentRepetition}/{totalRepetitions}] "; + + @RepeatedTest(value = 10, name = repetitionLabel + cutName + "()") + void test_with_no_argument() throws EaterException, EaterExceptionLocated { + final TValue tValue = cut.executeReturnFunction(null, null, null, Collections.emptyList(), null); + assertThat(tValue.toInt()).isIn(0, 1); + } + + @RepeatedTest(value = 10, name = repetitionLabel + cutName + "(7)") + void test_with_one_argument() throws EaterException, EaterExceptionLocated { + final TValue tValue = cut.executeReturnFunction(null, null, null, Arrays.asList(TValue.fromInt(7)), null); + assertThat(tValue.toInt()).isBetween(0, 7-1); + } + + @RepeatedTest(value = 10, name = repetitionLabel + cutName + "(0, 7)") + void test_with_two_argument_first_zero() throws EaterException, EaterExceptionLocated { + final TValue tValue = cut.executeReturnFunction(null, null, null, Arrays.asList(TValue.fromInt(0), TValue.fromInt(7)), null); + assertThat(tValue.toInt()).isBetween(0, 7-1); + } + + @RepeatedTest(value = 10, name = repetitionLabel + cutName + "(3, 7)") + void test_with_two_argument() throws EaterException, EaterExceptionLocated { + final TValue tValue = cut.executeReturnFunction(null, null, null, Arrays.asList(TValue.fromInt(3), TValue.fromInt(7)), null); + assertThat(tValue.toInt()).isBetween(3, 7-1); + } +} \ No newline at end of file