Skip to content

Commit 86ef507

Browse files
committed
Drops my silly idea of case( range(1,3) ) -> 1, 2, or 3.
1 parent 66ea094 commit 86ef507

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

switchlang.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def default(self, func: Callable[[], Any]):
1212

1313
def case(self, key, func: Callable[[], Any]):
1414
if isinstance(key, range):
15-
for n in range(key.start, key.stop + 1, key.step):
15+
for n in key:
1616
self.case(n, func)
1717
return
1818

tests/coretests.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,34 @@ def test_error_duplicate_case(self):
8888
s.case(1, lambda: None)
8989

9090
def test_multiple_values_one_case_range(self):
91-
value = 7
92-
9391
executed_case = None
9492

9593
def get_set_case(val):
9694
nonlocal executed_case
9795
executed_case = val
9896

99-
# I'm a little unsure what is the right way to handle this
100-
# On one hand, reading case(range(1,5)) seems like it should
101-
# include 1, 2, 3, 4, 5.
102-
# But list(range(1,5)) is [1,2,3,4]. So that would be inconsistent.
103-
# Thoughts?
104-
# I'm going with 1,2,3,4,5 for range(1,5) for now.
105-
with switch(value) as s:
106-
s.case(range(1, 5), lambda: get_set_case("1-to-5"))
97+
for value in range(1, 5):
98+
with switch(value) as s:
99+
s.case(range(1, 6), lambda: get_set_case("1-to-5"))
100+
s.case(range(6, 7), lambda: get_set_case("6-to-7"))
101+
s.default(lambda: get_set_case('default'))
102+
103+
self.assertEqual(executed_case, "1-to-5")
104+
105+
for value in range(6, 7):
106+
with switch(value) as s:
107+
s.case(range(1, 6), lambda: get_set_case("1-to-5"))
108+
s.case(range(6, 7), lambda: get_set_case("6-to-7"))
109+
s.default(lambda: get_set_case('default'))
110+
111+
self.assertEqual(executed_case, "6-to-7")
112+
113+
with switch(7) as s:
114+
s.case(range(1, 6), lambda: get_set_case("1-to-5"))
107115
s.case(range(6, 7), lambda: get_set_case("6-to-7"))
108116
s.default(lambda: get_set_case('default'))
109117

110-
self.assertEqual(executed_case, "6-to-7")
118+
self.assertEqual(executed_case, "default")
111119

112120
def test_multiple_values_one_case_list(self):
113121
value = 6

0 commit comments

Comments
 (0)