OILS / mycpp / examples / invalid_switch.py View on Github | oilshell.org

84 lines, 47 significant
1#!/usr/bin/env python2
2"""
3invalid_except.py
4"""
5from __future__ import print_function
6
7from mycpp.mylib import switch, str_switch, tagswitch
8
9
10def NoDefault():
11 # type: () -> None
12
13 s = "foo"
14 with str_switch(s) as case:
15 if case("bar"):
16 print('bar')
17
18
19def TagSwitch():
20 # type: () -> None
21
22 s = "foo"
23 with tagswitch(s) as case:
24 if 42:
25 print('ONE')
26 print('dupe')
27
28 elif 43:
29 print('TWO')
30
31 else:
32 print('neither')
33
34
35def SwitchMustHaveCase():
36 # type: () -> None
37
38 i = 49
39 with switch(i) as case:
40 if 42:
41 print('ONE')
42 print('dupe')
43
44 elif 43:
45 print('TWO')
46
47 else:
48 print('neither')
49
50
51def StrSwitchNoTuple():
52 # type: () -> None
53
54 s = "foo"
55 with str_switch(s) as case:
56 # Problem: if you switch on length, do you duplicate the bogies
57 if case('spam', 'different len'):
58 print('ONE')
59 print('dupe')
60
61 elif case('foo'):
62 print('TWO')
63
64 else:
65 print('neither')
66
67
68def StrSwitchNoInt():
69 # type: () -> None
70
71 s = "foo"
72 with str_switch(s) as case:
73 # integer not allowed
74 if case(42):
75 print('ONE')
76 print('dupe')
77
78 else:
79 print('neither')
80
81
82def run_tests():
83 # type: () -> None
84 pass