Skip to content

Commit

Permalink
Add coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Aug 11, 2024
1 parent e848f90 commit 9809e6f
Showing 1 changed file with 73 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,9 @@ public void toHtmlString_basic() {
assertThat(tag.render(text), is("display text"));
assertThat(tag.render(tsv), is("display text"));

// Use jsoup to ensure that the rendered HTML can be parsed, and check attributes without
// relying on the order that they are generated
final String renderedHtml = tag.render(html);
assertThat(renderedHtml, is(not(nullValue())));

final Document doc = Jsoup.parseBodyFragment(renderedHtml);
final Element span = doc.select("span").first();
final Element span = parseRenderedHtml(tag);

assertThat(span.attributesSize(), is(5));
assertThat(span.attr("sometag"), is("customvalue"));
assertThat(span.attr("nulltag"), is(""));
assertThat(span.attr("emptytag"), is(""));
Expand All @@ -82,30 +77,24 @@ public void toHtmlString_basic() {
assertThat(span.text(), is("display text"));
}

@DisplayName("toHtmlString: escape text, emphasize, and allow free width")
@DisplayName("toHtmlString: escape text, emphasize, and allow free width left aligned")
@Test
public void toHtmlString_escapeEmphasize() {
final Tag tag =
TagBuilder.span()
.withEscapedText("display & text")
.withAlignment(Alignment.right)
.withAlignment(Alignment.left)
.withEmphasis()
.make();
tag.addAttribute("sometag", "custom&value");

assertThat(tag.render(text), is("display & text"));
assertThat(tag.render(tsv), is("display & text"));

// Use jsoup to ensure that the rendered HTML can be parsed, and check attributes without
// relying on the order that they are generated
final String renderedHtml = tag.render(html);
assertThat(renderedHtml, is(not(nullValue())));

final Document doc = Jsoup.parseBodyFragment(renderedHtml);
final Element span = doc.select("span").first();
final Element span = parseRenderedHtml(tag);

assertThat(span.attr("sometag"), is("custom&value"));
assertThat(span.attr("align"), is("right"));
assertThat(span.attr("align"), is("left"));
assertThat(span.text(), is("display & text"));
assertThat(span.select("b").first().outerHtml(), is("<b><i>display &amp; text</i></b>"));
}
Expand All @@ -132,13 +121,7 @@ public void toHtmlString_innerTags() {
assertThat(outerTag.render(text), is("inner text"));
assertThat(outerTag.render(tsv), is("inner text"));

// Use jsoup to ensure that the rendered HTML can be parsed, and check attributes without
// relying on the order that they are generated
final String renderedHtml = outerTag.render(html);
assertThat(renderedHtml, is(not(nullValue())));

final Document doc = Jsoup.parseBodyFragment(renderedHtml);
final Element outerSpan = doc.select("span").first();
final Element outerSpan = parseRenderedHtml(outerTag);
final Element innerSpan = outerSpan.select("span").get(1);

assertThat(outerSpan.attr("sometag"), is("customvalue"));
Expand All @@ -148,4 +131,70 @@ public void toHtmlString_innerTags() {

assertThat(innerSpan.text(), is("inner text"));
}

@DisplayName("toHtmlString: bgcolor")
@Test
public void toHtmlString_bgcolor() {

// Test with color

final Tag tagBgColor =
TagBuilder.span()
.withText("text - color")
.withBackground(Color.fromRGB(255, 0, 100))
.make();

assertThat(tagBgColor.render(text), is("text - color"));
assertThat(tagBgColor.render(tsv), is("text - color"));

final Element spanBgColor = parseRenderedHtml(tagBgColor);

assertThat(spanBgColor.attributesSize(), is(1));
assertThat(spanBgColor.attr("bgcolor"), is("#FF0064"));
assertThat(spanBgColor.text(), is("text - color"));

// Test with no color

final Tag tagNoBg = TagBuilder.span().withText("text - no color").make();

assertThat(tagNoBg.render(text), is("text - no color"));
assertThat(tagNoBg.render(tsv), is("text - no color"));

final Element spanNoBg = parseRenderedHtml(tagNoBg);

assertThat(spanNoBg.attributesSize(), is(0));
assertThat(spanNoBg.attr("bgcolor"), is(""));
assertThat(spanNoBg.text(), is("text - no color"));

// Test with white

final Tag tagBgWhite =
TagBuilder.span().withText("text - white").withBackground(Color.white).make();

assertThat(tagBgWhite.render(text), is("text - white"));
assertThat(tagBgWhite.render(tsv), is("text - white"));

final Element spanBgWhite = parseRenderedHtml(tagBgWhite);

assertThat(spanBgWhite.attributesSize(), is(0));
assertThat(spanBgWhite.attr("bgcolor"), is(""));
assertThat(spanBgWhite.text(), is("text - white"));
}

/**
* Use jsoup to ensure that the rendered HTML can be parsed, and check attributes without relying
* on the order that they are generated
*
* @param tag Tag to render
* @return Top level HTML element
*/
private Element parseRenderedHtml(final Tag tag) {

final String renderedHtml = tag.render(html);
assertThat(renderedHtml, is(not(nullValue())));

final Document doc = Jsoup.parseBodyFragment(renderedHtml);
final Element span = doc.select(tag.getTagName()).first();
return span;
}
}

0 comments on commit 9809e6f

Please sign in to comment.