Skip to content

Commit

Permalink
Fix macro interpolation in NamedTuple#from (crystal-lang#14790)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored and kojix2 committed Jul 9, 2024
1 parent 66aa5f0 commit ba518e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions spec/std/named_tuple_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe "NamedTuple" do
t.should eq({"foo bar": 1, "baz qux": 2})
t.class.should eq(NamedTuple("foo bar": Int32, "baz qux": Int32))

t = NamedTuple("\"": Int32, "\#{exit}": Int32).from({"\"" => 2, "\#{exit}" => 3})
t.should eq({"\"": 2, "\#{exit}": 3})
t.class.should eq(NamedTuple("\"": Int32, "\#{exit}": Int32))

expect_raises ArgumentError do
NamedTuple(foo: Int32, bar: Int32).from({:foo => 1})
end
Expand Down Expand Up @@ -74,6 +78,10 @@ describe "NamedTuple" do
t = {foo: Int32, bar: Int32}.from({"foo" => 1, :bar => 2} of String | Int32 | Symbol => Int32)
t.should eq({foo: 1, bar: 2})
t.class.should eq(NamedTuple(foo: Int32, bar: Int32))

t = {"\"": Int32, "\#{exit}": Int32}.from({"\"" => 2, "\#{exit}" => 3})
t.should eq({"\"": 2, "\#{exit}": 3})
t.class.should eq(NamedTuple("\"": Int32, "\#{exit}": Int32))
end

it "gets size" do
Expand Down
2 changes: 1 addition & 1 deletion src/named_tuple.cr
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ struct NamedTuple
{% begin %}
NamedTuple.new(
{% for key, value in T %}
{{key.stringify}}: self[{{key.symbolize}}].cast(hash.fetch({{key.symbolize}}) { hash["{{key}}"] }),
{{key.stringify}}: self[{{key.symbolize}}].cast(hash.fetch({{key.symbolize}}) { hash[{{key.stringify}}] }),
{% end %}
)
{% end %}
Expand Down

0 comments on commit ba518e1

Please sign in to comment.