]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/markdown/MarkdownTests.java
Markdown to HTML generator has now genration options
[simantics/platform.git] / tests / org.simantics.scl.compiler.tests / src / org / simantics / scl / compiler / tests / markdown / MarkdownTests.java
1 package org.simantics.scl.compiler.tests.markdown;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.StringReader;
7 import java.nio.charset.Charset;
8
9 import org.junit.Test;
10 import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext;
11 import org.simantics.scl.compiler.markdown.internal.MarkdownParser;
12 import org.simantics.scl.compiler.markdown.nodes.Node;
13
14 public class MarkdownTests {
15     
16     public static int FAILED = 0;
17     public static int SUCCEEDED = 1;
18     public static int SKIPPED = 2;
19     
20     @Test
21     public void markdownTests() throws IOException {
22         BufferedReader reader = new BufferedReader(
23                 new InputStreamReader(
24                         MarkdownTests.class.getResourceAsStream("spec.txt"),
25                         Charset.forName("UTF-8")));
26         
27         StringBuilder in = new StringBuilder();
28         StringBuilder out = new StringBuilder();
29         int state = 0;
30         int testId = 0;
31         int passed = 0;
32         int failed = 0;
33         int skipped = 0;
34         while(true) {
35             String line = reader.readLine();
36             if(line == null)
37                 break;
38             switch(state) {
39             case 0:
40                 if(line.equals("```````````````````````````````` example"))
41                     ++state;
42                 break;
43             case 1:
44                 if(line.equals("."))
45                     ++state;
46                 else {
47                     if(in.length() > 0)
48                         in.append('\n');
49                     in.append(line);
50                 }
51                 break;
52             case 2:
53                 if(line.equals("````````````````````````````````")) {
54                     ++testId;
55                     int status = test(testId, in.toString(), out.toString());
56                     if(status == SUCCEEDED)
57                         ++passed;
58                     else if(status == FAILED)
59                         ++failed;
60                     else
61                         ++skipped;
62                     in = new StringBuilder();
63                     out = new StringBuilder();
64                     state = 0;
65                 }
66                 else {
67                     if(out.length() > 0)
68                         out.append('\n');
69                     out.append(line);
70                 }
71                 break;
72             }
73         }
74         
75         System.out.println("Passed: " + passed + "/" + testId);
76         System.out.println("Failed: " + failed + "/" + testId);
77         System.out.println("Skipped: " + skipped + "/" + testId);
78     }
79
80     public static int test(int id, String in, String out) throws IOException {
81         if(in.contains("\u2192"))
82             return SKIPPED;
83         MarkdownParser parser = new MarkdownParser();
84         Node node = parser.parseDocument(new StringReader(in.replace('\u2192', '\t')));
85         
86         String result = node.toHtml(HtmlGenerationContext.TEST_DEFAULT).replace('\t', '\u2192');
87                 
88         boolean passed = result.equals(out);
89         
90         if(!passed) {
91             System.out.println("Example " + id); // + (passed ? " passed" : " failed"));
92             System.out.println("---- in --------------------------------------------------------------------");
93             System.out.println(in);
94             System.out.println("---- expected --------------------------------------------------------------");
95             System.out.println(out);
96             System.out.println("---- actual ----------------------------------------------------------------");
97             System.out.println(result);
98             System.out.println("----------------------------------------------------------------------------");
99             System.out.println();
100         }
101             
102         return passed ? SUCCEEDED : FAILED;
103     }
104     
105 }