]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.tests.modelled/src/org/simantics/tests/modelled/utils/ModelledSTSTest.java
Enhancements to modelled tests
[simantics/platform.git] / bundles / org.simantics.tests.modelled / src / org / simantics / tests / modelled / utils / ModelledSTSTest.java
index 00d825d2e215017e9de0d554ece44dd6afa4eeb5..3430696b81456e5959d35b001da7319df906baac 100644 (file)
@@ -1,6 +1,26 @@
 package org.simantics.tests.modelled.utils;
 
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import org.simantics.scl.compiler.commands.CommandSession;
+import org.simantics.scl.compiler.commands.TestScriptExecutor;
 import org.simantics.scl.compiler.module.coverage.CombinedCoverage;
+import org.simantics.scl.compiler.module.options.ModuleCompilationOptions;
+import org.simantics.scl.compiler.module.options.ModuleCompilationOptionsAdvisor;
+import org.simantics.scl.compiler.module.repository.ModuleRepository;
+import org.simantics.scl.compiler.types.Type;
+import org.simantics.scl.compiler.types.Types;
+import org.simantics.scl.osgi.SCLOsgi;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.reporting.SCLReportingHandler;
 
 public class ModelledSTSTest {
 
@@ -8,14 +28,20 @@ public class ModelledSTSTest {
     private final String code;
     private final int priority;
     private final boolean ignored;
-
+    private Set<String> dependencies;
+    private Set<String> unresolvedDependencies;
+    
     private CombinedCoverage coverage;
+    private Map<String, String> variables;
 
-    ModelledSTSTest(String name, String code, int priority, boolean ignored) {
+    ModelledSTSTest(String name, String code, int priority, boolean ignored, Set<String> dependencies, Map<String, String> variables) {
         this.name = name;
         this.code = code;
         this.priority = priority;
         this.ignored = ignored;
+        this.dependencies = dependencies;
+        this.unresolvedDependencies = new HashSet<>(dependencies);
+        this.variables = variables;
     }
 
     public String getName() {
@@ -31,7 +57,7 @@ public class ModelledSTSTest {
     }
 
     public boolean isIgnored() {
-        return ignored;
+        return (ignored || !unresolvedDependencies.isEmpty());
     }
 
     public void setCoverage(CombinedCoverage coverage) {
@@ -41,4 +67,80 @@ public class ModelledSTSTest {
     public CombinedCoverage getCoverage() {
         return coverage;
     }
+
+    public static class CommandSessionVariable {
+
+        private final String name;
+        private final Type type;
+        private final Object value;
+
+        public CommandSessionVariable(String name, Type type, Object value) {
+            this.name = name;
+            this.type = type;
+            this.value = value;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public Type getType() {
+            return type;
+        }
+
+        public Object getValue() {
+            return value;
+        }
+    }
+    
+    public List<CommandSessionVariable> run(List<CommandSessionVariable> vars) throws IOException {
+        ModuleRepository repo = new ModuleRepository(SCLOsgi.SOURCE_REPOSITORY);
+        repo.setAdvisor(new ModuleCompilationOptionsAdvisor() {
+            
+            @Override
+            public ModuleCompilationOptions getOptions(String moduleName) {
+                // TODO: default to false
+                boolean coverage = true;
+                // TODO: add moduleName filters back
+//                for (Pattern p : getModuleNameFilterPatterns()) {
+//                    if (p.matcher(moduleName.toLowerCase()).find()) {
+//                        coverage = true;
+//                        break;
+//                    }
+//                }
+                return new ModuleCompilationOptions(coverage);
+            }
+        });
+        
+        SCLReportingHandler handler = (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
+        CommandSession session = new CommandSession(repo, handler);
+        
+        for (CommandSessionVariable var : vars)
+            session.setVariable(var.getName(), var.getType(), var.getValue());
+        
+        for (Map.Entry<String, String> entry : variables.entrySet())
+            session.setVariable(entry.getKey(), Types.STRING, entry.getValue());
+        
+        new TestScriptExecutor(session, new BufferedReader(new StringReader(code)), handler, true).execute();
+        STSSuiteTestCollector.setTestCoverage(this, session);
+        
+        // Return variables from this session
+        List<CommandSessionVariable> result = new ArrayList<>();
+        for (String var : session.getVariables())
+            result.add(new CommandSessionVariable(var, session.getVariableType(var), session.getVariableValue(var)));
+        
+        return result;
+    }
+
+    public Set<String> getDependencies() {
+        return dependencies;
+    }
+    
+    public Set<String> getUnresolvedDependencies() {
+        return unresolvedDependencies;
+    }
+    
+    public boolean resolveDependency(String testDep) {
+        return unresolvedDependencies.remove(testDep);
+    }
 }