use of io.usethesource.vallang.IString in project rascal by usethesource.
the class TestEvaluator method runTests.
private void runTests(ModuleEnvironment env, List<AbstractFunction> tests) {
testResultListener.start(env.getName(), tests.size());
// first, let's shuffle the tests
// just to be sure, clone the list
tests = new ArrayList<>(tests);
Collections.shuffle(tests);
QuickCheck qc = new QuickCheck(new Random(), eval.__getVf());
for (AbstractFunction test : tests) {
if (test.hasTag("ignore") || test.hasTag("Ignore") || test.hasTag("ignoreInterpreter") || test.hasTag("IgnoreInterpreter")) {
testResultListener.ignored(test.getName(), test.getAst().getLocation());
continue;
}
try {
int maxDepth = readIntTag(test, QuickCheck.MAXDEPTH, 5);
int maxWidth = readIntTag(test, QuickCheck.MAXWIDTH, 5);
int tries = readIntTag(test, QuickCheck.TRIES, 500);
String expected = null;
if (test.hasTag(QuickCheck.EXPECT_TAG)) {
expected = ((IString) test.getTag(QuickCheck.EXPECT_TAG)).getValue();
}
TestResult result = qc.test(test.getEnv().getName() + "::" + test.getName(), test.getFormals(), expected, (Type[] actuals, IValue[] args) -> {
try {
IValue testResult = test.call(actuals, args, null).getValue();
if ((testResult instanceof IBool) && ((IBool) testResult).getValue()) {
return QuickCheck.SUCCESS;
} else {
return new TestResult(false, null);
}
} catch (Throwable e) {
return new TestResult(false, e);
}
}, env.getRoot().getStore(), tries, maxDepth, maxWidth);
eval.getOutPrinter().flush();
eval.getErrorPrinter().flush();
if (!result.succeeded()) {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
result.writeMessage(out);
out.flush();
testResultListener.report(false, test.getName(), test.getAst().getLocation(), sw.getBuffer().toString(), result.thrownException());
} else {
testResultListener.report(true, test.getName(), test.getAst().getLocation(), "test succeeded", null);
}
} catch (Throwable e) {
testResultListener.report(false, test.getName(), test.getAst().getLocation(), e.getMessage(), e);
}
eval.getOutPrinter().flush();
eval.getErrorPrinter().flush();
}
testResultListener.done();
}
use of io.usethesource.vallang.IString in project rascal by usethesource.
the class HelpServer method validateQuestion.
private Response validateQuestion(Map<String, String> parms) {
try {
if (parms.get("listing") == null || parms.get("question") == null || parms.get("hole1") == null) {
newFixedLengthResponse(Status.NOT_FOUND, "text/plain", "missing listing, question or hole1 parameter");
}
String listing = getParameter(parms, "listing");
String question = getParameter(parms, "question");
ArrayList<String> holes = new ArrayList<>();
for (int i = 1; parms.containsKey("hole" + i); i++) {
holes.add(getParameter(parms, "hole" + i));
}
int k = 0;
while (listing.indexOf("_") >= 0 && k < holes.size()) {
listing = listing.replaceFirst("_", holes.get(k++));
}
// TODO: validate input
writeModule(question, listing);
try {
IList result = makeCommandExecutor().checkQuestions(question);
assert result.length() == 1;
IConstructor test = (IConstructor) result.get(0);
if (((IBool) test.get("success")).getValue()) {
return newFixedLengthResponse(Status.OK, "application/json", "{ \"ok\": true }");
} else {
IString failedMessage = (IString) test.asWithKeywordParameters().getParameter("message");
return newFixedLengthResponse(Status.OK, "application/json", "{ \"ok\": false, " + "\"failed\": [" + makeResult((ISourceLocation) test.get("src"), failedMessage) + "], \"exceptions\": [" + "]}");
}
} catch (StaticError e) {
return newFixedLengthResponse(Status.OK, "application/json", "{ \"ok\": false, \"failed\": [], \"exceptions\": [\"" + e.getMessage() + "\"] }");
} catch (ParseError e) {
return newFixedLengthResponse(Status.OK, "application/json", "{ \"ok\": false, \"failed\": [], \"exceptions\": [], \"syntax\": " + makeLoc(e) + " }");
}
} catch (IOException | URISyntaxException | StaticError e) {
return newFixedLengthResponse(Status.OK, "application/json", "{ \"ok\": false, \"failed\": [], \"exceptions\": [\"" + e.getMessage() + "\"]}");
}
}
use of io.usethesource.vallang.IString in project rascal by usethesource.
the class Prelude method writeFileEnc.
private void writeFileEnc(ISourceLocation sloc, IString charset, IList V, boolean append) {
URIResolverRegistry reg = URIResolverRegistry.getInstance();
if (!Charset.forName(charset.getValue()).canEncode()) {
throw RuntimeExceptionFactory.illegalArgument(charset);
}
Reader prefix = null;
Reader postfix = null;
try {
sloc = reg.logicalToPhysical(sloc);
if (reg.supportsInputScheme(sloc.getScheme())) {
if (sloc.hasOffsetLength()) {
prefix = new UnicodeOffsetLengthReader(reg.getCharacterReader(sloc.top(), charset.getValue()), 0, sloc.getOffset() + (append ? sloc.getLength() : 0));
postfix = new UnicodeOffsetLengthReader(reg.getCharacterReader(sloc.top(), charset.getValue()), sloc.getOffset() + sloc.getLength(), -1);
}
}
OutputStream outStream;
if (prefix != null) {
outStream = new ByteArrayOutputStream(FILE_BUFFER_SIZE);
} else {
outStream = reg.getOutputStream(sloc, append);
}
try (UnicodeOutputStreamWriter out = new UnicodeOutputStreamWriter(outStream, charset.getValue(), append)) {
if (prefix != null) {
copy(prefix, out);
}
for (IValue elem : V) {
if (elem.getType().isString()) {
((IString) elem).write(out);
} else if (elem.getType().isSubtypeOf(RascalValueFactory.Tree)) {
TreeAdapter.yield((IConstructor) elem, out);
} else {
out.append(elem.toString());
}
}
if (postfix != null) {
copy(postfix, out);
}
}
if (prefix != null) {
// we wrote to a buffer instead of the file
try (OutputStream out = reg.getOutputStream(sloc, false)) {
((ByteArrayOutputStream) outStream).writeTo(out);
}
}
} catch (FileNotFoundException fnfex) {
throw RuntimeExceptionFactory.pathNotFound(sloc);
} catch (UnsupportedOperationException e) {
// we tested for offset length above
assert false;
throw RuntimeExceptionFactory.io(values.string(e.getMessage()));
} catch (IOException ioex) {
throw RuntimeExceptionFactory.io(values.string(ioex.getMessage()));
} finally {
try {
if (prefix != null) {
prefix.close();
}
if (postfix != null) {
postfix.close();
}
} catch (IOException e) {
throw RuntimeExceptionFactory.io(values.string(e.getMessage()));
}
}
return;
}
use of io.usethesource.vallang.IString in project rascal by usethesource.
the class Prelude method makeNode.
public IValue makeNode(IString N, IList V, IMap kwParams) // @doc{makeNode -- create a node given its function name and arguments}
{
IList argList = V;
IValue[] args = new IValue[argList.length()];
int i = 0;
for (IValue v : argList) {
args[i++] = v;
}
Map<String, IValue> map = new HashMap<>();
for (IValue key : kwParams) {
map.put(((IString) key).getValue(), kwParams.get(key));
}
return values.node(N.getValue(), args, map);
}
use of io.usethesource.vallang.IString in project rascal by usethesource.
the class StringResult method addSourceLocation.
@Override
protected <U extends IValue> Result<U> addSourceLocation(SourceLocationResult that) {
Result<IValue> path = that.fieldAccess("path", new TypeStore());
String parent = ((IString) path.getValue()).getValue();
String child = getValue().getValue();
if (parent.endsWith("/")) {
parent = parent.substring(0, parent.length() - 1);
}
if (!child.startsWith("/")) {
child = "/" + child;
}
return that.fieldUpdate("path", makeResult(getTypeFactory().stringType(), getValueFactory().string(parent + child), ctx), new TypeStore());
}
Aggregations