Skip to content
This repository has been archived by the owner on Dec 2, 2018. It is now read-only.

Commit

Permalink
now builds test descriptions recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
woutergroeneveldkul committed Jun 27, 2011
1 parent 7ea8dc7 commit 45d3250
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 242 deletions.
40 changes: 12 additions & 28 deletions src/main/java/be/klak/junit/jasmine/JasmineDescriptions.java
@@ -1,40 +1,24 @@
package be.klak.junit.jasmine;

import java.util.Collection;
import java.util.Map;


import org.junit.runner.Description;

import be.klak.rhino.RhinoContext;


class JasmineDescriptions {

private final Description rootDescription;
private final Map<String, JasmineSpec> specsMap;
private final RhinoContext rhinoContext;

JasmineDescriptions(Description rootDescription, Map<String, JasmineSpec> specsMap, RhinoContext context) {
this.rootDescription = rootDescription;
this.specsMap = specsMap;
this.rhinoContext = context;
}

public Description getRootDescription() {
return rootDescription;
}

public Collection<JasmineSpec> getAllSpecs() {
return specsMap.values();
}
private final Description rootDescription;
private final Collection<JasmineSpec> specs;

public void executeSpec(Description description) {
getSpec(description).execute(rhinoContext);
}
public JasmineDescriptions(Description rootDescription, Collection<JasmineSpec> specs) {
this.rootDescription = rootDescription;
this.specs = specs;
}

public JasmineSpec getSpec(Description description) {
return specsMap.get(description.getDisplayName());
}
public Description getRootDescription() {
return rootDescription;
}

public Collection<JasmineSpec> getSpecs() {
return specs;
}
}
102 changes: 55 additions & 47 deletions src/main/java/be/klak/junit/jasmine/JasmineJSSuiteConverter.java
@@ -1,59 +1,67 @@
package be.klak.junit.jasmine;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

import java.util.ArrayList;
import java.util.List;

import org.junit.runner.Description;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.NativeObject;

import be.klak.rhino.RhinoContext;


class JasmineJSSuiteConverter {

private final NativeArray baseSuites;
private final RhinoContext context;

JasmineJSSuiteConverter(NativeArray baseSuites, RhinoContext context) {
this.baseSuites = baseSuites;
this.context = context;
}

public JasmineDescriptions convertToJunitDescriptions(Class<?> testClass) {
Description rootDescription = Description.createSuiteDescription(testClass);
Map<String, JasmineSpec> specsMap = convertSuiteArrayToDescriptions(this.baseSuites, rootDescription);
return new JasmineDescriptions(rootDescription, specsMap, context);
}

private Map<String, JasmineSpec> convertSuiteArrayToDescriptions(NativeArray suiteArray, Description rootDescription) {
Map<String, JasmineSpec> specsMap = new HashMap<String, JasmineSpec>();
for (Object idObj : suiteArray.getIds()) {
NativeObject suite = (NativeObject) suiteArray.get((Integer) idObj, suiteArray);

Description suiteDescription = Description
.createSuiteDescription((String) suite.get("description", suite), (Annotation[]) null);
rootDescription.addChild(suiteDescription);
specsMap.putAll(convertToJunitDescription(suite, suiteDescription));
}

return specsMap;
}

private Map<String, JasmineSpec> convertToJunitDescription(NativeObject suite, Description description) {
Map<String, JasmineSpec> specsMap = new HashMap<String, JasmineSpec>();
NativeArray specsArray = (NativeArray) context.executeFunction(suite, "specs");
for (Object idObj : specsArray.getIds()) {
NativeObject spec = (NativeObject) specsArray.get((Integer) idObj, specsArray);

JasmineSpec jasmineSpec = new JasmineSpec(spec);
specsMap.put(jasmineSpec.toString(), jasmineSpec);
description.addChild(jasmineSpec.getDescription());
}

return specsMap;
}
public class JasmineJSSuiteConverter {

private final RhinoContext context;

public JasmineJSSuiteConverter(RhinoContext context) {
this.context = context;
}

public JasmineDescriptions convertToJunitDescriptions(Class<?> testClass, NativeArray baseSuites) {
Description rootDescription = Description.createSuiteDescription(testClass);
List<JasmineSpec> specs = convertSuiteArrayToDescriptions(baseSuites, rootDescription, new ArrayList<String>());
return new JasmineDescriptions(rootDescription, specs);
}

private List<JasmineSpec> convertSuiteArrayToDescriptions(NativeArray suiteArray, Description rootDescription,
List<String> processed) {
List<JasmineSpec> specs = new ArrayList<JasmineSpec>();
for (Object idObj : suiteArray.getIds()) {
NativeObject suite = (NativeObject) suiteArray.get((Integer) idObj, suiteArray);

String description = (String) suite.get("description", suite);
if (!processed.contains(description)) {
Description suiteDescription = addSuiteToDescription(rootDescription, processed, description);
specs.addAll(convertToJunitDescription(suite, suiteDescription));

NativeArray subSuites = (NativeArray) context.executeFunction(suite, "suites");
convertSuiteArrayToDescriptions(subSuites, suiteDescription, processed);
}
}

return specs;
}

private Description addSuiteToDescription(Description description, List<String> processed, String suiteName) {
processed.add(suiteName);
Description suiteDescription = Description.createSuiteDescription(suiteName, (Annotation[]) null);
description.addChild(suiteDescription);
return suiteDescription;
}

private List<JasmineSpec> convertToJunitDescription(NativeObject suite, Description description) {
List<JasmineSpec> specsMap = new ArrayList<JasmineSpec>();
NativeArray specsArray = (NativeArray) context.executeFunction(suite, "specs");
for (Object idObj : specsArray.getIds()) {
NativeObject spec = (NativeObject) specsArray.get((Integer) idObj, specsArray);

JasmineSpec jasmineSpec = new JasmineSpec(spec);
specsMap.add(jasmineSpec);
description.addChild(jasmineSpec.getDescription());
}

return specsMap;
}

}

0 comments on commit 45d3250

Please sign in to comment.