|
| 1 | +package de.neuland.pug4j.expression; |
| 2 | + |
| 3 | +import de.neuland.pug4j.PugEngine; |
| 4 | +import de.neuland.pug4j.RenderContext; |
| 5 | +import de.neuland.pug4j.template.PugTemplate; |
| 6 | +import de.neuland.pug4j.template.ReaderTemplateLoader; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.io.StringReader; |
| 10 | +import java.util.*; |
| 11 | + |
| 12 | +import static org.hamcrest.CoreMatchers.is; |
| 13 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 14 | + |
| 15 | +public class RecordsJexlTest { |
| 16 | + |
| 17 | + // Define tiny records for tests (Java 17+) |
| 18 | + record Address(String city, String street) {} |
| 19 | + record Person(String name, int age, Address address) {} |
| 20 | + |
| 21 | + private String render(String pug, Map<String, Object> model) throws Exception { |
| 22 | + ReaderTemplateLoader loader = new ReaderTemplateLoader(new StringReader(pug), "inline"); |
| 23 | + PugEngine engine = PugEngine.builder() |
| 24 | + .templateLoader(loader) |
| 25 | + .expressionHandler(new JexlExpressionHandler()) |
| 26 | + .build(); |
| 27 | + PugTemplate template = engine.getTemplate("inline"); |
| 28 | + RenderContext context = RenderContext.defaults(); |
| 29 | + return engine.render(template, model, context); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void record_component_dot_access_works() throws Exception { |
| 34 | + Person person = new Person("Alice", 41, new Address("Hamburg", "Reeperbahn")); |
| 35 | + Map<String, Object> model = new HashMap<>(); |
| 36 | + model.put("person", person); |
| 37 | + |
| 38 | + String html = render("p= person.name", model); |
| 39 | + assertThat(html, is("<p>Alice</p>")); |
| 40 | + |
| 41 | + html = render("p= person.age + 1", model); |
| 42 | + assertThat(html, is("<p>42</p>")); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void record_component_parens_and_dot_both_work() throws Exception { |
| 47 | + Person person = new Person("Bob", 19, new Address("Berlin", "Unter den Linden")); |
| 48 | + Map<String, Object> model = new HashMap<>(); |
| 49 | + model.put("person", person); |
| 50 | + |
| 51 | + // In JEXL, record accessors are real zero-arg methods, so both styles work |
| 52 | + String html = render("p= person.name()", model); |
| 53 | + assertThat(html, is("<p>Bob</p>")); |
| 54 | + |
| 55 | + html = render("p= person.age() + 1", model); |
| 56 | + assertThat(html, is("<p>20</p>")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void nested_record_access_dot_and_parens() throws Exception { |
| 61 | + Person person = new Person("Clara", 30, new Address("Cologne", "Domplatz")); |
| 62 | + Map<String, Object> model = new HashMap<>(); |
| 63 | + model.put("person", person); |
| 64 | + |
| 65 | + String html = render("p= person.address.city", model); |
| 66 | + assertThat(html, is("<p>Cologne</p>")); |
| 67 | + |
| 68 | + html = render("p= person.address.city()", model); |
| 69 | + assertThat(html, is("<p>Cologne</p>")); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void list_of_records_in_each_loop() throws Exception { |
| 74 | + List<Person> people = Arrays.asList( |
| 75 | + new Person("Ann", 21, new Address("Bonn", "Mitte")), |
| 76 | + new Person("Eve", 25, new Address("Munich", "Marienplatz")) |
| 77 | + ); |
| 78 | + Map<String, Object> model = new HashMap<>(); |
| 79 | + model.put("people", people); |
| 80 | + |
| 81 | + String pug = String.join("\n", |
| 82 | + "ul", |
| 83 | + " each p in people", |
| 84 | + " li= p.name + ' (' + p.age + ')'" |
| 85 | + ); |
| 86 | + String html = render(pug, model); |
| 87 | + assertThat(html, is("<ul><li>Ann (21)</li><li>Eve (25)</li></ul>")); |
| 88 | + } |
| 89 | + |
| 90 | + public static class Pojo { |
| 91 | + private final String fullName; |
| 92 | + public Pojo(String fullName) { this.fullName = fullName; } |
| 93 | + public String fullName() { return fullName; } |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void real_zero_arg_method_calls_on_pojo_work_normally() throws Exception { |
| 98 | + Pojo pojo = new Pojo("Dora Explorer"); |
| 99 | + Map<String, Object> model = new HashMap<>(); |
| 100 | + model.put("pojo", pojo); |
| 101 | + |
| 102 | + String html = render("p= pojo.fullName()", model); |
| 103 | + assertThat(html, is("<p>Dora Explorer</p>")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void missing_component_is_null_and_renders_empty_string() throws Exception { |
| 108 | + Person person = new Person("Felix", 7, new Address("Essen", "Zentrum")); |
| 109 | + Map<String, Object> model = new HashMap<>(); |
| 110 | + model.put("person", person); |
| 111 | + |
| 112 | + // Unknown property should evaluate to null and render as empty string |
| 113 | + String html = render("p= person.unknown", model); |
| 114 | + assertThat(html, is("<p></p>")); |
| 115 | + } |
| 116 | +} |
0 commit comments