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.forMacro(Camel.LowerCase.class);
         camelLowerCase.fromInput("INPUT").results( "input");
         camelLowerCase.fromInput("INpUT").results( "input");
         camelLowerCase.fromInput("INpuT").results( "input");
         camelLowerCase.fromInput("INput").results( "input");
         camelLowerCase.fromInput("Input").results( "input");
         camelLowerCase.fromInput("input").results( "input");
         camelLowerCase.fromInput("IN-PUT").results( "inPut");
         camelLowerCase.fromInput("I-N-P-U-T").results( "iNPUT");
     

    If and when the macro may throw exception (probably BadSyntaxAt

    • 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​(javax0.jamal.api.Macro macro)
      You can use this method to define a local built-in macro.
      TestThat define​(javax0.jamal.api.Macro macro, java.lang.String alias)
      You can use this method to define a local built-in macro with an alias.
      static TestThat forMacro​(java.lang.Class<? extends javax0.jamal.api.Macro> klass)
      Create a new instance of the TestThat class.
      TestThat fromInput​(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​(javax0.jamal.api.Macro macro)
      You can use this method to define a global built-in macro.
      TestThat global​(javax0.jamal.api.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.
      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

      • forMacro

        public static TestThat forMacro​(java.lang.Class<? extends javax0.jamal.api.Macro> klass)
        Create a new instance of the TestThat class.
        Parameters:
        klass - is the class of the tested macro.
        Returns:
        the testing class
      • fromInput

        public TestThat fromInput​(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,
                            javax0.jamal.api.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
        javax0.jamal.api.BadSyntaxAt - if the macro evaluation throws BadSyntaxAt
        javax0.jamal.api.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 javax0.jamal.api.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:
        javax0.jamal.api.BadSyntax - when the underlying call throws this exception
      • global

        public TestThat global​(javax0.jamal.api.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​(javax0.jamal.api.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 javax0.jamal.api.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:
        javax0.jamal.api.BadSyntax - when the underlying call throws this exception
      • define

        public TestThat define​(javax0.jamal.api.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​(javax0.jamal.api.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