Class 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 macro Macro.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 than BadSyntaxAt then you can also use throwsUp(exception.class) instead of throwsBadSyntax(). 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
    
     TestThat.theInput("{@define a=alma}{a}").results("alma")
     
    that invokes not only one macro but rather the whole processing engine.
    • 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 instantiated
        java.lang.IllegalAccessException - if the macro class can not be instantiated
        java.lang.InstantiationException - if the macro class can not be instantiated
        java.lang.reflect.InvocationTargetException - if the macro class can not be instantiated
        BadSyntaxAt - if the macro evaluation throws BadSyntaxAt
        BadSyntax
      • 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 instantiated
        java.lang.IllegalAccessException - if the macro class can not be instantiated
        java.lang.InstantiationException - if the macro class can not be instantiated
        java.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 instantiated
        java.lang.IllegalAccessException - if the macro class can not be instantiated
        java.lang.InstantiationException - if the macro class can not be instantiated
        java.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 macro
        content - the content of the macro
        parameters - 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 functioning
        alias - 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 macro
        content - the content of the macro
        parameters - 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 functioning
        alias - the alias name for the macro
        Returns:
        this