1 package org.simantics.tests.modelled.utils;
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;
11 import java.util.regex.Pattern;
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;
25 public class ModelledSTSTest {
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;
34 private CombinedCoverage coverage;
35 private Map<String, String> variables;
37 ModelledSTSTest(String name, String code, int priority, boolean ignored, Set<String> dependencies, Map<String, String> variables) {
40 this.priority = priority;
41 this.ignored = ignored;
42 this.dependencies = dependencies;
43 this.unresolvedDependencies = new HashSet<>(dependencies);
44 this.variables = variables;
47 public String getName() {
51 public String getCode() {
55 public int getPriority() {
59 public boolean isIgnored() {
60 return (ignored || !unresolvedDependencies.isEmpty());
63 public void setCoverage(CombinedCoverage coverage) {
64 this.coverage = coverage;
67 public CombinedCoverage getCoverage() {
71 public static class CommandSessionVariable {
73 private final String name;
74 private final Type type;
75 private final Object value;
77 public CommandSessionVariable(String name, Type type, Object value) {
83 public String getName() {
87 public Type getType() {
91 public Object getValue() {
96 public List<CommandSessionVariable> run(List<CommandSessionVariable> vars) throws IOException {
97 ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
99 repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
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()) {
112 return new ModuleCompilationOptions(coverage);
116 SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
117 CommandSession session = new CommandSession(repo, handler);
119 for (CommandSessionVariable var : vars)
120 session.setVariable(var.getName(), var.getType(), var.getValue());
122 for (Map.Entry<String, String> entry : variables.entrySet())
123 session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
125 new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute();
126 STSSuiteTestCollector.setTestCoverage(this, session);
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)));
135 // remember to flush this repository
140 public Set<String> getDependencies() {
144 public Set<String> getUnresolvedDependencies() {
145 return unresolvedDependencies;
148 public boolean resolveDependency(String testDep) {
149 return unresolvedDependencies.remove(testDep);