| 1 | #!/usr/bin/env python2
|
| 2 | """
|
| 3 | test_switch.py
|
| 4 | """
|
| 5 | from __future__ import print_function
|
| 6 |
|
| 7 | import os
|
| 8 |
|
| 9 | from mycpp.mylib import switch, log
|
| 10 |
|
| 11 |
|
| 12 | def TestBreakSwitch():
|
| 13 | # type: () -> None
|
| 14 |
|
| 15 | x = 5
|
| 16 | while x < 10:
|
| 17 | with switch(x) as case:
|
| 18 | if case(7):
|
| 19 | print('seven')
|
| 20 | # ERROR - different behavior in C++ and Python
|
| 21 | break
|
| 22 |
|
| 23 | elif case(1, 2):
|
| 24 | for i in range(x):
|
| 25 | break # This is fine, breaks out of the nested loop
|
| 26 | print('one or two')
|
| 27 |
|
| 28 | elif case(3, 4):
|
| 29 | while True:
|
| 30 | break # Also fine, leaves nested loop
|
| 31 | print('three or four')
|
| 32 | break # ERROR
|
| 33 |
|
| 34 | else:
|
| 35 | for i in range(1):
|
| 36 | pass
|
| 37 | print('default')
|
| 38 | break # ERROR
|
| 39 |
|
| 40 | x += 1
|
| 41 |
|
| 42 |
|
| 43 | def run_tests():
|
| 44 | # type: () -> None
|
| 45 | TestBreakSwitch()
|
| 46 |
|
| 47 |
|
| 48 | def run_benchmarks():
|
| 49 | # type: () -> None
|
| 50 | raise NotImplementedError()
|
| 51 |
|
| 52 |
|
| 53 | if __name__ == '__main__':
|
| 54 | if os.getenv('BENCHMARK'):
|
| 55 | log('Benchmarking...')
|
| 56 | run_benchmarks()
|
| 57 | else:
|
| 58 | run_tests()
|