|
42 | 42 |
|
43 | 43 | import static org.junit.Assert.assertEquals;
|
44 | 44 | import static org.junit.Assert.assertNotNull;
|
| 45 | +import static org.junit.Assert.assertTrue; |
45 | 46 |
|
46 | 47 | import java.util.Arrays;
|
47 | 48 | import java.util.HashMap;
|
@@ -326,6 +327,75 @@ public void testReenterArgumentsAndValues() throws Throwable {
|
326 | 327 | }
|
327 | 328 | }
|
328 | 329 |
|
| 330 | + @Test |
| 331 | + public void testGettersSetters() throws Throwable { |
| 332 | + final Source source = Source.newBuilder("python", "" + |
| 333 | + "class P:\n" + |
| 334 | + " def __init__(self):\n" + |
| 335 | + " self.__x = None\n" + |
| 336 | + " self.__y = None\n" + |
| 337 | + " self.__nx = 0\n" + |
| 338 | + " self.__ny = 0\n" + |
| 339 | + "\n" + |
| 340 | + " @property\n" + |
| 341 | + " def x(self):\n" + |
| 342 | + " self.__nx += 1\n" + |
| 343 | + " return self.__x\n" + |
| 344 | + "\n" + |
| 345 | + " @x.setter\n" + |
| 346 | + " def x(self, value):\n" + |
| 347 | + " self.__nx += 1\n" + |
| 348 | + " self.__x = value\n" + |
| 349 | + "\n" + |
| 350 | + " @property\n" + |
| 351 | + " def y(self):\n" + |
| 352 | + " self.__ny += 1\n" + |
| 353 | + " return self.__y\n" + |
| 354 | + "\n" + |
| 355 | + "p = P()\n" + |
| 356 | + "str(p)\n" + |
| 357 | + "\n", "testGettersSetters.py").buildLiteral(); |
| 358 | + try (DebuggerSession session = tester.startSession()) { |
| 359 | + session.suspendNextExecution(); |
| 360 | + tester.startEval(source); |
| 361 | + expectSuspended((SuspendedEvent event) -> { |
| 362 | + DebugStackFrame frame = event.getTopStackFrame(); |
| 363 | + assertEquals(1, frame.getSourceSection().getStartLine()); |
| 364 | + event.prepareStepOver(5); |
| 365 | + }); |
| 366 | + expectSuspended((SuspendedEvent event) -> { |
| 367 | + DebugStackFrame frame = event.getTopStackFrame(); |
| 368 | + assertEquals("p = P()", frame.getSourceSection().getCharacters().toString()); |
| 369 | + event.prepareStepOver(1); |
| 370 | + }); |
| 371 | + expectSuspended((SuspendedEvent event) -> { |
| 372 | + DebugValue p = session.getTopScope("python").getDeclaredValue("p"); |
| 373 | + DebugValue x = p.getProperty("x"); |
| 374 | + assertTrue(x.hasReadSideEffects()); |
| 375 | + assertTrue(x.hasWriteSideEffects()); |
| 376 | + assertTrue(x.isReadable()); |
| 377 | + assertTrue(x.isWritable()); |
| 378 | + DebugValue nx = p.getProperty("__nx"); |
| 379 | + assertEquals(0, nx.as(Number.class).intValue()); |
| 380 | + assertEquals("None", x.as(String.class)); |
| 381 | + assertEquals(1, nx.as(Number.class).intValue()); |
| 382 | + x.set(42); |
| 383 | + assertEquals(2, nx.as(Number.class).intValue()); |
| 384 | + assertEquals("42", x.as(String.class)); |
| 385 | + assertEquals(3, nx.as(Number.class).intValue()); |
| 386 | + DebugValue y = p.getProperty("y"); |
| 387 | + assertTrue(y.hasReadSideEffects()); |
| 388 | + assertTrue(y.hasWriteSideEffects()); |
| 389 | + assertTrue(y.isReadable()); |
| 390 | + assertTrue(y.isWritable()); |
| 391 | + DebugValue ny = p.getProperty("__ny"); |
| 392 | + assertEquals(0, ny.as(Number.class).intValue()); |
| 393 | + y.set(24); |
| 394 | + assertEquals("24", y.as(String.class)); |
| 395 | + }); |
| 396 | + } |
| 397 | + } |
| 398 | + |
329 | 399 | private void expectSuspended(SuspendedCallback callback) {
|
330 | 400 | tester.expectSuspended(callback);
|
331 | 401 | }
|
|
0 commit comments