-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enum
outside libs. Similar to .Net's enum, allowing bitflags, to_s …
…and methods on them
- Loading branch information
Ary Borenszweig
committed
Nov 4, 2014
1 parent
04eefc5
commit 7e25c98
Showing
23 changed files
with
623 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
require "../../spec_helper" | ||
|
||
describe "Code gen: enum" do | ||
it "codegens enum" do | ||
run(%( | ||
enum Foo | ||
A = 1 | ||
end | ||
Foo::A | ||
)).to_i.should eq(1) | ||
end | ||
|
||
it "codegens enum without explicit value" do | ||
run(%( | ||
enum Foo | ||
A | ||
B | ||
C | ||
end | ||
Foo::C | ||
)).to_i.should eq(2) | ||
end | ||
|
||
it "codegens enum value" do | ||
run(%( | ||
enum Foo | ||
A = 1 | ||
end | ||
Foo::A.value | ||
)).to_i.should eq(1) | ||
end | ||
|
||
it "creates enum from value" do | ||
run(%( | ||
enum Foo | ||
A | ||
B | ||
end | ||
Foo.new(1).value | ||
)).to_i.should eq(1) | ||
end | ||
|
||
it "codegens enum bitflags (1)" do | ||
run(%( | ||
@[Flags] | ||
enum Foo | ||
A | ||
end | ||
Foo::A | ||
)).to_i.should eq(1) | ||
end | ||
|
||
it "codegens enum bitflags (2)" do | ||
run(%( | ||
@[Flags] | ||
enum Foo | ||
A | ||
B | ||
end | ||
Foo::B | ||
)).to_i.should eq(2) | ||
end | ||
|
||
it "codegens enum bitflags (4)" do | ||
run(%( | ||
@[Flags] | ||
enum Foo | ||
A | ||
B | ||
C | ||
end | ||
Foo::C | ||
)).to_i.should eq(4) | ||
end | ||
|
||
it "codegens enum bitflags None" do | ||
run(%( | ||
@[Flags] | ||
enum Foo | ||
A | ||
end | ||
Foo::None | ||
)).to_i.should eq(0) | ||
end | ||
|
||
it "codegens enum bitflags All" do | ||
run(%( | ||
@[Flags] | ||
enum Foo | ||
A | ||
B | ||
C | ||
end | ||
Foo::All | ||
)).to_i.should eq(1 + 2 + 4) | ||
end | ||
|
||
it "codegens enum None redefined" do | ||
run(%( | ||
@[Flags] | ||
enum Foo | ||
A | ||
None = 10 | ||
end | ||
Foo::None | ||
)).to_i.should eq(10) | ||
end | ||
|
||
it "codegens enum All redefined" do | ||
run(%( | ||
@[Flags] | ||
enum Foo | ||
A | ||
All = 10 | ||
end | ||
Foo::All | ||
)).to_i.should eq(10) | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,185 @@ | ||
require "../../spec_helper" | ||
|
||
describe "Type inference: enum" do | ||
it "types enum" do | ||
assert_type(%( | ||
enum Foo | ||
A = 1 | ||
end | ||
Foo::A | ||
)) { types["Foo"] } | ||
end | ||
|
||
it "types enum value" do | ||
assert_type(%( | ||
enum Foo | ||
A = 1 | ||
end | ||
Foo::A.value | ||
)) { int32 } | ||
end | ||
|
||
it "disallows implicit conversion of int to enum" do | ||
assert_error %( | ||
enum Foo | ||
A = 1 | ||
end | ||
def foo(x : Foo) | ||
end | ||
foo 1 | ||
), "mno overload matches 'foo' with types Int32" | ||
end | ||
|
||
it "finds method in enum type" do | ||
assert_type(%( | ||
struct Enum | ||
def foo | ||
1 | ||
end | ||
end | ||
enum Foo | ||
A = 1 | ||
end | ||
Foo::A.foo | ||
)) { int32 } | ||
end | ||
|
||
it "finds class method in enum type" do | ||
assert_type(%( | ||
struct Enum | ||
def self.foo | ||
1 | ||
end | ||
end | ||
enum Foo | ||
A = 1 | ||
end | ||
Foo.foo | ||
)) { int32 } | ||
end | ||
|
||
it "errors if using a name twice" do | ||
assert_error %( | ||
enum Foo | ||
A | ||
A | ||
end | ||
), | ||
"enum 'Foo' already contains a member named 'A'" | ||
end | ||
|
||
it "creates enum from value" do | ||
assert_type(%( | ||
enum Foo | ||
A | ||
B | ||
end | ||
Foo.new(1) | ||
)) { types["Foo"] } | ||
end | ||
|
||
it "defines method on enum" do | ||
assert_type(%( | ||
enum Foo | ||
A | ||
B | ||
def foo | ||
1 | ||
end | ||
end | ||
Foo::A.foo | ||
)) { int32 } | ||
end | ||
|
||
it "defines class method on enum" do | ||
assert_type(%( | ||
enum Foo | ||
A | ||
B | ||
def self.foo | ||
1 | ||
end | ||
end | ||
Foo.foo | ||
)) { int32 } | ||
end | ||
|
||
it "reopens an enum" do | ||
assert_type(%( | ||
enum Foo | ||
A | ||
B | ||
end | ||
enum Foo | ||
def foo | ||
1 | ||
end | ||
end | ||
Foo::A.foo | ||
)) { int32 } | ||
end | ||
|
||
it "errors if reopen but not enum" do | ||
assert_error %( | ||
class Foo | ||
end | ||
enum Foo | ||
A | ||
B | ||
end | ||
), | ||
"Foo is not a enum, it's a class" | ||
end | ||
|
||
it "errors if reopen and tries to define constant" do | ||
assert_error %( | ||
enum Foo | ||
A | ||
B | ||
end | ||
enum Foo | ||
C | ||
end | ||
), | ||
"can't reopen enum and add more constants to it" | ||
end | ||
|
||
it "has None value when defined as @[Flags]" do | ||
assert_type(%( | ||
@[Flags] | ||
enum Foo | ||
A | ||
B | ||
end | ||
Foo::None.value | ||
)) { int32 } | ||
end | ||
|
||
it "has All value when defined as @[Flags]" do | ||
assert_type(%( | ||
@[Flags] | ||
enum Foo | ||
A | ||
B | ||
end | ||
Foo::All.value | ||
)) { int32 } | ||
end | ||
end |
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
Oops, something went wrong.