6d9e4f4fece76c6cc9fe09e5e805735ff6c39206
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / utils / ModelledSTSTest.java
1 package org.simantics.tests.modelled.utils;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.StringReader;
6 import java.util.ArrayList;
7 import java.util.HashSet;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11 import java.util.regex.Pattern;
12
13 import org.simantics.scl.compiler.commands.CommandSession;
14 import org.simantics.scl.compiler.commands.TestScriptExecutor;
15 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
16 import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
17 import org.simantics.scl.compiler.module.options.ModuleCompilationOptionsAdvisor;
18 import org.simantics.scl.compiler.module.repository.ModuleRepository;
19 import org.simantics.scl.compiler.types.Type;
20 import org.simantics.scl.compiler.types.Types;
21 import org.simantics.scl.osgi.SCLOsgi;
22 import org.simantics.scl.runtime.SCLContext;
23 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
24
25 public class ModelledSTSTest {
26
27     private final String name;
28     private final String code;
29     private final int priority;
30     private final boolean ignored;
31     private Set<String> dependencies;
32     private Set<String> unresolvedDependencies;
33     
34     private CombinedCoverage coverage;
35     private Map<String, String> variables;
36
37     ModelledSTSTest(String name, String code, int priority, boolean ignored, Set<String> dependencies, Map<String, String> variables) {
38         this.name = name;
39         this.code = code;
40         this.priority = priority;
41         this.ignored = ignored;
42         this.dependencies = dependencies;
43         this.unresolvedDependencies = new HashSet<>(dependencies);
44         this.variables = variables;
45     }
46
47     public String getName() {
48         return name;
49     }
50
51     public String getCode() {
52         return code;
53     }
54
55     public int getPriority() {
56         return priority;
57     }
58
59     public boolean isIgnored() {
60         return (ignored || !unresolvedDependencies.isEmpty());
61     }
62
63     public void setCoverage(CombinedCoverage coverage) {
64         this.coverage = coverage;
65     }
66
67     public CombinedCoverage getCoverage() {
68         return coverage;
69     }
70
71     public static class CommandSessionVariable {
72
73         private final String name;
74         private final Type type;
75         private final Object value;
76
77         public CommandSessionVariable(String name, Type type, Object value) {
78             this.name = name;
79             this.type = type;
80             this.value = value;
81         }
82
83         public String getName() {
84             return name;
85         }
86
87         public Type getType() {
88             return type;
89         }
90
91         public Object getValue() {
92             return value;
93         }
94     }
95     
96     public List<CommandSessionVariable> run(List<CommandSessionVariable> vars) throws IOException {
97         ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
98         try {
99             repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
100                 
101                 @Override
102                 public ModuleCompilationOptions getOptions(String moduleName) {
103                     // TODO: default to false
104                     boolean coverage = true;
105                     // TODO: add moduleName filters back
106     //                for (Pattern p : getModuleNameFilterPatterns()) {
107     //                    if (p.matcher(moduleName.toLowerCase()).find()) {
108     //                        coverage = true;
109     //                        break;
110     //                    }
111     //                }
112                     return new ModuleCompilationOptions(coverage);
113                 }
114             });
115             
116             SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
117             CommandSession session = new CommandSession(repo, handler);
118             
119             for (CommandSessionVariable var : vars)
120                 session.setVariable(var.getName(), var.getType(), var.getValue());
121             
122             for (Map.Entry<String, String> entry : variables.entrySet())
123                 session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
124             
125             new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute();
126             STSSuiteTestCollector.setTestCoverage(this, session);
127             
128             // Return variables from this session
129             List<CommandSessionVariable> result = new ArrayList<>();
130             for (String var : session.getVariables())
131                 result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var)));
132             
133             return result;
134         } finally {
135             // remember to flush this repository
136             repo.flush();
137         }
138     }
139
140     public Set<String> getDependencies() {
141         return dependencies;
142     }
143     
144     public Set<String> getUnresolvedDependencies() {
145         return unresolvedDependencies;
146     }
147     
148     public boolean resolveDependency(String testDep) {
149         return unresolvedDependencies.remove(testDep);
150     }
151 }