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.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;
14 public class TestCommandSession {
16 ModuleRepository moduleRepository;
19 public void initialize() throws Exception {
20 moduleRepository = InitialRepository.getInitialRepository();
23 private static class SCLErrorMessageException extends RuntimeException {
24 private static final long serialVersionUID = 418954639267697065L;
25 public SCLErrorMessageException(String message) {
30 private static final SCLReportingHandler TEST_HANDLER = new AbstractSCLReportingHandler() {
32 public void print(String text) {
33 System.out.println(text);
36 public void printError(String error) {
37 System.err.println(error);
38 throw new SCLErrorMessageException(error);
43 public void testCommandSession() {
44 CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
46 session.execute("a = 1");
47 session.execute("b = 2");
48 session.execute("a + b");
50 session.execute("x = 1\ny = 2");
51 session.execute("x + y");
55 public void testCommandSession2() {
56 CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
58 session.execute("f name coords = print \"\\(name :: String) \\(coords :: (Double, Double))\"");
59 session.execute("g name (x,y) = f name (x,y)");
63 public void testTyping1() {
64 CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
66 session.execute("apply f = f ()");
67 session.execute("printHello () = print \"Hello\"");
68 session.execute("apply printHello");
72 public void testTyping2() {
73 CommandSession session = new CommandSession(moduleRepository, TEST_HANDLER);
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) [(),(),()]");
81 public void testValidation() {
83 CompilationError[] errors = CommandSession.validate(moduleRepository, "1+1");
84 Assert.assertEquals(0, errors.length);
87 CompilationError[] errors = CommandSession.validate(moduleRepository, "\"a\"+1");
88 Assert.assertEquals(1, errors.length);
91 CompilationError[] errors = CommandSession.validate(moduleRepository,
95 Assert.assertEquals(0, errors.length);
102 CompilationError[] errors = CommandSession.validate(moduleRepository, source);
103 //System.out.println(CompilationErrorFormatter.toString(source, errors));
104 Assert.assertEquals(1, errors.length);