OILS / mycpp / examples / invalid_switch_break.py View on Github | oils.pub

58 lines, 35 significant
1#!/usr/bin/env python2
2"""
3test_switch.py
4"""
5from __future__ import print_function
6
7import os
8
9from mycpp.mylib import switch, log
10
11
12def 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
43def run_tests():
44 # type: () -> None
45 TestBreakSwitch()
46
47
48def run_benchmarks():
49 # type: () -> None
50 raise NotImplementedError()
51
52
53if __name__ == '__main__':
54 if os.getenv('BENCHMARK'):
55 log('Benchmarking...')
56 run_benchmarks()
57 else:
58 run_tests()