forked from mikey/micropython
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests/basics: Add tests for assignment operator :=.
- Loading branch information
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
(x := 4) | ||
print(x) | ||
|
||
if x := 2: | ||
print(True) | ||
print(x) | ||
|
||
print(4, x := 5) | ||
print(x) | ||
|
||
x = 1 | ||
print(x, x := 5, x) | ||
print(x) | ||
|
||
|
||
def foo(): | ||
print("any", any((hit := i) % 5 == 3 and (hit % 2) == 0 for i in range(10))) | ||
return hit | ||
|
||
|
||
hit = 123 | ||
print(foo()) | ||
print(hit) # shouldn't be changed by foo | ||
|
||
print("any", any((hit := i) % 5 == 3 and (hit % 2) == 0 for i in range(10))) | ||
print(hit) # should be changed by above | ||
|
||
print([((m := k + 1), k * m) for k in range(4)]) | ||
print(m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
4 | ||
True | ||
2 | ||
4 5 | ||
5 | ||
1 5 5 | ||
5 | ||
any True | ||
8 | ||
123 | ||
any True | ||
8 | ||
[(1, 0), (2, 2), (3, 6), (4, 12)] | ||
4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# test SyntaxError with := operator | ||
|
||
def test(code): | ||
try: | ||
print(eval(code)) | ||
except SyntaxError: | ||
print('SyntaxError') | ||
|
||
test("x := 1") | ||
test("((x, y) := 1)") | ||
|
||
# these are currently all allowed in MicroPython, but not in CPython | ||
test("([i := i + 1 for i in range(4)])") | ||
test("([i := -1 for i, j in [(1, 2)]])") | ||
test("([[(i := j) for i in range(2)] for j in range(2)])") | ||
test("([[(j := i) for i in range(2)] for j in range(2)])") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
SyntaxError | ||
SyntaxError | ||
[1, 2, 3, 4] | ||
[-1] | ||
[[0, 0], [1, 1]] | ||
[[0, 1], [0, 1]] |