| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import java.io.File; |
| 8 |
import java.io.FileFilter; |
| 9 |
import java.io.FileReader; |
| 10 |
import java.io.IOException; |
| 11 |
import java.util.*; |
| 12 |
|
| 13 |
import org.junit.*; |
| 14 |
import org.junit.runner.RunWith; |
| 15 |
import org.junit.runners.Parameterized; |
| 16 |
import org.junit.runners.Parameterized.Parameters; |
| 17 |
import org.mozilla.javascript.Context; |
| 18 |
import org.mozilla.javascript.ContextFactory; |
| 19 |
import org.mozilla.javascript.drivers.TestUtils; |
| 20 |
import org.mozilla.javascript.tools.shell.Global; |
| 21 |
|
| 22 |
import static org.junit.Assert.*; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
@RunWith(Parameterized.class) |
| 34 |
public class DoctestsTest { |
| 35 |
static final String baseDirectory = "testsrc" + File.separator + "doctests"; |
| 36 |
static final String doctestsExtension = ".doctest"; |
| 37 |
String name; |
| 38 |
String source; |
| 39 |
int optimizationLevel; |
| 40 |
|
| 41 |
public DoctestsTest(String name, String source, int optimizationLevel) { |
| 42 |
this.name = name; |
| 43 |
this.source = source; |
| 44 |
this.optimizationLevel = optimizationLevel; |
| 45 |
} |
| 46 |
|
| 47 |
public static File[] getDoctestFiles() { |
| 48 |
return TestUtils.recursiveListFiles(new File(baseDirectory), |
| 49 |
new FileFilter() { |
| 50 |
public boolean accept(File f) { |
| 51 |
return f.getName().endsWith(doctestsExtension); |
| 52 |
} |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
public static String loadFile(File f) throws IOException { |
| 57 |
int length = (int) f.length(); |
| 58 |
char[] buf = new char[length]; |
| 59 |
new FileReader(f).read(buf, 0, length); |
| 60 |
return new String(buf); |
| 61 |
} |
| 62 |
|
| 63 |
@Parameters(name = "{0}") |
| 64 |
public static Collection<Object[]> doctestValues() throws IOException { |
| 65 |
File[] doctests = getDoctestFiles(); |
| 66 |
List<Object[]> result = new ArrayList<Object[]>(); |
| 67 |
for (File f : doctests) { |
| 68 |
String contents = loadFile(f); |
| 69 |
result.add(new Object[] { f.getName(), contents, -1 }); |
| 70 |
result.add(new Object[] { f.getName(), contents, 0 }); |
| 71 |
result.add(new Object[] { f.getName(), contents, 9 }); |
| 72 |
} |
| 73 |
return result; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
public static Collection<Object[]> singleDoctest() throws IOException { |
| 78 |
List<Object[]> result = new ArrayList<Object[]>(); |
| 79 |
File f = new File(baseDirectory, "Counter.doctest"); |
| 80 |
String contents = loadFile(f); |
| 81 |
result.add(new Object[] { f.getName(), contents, -1 }); |
| 82 |
return result; |
| 83 |
} |
| 84 |
|
| 85 |
@Test |
| 86 |
public void runDoctest() throws Exception { |
| 87 |
ContextFactory factory = ContextFactory.getGlobal(); |
| 88 |
Context cx = factory.enterContext(); |
| 89 |
try { |
| 90 |
cx.setOptimizationLevel(optimizationLevel); |
| 91 |
Global global = new Global(cx); |
| 92 |
|
| 93 |
int testsPassed = global.runDoctest(cx, global, source, name, 1); |
| 94 |
assertTrue(testsPassed > 0); |
| 95 |
} catch (Exception ex) { |
| 96 |
System.out.println(name + "(" + optimizationLevel + "): FAILED due to "+ex); |
| 97 |
throw ex; |
| 98 |
} finally { |
| 99 |
Context.exit(); |
| 100 |
} |
| 101 |
} |
| 102 |
} |