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

Add size to Map class #8068

Merged
merged 1 commit into from Oct 13, 2021
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
1 change: 1 addition & 0 deletions ruby/ext/google/protobuf_c/map.c
Expand Up @@ -680,6 +680,7 @@ void Map_register(VALUE module) {
rb_define_method(klass, "delete", Map_delete, 1);
rb_define_method(klass, "clear", Map_clear, 0);
rb_define_method(klass, "length", Map_length, 0);
rb_define_method(klass, "size", Map_length, 0);
rb_define_method(klass, "dup", Map_dup, 0);
rb_define_method(klass, "==", Map_eq, 1);
rb_define_method(klass, "freeze", Map_freeze, 0);
Expand Down
2 changes: 1 addition & 1 deletion ruby/src/main/java/com/google/protobuf/jruby/RubyMap.java
Expand Up @@ -332,7 +332,7 @@ public IRubyObject hasKey(ThreadContext context, IRubyObject key) {
*
* Returns the number of entries (key-value pairs) in the map.
*/
@JRubyMethod
@JRubyMethod(name = {"length", "size"})
public IRubyObject length(ThreadContext context) {
return context.runtime.newFixnum(this.table.size());
}
Expand Down
16 changes: 16 additions & 0 deletions ruby/tests/basic.rb
Expand Up @@ -620,5 +620,21 @@ def test_map_freeze
assert_raise(FrozenErrorType) { m.map_string_int32.delete('a') }
assert_raise(FrozenErrorType) { m.map_string_int32.clear }
end

def test_map_length
m = proto_module::MapMessage.new
assert_equal 0, m.map_string_int32.length
assert_equal 0, m.map_string_msg.length
assert_equal 0, m.map_string_int32.size
assert_equal 0, m.map_string_msg.size

m.map_string_int32['a'] = 1
m.map_string_int32['b'] = 2
m.map_string_msg['a'] = proto_module::TestMessage2.new
assert_equal 2, m.map_string_int32.length
assert_equal 1, m.map_string_msg.length
assert_equal 2, m.map_string_int32.size
assert_equal 1, m.map_string_msg.size
end
end
end