Use dot notation for enum variants
This commit is contained in:
parent
ab783c33ed
commit
117d188194
13 changed files with 162 additions and 154 deletions
|
|
@ -12,13 +12,13 @@ enum PaymentResult { Accepted(String), Rejected(String), Pending }
|
|||
fun describe(result: PaymentResult): String {
|
||||
var description = ""
|
||||
match (result) {
|
||||
PaymentResult::Accepted(id) -> { description = id }
|
||||
PaymentResult::Rejected(reason) -> { description = reason }
|
||||
PaymentResult::Pending -> { description = "pending" }
|
||||
PaymentResult.Accepted(id) -> { description = id }
|
||||
PaymentResult.Rejected(reason) -> { description = reason }
|
||||
PaymentResult.Pending -> { description = "pending" }
|
||||
}
|
||||
return description
|
||||
}
|
||||
fun main() { println(describe(PaymentResult::Accepted("p1"))) }
|
||||
fun main() { println(describe(PaymentResult.Accepted("p1"))) }
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
@ -37,7 +37,7 @@ fun main() { println(describe(PaymentResult::Accepted("p1"))) }
|
|||
func TestRejectNonExhaustiveEnumMatch(t *testing.T) {
|
||||
prog, err := Parse(`package demo
|
||||
enum Result { Ok, Error(String) }
|
||||
fun use(result: Result) { match (result) { Result::Ok -> { println("ok") } } }`)
|
||||
fun use(result: Result) { match (result) { Result.Ok -> { println("ok") } } }`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -50,7 +50,7 @@ fun use(result: Result) { match (result) { Result::Ok -> { println("ok") } } }`)
|
|||
func TestRejectWrongVariantPayloadCount(t *testing.T) {
|
||||
prog, err := Parse(`package demo
|
||||
enum Outcome { Ok(String) }
|
||||
fun main() { val result = Outcome::Ok() }`)
|
||||
fun main() { val result = Outcome.Ok() }`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -60,10 +60,17 @@ fun main() { val result = Outcome::Ok() }`)
|
|||
}
|
||||
}
|
||||
|
||||
func TestRejectDoubleColonEnumSyntax(t *testing.T) {
|
||||
_, err := Parse(`package demo enum State { Ready } fun main() { println(State::Ready) }`)
|
||||
if err == nil {
|
||||
t.Fatal("deprecated double-colon enum syntax parsed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadlessEnumUsesExactVariantStrings(t *testing.T) {
|
||||
prog, err := Parse(`package demo
|
||||
enum Status { PendingReservation, Initiated }
|
||||
fun main() { println(Status::PendingReservation) }`)
|
||||
fun main() { println(Status.PendingReservation) }`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
@ -83,8 +90,8 @@ func TestGenerateExhaustiveMatchExpression(t *testing.T) {
|
|||
enum AccountType { BASIC, SAVINGS }
|
||||
fun interestRate(accountType: AccountType): Double {
|
||||
return match (accountType) {
|
||||
AccountType::BASIC -> 0.0
|
||||
AccountType::SAVINGS -> 0.02
|
||||
AccountType.BASIC -> 0.0
|
||||
AccountType.SAVINGS -> 0.02
|
||||
}
|
||||
}`)
|
||||
if err != nil {
|
||||
|
|
@ -106,8 +113,8 @@ func TestGeneratePayloadMatchExpression(t *testing.T) {
|
|||
enum Outcome { Success(String), Failure(String) }
|
||||
fun message(outcome: Outcome): String {
|
||||
return match (outcome) {
|
||||
Outcome::Success(value) -> value
|
||||
Outcome::Failure(reason) -> reason
|
||||
Outcome.Success(value) -> value
|
||||
Outcome.Failure(reason) -> reason
|
||||
}
|
||||
}`)
|
||||
if err != nil {
|
||||
|
|
@ -129,8 +136,8 @@ func TestRejectInvalidMatchExpression(t *testing.T) {
|
|||
source string
|
||||
want string
|
||||
}{
|
||||
{`package demo enum State { On, Off } fun value(state: State): Int { return match (state) { State::On -> 1 } }`, "missing Off"},
|
||||
{`package demo enum State { On, Off } fun value(state: State): Int { return match (state) { State::On -> 1 State::Off -> "off" } }`, "has type String, expected Int"},
|
||||
{`package demo enum State { On, Off } fun value(state: State): Int { return match (state) { State.On -> 1 } }`, "missing Off"},
|
||||
{`package demo enum State { On, Off } fun value(state: State): Int { return match (state) { State.On -> 1 State.Off -> "off" } }`, "has type String, expected Int"},
|
||||
} {
|
||||
prog, err := Parse(test.source)
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue