Skip to content

Commit bba4077

Browse files
committed
Fix unittests that rely on PException.toString/getMessage
1 parent d81556c commit bba4077

File tree

9 files changed

+245
-181
lines changed

9 files changed

+245
-181
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.builtins.modules;
42+
43+
import org.hamcrest.CoreMatchers;
44+
import org.hamcrest.Description;
45+
import org.hamcrest.Factory;
46+
import org.hamcrest.Matcher;
47+
import org.hamcrest.TypeSafeMatcher;
48+
import org.junit.Rule;
49+
import org.junit.rules.ExpectedException;
50+
51+
import com.oracle.graal.python.runtime.exception.PException;
52+
import com.oracle.graal.python.test.PythonTests;
53+
54+
public class ConversionNodeTests {
55+
@Rule public ExpectedException expectedException = ExpectedException.none();
56+
57+
static class PExceptionMessageMatcher extends
58+
TypeSafeMatcher<PException> {
59+
60+
private final Matcher<String> matcher;
61+
62+
public PExceptionMessageMatcher(Matcher<String> matcher) {
63+
this.matcher = matcher;
64+
}
65+
66+
public void describeTo(Description description) {
67+
description.appendText("exception with message ");
68+
description.appendDescriptionOf(matcher);
69+
}
70+
71+
@Override
72+
protected boolean matchesSafely(PException item) {
73+
return matcher.matches(PythonTests.getExceptionMessage(item));
74+
}
75+
76+
@Override
77+
protected void describeMismatchSafely(PException item, Description description) {
78+
description.appendText("message ");
79+
matcher.describeMismatch(PythonTests.getExceptionMessage(item), description);
80+
}
81+
82+
@Factory
83+
public static <T extends Throwable> Matcher<T> hasMessage(final Matcher<String> matcher) {
84+
return new org.junit.internal.matchers.ThrowableMessageMatcher<T>(matcher);
85+
}
86+
}
87+
88+
protected void expectPythonMessage(String expectedMessage) {
89+
expectedException.expect(PException.class);
90+
expectedException.expect(new PExceptionMessageMatcher(CoreMatchers.containsString(expectedMessage)));
91+
}
92+
}

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/DirFdConversionNodeTests.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,22 @@
5050
import org.junit.After;
5151
import org.junit.Assert;
5252
import org.junit.Before;
53-
import org.junit.Rule;
5453
import org.junit.Test;
55-
import org.junit.rules.ExpectedException;
5654

5755
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins.DirFdConversionNode;
5856
import com.oracle.graal.python.builtins.objects.PNone;
5957
import com.oracle.graal.python.builtins.objects.frame.PFrame;
6058
import com.oracle.graal.python.builtins.objects.function.PArguments;
6159
import com.oracle.graal.python.runtime.ExecutionContext;
6260
import com.oracle.graal.python.runtime.PythonContext;
63-
import com.oracle.graal.python.runtime.exception.PException;
6461
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6562
import com.oracle.graal.python.test.PythonTests;
6663
import com.oracle.truffle.api.RootCallTarget;
6764
import com.oracle.truffle.api.Truffle;
6865
import com.oracle.truffle.api.frame.VirtualFrame;
6966
import com.oracle.truffle.api.nodes.RootNode;
7067

71-
public class DirFdConversionNodeTests {
72-
@Rule public ExpectedException expectedException = ExpectedException.none();
68+
public class DirFdConversionNodeTests extends ConversionNodeTests {
7369

7470
@Before
7571
public void setUp() {
@@ -105,15 +101,13 @@ public void longFitsInt() {
105101

106102
@Test
107103
public void longTooBig() {
108-
expectedException.expect(PException.class);
109-
expectedException.expectMessage("OverflowError: fd is greater than maximum");
104+
expectPythonMessage("OverflowError: fd is greater than maximum");
110105
call(1L << 40);
111106
}
112107

113108
@Test
114109
public void longTooSmall() {
115-
expectedException.expect(PException.class);
116-
expectedException.expectMessage("OverflowError: fd is less than minimum");
110+
expectPythonMessage("OverflowError: fd is less than minimum");
117111
call(-1L << 40);
118112
}
119113

@@ -124,15 +118,13 @@ public void pintFitsInt() {
124118

125119
@Test
126120
public void pintTooBig() {
127-
expectedException.expect(PException.class);
128-
expectedException.expectMessage("OverflowError: fd is greater than maximum");
121+
expectPythonMessage("OverflowError: fd is greater than maximum");
129122
call(factory().createInt(BigInteger.ONE.shiftLeft(100)));
130123
}
131124

132125
@Test
133126
public void pintTooSmall() {
134-
expectedException.expect(PException.class);
135-
expectedException.expectMessage("OverflowError: fd is less than minimum");
127+
expectPythonMessage("OverflowError: fd is less than minimum");
136128
call(factory().createInt(BigInteger.ONE.shiftLeft(100).negate()));
137129
}
138130

@@ -143,15 +135,13 @@ public void indexFitsInt() {
143135

144136
@Test
145137
public void indexTooBig() {
146-
expectedException.expect(PException.class);
147-
expectedException.expectMessage("OverflowError: fd is greater than maximum");
138+
expectPythonMessage("OverflowError: fd is greater than maximum");
148139
call(evalValue("class C:\n def __index__(self):\n return 1 << 40\nC()"));
149140
}
150141

151142
@Test
152143
public void indexTooSmall() {
153-
expectedException.expect(PException.class);
154-
expectedException.expectMessage("OverflowError: fd is less than minimum");
144+
expectPythonMessage("OverflowError: fd is less than minimum");
155145
call(evalValue("class C:\n def __index__(self):\n return -1 << 100\nC()"));
156146
}
157147

@@ -162,8 +152,7 @@ public void indexInIntSubclass() {
162152

163153
@Test
164154
public void unsupportedType1() {
165-
expectedException.expect(PException.class);
166-
expectedException.expectMessage("TypeError: argument should be integer or None, not float");
155+
expectPythonMessage("TypeError: argument should be integer or None, not float");
167156
call(3.14);
168157
}
169158

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/builtins/modules/FileDescriptorConversionNodeTests.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,22 @@
5050
import org.junit.After;
5151
import org.junit.Assert;
5252
import org.junit.Before;
53-
import org.junit.Rule;
5453
import org.junit.Test;
55-
import org.junit.rules.ExpectedException;
5654

5755
import com.oracle.graal.python.builtins.modules.PosixModuleBuiltins.FileDescriptorConversionNode;
5856
import com.oracle.graal.python.builtins.objects.PNone;
5957
import com.oracle.graal.python.builtins.objects.frame.PFrame;
6058
import com.oracle.graal.python.builtins.objects.function.PArguments;
6159
import com.oracle.graal.python.runtime.ExecutionContext;
6260
import com.oracle.graal.python.runtime.PythonContext;
63-
import com.oracle.graal.python.runtime.exception.PException;
6461
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6562
import com.oracle.graal.python.test.PythonTests;
6663
import com.oracle.truffle.api.RootCallTarget;
6764
import com.oracle.truffle.api.Truffle;
6865
import com.oracle.truffle.api.frame.VirtualFrame;
6966
import com.oracle.truffle.api.nodes.RootNode;
7067

71-
public class FileDescriptorConversionNodeTests {
72-
@Rule public ExpectedException expectedException = ExpectedException.none();
68+
public class FileDescriptorConversionNodeTests extends ConversionNodeTests {
7369

7470
@Before
7571
public void setUp() {
@@ -83,16 +79,14 @@ public void tearDown() {
8379

8480
@Test
8581
public void none() {
86-
expectedException.expect(PException.class);
87-
expectedException.expectMessage("TypeError: argument must be an int, or have a fileno() method.");
82+
expectPythonMessage("TypeError: argument must be an int, or have a fileno() method.");
8883
call(PNone.NONE);
8984
Assert.assertEquals(AT_FDCWD.value, call(PNone.NO_VALUE));
9085
}
9186

9287
@Test
9388
public void noValue() {
94-
expectedException.expect(PException.class);
95-
expectedException.expectMessage("TypeError: argument must be an int, or have a fileno() method.");
89+
expectPythonMessage("TypeError: argument must be an int, or have a fileno() method.");
9690
call(PNone.NO_VALUE);
9791
}
9892

@@ -114,15 +108,13 @@ public void longFitsInt() {
114108

115109
@Test
116110
public void longTooBig() {
117-
expectedException.expect(PException.class);
118-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
111+
expectPythonMessage("OverflowError: Python int too large to convert to int");
119112
call(1L << 40);
120113
}
121114

122115
@Test
123116
public void longTooSmall() {
124-
expectedException.expect(PException.class);
125-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
117+
expectPythonMessage("OverflowError: Python int too large to convert to int");
126118
call(-1L << 40);
127119
}
128120

@@ -133,22 +125,19 @@ public void pintFitsInt() {
133125

134126
@Test
135127
public void pintTooBig() {
136-
expectedException.expect(PException.class);
137-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
128+
expectPythonMessage("OverflowError: Python int too large to convert to int");
138129
call(factory().createInt(BigInteger.ONE.shiftLeft(100)));
139130
}
140131

141132
@Test
142133
public void pintTooSmall() {
143-
expectedException.expect(PException.class);
144-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
134+
expectPythonMessage("OverflowError: Python int too large to convert to int");
145135
call(factory().createInt(BigInteger.ONE.shiftLeft(100).negate()));
146136
}
147137

148138
@Test
149139
public void indexNotUsed() {
150-
expectedException.expect(PException.class);
151-
expectedException.expectMessage("TypeError: argument must be an int, or have a fileno() method.");
140+
expectPythonMessage("TypeError: argument must be an int, or have a fileno() method.");
152141
call(evalValue("class C:\n def __index__(self):\n return 42\nC()"));
153142
}
154143

@@ -159,22 +148,19 @@ public void filenoOk() {
159148

160149
@Test
161150
public void filenoWrongType() {
162-
expectedException.expect(PException.class);
163-
expectedException.expectMessage("TypeError: fileno() returned a non-integer");
151+
expectPythonMessage("TypeError: fileno() returned a non-integer");
164152
call(evalValue("class C:\n def fileno(self):\n return 3.14\nC()"));
165153
}
166154

167155
@Test
168156
public void filenoTooBig() {
169-
expectedException.expect(PException.class);
170-
expectedException.expectMessage("OverflowError: Python int too large to convert to int");
157+
expectPythonMessage("OverflowError: Python int too large to convert to int");
171158
call(evalValue("class C:\n def fileno(self):\n return 1 << 40\nC()"));
172159
}
173160

174161
@Test
175162
public void unsupportedType1() {
176-
expectedException.expect(PException.class);
177-
expectedException.expectMessage("TypeError: argument must be an int, or have a fileno() method.");
163+
expectPythonMessage("TypeError: argument must be an int, or have a fileno() method.");
178164
call(3.14);
179165
}
180166

0 commit comments

Comments
 (0)