forked from jython/jython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyUnicodeComparisonTest.java
More file actions
69 lines (57 loc) · 2.34 KB
/
PyUnicodeComparisonTest.java
File metadata and controls
69 lines (57 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package org.python.core;
import static java.lang.String.format;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.python.core.JavaLangStringProvider.getBeautiful;
import org.junit.Test;
import org.python.util.PythonInterpreter;
public class PyUnicodeComparisonTest {
// variable name
private static final String RESULT = "result";
private String compare() {
StringBuffer b = new StringBuffer();
b.append("from org.python.core import JavaLangStringProvider\n");
b.append("value = JavaLangStringProvider.getBeautiful()\n");
b.append("result = False\n");
b.append("if value == '%s':\n");
b.append(" result = True\n");
return format(b.toString(), getBeautiful());
}
@Test
public void testCompare() {
try (PythonInterpreter interpreter = new PythonInterpreter()) {
interpreter.exec(compare());
assertResultBoolean(true, interpreter);
}
}
private String compareConstructed() {
StringBuffer b = new StringBuffer();
b.append("from org.python.core import JavaLangStringConstructor\n");
b.append("value = JavaLangStringConstructor('%s').getConstructedValue()\n");
b.append("result = False\n");
b.append("if value == '%s':\n");
b.append(" result = True\n");
return format(b.toString(), getBeautiful(), getBeautiful());
}
@Test
public void testCompareConstructed() {
try (PythonInterpreter interpreter = new PythonInterpreter()) {
interpreter.exec(compareConstructed());
assertResultBoolean(true, interpreter);
}
}
private void assertResultBoolean(boolean expected, PythonInterpreter interpreter) {
Object resultObject = interpreter.get(RESULT);
if (resultObject instanceof PyBoolean) {
PyBoolean result = (PyBoolean) resultObject;
if (expected) {
assertTrue("expected result to be True, but was False", result.getBooleanValue());
} else {
assertFalse("expected result to be False, but was True", result.getBooleanValue());
}
} else {
fail("expected result to be PyBoolean but was " + resultObject.getClass().getName());
}
}
}