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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
package org.python.core;
import java.math.BigInteger;
import junit.framework.TestCase;
import org.python.core.stringlib.FieldNameIterator;
import org.python.core.stringlib.IntegerFormatter;
import org.python.core.stringlib.InternalFormat;
import org.python.core.stringlib.MarkupIterator;
import org.python.core.stringlib.TextFormatter;
import org.python.core.stringlib.InternalFormat.Spec;
import org.python.util.PythonInterpreter;
/**
* Tests for internal bits and pieces of string.format implementation.
*/
public class StringFormatTest extends TestCase {
/** Exception-raising seems to need the interpreter to be initialised **/
PythonInterpreter interp = new PythonInterpreter();
/** Switches mode in tests that have a shared implementation for bytes and Unicode modes. */
private boolean useBytes = true;
public void testInternalFormatSpec() {
InternalFormat.Spec spec;
spec = InternalFormat.fromText("x");
assertFalse(Spec.specified(spec.align));
assertFalse(Spec.specified(spec.fill));
assertFalse(Spec.specified(spec.width));
assertFalse(Spec.specified(spec.precision));
assertEquals('x', spec.type);
spec = InternalFormat.fromText("<x");
assertEquals('<', spec.align);
assertEquals('x', spec.type);
spec = InternalFormat.fromText("~<x");
assertEquals('~', spec.fill);
assertEquals('<', spec.align);
assertEquals('x', spec.type);
spec = InternalFormat.fromText("+x");
assertEquals('+', spec.sign);
assertEquals('x', spec.type);
spec = InternalFormat.fromText("#x");
assertEquals(true, spec.alternate);
spec = InternalFormat.fromText("0x");
assertEquals('=', spec.align);
assertEquals('0', spec.fill);
spec = InternalFormat.fromText("123x");
assertEquals(123, spec.width);
spec = InternalFormat.fromText("123.456x");
assertEquals(123, spec.width);
assertEquals(456, spec.precision);
assertParseError("123.x", "Format specifier missing precision");
assertParseError("123xx", "Invalid conversion specification");
spec = InternalFormat.fromText("");
assertEquals(Spec.NONE, spec.type);
}
private void assertParseError(String spec, String expected) {
String error = null;
try {
InternalFormat.fromText(spec);
} catch (PyException e) {
assertEquals(Py.ValueError, e.type);
error = e.value.toString();
}
assertEquals(expected, error);
}
/**
* Test the IntegerFormatter returned by {@link PyInteger#prepareFormat}. This is based on the
* original <code>testFormatIntOrLong</code> which tested <code>PyInteger.formatIntOrLong</code>
* .
*/
public void testPrepareFormatter() {
int v = 123;
IntegerFormatter f;
f = PyInteger.prepareFormatter(InternalFormat.fromText("d"));
assertEquals("123", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("o"));
assertEquals("173", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("x"));
assertEquals("7b", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("X"));
assertEquals("7B", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("b"));
assertEquals("1111011", f.format(v).pad().getResult());
int v2 = 1234567890;
f = PyInteger.prepareFormatter(InternalFormat.fromText(",d"));
assertEquals("1,234,567,890", f.format(v2).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("#o"));
assertEquals("0o173", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("#X"));
assertEquals("0X7B", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("c"));
assertEquals("{", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("+d"));
assertEquals("+123", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText(" d"));
assertEquals(" 123", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("5"));
assertEquals(" 123", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("^6"));
assertEquals(" 123 ", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("~<5"));
assertEquals("123~~", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("0=+6"));
assertEquals("+00123", f.format(v).pad().getResult());
assertValueError("0=+6.1", "Precision not allowed in integer format specifier");
assertValueError("+c", "Sign not allowed with integer format specifier 'c'");
f = PyInteger.prepareFormatter(InternalFormat.fromText("c"));
f.setBytes(true);
assertOverflowError(256, f, "%c arg not in range(0x100)");
assertOverflowError(-1, f, "%c arg not in range(0x100)");
assertOverflowError(0x110000, f, "%c arg not in range(0x100)");
f = PyInteger.prepareFormatter(InternalFormat.fromText("c"));
assertOverflowError(0x110000, f, "%c arg not in range(0x110000)");
assertOverflowError(-1, f, "%c arg not in range(0x110000)");
}
/**
* Test the IntegerFormatter returned by {@link PyInteger#prepareFormat}. This is based on the
* original <code>testFormatIntOrLong</code> which tested <code>PyInteger.formatIntOrLong</code>
* .
*/
public void testPrepareFormatterLong() {
BigInteger v = BigInteger.valueOf(123);
IntegerFormatter f;
f = PyInteger.prepareFormatter(InternalFormat.fromText("d"));
assertEquals("123", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("o"));
assertEquals("173", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("x"));
assertEquals("7b", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("X"));
assertEquals("7B", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("b"));
assertEquals("1111011", f.format(v).pad().getResult());
BigInteger v2 = BigInteger.valueOf(1234567890);
f = PyInteger.prepareFormatter(InternalFormat.fromText(",d"));
assertEquals("1,234,567,890", f.format(v2).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("#o"));
assertEquals("0o173", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("#X"));
assertEquals("0X7B", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("c"));
assertEquals("{", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("+d"));
assertEquals("+123", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText(" d"));
assertEquals(" 123", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("5"));
assertEquals(" 123", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("^6"));
assertEquals(" 123 ", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("~<5"));
assertEquals("123~~", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("0=+6"));
assertEquals("+00123", f.format(v).pad().getResult());
f = PyInteger.prepareFormatter(InternalFormat.fromText("c"));
f.setBytes(true);
assertOverflowError(BigInteger.valueOf(256), f, "%c arg not in range(0x100)");
assertOverflowError(BigInteger.valueOf(-1), f, "%c arg not in range(0x100)");
assertOverflowError(BigInteger.valueOf(0x110000), f, "%c arg not in range(0x100)");
f = PyInteger.prepareFormatter(InternalFormat.fromText("c"));
assertOverflowError(BigInteger.valueOf(0x110000), f, "%c arg not in range(0x110000)");
assertOverflowError(BigInteger.valueOf(-1), f, "%c arg not in range(0x110000)");
}
private void assertValueError(String formatSpec, String expected) {
try {
IntegerFormatter f = PyInteger.prepareFormatter(InternalFormat.fromText(formatSpec));
// f.format(123).pad().getResult();
fail("ValueError not thrown, expected: " + expected);
} catch (PyException pye) {
assertEquals(expected, pye.value.toString());
}
}
private void assertOverflowError(int v, IntegerFormatter f, String expected) {
// Test with Java int for PyInteger
try {
f.format(v).pad().getResult();
fail("OverflowError not thrown, expected: " + expected);
} catch (PyException pye) {
assertEquals(expected, pye.value.toString());
}
}
private void assertOverflowError(BigInteger v, IntegerFormatter f, String expected) {
// Test with BigInteger for PyLong
try {
f.format(v).pad().getResult();
fail("OverflowError not thrown, expected: " + expected);
} catch (PyException pye) {
assertEquals(expected, pye.value.toString());
}
}
public void testFormatString() {
String v = "abc";
TextFormatter f;
f = PyString.prepareFormatter(InternalFormat.fromText(""));
assertEquals("abc", f.format(v).pad().getResult());
String v2 = "abcdef";
f = PyString.prepareFormatter(InternalFormat.fromText(".3"));
assertEquals("abc", f.format(v2).pad().getResult());
f = PyString.prepareFormatter(InternalFormat.fromText("6"));
assertEquals("abc ", f.format(v).pad().getResult());
}
public void implTestMarkupIterator() {
MarkupIterator iterator = newMarkupIterator("abc");
assertEquals("abc", iterator.nextChunk().literalText);
assertNull(iterator.nextChunk());
iterator = newMarkupIterator("First, thou shalt count to {0}");
MarkupIterator.Chunk chunk = iterator.nextChunk();
assertEquals("First, thou shalt count to ", chunk.literalText);
assertEquals("0", chunk.fieldName);
assertNull(iterator.nextChunk());
iterator = newMarkupIterator("Weight in tons {0.weight!r:s}");
chunk = iterator.nextChunk();
assertEquals("Weight in tons ", chunk.literalText);
assertEquals("0.weight", chunk.fieldName);
assertEquals("r", chunk.conversion);
assertEquals("s", chunk.formatSpec);
chunk = newMarkupIterator("{{").nextChunk();
assertEquals("{", chunk.literalText);
chunk = newMarkupIterator("}}").nextChunk();
assertEquals("}", chunk.literalText);
chunk = newMarkupIterator("{{}}").nextChunk();
assertEquals("{}", chunk.literalText);
chunk = newMarkupIterator("{0:.{1}}").nextChunk();
assertEquals("0", chunk.fieldName);
assertEquals(".{1}", chunk.formatSpec);
assertTrue(chunk.formatSpecNeedsExpanding);
assertMarkupError("{!}", "end of format while looking for conversion specifier");
assertMarkupError("{!rrrr}", "expected ':' after conversion specifier");
assertMarkupError("{", "Single '{' encountered in format string");
assertMarkupError("}", "Single '}' encountered in format string");
}
public void testMarkupIteratorBytes() {
useBytes = true;
implTestMarkupIterator();
}
public void testMarkupIteratorUnicode() {
useBytes = false;
implTestMarkupIterator();
}
private MarkupIterator newMarkupIterator(String markup) {
PyString markupObject = useBytes ? Py.newString(markup) : Py.newUnicode(markup);
return new MarkupIterator(markupObject);
}
private void assertMarkupError(String markup, String expected) {
MarkupIterator iterator = newMarkupIterator(markup);
String error = null;
try {
iterator.nextChunk();
} catch (IllegalArgumentException e) {
error = e.getMessage();
}
assertEquals(expected, error);
}
public void implTestFieldNameIterator() {
FieldNameIterator it = newFieldNameIterator("abc");
assertEquals("abc", it.head());
assertNull(it.nextChunk());
it = newFieldNameIterator("3");
assertEquals(3, it.head());
assertNull(it.nextChunk());
it = newFieldNameIterator("abc[0]");
assertEquals("abc", it.head());
FieldNameIterator.Chunk chunk = it.nextChunk();
assertEquals(0, chunk.value);
assertFalse(chunk.is_attr);
assertNull(it.nextChunk());
it = newFieldNameIterator("abc.def");
assertEquals("abc", it.head());
chunk = it.nextChunk();
assertEquals("def", chunk.value);
assertTrue(chunk.is_attr);
assertNull(it.nextChunk());
}
public void testFieldNameIteratorBytes() {
useBytes = true;
implTestFieldNameIterator();
}
public void testFieldNameIteratorUnicode() {
useBytes = false;
implTestFieldNameIterator();
}
private FieldNameIterator newFieldNameIterator(String field) {
PyString fieldObject = useBytes ? Py.newString(field) : Py.newUnicode(field);
return new FieldNameIterator(fieldObject);
}
}
|