Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions htmlflow-core/src/main/java/htmlflow/HtmlPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.xmlet.htmlapifaster.Element;
import org.xmlet.htmlapifaster.Html;
import org.xmlet.htmlapifaster.Span;
import org.xmlet.htmlapifaster.Tbody;
import org.xmlet.htmlapifaster.Tr;

/**
Expand Down Expand Up @@ -90,6 +91,10 @@ public final Span<HtmlPage> span() {
return new Span<>(this);
}

public final Tbody<HtmlPage> tbody() {
return new Tbody<>(this);
}

/**
* Returns a new instance of HtmlFlow with the same properties of this object but with indented
* set to the value of isIndented parameter.
Expand Down
45 changes: 45 additions & 0 deletions htmlflow-core/src/test/java/htmlflow/test/TestTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import htmlflow.HtmlFlow;
import htmlflow.HtmlView;
import htmlflow.test.model.Agent;
import htmlflow.test.model.Priority;
import htmlflow.test.model.Status;
import htmlflow.test.model.Task;
Expand Down Expand Up @@ -244,6 +245,20 @@
validateBindingTable(view.render(dataSource));
}

@Test
public void testRenderTableBody() {
List<Agent> agents = Arrays.asList(
new Agent(1, "Agent Smith " + 1, "void"+ 1 +"@null.org"),
new Agent(2, "Agent Smith " + 2, "void"+ 2 +"@null.org"),
new Agent(3, "Agent Smith " + 3, "void"+ 3 +"@null.org")
);

String actualTableBody = HtmlTables.agentsTableBodyView.render(agents);

assertEquals(expectedTableBodyPartial, actualTableBody);

}

static void validateBindingTable(String actual){
Iterator<String> iter = NEWLINE
.splitAsStream(actual)
Expand Down Expand Up @@ -311,4 +326,34 @@
}
}
}

private final static String expectedTableBodyPartial =

Check warning on line 330 in htmlflow-core/src/test/java/htmlflow/test/TestTable.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Reorder the modifiers to comply with the Java Language Specification.

See more on https://sonarcloud.io/project/issues?id=fmcarvalho-xmlet%3AHtmlFlow&issues=AZ0v1G0aLQrmbX5-yGGG&open=AZ0v1G0aLQrmbX5-yGGG&pullRequest=141

Check failure on line 330 in htmlflow-core/src/test/java/htmlflow/test/TestTable.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Rename this constant name to match the regular expression '^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'.

See more on https://sonarcloud.io/project/issues?id=fmcarvalho-xmlet%3AHtmlFlow&issues=AZ0v1G0aLQrmbX5-yGGF&open=AZ0v1G0aLQrmbX5-yGGF&pullRequest=141
"""
<tbody>
<tr>
<td>
Agent Smith 1
</td>
<td>
void1@null.org
</td>
</tr>
<tr>
<td>
Agent Smith 2
</td>
<td>
void2@null.org
</td>
</tr>
<tr>
<td>
Agent Smith 3
</td>
<td>
void3@null.org
</td>
</tr>
</tbody>
""".trim();
}
24 changes: 24 additions & 0 deletions htmlflow-core/src/test/java/htmlflow/test/model/Agent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package htmlflow.test.model;

public class Agent {
private final long id;
private final String name;
private final String email;

public Agent(long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}

public long getId() {
return id;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}

}
13 changes: 13 additions & 0 deletions htmlflow-core/src/test/java/htmlflow/test/views/HtmlTables.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import htmlflow.HtmlPage;
import htmlflow.HtmlTemplate;
import htmlflow.HtmlView;
import htmlflow.test.model.Agent;
import htmlflow.test.model.Task;
import org.xmlet.htmlapifaster.EnumRelType;
import org.xmlet.htmlapifaster.EnumTypeContentType;
Expand Down Expand Up @@ -201,4 +202,16 @@ public static void taskTableView(HtmlPage view){
.__() // body
.__() // html
);

public static HtmlView agentsTableBodyView = HtmlFlow.view(view -> view
.tbody()
.<Iterable<Agent>>dynamic((tbody, agents) -> agents.forEach(agent ->
tbody
.tr()
.td().text(agent.getName()).__()
.td().text(agent.getEmail()).__()
.__() // tr
))
.__()
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.xmlet.htmlapifaster.Div
import org.xmlet.htmlapifaster.Element
import org.xmlet.htmlapifaster.Html
import org.xmlet.htmlapifaster.Span
import org.xmlet.htmlapifaster.Tbody
import org.xmlet.htmlapifaster.Text
import org.xmlet.htmlapifaster.Tr

Expand All @@ -31,6 +32,8 @@ inline fun HtmlPage.tr(block: Tr<HtmlPage>.() -> Unit): HtmlPage = this.tr().app

inline fun HtmlPage.span(block: Span<HtmlPage>.() -> Unit): HtmlPage = this.span().apply(block).l

inline fun HtmlPage.tbody(block: Tbody<HtmlPage>.() -> Unit): HtmlPage = this.tbody().apply(block).l

/** Text node property. */
inline var <T : Element<T, Z>, Z : Element<*, *>> T.text: T
get() = self()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,52 @@ data class Agent(
}
data class Counter(val count: Int)

@Test
fun testThatTbodyElementCanBeUsedOnHtmlDocAndHtmlView(){
val agentsTableDoc =
StringBuilder()
.apply {
doc {
tbody {
tr {
td { text("James Bond") }
td { text("bond@mi6.gov.uk") }
td { text("007") }
}
tr {
td { text("Ethan Hunt") }
td { text("ehunt@imf.gov") }
td { text("001") }
}
}
}
}.toString()

assertEquals(expectedAgentsTableDoc, agentsTableDoc)

val agents = listOf(
Agent("Jason Bourne", "bourne@cia.gov", "002"),
Agent("Napoleon Solo", "solo@uncle.org", "003")
)

val agentsTableView =
view<List<Agent>> {
tbody {
dyn { agents: List<Agent> ->
agents.forEach { agent ->
tr {
td { text(agent.name) }
td { text(agent.email) }
td { text(agent.id) }
}
}
}
}
}.render(agents)

assertEquals(expectedAgentsTableView, agentsTableView)
}

}

private const val expectedFavoriteMoviesView =
Expand Down Expand Up @@ -463,4 +509,58 @@ private val expectedCounterSpans =
</span>
<span>
The counter 2 has the value 1
</span>"""
</span>"""


private const val expectedAgentsTableDoc =
"""
<tbody>
<tr>
<td>
James Bond
</td>
<td>
bond@mi6.gov.uk
</td>
<td>
007
</td>
</tr>
<tr>
<td>
Ethan Hunt
</td>
<td>
ehunt@imf.gov
</td>
<td>
001
</td>
</tr>
</tbody>"""

private const val expectedAgentsTableView =
"""<tbody>
<tr>
<td>
Jason Bourne
</td>
<td>
bourne@cia.gov
</td>
<td>
002
</td>
</tr>
<tr>
<td>
Napoleon Solo
</td>
<td>
solo@uncle.org
</td>
<td>
003
</td>
</tr>
</tbody>"""
Loading