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