Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the jetpack domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/feedavenue.com/public_html/wp-includes/functions.php on line 6114
Test-Driving HTML Templates - Feedavenue
Saturday, December 21, 2024
HomeTechnologySoftwareTest-Driving HTML Templates

Test-Driving HTML Templates

Date:

Related stories

spot_imgspot_img


foo

Let’s see how to do it in stages: we start with the following test that
tries to compile the template. In Go we use the standard html/template package.

Go

  func Test_wellFormedHtml(t *testing.T) {
    templ := template.Must(template.ParseFiles("index.tmpl"))
    _ = templ
  }

In Java, we use jmustache
because it’s very simple to use; Freemarker or
Velocity are other common choices.

Java

  @Test
  void indexIsSoundHtml() {
      var template = Mustache.compiler().compile(
              new InputStreamReader(
                      getClass().getResourceAsStream("/index.tmpl")));
  }

If we run this test, it will fail, because the index.tmpl file does
not exist. So we create it, with the above broken HTML. Now the test should pass.

Then we create a model for the template to use. The application manages a todo-list, and
we can create a minimal model for demonstration purposes.

Go

  func Test_wellFormedHtml(t *testing.T) {
    templ := template.Must(template.ParseFiles("index.tmpl"))
    model := todo.NewList()
    _ = templ
    _ = model
  }

Java

  @Test
  void indexIsSoundHtml() {
      var template = Mustache.compiler().compile(
              new InputStreamReader(
                      getClass().getResourceAsStream("/index.tmpl")));
      var model = new TodoList();
  }

Now we render the template, saving the results in a bytes buffer (Go) or as a String (Java).

Go

  func Test_wellFormedHtml(t *testing.T) {
    templ := template.Must(template.ParseFiles("index.tmpl"))
    model := todo.NewList()
    var buf bytes.Buffer
    err := templ.Execute(&buf, model)
    if err != nil {
      panic(err)
    }
  }

Java

  @Test
  void indexIsSoundHtml() {
      var template = Mustache.compiler().compile(
              new InputStreamReader(
                      getClass().getResourceAsStream("/index.tmpl")));
      var model = new TodoList();
  
      var html = template.execute(model);
  }

At this point, we want to parse the HTML and we expect to see an
error, because in our broken HTML there is a div element that
is closed by a p element. There is an HTML parser in the Go
standard library, but it is too lenient: if we run it on our broken HTML, we don’t get an
error. Luckily, the Go standard library also has an XML parser that can be
configured to parse HTML (thanks to this Stack Overflow answer)

Go

  func Test_wellFormedHtml(t *testing.T) {
    templ := template.Must(template.ParseFiles("index.tmpl"))
    model := todo.NewList()
    
    // render the template into a buffer
    var buf bytes.Buffer
    err := templ.Execute(&buf, model)
    if err != nil {
      panic(err)
    }
  
    // check that the template can be parsed as (lenient) XML
    decoder := xml.NewDecoder(bytes.NewReader(buf.Bytes()))
    decoder.Strict = false
    decoder.AutoClose = xml.HTMLAutoClose
    decoder.Entity = xml.HTMLEntity
    for {
      _, err := decoder.Token()
      switch err {
      case io.EOF:
        return // We're done, it's valid!
      case nil:
        // do nothing
      default:
        t.Fatalf("Error parsing html: %s", err)
      }
    }
  }

source

This code configures the HTML parser to have the right level of leniency
for HTML, and then parses the HTML token by token. Indeed, we see the error
message we wanted:

--- FAIL: Test_wellFormedHtml (0.00s)
    index_template_test.go:61: Error parsing html: XML syntax error on line 4: unexpected end element 

In Java, a versatile library to use is jsoup:

Java

  @Test
  void indexIsSoundHtml() {
      var template = Mustache.compiler().compile(
              new InputStreamReader(
                      getClass().getResourceAsStream("/index.tmpl")));
      var model = new TodoList();
  
      var html = template.execute(model);
  
      var parser = Parser.htmlParser().setTrackErrors(10);
      Jsoup.parse(html, "", parser);
      assertThat(parser.getErrors()).isEmpty();
  }

source

And we see it fail:

java.lang.AssertionError: 
Expecting empty but was:<[<1:13>: Unexpected EndTag token [] when in state [InBody],

Success! Now if we copy over the contents of the TodoMVC
template
to our index.tmpl file, the test passes.

The test, however, is too verbose: we extract two helper functions, in
order to make the intention of the test clearer, and we get

Go

  func Test_wellFormedHtml(t *testing.T) {
    model := todo.NewList()
  
    buf := renderTemplate("index.tmpl", model)
  
    assertWellFormedHtml(t, buf)
  }

source

Java

  @Test
  void indexIsSoundHtml() {
      var model = new TodoList();
  
      var html = renderTemplate("/index.tmpl", model);
  
      assertSoundHtml(html);
  }

source

We’re releasing this article in installments. Future installments
will go beyond simple validity and
explain how to test the content of the generated HTML.

To find out when we publish the next installment subscribe to this
site’s
RSS feed, or Martin’s feeds on
Mastodon,
LinkedIn, or
X (Twitter).






Source link

Latest stories

spot_img