Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/TruffleTCK.java @ 22375:78594d342228
Give Truffle languages access to evaluation functions of other languages.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Mon, 16 Nov 2015 15:41:29 +0100 |
parents | 33d49924a921 |
children | 06bdf4a43126 |
comparison
equal
deleted
inserted
replaced
22369:05f812bce8cc | 22375:78594d342228 |
---|---|
36 import static org.junit.Assert.assertEquals; | 36 import static org.junit.Assert.assertEquals; |
37 import static org.junit.Assert.assertNotNull; | 37 import static org.junit.Assert.assertNotNull; |
38 import static org.junit.Assert.assertNotSame; | 38 import static org.junit.Assert.assertNotSame; |
39 import static org.junit.Assert.assertNull; | 39 import static org.junit.Assert.assertNull; |
40 import static org.junit.Assert.assertSame; | 40 import static org.junit.Assert.assertSame; |
41 import static org.junit.Assert.assertTrue; | |
41 import static org.junit.Assert.fail; | 42 import static org.junit.Assert.fail; |
42 import org.junit.Test; | 43 import org.junit.Test; |
43 | 44 |
44 /** | 45 /** |
45 * A collection of tests that can certify language implementation to be compliant with most recent | 46 * A collection of tests that can certify language implementation to be compliant with most recent |
152 * @return name of globally exported symbol, return <code>null</code> if the language doesn't | 153 * @return name of globally exported symbol, return <code>null</code> if the language doesn't |
153 * support the concept of global object | 154 * support the concept of global object |
154 */ | 155 */ |
155 protected String globalObject() { | 156 protected String globalObject() { |
156 throw new UnsupportedOperationException("globalObject() method not implemented"); | 157 throw new UnsupportedOperationException("globalObject() method not implemented"); |
158 } | |
159 | |
160 /** | |
161 * Name of a function to parse source written in some other language. When the function is | |
162 * executed, it expects two arguments. First one is MIME type identifying | |
163 * {@link TruffleLanguage} and the second one is the source code to parse in that language and | |
164 * execute it. The result of the execution is then returned back to the caller. | |
165 * | |
166 * @return name of globally exported symbol to invoke when one wants to execute some code | |
167 */ | |
168 protected String evaluateSource() { | |
169 throw new UnsupportedOperationException("evaluateSource() method not implemented"); | |
157 } | 170 } |
158 | 171 |
159 /** | 172 /** |
160 * Name of a function that counts number of its invocations in current {@link PolyglotEngine} | 173 * Name of a function that counts number of its invocations in current {@link PolyglotEngine} |
161 * context. Your function should somehow keep a counter to remember number of its invocations | 174 * context. Your function should somehow keep a counter to remember number of its invocations |
592 PolyglotEngine.Value function = vm().findGlobalSymbol(globalObjectFunction); | 605 PolyglotEngine.Value function = vm().findGlobalSymbol(globalObjectFunction); |
593 Object global = function.invoke(null).get(); | 606 Object global = function.invoke(null).get(); |
594 assertEquals("Global from the language same with Java obtained one", language.getGlobalObject().get(), global); | 607 assertEquals("Global from the language same with Java obtained one", language.getGlobalObject().get(), global); |
595 } | 608 } |
596 | 609 |
610 @Test | |
611 public void testEvaluateSource() throws Exception { | |
612 Language language = vm().getLanguages().get(mimeType()); | |
613 assertNotNull("Langugage for " + mimeType() + " found", language); | |
614 | |
615 PolyglotEngine.Value function = vm().findGlobalSymbol(evaluateSource()); | |
616 assertNotNull(evaluateSource() + " found", function); | |
617 | |
618 Random r = new Random(); | |
619 double expect = Math.floor(r.nextDouble() * 100000.0) / 10.0; | |
620 Object parsed = function.invoke(null, "x-application/tck", "" + expect).get(); | |
621 assertTrue("Expecting numeric result, was:" + expect, parsed instanceof Number); | |
622 double value = ((Number)parsed).doubleValue(); | |
623 assertEquals("Gets the double", expect, value, 0.01); | |
624 } | |
625 | |
597 private PolyglotEngine.Value findGlobalSymbol(String name) throws Exception { | 626 private PolyglotEngine.Value findGlobalSymbol(String name) throws Exception { |
598 PolyglotEngine.Value s = vm().findGlobalSymbol(name); | 627 PolyglotEngine.Value s = vm().findGlobalSymbol(name); |
599 assert s != null : "Symbol " + name + " is not found!"; | 628 assert s != null : "Symbol " + name + " is not found!"; |
600 return s; | 629 return s; |
601 } | 630 } |