3430696b81456e5959d35b001da7319df906baac
[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         repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
99             
100             @Override
101             public ModuleCompilationOptions getOptions(String moduleName) {
102                 // TODO: default to false
103                 boolean coverage = true;
104                 // TODO: add moduleName filters back
105 //                for (Pattern p : getModuleNameFilterPatterns()) {
106 //                    if (p.matcher(moduleName.toLowerCase()).find()) {
107 //                        coverage = true;
108 //                        break;
109 //                    }
110 //                }
111                 return new ModuleCompilationOptions(coverage);
112             }
113         });
114         
115         SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
116         CommandSession session = new CommandSession(repo, handler);
117         
118         for (CommandSessionVariable var : vars)
119             session.setVariable(var.getName(), var.getType(), var.getValue());
120         
121         for (Map.Entry<String, String> entry : variables.entrySet())
122             session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
123         
124         new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute();
125         STSSuiteTestCollector.setTestCoverage(this, session);
126         
127         // Return variables from this session
128         List<CommandSessionVariable> result = new ArrayList<>();
129         for (String var : session.getVariables())
130             result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var)));
131         
132         return result;
133     }
134
135     public Set<String> getDependencies() {
136         return dependencies;
137     }
138     
139     public Set<String> getUnresolvedDependencies() {
140         return unresolvedDependencies;
141     }
142     
143     public boolean resolveDependency(String testDep) {
144         return unresolvedDependencies.remove(testDep);
145     }
146 }