Skip to content

Commit

Permalink
Refactor spec with helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Apr 11, 2021
1 parent 5622eb6 commit 5da5886
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions spec/compiler/normalize/def_spec.cr
Expand Up @@ -232,24 +232,32 @@ module Crystal
other_def.to_s.should eq("def foo:abstract(abstract __arg0)\n options = {}\n @abstract = __arg0\nend")
end

describe "gives correct body location with" do
{"default arg" => "def testing(foo = 5)",
"default arg with restriction" => "def testing(foo : Int = 5)",
"splat arg" => "def testing(*foo : Array)",
"block instance var arg" => "def testing(&@foo : ->)",
}.each do |(suf, definition)|
describe suf + "," do
{"with body" => "zzz = 7\n", "without body" => ""}.each do |(suf, body)|
it suf do
a_def = parse("#{definition}\n#{body}end").as(Def)
actual = a_def.expand_default_arguments(Program.new, 1)

actual.location.should eq Location.new("", line_number: 1, column_number: 1)
actual.body.location.should eq Location.new("", line_number: 2, column_number: 1)
end
end
end
describe "gives correct body location with 2" do
context "with body" do
it_sets_def_location "default arg", "def testing(foo = 5)\nzz = z\nend"
it_sets_def_location "default arg with restriction", "def testing(foo : Int = 5)\nzz = z\nend"
it_sets_def_location "splat arg", "def testing(*foo : Array)\nzz = z\nend"
it_sets_def_location "block instance var arg", "def testing(&@foo : ->)\nzz = z\nend"
end
context "without body" do
it_sets_def_location "default arg", "def testing(foo = 5)\nend"
it_sets_def_location "default arg with restriction", "def testing(foo : Int = 5)\nend"
it_sets_def_location "splat arg", "def testing(*foo : Array)\nend"
it_sets_def_location "block instance var arg", "def testing(&@foo : ->)\nend"
end
end
end
end

private def it_sets_def_location(description, source,
def_location = Location.new("", line_number: 1, column_number: 1),
body_location = Location.new("", line_number: 2, column_number: 1),
file = __FILE__, line = __LINE__)
it description, file: file, line: line do
a_def = parse(source).as(Def)
actual = a_def.expand_default_arguments(Program.new, 1)

actual.location.should eq def_location
actual.body.location.should eq body_location
end
end

0 comments on commit 5da5886

Please sign in to comment.