Test Template for valid syntax during unit test?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Test Template for valid syntax during unit test?

Antibrumm
Hi

I would like to check some/all templates during our unit tests. Does someone know a way already?

What I want to do is basically load a template and check if the syntax is correct. This should go that far that expressions are parsed, but not evaluated as I do not want to create a whole bunch of data just to check if the design is ok or not. The final test is done in Selenium anyway, but during a build it would be very nice to check if the developers do a good job :)

Somebody knows a good way to achieve that?
Reply | Threaded
Open this post in threaded view
|

Re: Test Template for valid syntax during unit test?

Emanuel
Administrator
What kind of things would you be aiming to test then if you're not evaluating the expressions?
Reply | Threaded
Open this post in threaded view
|

Re: Test Template for valid syntax during unit test?

Antibrumm
It's supposed to be a quick fail to check if each html template is well balanced and that all expressions will be able to execute without a parseexception before we launch the extensive selenium tests. It's just a time safer in the end.

It's not that neccessary as we are able to develop the templates on the fly without redeployment already, so most of the parseexpression exceptions are directly solved during this period. It still happens frequently that a developer misses a quote or a brace in the first writing of the expression, even after one year ;)

I have already found a way to validate most of the expressions, but it involves quite some handling for the different expression types. I currently validate just simple expressions by taking the attribute value and call expressionparser.parse myself. This however does not work for iteration expressions nor "with" expressions without some upfront modification.

Do you know if there is a single point of entry to parse expressions in Thymeleaf?
Reply | Threaded
Open this post in threaded view
|

Re: Test Template for valid syntax during unit test?

Emanuel
Administrator
For HTML correctness, you're covered by the horde of HTML/XML parsers out there.  For Thymeleaf expressions, you're probably already doing the right thing.  I was going to point to the standard expression parser as a place to start: https://github.com/thymeleaf/thymeleaf/blob/2.1-master/src/main/java/org/thymeleaf/standard/expression/StandardExpressionParser.java#L65  It needs a bit of setup with the method arguments, but if an expression can't be parsed it'll throws a TemplateProcessingException, just like it would in a running application server.
Reply | Threaded
Open this post in threaded view
|

Re: Test Template for valid syntax during unit test?

KurtS
Hi,

I'm also looking for a way to validate template syntax like in
for (expression : template) {
checkExpressionValid(expression)
}

without depending on data like in thymeleaf-tests.
@Antibrumm: how did you do it and is there a chance, you could share a class for inclusion in the framework?

Thanks!
Kurt
Reply | Threaded
Open this post in threaded view
|

Re: Test Template for valid syntax during unit test?

Antibrumm
Here is the class I wrote at the time. This was working well enough for us and was running against Thymeleaf 2.1.4+


TemplateSyntaxTestRunner.java


We used it with Spring so the JUnit test looked like this:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:root-context.xml", "classpath:servlet-context.xml" })
@Transactional
public class TemplateSyntaxTests {

    /** The test runner. */
    @Autowired
    private TemplateSyntaxTestRunner testRunner;

    /**
     * Test views.
     *
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    @Test
    public void testViews() throws IOException {
        testRunner.testViews();
    }
}