- java.lang.Object
-
- javax0.jamal.testsupport.TestThat
-
public class TestThat extends java.lang.Object
A simple class that helps the testing of built-in macros.A built-in macro most of the time converts the content to another string. To test that you need an instance of the macro class, a processor, an input object that contains the
StringBuilder
and a null reference file name. Then the test just invokes the macroMacro.evaluate(Input, javax0.jamal.api.Processor)
method and checks the returned string with what was expected.To ease this task you can put this module on the test dependencies and have a test like
var camelLowerCase = TestThat.theMacro(Camel.LowerCase.class); camelLowerCase.fromTheInput("INPUT").results( "input"); camelLowerCase.fromTheInput("INpUT").results( "input"); camelLowerCase.fromTheInput("INpuT").results( "input"); camelLowerCase.fromTheInput("INput").results( "input"); camelLowerCase.fromTheInput("Input").results( "input"); camelLowerCase.fromTheInput("input").results( "input"); camelLowerCase.fromTheInput("IN-PUT").results( "inPut"); camelLowerCase.fromTheInput("I-N-P-U-T").results( "iNPUT");
If and when the macro is expected to throw exception (probably BadSyntaxAt) then you can write
TestThat.forMacro(For.class).fromInput(" x in a,b,c,d= x is either a, b, c or d\n").throwsBadSyntax();
If you expect any other exception, other thanBadSyntaxAt
then you can also usethrowsUp(exception.class)
instead ofthrowsBadSyntax()
. You can also define user defined and built-in macros on the outermost and also on the global level. Another possibility to use this class is to
that invokes not only one macro but rather the whole processing engine.TestThat.theInput("{@define a=alma}{a}").results("alma")
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TestThat
define(java.lang.String id, java.lang.String content, java.lang.String... parameters)
You can use this method to call to define a local user defined macro for the test in case the tested macro depends on the existence of some user defined macros.TestThat
define(Macro macro)
You can use this method to define a local built-in macro.TestThat
define(Macro macro, java.lang.String alias)
You can use this method to define a local built-in macro with an alias.TestThat
fromTheInput(java.lang.String input)
TestThat
global(java.lang.String id, java.lang.String content, java.lang.String... parameters)
You can use this method to call to define a global user defined macro for the test in case the tested macro depends on the existence of some user defined macros.TestThat
global(Macro macro)
You can use this method to define a global built-in macro.TestThat
global(Macro macro, java.lang.String alias)
You can use this method to define a global built-in macro with an alias.void
results(java.lang.String expected)
Create a new macro, a new processor and test that the input creates the expected output.static TestThat
theInput(java.lang.String input)
static TestThat
theMacro(java.lang.Class<? extends Macro> klass)
Create a new instance of the TestThat class.void
throwsBadSyntax()
Checks that the macro throws a bad syntax exception for the given input.void
throwsUp(java.lang.Class<? extends java.lang.Throwable> throwable)
Checks that the macro throws an exception for a given input.
-
-
-
Method Detail
-
theMacro
public static TestThat theMacro(java.lang.Class<? extends Macro> klass)
Create a new instance of the TestThat class.- Parameters:
klass
- is the class of the tested macro.- Returns:
- the testing class
-
theInput
public static TestThat theInput(java.lang.String input)
-
fromTheInput
public TestThat fromTheInput(java.lang.String input)
-
results
public void results(java.lang.String expected) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException, BadSyntax
Create a new macro, a new processor and test that the input creates the expected output. If they are not the same then JUnit5 assertion failure will happen.- Parameters:
expected
- the expected output of the macro- Throws:
java.lang.NoSuchMethodException
- if the macro class can not be instantiatedjava.lang.IllegalAccessException
- if the macro class can not be instantiatedjava.lang.InstantiationException
- if the macro class can not be instantiatedjava.lang.reflect.InvocationTargetException
- if the macro class can not be instantiatedBadSyntaxAt
- if the macro evaluation throws BadSyntaxAtBadSyntax
-
throwsUp
public void throwsUp(java.lang.Class<? extends java.lang.Throwable> throwable) throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
Checks that the macro throws an exception for a given input.- Parameters:
throwable
- the exception we expect- Throws:
java.lang.NoSuchMethodException
- if the macro class can not be instantiatedjava.lang.IllegalAccessException
- if the macro class can not be instantiatedjava.lang.InstantiationException
- if the macro class can not be instantiatedjava.lang.reflect.InvocationTargetException
- if the macro class can not be instantiated
-
throwsBadSyntax
public void throwsBadSyntax() throws java.lang.NoSuchMethodException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
Checks that the macro throws a bad syntax exception for the given input.- Throws:
java.lang.NoSuchMethodException
- if the macro class can not be instantiatedjava.lang.IllegalAccessException
- if the macro class can not be instantiatedjava.lang.InstantiationException
- if the macro class can not be instantiatedjava.lang.reflect.InvocationTargetException
- if the macro class can not be instantiated
-
global
public TestThat global(java.lang.String id, java.lang.String content, java.lang.String... parameters) throws BadSyntax
You can use this method to call to define a global user defined macro for the test in case the tested macro depends on the existence of some user defined macros.- Parameters:
id
- the identifier / name of the macrocontent
- the content of the macroparameters
- the list o formal parameters of the macro- Returns:
- this
- Throws:
BadSyntax
- when the underlying call throws this exception
-
global
public TestThat global(Macro macro)
You can use this method to define a global built-in macro. This may be needed when the macro tested needs the services of other macros.- Parameters:
macro
- the macro that the tested macro needs for its functioning- Returns:
- this
-
global
public TestThat global(Macro macro, java.lang.String alias)
You can use this method to define a global built-in macro with an alias. This may be needed when the macro tested needs the services of other macros.- Parameters:
macro
- the macro that the tested macro needs for its functioningalias
- the alias name for the macro- Returns:
- this
-
define
public TestThat define(java.lang.String id, java.lang.String content, java.lang.String... parameters) throws BadSyntax
You can use this method to call to define a local user defined macro for the test in case the tested macro depends on the existence of some user defined macros.- Parameters:
id
- the identifier / name of the macrocontent
- the content of the macroparameters
- the list o formal parameters of the macro- Returns:
- this
- Throws:
BadSyntax
- when the underlying call throws this exception
-
define
public TestThat define(Macro macro)
You can use this method to define a local built-in macro. This may be needed when the macro tested needs the services of other macros.- Parameters:
macro
- the macro that the tested macro needs for its functioning- Returns:
- this
-
define
public TestThat define(Macro macro, java.lang.String alias)
You can use this method to define a local built-in macro with an alias. This may be needed when the macro tested needs the services of other macros.- Parameters:
macro
- the macro that the tested macro needs for its functioningalias
- the alias name for the macro- Returns:
- this
-
-