|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.builtins.modules;
|
42 | 42 |
|
43 |
| -import org.hamcrest.CoreMatchers; |
44 |
| -import org.hamcrest.Description; |
45 |
| -import org.hamcrest.Matcher; |
46 |
| -import org.hamcrest.TypeSafeMatcher; |
47 | 43 | import org.junit.Rule;
|
48 | 44 | import org.junit.rules.ExpectedException;
|
49 | 45 |
|
| 46 | +import com.oracle.graal.python.PythonLanguage; |
| 47 | +import com.oracle.graal.python.builtins.objects.frame.PFrame; |
| 48 | +import com.oracle.graal.python.builtins.objects.function.PArguments; |
| 49 | +import com.oracle.graal.python.builtins.objects.function.Signature; |
| 50 | +import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary; |
| 51 | +import com.oracle.graal.python.nodes.PRootNode; |
| 52 | +import com.oracle.graal.python.nodes.call.CallTargetInvokeNode; |
| 53 | +import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaise; |
| 54 | +import com.oracle.graal.python.runtime.ExecutionContext.CalleeContext; |
| 55 | +import com.oracle.graal.python.runtime.ExecutionContext.IndirectCalleeContext; |
| 56 | +import com.oracle.graal.python.runtime.PythonContext; |
50 | 57 | import com.oracle.graal.python.runtime.exception.PException;
|
51 |
| -import com.oracle.graal.python.test.PythonTests; |
| 58 | +import com.oracle.graal.python.runtime.object.PythonObjectFactory; |
| 59 | +import com.oracle.truffle.api.RootCallTarget; |
| 60 | +import com.oracle.truffle.api.Truffle; |
| 61 | +import com.oracle.truffle.api.frame.VirtualFrame; |
52 | 62 |
|
53 | 63 | public class ConversionNodeTests {
|
| 64 | + static final Signature SIGNATURE = new Signature(-1, false, -1, false, new String[]{"arg"}, null); |
54 | 65 | @Rule public ExpectedException expectedException = ExpectedException.none();
|
55 | 66 |
|
56 |
| - static class PExceptionMessageMatcher extends |
57 |
| - TypeSafeMatcher<PException> { |
| 67 | + protected static Object call(Object arg, ArgumentCastNodeWithRaise castNode) { |
| 68 | + final PythonContext pythonContext = PythonLanguage.getContext(); |
| 69 | + RootCallTarget callTarget = Truffle.getRuntime().createCallTarget(new PRootNode(null) { |
| 70 | + @Child private CalleeContext calleeContext = CalleeContext.create(); |
| 71 | + @Child private ArgumentCastNodeWithRaise node = castNode; |
58 | 72 |
|
59 |
| - private final Matcher<String> matcher; |
| 73 | + @Override |
| 74 | + public Object execute(VirtualFrame frame) { |
| 75 | + calleeContext.enter(frame); |
| 76 | + try { |
| 77 | + return node.execute(frame, PArguments.getArgument(frame, 0)); |
| 78 | + } finally { |
| 79 | + calleeContext.exit(frame, this); |
| 80 | + } |
| 81 | + } |
60 | 82 |
|
61 |
| - public PExceptionMessageMatcher(Matcher<String> matcher) { |
62 |
| - this.matcher = matcher; |
63 |
| - } |
64 |
| - |
65 |
| - public void describeTo(Description description) { |
66 |
| - description.appendText("exception with message "); |
67 |
| - description.appendDescriptionOf(matcher); |
68 |
| - } |
69 |
| - |
70 |
| - @Override |
71 |
| - protected boolean matchesSafely(PException item) { |
72 |
| - return matcher.matches(PythonTests.getExceptionMessage(item)); |
73 |
| - } |
| 83 | + @Override |
| 84 | + public Signature getSignature() { |
| 85 | + return SIGNATURE; |
| 86 | + } |
74 | 87 |
|
75 |
| - @Override |
76 |
| - protected void describeMismatchSafely(PException item, Description description) { |
77 |
| - description.appendText("message "); |
78 |
| - matcher.describeMismatch(PythonTests.getExceptionMessage(item), description); |
| 88 | + @Override |
| 89 | + public boolean isPythonInternal() { |
| 90 | + return true; |
| 91 | + } |
| 92 | + }); |
| 93 | + try { |
| 94 | + Object[] arguments = PArguments.create(1); |
| 95 | + PArguments.setGlobals(arguments, PythonObjectFactory.getUncached().createDict()); |
| 96 | + PArguments.setException(arguments, PException.NO_EXCEPTION); |
| 97 | + PArguments.setArgument(arguments, 0, arg); |
| 98 | + PFrame.Reference frameInfo = IndirectCalleeContext.enter(pythonContext, arguments, callTarget); |
| 99 | + try { |
| 100 | + return CallTargetInvokeNode.invokeUncached(callTarget, arguments); |
| 101 | + } finally { |
| 102 | + IndirectCalleeContext.exit(pythonContext, frameInfo); |
| 103 | + } |
| 104 | + } catch (PException e) { |
| 105 | + // materialize PException's error message since we are leaving Python |
| 106 | + PException exceptionForReraise = e.getExceptionForReraise(); |
| 107 | + PythonObjectLibrary pythonObjectLibrary = PythonObjectLibrary.getUncached(); |
| 108 | + exceptionForReraise.setMessage(exceptionForReraise.getUnreifiedException().getFormattedMessage(pythonObjectLibrary, pythonObjectLibrary)); |
| 109 | + throw exceptionForReraise; |
79 | 110 | }
|
80 | 111 | }
|
81 | 112 |
|
82 | 113 | protected void expectPythonMessage(String expectedMessage) {
|
83 | 114 | expectedException.expect(PException.class);
|
84 |
| - expectedException.expect(new PExceptionMessageMatcher(CoreMatchers.containsString(expectedMessage))); |
| 115 | + expectedException.expectMessage(expectedMessage); |
85 | 116 | }
|
86 | 117 | }
|
0 commit comments