1 package org.simantics.scl.compiler.tests;
3 import org.junit.Assert;
4 import org.junit.Before;
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;
15 public class TestCommandSession {
17 private static final Logger LOGGER = LoggerFactory.getLogger(TestCommandSession.class);
18 ModuleRepository moduleRepository;
21 public void initialize() throws Exception {
22 moduleRepository = InitialRepository.getInitialRepository();
25 private static class SCLErrorMessageException extends RuntimeException {
26 private static final long serialVersionUID = 418954639267697065L;
27 public SCLErrorMessageException(String message) {
32 private static final SCLReportingHandler TEST_HANDLER = new AbstractSCLReportingHandler() {
34 public void print(String text) {
38 public void printError(String error) {
40 throw new SCLErrorMessageException(error);
45 public void testCommandSession() {
46 CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
48 session.execute("a = 1");
49 session.execute("b = 2");
50 session.execute("a + b");
52 session.execute("x = 1\ny = 2");
53 session.execute("x + y");
57 public void testCommandSession2() {
58 CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
60 session.execute("f name coords = print \"\\(name :: String) \\(coords :: (Double, Double))\"");
61 session.execute("g name (x,y) = f name (x,y)");
65 public void testTyping1() {
66 CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
68 session.execute("apply f = f ()");
69 session.execute("printHello () = print \"Hello\"");
70 session.execute("apply printHello");
74 public void testTyping2() {
75 CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
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) [(),(),()]");
83 public void testValidation() {
85 CompilationError[] errors = CommandSession.validate(moduleRepository, "1+1");
86 Assert.assertEquals(0, errors.length);
89 CompilationError[] errors = CommandSession.validate(moduleRepository, "\"a\"+1");
90 Assert.assertEquals(1, errors.length);
93 CompilationError[] errors = CommandSession.validate(moduleRepository,
97 Assert.assertEquals(0, errors.length);
104 CompilationError[] errors = CommandSession.validate(moduleRepository, source);
105 //System.out.println(CompilationErrorFormatter.toString(source, errors));
106 Assert.assertEquals(1, errors.length);