]> gerrit.simantics Code Review - simantics/platform.git/blob - tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/TestCommandSession.java
(refs #7307) Added features field to SCL module header
[simantics/platform.git] / tests / org.simantics.scl.compiler.tests / src / org / simantics / scl / compiler / tests / TestCommandSession.java
1 package org.simantics.scl.compiler.tests;
2
3 import org.junit.Assert;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.simantics.scl.compiler.commands.CommandSession;
7 import org.simantics.scl.compiler.errors.CompilationError;
8 import org.simantics.scl.compiler.errors.CompilationErrorFormatter;
9 import org.simantics.scl.compiler.module.repository.ModuleRepository;
10 import org.simantics.scl.runtime.reporting.AbstractSCLReportingHandler;
11 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
12
13
14 public class TestCommandSession {
15
16     ModuleRepository moduleRepository;
17
18     @Before
19     public void initialize() throws Exception {
20         moduleRepository = InitialRepository.getInitialRepository();
21     }
22     
23     private static class SCLErrorMessageException extends RuntimeException {
24         private static final long serialVersionUID = 418954639267697065L;
25         public SCLErrorMessageException(String message) {
26             super(message);
27         }
28     }
29     
30     private static final SCLReportingHandler TEST_HANDLER = new AbstractSCLReportingHandler() {
31         @Override
32         public void print(String text) {
33             System.out.println(text);
34         }
35         
36         public void printError(String error) {
37             System.err.println(error);
38             throw new SCLErrorMessageException(error);
39         }
40     };
41
42     @Test
43     public void testCommandSession() {
44         CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
45         
46         session.execute("a = 1");
47         session.execute("b = 2");
48         session.execute("a + b");
49         
50         session.execute("x = 1\ny = 2");
51         session.execute("x + y");
52     }
53     
54     @Test
55     public void testCommandSession2() {
56         CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
57         
58         session.execute("f name coords = print \"\\(name :: String) \\(coords :: (Double, Double))\"");
59         session.execute("g name (x,y) = f name (x,y)");
60     }
61     
62     @Test
63     public void testTyping1() {
64         CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
65         
66         session.execute("apply f = f ()");
67         session.execute("printHello () = print \"Hello\"");
68         session.execute("apply printHello");
69     }
70     
71     @Test
72     public void testTyping2() {
73         CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
74         
75         session.execute("iter f (list :: [a]) = loop 0 where { len = length list ; loop i | i == len = 0 | otherwise = do f (list!i) ; loop (i+1) }");
76         session.execute("iter (\\i -> print i) [1,2,3]");
77         session.execute("iter (\\i -> print i) [(),(),()]");
78     }
79     
80     @Test
81     public void testValidation() {
82         {
83             CompilationError[] errors = CommandSession.validate(moduleRepository, "1+1");
84             Assert.assertEquals(0, errors.length);
85         }
86         {
87             CompilationError[] errors = CommandSession.validate(moduleRepository, "\"a\"+1");
88             Assert.assertEquals(1, errors.length);
89         }
90         {
91             CompilationError[] errors = CommandSession.validate(moduleRepository,
92                     "a = 1\n" +
93                     "b = 2\n" +
94                     "a + b");
95             Assert.assertEquals(0, errors.length);
96         }
97         {
98             String source = 
99                     "a = 1\n" +
100                     "b = 2.0\n" +
101                     "a + b"; 
102             CompilationError[] errors = CommandSession.validate(moduleRepository, source);
103             //System.out.println(CompilationErrorFormatter.toString(source, errors));
104             Assert.assertEquals(1, errors.length);
105         }
106     }
107
108 }