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