Search in sources :

Example 1 with IString

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();
}
Also used : IValue(io.usethesource.vallang.IValue) QuickCheck(org.rascalmpl.test.infrastructure.QuickCheck) Random(java.util.Random) AbstractFunction(org.rascalmpl.interpreter.result.AbstractFunction) StringWriter(java.io.StringWriter) IBool(io.usethesource.vallang.IBool) TestResult(org.rascalmpl.test.infrastructure.QuickCheck.TestResult) IString(io.usethesource.vallang.IString) PrintWriter(java.io.PrintWriter)

Example 2 with IString

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() + "\"]}");
    }
}
Also used : ArrayList(java.util.ArrayList) IString(io.usethesource.vallang.IString) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) IList(io.usethesource.vallang.IList) IConstructor(io.usethesource.vallang.IConstructor) IBool(io.usethesource.vallang.IBool) StaticError(org.rascalmpl.interpreter.staticErrors.StaticError) ParseError(org.rascalmpl.parser.gtd.exception.ParseError) IString(io.usethesource.vallang.IString)

Example 3 with IString

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;
}
Also used : URIResolverRegistry(org.rascalmpl.uri.URIResolverRegistry) IValue(io.usethesource.vallang.IValue) UnicodeOutputStreamWriter(org.rascalmpl.unicode.UnicodeOutputStreamWriter) IConstructor(io.usethesource.vallang.IConstructor) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IValueOutputStream(io.usethesource.vallang.io.binary.stream.IValueOutputStream) OutputStream(java.io.OutputStream) FileNotFoundException(java.io.FileNotFoundException) StandardTextReader(io.usethesource.vallang.io.StandardTextReader) UnicodeOffsetLengthReader(org.rascalmpl.unicode.UnicodeOffsetLengthReader) Reader(java.io.Reader) StringReader(java.io.StringReader) BufferedReader(java.io.BufferedReader) UnicodeOffsetLengthReader(org.rascalmpl.unicode.UnicodeOffsetLengthReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IString(io.usethesource.vallang.IString) IOException(java.io.IOException)

Example 4 with IString

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);
}
Also used : IValue(io.usethesource.vallang.IValue) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IString(io.usethesource.vallang.IString) IList(io.usethesource.vallang.IList)

Example 5 with IString

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());
}
Also used : IValue(io.usethesource.vallang.IValue) TypeStore(io.usethesource.vallang.type.TypeStore) IString(io.usethesource.vallang.IString) IString(io.usethesource.vallang.IString)

Aggregations

IString (io.usethesource.vallang.IString)80 IValue (io.usethesource.vallang.IValue)32 IConstructor (io.usethesource.vallang.IConstructor)25 ISourceLocation (io.usethesource.vallang.ISourceLocation)22 IOException (java.io.IOException)21 IList (io.usethesource.vallang.IList)15 IInteger (io.usethesource.vallang.IInteger)9 HashMap (java.util.HashMap)9 ISetWriter (io.usethesource.vallang.ISetWriter)8 ITuple (io.usethesource.vallang.ITuple)8 IBool (io.usethesource.vallang.IBool)7 IMap (io.usethesource.vallang.IMap)7 Throw (org.rascalmpl.exceptions.Throw)7 Type (io.usethesource.vallang.type.Type)6 BufferedReader (java.io.BufferedReader)6 URISyntaxException (java.net.URISyntaxException)6 IListWriter (io.usethesource.vallang.IListWriter)5 CharTermAttribute (org.apache.lucene.analysis.tokenattributes.CharTermAttribute)5 ImplementationError (org.rascalmpl.exceptions.ImplementationError)5 ArrayList (java.util.ArrayList)4