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