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

[Bug #19259] Data#with should call initialize method #7031

Merged
merged 1 commit into from
Feb 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 6 additions & 17 deletions struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -1820,10 +1820,12 @@ rb_data_initialize_m(int argc, const VALUE *argv, VALUE self)
arg.self = self;
arg.unknown_keywords = Qnil;
rb_hash_foreach(argv[0], struct_hash_set_i, (VALUE)&arg);
// Freeze early before potentially raising, so that we don't leave an
// unfrozen copy on the heap, which could get exposed via ObjectSpace.
OBJ_FREEZE_RAW(self);
if (arg.unknown_keywords != Qnil) {
rb_exc_raise(rb_keyword_error_new("unknown", arg.unknown_keywords));
}
OBJ_FREEZE_RAW(self);
return Qnil;
}

Expand Down Expand Up @@ -1875,22 +1877,9 @@ rb_data_with(int argc, const VALUE *argv, VALUE self)
return self;
}

VALUE copy = rb_obj_alloc(rb_obj_class(self));
rb_struct_init_copy(copy, self);

struct struct_hash_set_arg arg;
arg.self = copy;
arg.unknown_keywords = Qnil;
rb_hash_foreach(kwargs, struct_hash_set_i, (VALUE)&arg);
// Freeze early before potentially raising, so that we don't leave an
// unfrozen copy on the heap, which could get exposed via ObjectSpace.
RB_OBJ_FREEZE_RAW(copy);

if (arg.unknown_keywords != Qnil) {
rb_exc_raise(rb_keyword_error_new("unknown", arg.unknown_keywords));
}

return copy;
VALUE h = rb_struct_to_h(self);
rb_hash_update_by(h, kwargs, NULL);
return rb_class_new_instance_kw(1, &h, rb_obj_class(self), TRUE);
}

/*
Expand Down
16 changes: 16 additions & 0 deletions test/ruby/test_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@ def test_with
end
end

def test_with_initialize
oddclass = Data.define(:odd) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a structuring comment like "Testing #initialize is called" or something? The rest of the method has brief structuring, and I believe it is helpful for maintainers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather I'd prefer splitting this test method per the comments later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or this 🤷
(Honestly, I miss RSpec's nesting. It might become unhinged sometimes, but at least describe '#initialize with nested examples and contexts really helps with reading and running tests.)

def initialize(odd:)
raise ArgumentError, "Not odd" unless odd.odd?
super(odd: odd)
end
end
assert_raise_with_message(ArgumentError, "Not odd") {
oddclass.new(odd: 0)
}
odd = oddclass.new(odd: 1)
assert_raise_with_message(ArgumentError, "Not odd") {
odd.with(odd: 2)
}
end

def test_memberless
klass = Data.define

Expand Down