Skip to content

Commit

Permalink
[Adding Guice] making a lot of Desktop HTML tags tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
ceci committed Mar 26, 2013
1 parent b783403 commit 089535f
Show file tree
Hide file tree
Showing 23 changed files with 182 additions and 167 deletions.
11 changes: 5 additions & 6 deletions src/main/java/br/com/caelum/tubaina/chunk/TableChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

public class TableChunk extends CompositeChunk<TableChunk> {

private boolean noborder;
private boolean noBorder;
private String title;

public TableChunk(String options, List<Chunk> rows) {
super(rows);
this.noborder = false;
this.noBorder = false;
this.title = "";
parseOptions(options);
}
Expand All @@ -23,7 +23,7 @@ private void parseOptions(String options) {
Pattern noborderPattern = Pattern.compile("(\".+\")*noborder(\".+\")*");
Matcher noborderMatcher = noborderPattern.matcher(options);
if (noborderMatcher.find())
this.noborder = true;
this.noBorder = true;

Pattern titlePattern = Pattern.compile("\"(.+)\"");
Matcher titleMatcher = titlePattern.matcher(options);
Expand All @@ -45,9 +45,8 @@ public int getMaxNumberOfColumns() {
return maxColumns;
}

//TODO: change with the opposite
public boolean hasNoborder() {
return noborder;
public boolean hasBorder() {
return !noBorder;
}

public String getTitle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,48 @@

public class HtmlAndKindleCodeTag implements Tag<CodeChunk> {

public static final String BEGIN_START = "<pre ";
public static final String BEGIN_END = ">";
public static final String END = "</pre>";
private SyntaxHighlighter htmlCodeHighlighter;
private CodeTagOptionsParser codeTagOptionsParser;

public HtmlAndKindleCodeTag(SyntaxHighlighter htmlCodeHighlighter) {
codeTagOptionsParser = new CodeTagOptionsParser();
this.htmlCodeHighlighter = htmlCodeHighlighter;
}

@Override
public static final String BEGIN_START = "<pre ";
public static final String BEGIN_END = ">";
public static final String END = "</pre>";
private SyntaxHighlighter htmlCodeHighlighter;
private CodeTagOptionsParser codeTagOptionsParser;

public HtmlAndKindleCodeTag(SyntaxHighlighter htmlCodeHighlighter) {
codeTagOptionsParser = new CodeTagOptionsParser();
this.htmlCodeHighlighter = htmlCodeHighlighter;
}

@Override
public String parse(CodeChunk chunk) {
String options = chunk.getOptions();
String options = chunk.getOptions();
String language = detectLanguage(options);
List<Integer> highlights = detectHighlights(options);
boolean numbered = options.contains("#");
SimpleIndentator simpleIndentator = new SimpleIndentator(2);
String indentedCode = simpleIndentator.indent(chunk.getContent());
String label = matchLabel(options);
String code = htmlCodeHighlighter.highlight(indentedCode, language, numbered, highlights);
String result = "";
if (!label.isEmpty()) {
result = BEGIN_START + "id='" + label + "'" + BEGIN_END + code + END;
} else {
result = BEGIN_START + BEGIN_END + code + END;
}
return result;
}

private String detectLanguage(String options) {
return codeTagOptionsParser.parseLanguage(options);
}

private List<Integer> detectHighlights(String options) {
return codeTagOptionsParser.parseHighlights(options);
}

private String matchLabel(String options) {
return codeTagOptionsParser.parseLabel(options);
}
List<Integer> highlights = detectHighlights(options);
boolean numbered = options.contains("#");
SimpleIndentator simpleIndentator = new SimpleIndentator(2);
String indentedCode = simpleIndentator.indent(chunk.getContent());
String label = matchLabel(options);

String code = htmlCodeHighlighter.highlight(indentedCode, language, numbered, highlights);

String result = "";
if (!label.isEmpty()) {
result = BEGIN_START + "id='" + label + "'" + BEGIN_END + code + END;
} else {
result = BEGIN_START + BEGIN_END + code + END;
}
return result;
}

private String detectLanguage(String options) {
return codeTagOptionsParser.parseLanguage(options);
}

private List<Integer> detectHighlights(String options) {
return codeTagOptionsParser.parseHighlights(options);
}

private String matchLabel(String options) {
return codeTagOptionsParser.parseLabel(options);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@

public class TableTagTemplate implements Tag<TableChunk> {

private boolean noborder;

public TableTagTemplate(boolean noborder) {
this.noborder = noborder;
}

@Override
public String parse(TableChunk chunk) {
String result = "";
String title = chunk.getTitle();
if (title != null && !title.trim().isEmpty())
result += "<h3>" + title + "</h3>";
result += "<table";
if (!this.noborder)
if (chunk.hasBorder())
result += " border=1";
result += ">" + chunk.getContent() + "</table>";
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class TableTag implements Tag<TableChunk> {

private TableTagTemplate template;

public TableTag(boolean noborder) {
template = new TableTagTemplate(noborder);
public TableTag() {
template = new TableTagTemplate();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ public class TableTag implements Tag<TableChunk> {
@Override
public String parse(TableChunk chunk) {
String title = chunk.getTitle();
boolean hasNoborder = chunk.hasNoborder();
boolean hasBorder = chunk.hasBorder();
int numberOfColumns = chunk.getMaxNumberOfColumns();
if (numberOfColumns <= 0)
throw new TubainaException("There are no columns inside table " + title);
String tag = "\\begin{table}[!h]\n\\caption{" + title + "}\n\\begin{center}\n";
if (!hasNoborder)
if (hasBorder)
tag += "\\rowcolors[]{1}{gray!30}{gray!15}\n";
tag += "\\begin{tabularx}{";
for (int i = 0; i < numberOfColumns; i++)
tag += "X";
tag += "}\n";
if (!hasNoborder)
if (hasBorder)
tag += "\\hline\n";
tag += chunk.getContent();
if (!hasNoborder)
if (hasBorder)
tag += "\n\\hline";
tag += "\n\\end{tabularx}\n\\end{center}\n\\end{table}";
return tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package br.com.caelum.tubaina.parser.html.desktop;

import java.util.Arrays;
import java.util.List;

import br.com.caelum.tubaina.Chunk;
import br.com.caelum.tubaina.chunk.MockedChunk;

public class AbstractTagTest {
protected String getContent(Chunk chunk) {
new HtmlModule().inject(chunk);
return chunk.asString();
}

protected List<Chunk> text(String text) {
return Arrays.<Chunk>asList(new MockedChunk(text));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import org.junit.Assert;
import org.junit.Test;

import br.com.caelum.tubaina.parser.html.desktop.AnswerTag;
import br.com.caelum.tubaina.chunk.AnswerChunk;


public class AnswerTagTest {
public class AnswerTagTest extends AbstractTagTest {
@Test
public void testAnswerTag(){
AnswerTag tag = new AnswerTag();
String result = tag.parse(chunk);
AnswerChunk chunk = new AnswerChunk(text("texto da resposta"));
String result = getContent(chunk);
Assert.assertEquals("<a class=\"answer\" onclick=\"toogleAnswer('answer0');\">" +
"Click here for the answer</a><br /><div class=\"answer\" " +
"id=\"answer0\">texto da resposta</div><br/>", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import org.junit.Assert;
import org.junit.Test;

import br.com.caelum.tubaina.parser.html.desktop.BoxTag;
import br.com.caelum.tubaina.chunk.BoxChunk;

public class BoxTagTest {
public class BoxTagTest extends AbstractTagTest {

@Test
public void testBox() {
BoxTag tag = new BoxTag();
String result = tag.parse(chunk);
BoxChunk chunk = new BoxChunk("Titulo do Box", text("Texto do Box"));
String result = getContent(chunk);
Assert.assertEquals("<div class=\"box\"><h4>Titulo do Box</h4>\nTexto do Box</div>", result);
}

@Test
public void testBoxWithMultilineContent() {
BoxTag tag = new BoxTag();
String result = tag.parse(chunk);
BoxChunk chunk = new BoxChunk("Titulo do Box", text("Texto do Box\n blablabla"));
String result = getContent(chunk);
Assert.assertEquals("<div class=\"box\"><h4>Titulo do Box</h4>\nTexto do Box\n blablabla</div>", result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import org.junit.Assert;
import org.junit.Test;

import br.com.caelum.tubaina.parser.html.desktop.CenteredParagraphTag;
import br.com.caelum.tubaina.chunk.CenteredParagraphChunk;

public class CenteredParagraphTagTest {
public class CenteredParagraphTagTest extends AbstractTagTest {
@Test
public void testCenteredParagraphTest() {
CenteredParagraphTag tag = new CenteredParagraphTag();
String result = tag.parse(chunk);
CenteredParagraphChunk chunk = new CenteredParagraphChunk("texto centralizado");
String result = getContent(chunk);
Assert.assertEquals("<p class=\"center\">texto centralizado</p>", result);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package br.com.caelum.tubaina.parser.html.desktop;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import org.junit.Assert;
import org.junit.Test;

import br.com.caelum.tubaina.parser.html.HtmlAndKindleCodeTag;
import br.com.caelum.tubaina.chunk.CodeChunk;

public class CodeTagTest {
public class CodeTagTest extends AbstractTagTest {

@Test
public void shouldCallHtmlCodeTag() {
String code = "public static void main(String[] args) {\n" +
" String name = \"Gabriel\";\n" +
" System.out.println(\"Hello, \" + name);\n" +
"}";
HtmlAndKindleCodeTag htmlCodeTag = mock(HtmlAndKindleCodeTag.class);
CodeTag codeTag = new CodeTag(htmlCodeTag);
codeTag.parse(chunk);
verify(htmlCodeTag).parse(chunk);
}
CodeChunk chunk = new CodeChunk(code, "");
String result = getContent(chunk);
Assert.assertEquals("<pre ><div class=\"highlight\">" +
"<pre><span class=\"lineno\">1</span> " +
"public static void main(String[] args) {" +
"\n<span class=\"lineno\">2</span> " +
" String name = &quot;Gabriel&quot;;" +
"\n<span class=\"lineno\">3</span> " +
" System.out.println(&quot;Hello, &quot; + name);" +
"\n<span class=\"lineno\">4</span>" +
" }" +
"\n</pre>" +
"</div></pre>", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import org.junit.Assert;
import org.junit.Test;

import br.com.caelum.tubaina.parser.html.desktop.ExerciseTag;
import br.com.caelum.tubaina.chunk.ExerciseChunk;


public class ExerciseTagTest {
public class ExerciseTagTest extends AbstractTagTest {

@Test
public void testExerciseTag(){
String result = new ExerciseTag().parse(chunk);
ExerciseChunk chunk = new ExerciseChunk(text("texto do exercicio"));
String result = getContent(chunk);
Assert.assertEquals("<ol class=\"exercise\">texto do exercicio</ol>", result);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package br.com.caelum.tubaina.parser.html.desktop;

import junit.framework.Assert;

import org.junit.Test;

import br.com.caelum.tubaina.TubainaException;
import br.com.caelum.tubaina.chunk.ImageChunk;

public class ImageTagTest {
public class ImageTagTest extends AbstractTagTest {

@Test
public void shouldThrowExpectionWhenTagContainsLabel() {
ImageTag tag = new ImageTag();
try {
tag.parse(chunk);
Assert.fail("should throw excpetion");
} catch (TubainaException e) {
}
}
@Test(expected=TubainaException.class)
public void shouldThrowExceptionWhenTagContainsLabel() {
ImageChunk chunk = new ImageChunk("image.png", "label=someLabel", 100, 1);
getContent(chunk);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import br.com.caelum.tubaina.Chunk;
import br.com.caelum.tubaina.builder.ChunkSplitter;
import br.com.caelum.tubaina.parser.html.desktop.ItemTag;
import br.com.caelum.tubaina.chunk.ItemChunk;

public class ItemTagTest extends AbstractTagTest {

public class ItemTagTest {
@Test
public void testItem() {
String result = new ItemTag().parse(chunk);
ItemChunk chunk = new ItemChunk(text("texto do item"));
String result = getContent(chunk);
Assert.assertEquals("<li>texto do item</li>", result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import org.junit.Test;

import br.com.caelum.tubaina.TubainaException;
import br.com.caelum.tubaina.parser.SimpleIndentator;
import br.com.caelum.tubaina.chunk.JavaChunk;

@SuppressWarnings("deprecation")
public class JavaTagTest {
@Deprecated
public class JavaTagTest extends AbstractTagTest {

@Test(expected=TubainaException.class)
@Test(expected=Exception.class)
public void tagIsDeprecatedAndParsingAgainstItWillAlwaysThrowAnException() {
JavaTag tag = new JavaTag(new SimpleIndentator(4));
tag.parse(chunk);
JavaChunk chunk = new JavaChunk("#", "System.out.println()");
getContent(chunk);
}

}
Loading

0 comments on commit 089535f

Please sign in to comment.