private PolyglotEngine.Value findGlobalSymbol(String name) throws Exception { PolyglotEngine.Value s = vm().findGlobalSymbol(name); assert s != null : "Symbol " + name + " is not found!"; return s; }
private TruffleVM.Symbol findGlobalSymbol(String name) throws Exception { TruffleVM.Symbol s = vm().findGlobalSymbol(name); assert s != null : "Symbol " + name + " is not found!"; return s; } }
/** * @since 0.30 */ @Before public final void enterTCK() throws Exception { PolyglotEngine vm = vm(); this.prev = TruffleTCKAccessor.engineAccess().legacyTckEnter(vm); }
/** * @since 0.30 */ @After public final void afterTCK() throws Exception { TruffleTCKAccessor.engineAccess().legacyTckLeave(vm(), this.prev); }
/** @since 0.8 or earlier */ @Test public void testEvaluateSource() throws Exception { Language language = vm().getLanguages().get(mimeType()); assertNotNull("Language for " + mimeType() + " found", language); PolyglotEngine.Value function = vm().findGlobalSymbol(evaluateSource()); assertNotNull(evaluateSource() + " found", function); double expect = Math.floor(RANDOM.nextDouble() * 100000.0) / 10.0; Object parsed = function.execute("application/x-tck", "" + expect).get(); assertTrue("Expecting numeric result, was:" + expect, parsed instanceof Number); double value = ((Number) parsed).doubleValue(); assertDouble("Gets the double", expect, value); }
@Test(expected = IOException.class) public void testInvalidTestMethod() throws Exception { String mime = mimeType(); String code = invalidCode(); Object ret = vm().eval(mime, code); fail("Should yield IOException, but returned " + ret); }
private void assertIsObjectOfLanguage(Object obj) throws Exception { enterTCK(); // hack to ensure entered PolyglotRuntime.Instrument instr = vm().getRuntime().getInstruments().get(TckInstrument.ID); TruffleLanguage.Env env = TruffleTCKAccessor.engineAccess().getEnvForInstrument(instr, null, mimeType()); assertTrue(obj.toString(), TruffleTCKAccessor.langAccess().isObjectOfLanguage(env, obj)); }
/** @since 0.8 or earlier */ @Test public void testGlobalObjectIsAccessible() throws Exception { String globalObjectFunction = globalObject(); if (globalObjectFunction == null) { return; } Language language = vm().getLanguages().get(mimeType()); assertNotNull("Language for " + mimeType() + " found", language); PolyglotEngine.Value function = vm().findGlobalSymbol(globalObjectFunction); Object global = function.execute().get(); assertEquals("Global from the language same with Java obtained one", language.getGlobalObject().get(), global); assertIsObjectOfLanguage(global); }
/** @since 0.8 or earlier */ @Test public void multiplyTwoVariables() throws Exception { final String firstVar = "var" + (char) ('A' + RANDOM.nextInt(24)); final String secondVar = "var" + (char) ('0' + RANDOM.nextInt(10)); String mulCode = multiplyCode(firstVar, secondVar); // @formatter:off Source source = Source.newBuilder("TCK42:" + mimeType() + ":" + mulCode). name("evaluate " + firstVar + " * " + secondVar). mimeType("application/x-tck"). build(); // @formatter:on final PolyglotEngine.Value evalSource = vm().eval(source); final PolyglotEngine.Value invokeMul = evalSource.execute(firstVar, secondVar); Object result = invokeMul.get(); assertTrue("Expecting numeric result, was:" + result + " for " + firstVar + " and " + secondVar, result instanceof Number); assertEquals("Right value for " + firstVar + " and " + secondVar, 42, ((Number) result).intValue()); }
private CompoundObject findCompoundSymbol() throws Exception { final String compoundObjectName = compoundObject(); PolyglotEngine.Value s = vm().findGlobalSymbol(compoundObjectName); assert s != null : "Symbol " + compoundObjectName + " is not found!"; final PolyglotEngine.Value value = s.execute(); assertIsObjectOfLanguage(value.get()); CompoundObject obj = value.as(CompoundObject.class); assertNotNull("Compound object for " + value + " found", obj); int traverse = RANDOM.nextInt(10); for (int i = 1; i <= traverse; i++) { obj = obj.returnsThis(); assertNotNull("Remains non-null even after " + i + " iteration", obj); } return obj; }
/** @since 0.8 or earlier */ @Test(expected = Exception.class) public void testInvalidTestMethod() throws Exception { String mime = mimeType(); String code = invalidCode(); // @formatter:off Source invalidCode = Source.newBuilder(code). name("Invalid code"). mimeType(mime). build(); // @formatter:on Object ret = vm().eval(invalidCode).get(); fail("Should yield IOException, but returned " + ret); }
/** @since 0.22 */ @Test public void testValueWithSource() throws Exception { String id = valueWithSource(); if (id == null) { return; } PolyglotEngine.Value valueFunction = findGlobalSymbol(id); SourceSection sourceLocation; PolyglotRuntime.Instrument instr = vm().getRuntime().getInstruments().get(TckInstrument.ID); instr.setEnabled(true); try { Value value = valueFunction.execute(); TckInstrument tckInstrument = instr.lookup(TckInstrument.class); assertNotNull(tckInstrument); TruffleInstrument.Env env = tckInstrument.getEnvironment(); assertNotNull(env); sourceLocation = value.getSourceLocation(); assertNotNull(sourceLocation); List<SourceSection> lss = env.getInstrumenter().querySourceSections(SourceSectionFilter.ANY); assertTrue("Source section not among loaded sections", lss.contains(sourceLocation)); } finally { instr.setEnabled(false); } }
/** @since 0.22 */ @Test public void testMetaObject() throws Exception { String[] ids = metaObjects(); if (ids == null) { return; } assert (ids.length > 0); assert (ids.length % 2 == 0); for (int i = 0; i < ids.length; i += 2) { PolyglotEngine.Value valueFunction = findGlobalSymbol(ids[i]); String metaObjectStr; PolyglotRuntime.Instrument instr = vm().getRuntime().getInstruments().get(TckInstrument.ID); instr.setEnabled(true); try { Value value = valueFunction.execute(); metaObjectStr = value.getMetaObject().as(String.class); } finally { instr.setEnabled(false); } PolyglotEngine.Value moFunction = findGlobalSymbol(ids[i + 1]); Object mo = moFunction.execute().get(); assertEquals(mo, metaObjectStr); } }