1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/**************************************************************************/
/* N I C E */
/* A high-level object-oriented research language */
/* (c) Daniel Bonniot 2005 */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License, or */
/* (at your option) any later version. */
/* */
/**************************************************************************/
/**
Helper methods for the testsuite Embedded-DSL.
The tests are written as nice programs with _test methods.
The test methods exercise some aspect of compilation by calling the compiler
on certain input, with certain arguments, and in a certain order.
These helper methods provide a convenient way to make those calls to the
compiler.
@author Daniel Bonniot (bonniot@users.sf.net)
*/
import nice.io;
import nice.functional;
import nice.tools.compiler.console;
import nice.tools.compiler;
import bossa.modules;
import java.io.*;
import java.util.jar.*;
public let File TMP = new File("temp-testcase");
public let String NICE_JAR = "share/java/nice.jar";
class HelpersOutput extends nice.tools.compiler.console.ConsoleOutput {
File source;
error(location, message){
printSourceWithLNums(source);
super;
}
progress(packageName, phase){}
}
public void printSourceWithLNums(File source){
println("----1--------10--------20--------30--------40--------50--------60--------70--");
int lnum = 0; foreach(readLines(source),
(String line) => println((++lnum > 9 ? ""lnum : "0"lnum) + "| " + line));
}
public File compile
(String pkg,
?String toplevel = null,
?String imp = null,
?String main = null,
?String check = null)
{
let dirname = pkg.replace('.', File.separatorChar);
let dir = new File(TMP, dirname);
dir.mkdirs();
let source = new File(dir, "main.nice");
writeProgram(source, imp, toplevel, main, check);
let output = new HelpersOutput( source: source );
let compilation = new bossa.modules.Compilation
(listener: output, parser: new bossa.parser.JavaccParser());
compilation.sourcePath = TMP.getPath();
if (imp != null)
compilation.packagePath = new File(TMP, imp + ".jar").toString();
compilation.runtimeFile = "classes";
let outputJar = new File(TMP, pkg + ".jar");
compilation.output = outputJar.getPath();
compile(compilation, pkg);
!assert output.errorCount == 0;
return outputJar;
}
private void writeProgram
(File to, ?String imp, ?String toplevel, ?String main, ?String check)
{
let lines = new LinkedList();
if (imp != null)
lines.add("import "imp";");
if (toplevel != null)
lines.add(toplevel);
if (main != null || check != null)
{
lines.add("public void main(String[] args) {");
if (main != null)
lines.add(main);
if (check != null)
lines.add("assert "check";");
lines.add("}");
}
using(let w = new BufferedWriter(new FileWriter(to))) {
w.write(lines.join("\n"));
}
}
public void run(String jar)
{
run(new java.io.File(TMP, jar + ".jar"));
}
public void run(java.io.File jar)
{
let jarFile = new JarFile(jar);
let mainClassName =
jarFile.getManifest().getMainAttributes().getValue("Main-Class");
let loader = new nice.tools.util.SelfContainedClassLoader([jar.toURL(),new File(NICE_JAR).toURL()]);
nice.tools.util.JDK.setDefaultAssertionStatus(loader, true);
let mainClass = loader.loadClass(mainClassName);
let main = mainClass.getMethod("main", [String[].class]);
try {
main.invoke(null, [cast(null)]);
} catch (java.lang.reflect.InvocationTargetException e) {
throw e.getTargetException();
}
}
|