|
10 | 10 | package org.truffleruby;
|
11 | 11 |
|
12 | 12 | import org.graalvm.polyglot.Context;
|
| 13 | +import org.graalvm.polyglot.PolyglotException; |
13 | 14 | import org.graalvm.polyglot.Source;
|
14 | 15 | import org.graalvm.polyglot.Value;
|
| 16 | +import org.graalvm.polyglot.tck.InlineSnippet; |
15 | 17 | import org.graalvm.polyglot.tck.LanguageProvider;
|
16 | 18 | import org.graalvm.polyglot.tck.ResultVerifier;
|
17 | 19 | import org.graalvm.polyglot.tck.Snippet;
|
18 | 20 | import org.graalvm.polyglot.tck.TypeDescriptor;
|
19 | 21 | import org.junit.Assert;
|
| 22 | +import org.truffleruby.language.control.RaiseException; |
20 | 23 | import org.truffleruby.shared.TruffleRuby;
|
21 | 24 |
|
22 | 25 | import java.io.File;
|
@@ -142,6 +145,39 @@ public Collection<? extends Snippet> createScripts(Context context) {
|
142 | 145 | return Collections.unmodifiableList(res);
|
143 | 146 | }
|
144 | 147 |
|
| 148 | + @Override |
| 149 | + public Collection<? extends InlineSnippet> createInlineScripts(Context context) { |
| 150 | + List<InlineSnippet> res = new ArrayList<>(); |
| 151 | + res.add(createInlineSnippet( |
| 152 | + context, |
| 153 | + getSource("src/test/ruby/lexical-context.rb"), |
| 154 | + 16, |
| 155 | + "a + b + c", |
| 156 | + 14 + 2 + 6)); |
| 157 | + res.add(createInlineSnippet( |
| 158 | + context, |
| 159 | + getSource("src/test/ruby/lexical-context.rb"), |
| 160 | + 16, |
| 161 | + "binding.local_variable_get(:a) + binding.local_variable_get(:b) + binding.local_variable_get(:c)", |
| 162 | + 14 + 2 + 6)); |
| 163 | + return Collections.unmodifiableList(res); |
| 164 | + } |
| 165 | + |
| 166 | + private InlineSnippet createInlineSnippet(Context context, Source mainSource, int line, String inlineSource, int expected) { |
| 167 | + final Snippet mainSnippet = Snippet.newBuilder(mainSource.getName(), context.eval(mainSource), TypeDescriptor.ANY).build(); |
| 168 | + |
| 169 | + return InlineSnippet.newBuilder(mainSnippet, inlineSource) |
| 170 | + .locationPredicate(sourceSection -> |
| 171 | + sourceSection.getSource().getName().endsWith(mainSource.getName()) && sourceSection.getStartLine() == line) |
| 172 | + .resultVerifier(snippetRun -> { |
| 173 | + final PolyglotException exception = snippetRun.getException(); |
| 174 | + if (exception != null) { |
| 175 | + throw exception; |
| 176 | + } |
| 177 | + Assert.assertEquals(expected, snippetRun.getResult().asInt()); |
| 178 | + }).build(); |
| 179 | + } |
| 180 | + |
145 | 181 | private Snippet createValueConstructor(Context context, String value, TypeDescriptor type) {
|
146 | 182 | return Snippet.newBuilder(value, context.eval(getId(), String.format("-> { %s }", value)), type).build();
|
147 | 183 | }
|
|
0 commit comments