forked from godotengine/godot
-
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.
GDScript: Treat enum values as int and enum types as dictionary
Since enums resolve to a dictionary at runtime, calling dictionary methods on an enum type is a valid use case. This ensures this is true by adding test cases. This also makes enum values be treated as ints when used in operations.
- Loading branch information
Showing
6 changed files
with
127 additions
and
31 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
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
21 changes: 21 additions & 0 deletions
21
modules/gdscript/tests/scripts/analyzer/features/enum_is_treated_as_int.gd
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,21 @@ | ||
# Enum is equivalent to int for comparisons and operations. | ||
enum MyEnum { | ||
ZERO, | ||
ONE, | ||
TWO, | ||
} | ||
|
||
enum OtherEnum { | ||
ZERO, | ||
ONE, | ||
TWO, | ||
} | ||
|
||
func test(): | ||
print(MyEnum.ZERO == OtherEnum.ZERO) | ||
print(MyEnum.ZERO == 1) | ||
print(MyEnum.ZERO != OtherEnum.ONE) | ||
print(MyEnum.ZERO != 0) | ||
|
||
print(MyEnum.ONE + OtherEnum.TWO) | ||
print(2 - MyEnum.ONE) |
7 changes: 7 additions & 0 deletions
7
modules/gdscript/tests/scripts/analyzer/features/enum_is_treated_as_int.out
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,7 @@ | ||
GDTEST_OK | ||
true | ||
false | ||
true | ||
false | ||
3 | ||
1 |
13 changes: 13 additions & 0 deletions
13
modules/gdscript/tests/scripts/analyzer/features/enum_type_is_treated_as_dictionary.gd
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,13 @@ | ||
enum MyEnum { | ||
ZERO, | ||
ONE, | ||
TWO, | ||
} | ||
|
||
func test(): | ||
for key in MyEnum.keys(): | ||
prints(key, MyEnum[key]) | ||
|
||
# https://github.com/godotengine/godot/issues/55491 | ||
for key in MyEnum: | ||
prints(key, MyEnum[key]) |
7 changes: 7 additions & 0 deletions
7
modules/gdscript/tests/scripts/analyzer/features/enum_type_is_treated_as_dictionary.out
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,7 @@ | ||
GDTEST_OK | ||
ZERO 0 | ||
ONE 1 | ||
TWO 2 | ||
ZERO 0 | ||
ONE 1 | ||
TWO 2 |