]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/tests/org/simantics/graph/compiler/tests/GraphCompilerTests.java
c0e328d04a0e6dda87000fb351a2bb6426fd462b
[simantics/platform.git] / bundles / org.simantics.graph.compiler / tests / org / simantics / graph / compiler / tests / GraphCompilerTests.java
1 package org.simantics.graph.compiler.tests;
2
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.util.ArrayList;
6
7 import junit.framework.Assert;
8
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.simantics.graph.compiler.CompilationResult;
12 import org.simantics.graph.compiler.ExternalFileLoader;
13 import org.simantics.graph.compiler.GraphCompiler;
14 import org.simantics.graph.compiler.GraphCompilerPreferences;
15 import org.simantics.graph.representation.TransferableGraph1;
16 import org.simantics.ltk.ISource;
17 import org.simantics.ltk.LocalResourceSource;
18
19 public class GraphCompilerTests {
20     
21     @Before
22     public void initialize() {
23         
24     }
25     
26     public CompilationResult compile(String source) throws Exception {
27         ArrayList<ISource> sources = new ArrayList<ISource>();
28         sources.add(new LocalResourceSource(GraphCompilerTests.class, source));
29         ArrayList<TransferableGraph1> dependencies = new ArrayList<TransferableGraph1>();
30         dependencies.add(GraphCompiler.read(GraphCompilerTests.class.getResourceAsStream("graph.tg")));
31         ExternalFileLoader fileLoader = new ExternalFileLoader() {                  
32             @Override
33             public byte[] load(String fileName) throws IOException {
34                 throw new FileNotFoundException();
35             }
36         };
37         GraphCompilerPreferences preferences = new GraphCompilerPreferences();
38         preferences.validate = true;
39         return GraphCompiler.compile(
40                 "1.0",
41                 sources, 
42                 dependencies, 
43                 fileLoader,
44                 preferences);
45     }
46     
47     @Test
48     public void testUndefinedVariable() throws Exception {
49         CompilationResult result = compile("graphs/UndefinedVariable.pgraph");
50         Assert.assertEquals(1, result.getErrors().size());
51     }
52     
53     @Test
54     public void testUndefinedConcept() throws Exception {
55         CompilationResult result = compile("graphs/UndefinedConcept.pgraph");
56         Assert.assertEquals(1, result.getErrors().size());
57     }
58     
59     @Test
60     public void testDomainAndRangeSyntax() throws Exception {
61         CompilationResult result = compile("graphs/DomainAndRangeSyntax.pgraph");
62         Assert.assertEquals(0, result.getErrors().size());
63     }
64     
65     @Test
66     public void testLists() throws Exception {
67         CompilationResult result = compile("graphs/Lists.pgraph");
68         Assert.assertEquals(0, result.getErrors().size());
69         Assert.assertEquals(3 /*InstanceOf*/ + 2 /*Element*/ + 3 /*Next*/, 
70                 result.getGraph().statements.length/4);
71         result.getGraph().print();
72     }
73 }