Skip to content

Commit

Permalink
Fix issue with blank strings and type extensions
Browse files Browse the repository at this point in the history
Closes #377
  • Loading branch information
paulcsmith committed May 29, 2020
1 parent 42ce5b5 commit f204165
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 25 deletions.
17 changes: 17 additions & 0 deletions spec/type_extensions/bool_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@ describe Bool do
true.blank?.should be_false
end
end

describe "parsing" do
it "parses empty string as nil" do
parse("").should be_a(Avram::Type::SuccessfulCast(Nil))
end

it "parses strings" do
parse("true").value.should eq(true)
parse("1").value.should eq(true)
parse("false").value.should eq(false)
parse("0").value.should eq(false)
end
end
end

private def parse(value)
Bool::Lucky.parse(value)
end
14 changes: 11 additions & 3 deletions spec/type_extensions/enum_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ require "../spec_helper"
include LazyLoadHelpers

describe "Enum" do
it "check enum" do
it "parses String enum" do
Issue::Status::Lucky.parse("1").value.should eq(Issue::Status.new(Issue::AvramStatus::Closed))
end

it "parses empty String enum" do
Issue::Status::Lucky.parse("").should be_a(Avram::Type::SuccessfulCast(Nil))
end

it "checks enum" do
issue = IssueBox.create

issue.status.enum.should eq(Issue::AvramStatus::Opened)
issue.role.enum.should eq(Issue::AvramRole::Issue)
end

it "update enum" do
it "updates enum" do
issue = IssueBox.create

updated_issue = Issue::SaveOperation.update!(issue, status: Issue::Status.new(:closed))
Expand All @@ -19,7 +27,7 @@ describe "Enum" do
updated_issue.role.enum.should eq(Issue::AvramRole::Issue)
end

it "access enum methods" do
it "accesses enum methods" do
issue = IssueBox.create

issue.status.opened?.should eq(true)
Expand Down
19 changes: 0 additions & 19 deletions spec/type_extensions/int32_spec.cr

This file was deleted.

67 changes: 67 additions & 0 deletions spec/type_extensions/int_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "../spec_helper"

describe "Int16" do
it "parses Int16 from String" do
result = Int16::Lucky.parse("10")
result.value.should eq(10_i16)
end

it "parses Int16 from Int32" do
result = Int16::Lucky.parse(400)
result.value.should eq(400_i16)
end

it "returns FailedCast when overflow from Int16 to Int32/64" do
result = Int16::Lucky.parse(1234556)
result.value.should eq(nil)
result.should be_a(Avram::Type::FailedCast)
end

it "returns nil if String is blank" do
result = Int16::Lucky.parse("")
result.value.should be_nil
result.should be_a(Avram::Type::SuccessfulCast(Nil))
end
end

describe "Int32" do
it "parses Int32 from String" do
result = Int32::Lucky.parse("10")
result.value.should eq(10)
end

it "parses Int32 from Int64" do
result = Int32::Lucky.parse(400_i64)
result.value.should eq(400)
end

it "returns FailedCast when overflow from Int64 to Int32" do
result = Int32::Lucky.parse(2147483648)
result.value.should eq(nil)
result.should be_a(Avram::Type::FailedCast)
end

it "returns nil if String is blank" do
result = Int32::Lucky.parse("")
result.value.should be_nil
result.should be_a(Avram::Type::SuccessfulCast(Nil))
end
end

describe "Int64" do
it "parses Int64 from String" do
result = Int64::Lucky.parse("10")
result.value.should eq(10_i64)
end

it "parses Int64 from Int32" do
result = Int64::Lucky.parse(400)
result.value.should eq(400_i64)
end

it "returns nil if String is blank" do
result = Int64::Lucky.parse("")
result.value.should be_nil
result.should be_a(Avram::Type::SuccessfulCast(Nil))
end
end
6 changes: 6 additions & 0 deletions spec/type_extensions/time_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ require "../spec_helper"

describe "Time column type" do
describe ".parse" do
it "returns nil if parsing an empty String" do
result = Time::Lucky.parse("")

result.should be_a(Avram::Type::SuccessfulCast(Nil))
end

it "casts various formats successfully" do
time = Time.local
times = {
Expand Down
4 changes: 4 additions & 0 deletions spec/type_extensions/uuid_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ require "../spec_helper"

describe "UUID column type" do
describe ".parse" do
it "returns successful cast if string is nil" do
UUID::Lucky.parse("").should be_a(Avram::Type::SuccessfulCast(Nil))
end

it "casts a UUID successfully" do
uuid = UUID.new("a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11")
UUID::Lucky.parse(uuid).value.should eq uuid
Expand Down
2 changes: 2 additions & 0 deletions src/avram/charms/bool_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ struct Bool
include Avram::Type

def parse(value : String)
return parse(nil) if value.blank?

if %w(true 1).includes? value
SuccessfulCast(Bool).new true
elsif %w(false 0).includes? value
Expand Down
2 changes: 2 additions & 0 deletions src/avram/charms/enum_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ macro avram_enum(enum_name, &block)
Lucky
end

def_equals @enum
getter :enum

# You may need to prefix with {{ @type }}
Expand Down Expand Up @@ -39,6 +40,7 @@ macro avram_enum(enum_name, &block)
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast({{ enum_name }}).new({{ enum_name }}.new(value.to_i))
end

Expand Down
7 changes: 7 additions & 0 deletions src/avram/charms/int16_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ struct Int16
SuccessfulCast(Array(Int16)).new values
end

def parse(value : Int32)
SuccessfulCast(Int16).new value.to_i16
rescue OverflowError
FailedCast.new
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast(Int16).new value.to_i16
rescue ArgumentError
FailedCast.new
Expand Down
1 change: 1 addition & 0 deletions src/avram/charms/int32_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Int32
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast(Int32).new value.to_i
rescue ArgumentError
FailedCast.new
Expand Down
1 change: 1 addition & 0 deletions src/avram/charms/int64_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct Int64
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast(Int64).new value.to_i64
rescue ArgumentError
FailedCast.new
Expand Down
4 changes: 3 additions & 1 deletion src/avram/charms/time_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ struct Time
value
end

def parse(value : String) : SuccessfulCast(Time) | FailedCast
def parse(value : String) : SuccessfulCast(Time) | SuccessfulCast(Nil) | FailedCast
return parse(nil) if value.blank?

# Prefer user defined string formats
try_parsing_with_string_formats(value) ||
# Then try default formats
Expand Down
1 change: 1 addition & 0 deletions src/avram/charms/uuid_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct UUID
end

def parse(value : String)
return parse(nil) if value.blank?
SuccessfulCast(UUID).new(UUID.new(value))
rescue
FailedCast.new
Expand Down
3 changes: 1 addition & 2 deletions src/avram/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ module Avram::Type
def parse(values : Array(String))
casts = values.map { |value| parse(value) }
if casts.all?(&.is_a?(SuccessfulCast))
values = casts.map { |c| c.as(SuccessfulCast).value }
parse(values)
casts
else
FailedCast.new
end
Expand Down

0 comments on commit f204165

Please sign in to comment.