33e4c25e2fc6b2f121bd3e6264a1ede8b3f09068
[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
12 import org.simantics.scl.compiler.commands.CommandSession;
13 import org.simantics.scl.compiler.commands.TestScriptExecutor;
14 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
15 import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
16 import org.simantics.scl.compiler.module.options.ModuleCompilationOptionsAdvisor;
17 import org.simantics.scl.compiler.module.repository.ModuleRepository;
18 import org.simantics.scl.compiler.types.Type;
19 import org.simantics.scl.compiler.types.Types;
20 import org.simantics.scl.osgi.SCLOsgi;
21 import org.simantics.scl.runtime.SCLContext;
22 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
23
24 public class ModelledSTSTest {
25
26     private final String name;
27     private final String code;
28     private final int priority;
29     private final boolean ignored;
30     private Set<String> dependencies;
31     private Set<String> unresolvedDependencies;
32     
33     private CombinedCoverage coverage;
34     private Map<String, String> variables;
35
36     ModelledSTSTest(String name, String code, int priority, boolean ignored, Set<String> dependencies, Map<String, String> variables) {
37         this.name = name;
38         this.code = code;
39         this.priority = priority;
40         this.ignored = ignored;
41         this.dependencies = dependencies;
42         this.unresolvedDependencies = new HashSet<>(dependencies);
43         this.variables = variables;
44     }
45
46     public String getName() {
47         return name;
48     }
49
50     public String getCode() {
51         return code;
52     }
53
54     public int getPriority() {
55         return priority;
56     }
57
58     public boolean isIgnored() {
59         return (ignored || !unresolvedDependencies.isEmpty());
60     }
61
62     public void setCoverage(CombinedCoverage coverage) {
63         this.coverage = coverage;
64     }
65
66     public CombinedCoverage getCoverage() {
67         return coverage;
68     }
69
70     public static class CommandSessionVariable {
71
72         private final String name;
73         private final Type type;
74         private final Object value;
75
76         public CommandSessionVariable(String name, Type type, Object value) {
77             this.name = name;
78             this.type = type;
79             this.value = value;
80         }
81
82         public String getName() {
83             return name;
84         }
85
86         public Type getType() {
87             return type;
88         }
89
90         public Object getValue() {
91             return value;
92         }
93     }
94     
95     public List<CommandSessionVariable> run(List<CommandSessionVariable> vars) throws IOException {
96         ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
97         try {
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         } finally {
134             // remember to flush this repository
135             repo.flush();
136         }
137     }
138
139     public Set<String> getDependencies() {
140         return dependencies;
141     }
142     
143     public Set<String> getUnresolvedDependencies() {
144         return unresolvedDependencies;
145     }
146     
147     public boolean resolveDependency(String testDep) {
148         return unresolvedDependencies.remove(testDep);
149     }
150
151     @Override
152     public int hashCode() {
153         final int prime = 31;
154         int result = 1;
155         result = prime * result + ((code == null) ? 0 : code.hashCode());
156         result = prime * result + ((coverage == null) ? 0 : coverage.hashCode());
157         result = prime * result + ((dependencies == null) ? 0 : dependencies.hashCode());
158         result = prime * result + (ignored ? 1231 : 1237);
159         result = prime * result + ((name == null) ? 0 : name.hashCode());
160         result = prime * result + priority;
161         result = prime * result + ((unresolvedDependencies == null) ? 0 : unresolvedDependencies.hashCode());
162         result = prime * result + ((variables == null) ? 0 : variables.hashCode());
163         return result;
164     }
165
166     @Override
167     public boolean equals(Object obj) {
168         if (this == obj)
169             return true;
170         if (obj == null)
171             return false;
172         if (getClass() != obj.getClass())
173             return false;
174         ModelledSTSTest other = (ModelledSTSTest) obj;
175         if (code == null) {
176             if (other.code != null)
177                 return false;
178         } else if (!code.equals(other.code))
179             return false;
180         if (coverage == null) {
181             if (other.coverage != null)
182                 return false;
183         } else if (!coverage.equals(other.coverage))
184             return false;
185         if (dependencies == null) {
186             if (other.dependencies != null)
187                 return false;
188         } else if (!dependencies.equals(other.dependencies))
189             return false;
190         if (ignored != other.ignored)
191             return false;
192         if (name == null) {
193             if (other.name != null)
194                 return false;
195         } else if (!name.equals(other.name))
196             return false;
197         if (priority != other.priority)
198             return false;
199         if (unresolvedDependencies == null) {
200             if (other.unresolvedDependencies != null)
201                 return false;
202         } else if (!unresolvedDependencies.equals(other.unresolvedDependencies))
203             return false;
204         if (variables == null) {
205             if (other.variables != null)
206                 return false;
207         } else if (!variables.equals(other.variables))
208             return false;
209         return true;
210     }
211 }