-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The previous approach still referenced years as integers, but not all integers are valid years. This new approach uses an enumerated type throughout the application.
- Loading branch information
Showing
8 changed files
with
104 additions
and
42 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 |
---|---|---|
@@ -1,20 +1,60 @@ | ||
class_name Year | ||
|
||
enum { | ||
Freshman, | ||
Sophomore, | ||
Junior, | ||
Senior, | ||
enum Name { | ||
FRESHMAN, | ||
SOPHOMORE, | ||
JUNIOR, | ||
SENIOR | ||
} | ||
|
||
## Get the common English name of the academic year. | ||
static func as_string(year:Name) -> String: | ||
match year: | ||
Name.FRESHMAN: | ||
return "Freshman" | ||
Name.SOPHOMORE: | ||
return "Sophomore" | ||
Name.JUNIOR: | ||
return "Junior" | ||
Name.SENIOR: | ||
return "Senior" | ||
push_error("Unmatched year: %s" % str(year)) | ||
return "Splunge" | ||
|
||
static func values() -> Array[int]: | ||
return [Freshman, Sophomore, Junior, Senior] | ||
|
||
## Determine if the given year has a next year or if it is terminal. | ||
static func has_next(year:Name) -> bool: | ||
return year!=Name.SENIOR | ||
|
||
static func keys() -> Array[String]: | ||
return ["Freshman", "Sophomore", "Junior", "Senior"] | ||
|
||
## Get the year that comes after the given year. | ||
## If the year doesn't have a next year, this will push an error. | ||
static func next(year:Name) -> Name: | ||
match year: | ||
Name.FRESHMAN: return Name.SOPHOMORE | ||
Name.SOPHOMORE: return Name.JUNIOR | ||
Name.JUNIOR: return Name.SENIOR | ||
push_error("Year does not have a next: %s" % str(year)) | ||
# This is a meaningless return but is required for the interpreter | ||
# to see that all code paths return a value. | ||
return Name.FRESHMAN | ||
|
||
static func not_in(years: Array[int]) -> Array[int]: | ||
return Year.values().filter(func(year: int): return not years.has(year)) | ||
|
||
## Return the array of all the years except the given one. | ||
static func not_year(year:Name) -> Array[Name]: | ||
return values().filter(func(y): return y != year) | ||
|
||
|
||
## Return all the years except the given ones. | ||
static func not_years(years: Array[Name]) -> Array[Name]: | ||
return values().filter(func(y): return not years.has(y)) | ||
|
||
|
||
## Get all the possible Year values. | ||
static func values() -> Array[Name]: | ||
return [ | ||
Name.FRESHMAN, | ||
Name.SOPHOMORE, | ||
Name.JUNIOR, | ||
Name.SENIOR, | ||
] |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
extends GutTest | ||
|
||
|
||
func test_year_not_in_single() -> void: | ||
var years: Array[int] = [ Year.Freshman ] | ||
|
||
assert_eq(Year.not_in(years), [ Year.Sophomore, Year.Junior, Year.Senior ]) | ||
|
||
|
||
func test_year_not_in_multiple() -> void: | ||
var years: Array[int] = [ Year.Sophomore, Year.Senior ] | ||
|
||
assert_eq(Year.not_in(years), [ Year.Freshman, Year.Junior ]) | ||
|
||
|
||
func test_year_not_in_all() -> void: | ||
var years: Array[int] = [ Year.Freshman, Year.Sophomore, Year.Junior, Year.Senior ] | ||
|
||
assert_eq(Year.not_in(years), []) | ||
func test_not_year() -> void: | ||
assert_eq(Year.not_year(Year.Name.SOPHOMORE), | ||
[ Year.Name.FRESHMAN, Year.Name.JUNIOR, Year.Name.SENIOR ]) | ||
|
||
|
||
func test_not_years() -> void: | ||
assert_eq(Year.not_years([Year.Name.FRESHMAN, Year.Name.SOPHOMORE]), | ||
[ Year.Name.JUNIOR, Year.Name.SENIOR ]) | ||
|
||
|
||
func test_has_next(params=use_parameters([ | ||
[Year.Name.FRESHMAN, true], | ||
[Year.Name.SOPHOMORE, true], | ||
[Year.Name.JUNIOR, true], | ||
[Year.Name.SENIOR, false] | ||
])) -> void: | ||
var year :Year.Name= params[0] | ||
var expected :bool = params[1] | ||
assert_eq(Year.has_next(year), expected, | ||
"Expected year %s result to be %s" % [str(year), str(expected)] | ||
) | ||
|
||
|
||
func test_next(params=use_parameters([ | ||
[Year.Name.FRESHMAN, Year.Name.SOPHOMORE], | ||
[Year.Name.SOPHOMORE, Year.Name.JUNIOR], | ||
[Year.Name.JUNIOR, Year.Name.SENIOR] | ||
])): | ||
var year :Year.Name = params[0] | ||
var next :Year.Name = params[1] | ||
assert_eq(Year.next(year), next, | ||
"Year after %s should be %s" % [str(year), str(next)]) |
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
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