Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Enum#to_i128 and #to_u128 #13576

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions spec/std/enum_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,40 @@ describe Enum do
it "gets value with to_i" do
SpecEnum::Two.to_i.should eq(1)
SpecEnum::Two.to_i.should be_a(Int32)
end

it "gets value with to_i<bit>" do
SpecEnum::Two.to_i8.should eq(1)
SpecEnum::Two.to_i8.should be_a(Int8)

SpecEnum::Two.to_i16.should eq(1)
SpecEnum::Two.to_i16.should be_a(Int16)

SpecEnum::Two.to_i32.should eq(1)
SpecEnum::Two.to_i32.should be_a(Int32)

SpecEnum::Two.to_i64.should eq(1)
SpecEnum::Two.to_i64.should be_a(Int64)

SpecEnum::Two.to_i128.should eq(1)
SpecEnum::Two.to_i128.should be_a(Int128)
end

it "gets value with to_u<bit>" do
SpecEnum::Two.to_u8.should eq(1)
SpecEnum::Two.to_u8.should be_a(UInt8)

SpecEnum::Two.to_u16.should eq(1)
SpecEnum::Two.to_u16.should be_a(UInt16)

SpecEnum::Two.to_u32.should eq(1)
SpecEnum::Two.to_u32.should be_a(UInt32)

SpecEnum::Two.to_u64.should eq(1)
SpecEnum::Two.to_u64.should be_a(UInt64)

SpecEnum::Two.to_u128.should eq(1)
SpecEnum::Two.to_u128.should be_a(UInt128)
end

it "does +" do
Expand Down
2 changes: 1 addition & 1 deletion src/enum.cr
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ struct Enum
value.to_i32
end

{% for name in %w(i8 i16 i32 i64 u8 u16 u32 u64 f32 f64) %}
{% for name in %w(i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64) %}
{% prefix = name.starts_with?('i') ? "Int".id : (name.starts_with?('u') ? "UInt".id : "Float".id) %}
{% type = "#{prefix}#{name[1..-1].id}".id %}
# Returns the value of this enum member as a `{{type}}`
Expand Down