Skip to content

Commit

Permalink
Add Path#resolve? macro method
Browse files Browse the repository at this point in the history
  • Loading branch information
RX14 committed May 12, 2017
1 parent 08c6e8f commit db2841a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
15 changes: 15 additions & 0 deletions spec/compiler/macro/macro_methods_spec.cr
Expand Up @@ -1566,6 +1566,21 @@ describe "macro methods" do
end
end

describe "path methods" do
it "executes resolve" do
assert_macro "x", %({{x.resolve}}), [Path.new("String")] of ASTNode, %(String)

expect_raises(Crystal::TypeException, "undefined constant Foo") do
assert_macro "x", %({{x.resolve}}), [Path.new("Foo")] of ASTNode, %(Foo)
end
end

it "executes resolve?" do
assert_macro "x", %({{x.resolve?}}), [Path.new("String")] of ASTNode, %(String)
assert_macro "x", %({{x.resolve?}}), [Path.new("Foo")] of ASTNode, %(nil)
end
end

describe "env" do
it "has key" do
ENV["FOO"] = "foo"
Expand Down
9 changes: 9 additions & 0 deletions src/compiler/crystal/macros/methods.cr
Expand Up @@ -1739,6 +1739,15 @@ module Crystal
interpret_argless_method(method, args) { BoolLiteral.new(@global) }
when "resolve"
interpret_argless_method(method, args) { interpreter.resolve(self) }
when "resolve?"
interpret_argless_method(method, args) do
begin
interpreter.resolve(self)
rescue ex : Crystal::TypeException
::raise ex unless ex.message.try &.includes? "undefined constant"
NilLiteral.new
end
end
else
super
end
Expand Down

0 comments on commit db2841a

Please sign in to comment.