Skip to content

Commit 3bd4c3c

Browse files
committed
Add specs for rb_check_symbol_cstr()
1 parent 8c4783f commit 3bd4c3c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

spec/ruby/optional/capi/ext/symbol_spec.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ VALUE symbol_spec_rb_intern_str(VALUE self, VALUE str) {
5151
return ID2SYM(rb_intern_str(str));
5252
}
5353

54+
VALUE symbol_spec_rb_check_symbol_cstr(VALUE self, VALUE str) {
55+
return rb_check_symbol_cstr(RSTRING_PTR(str), RSTRING_LEN(str), rb_enc_get(str));
56+
}
57+
5458
VALUE symbol_spec_rb_is_class_id(VALUE self, VALUE sym) {
5559
return rb_is_class_id(SYM2ID(sym)) ? Qtrue : Qfalse;
5660
}
@@ -79,6 +83,7 @@ void Init_symbol_spec(void) {
7983
rb_define_method(cls, "rb_id2name", symbol_spec_rb_id2name, 1);
8084
rb_define_method(cls, "rb_id2str", symbol_spec_rb_id2str, 1);
8185
rb_define_method(cls, "rb_intern_str", symbol_spec_rb_intern_str, 1);
86+
rb_define_method(cls, "rb_check_symbol_cstr", symbol_spec_rb_check_symbol_cstr, 1);
8287
rb_define_method(cls, "rb_is_class_id", symbol_spec_rb_is_class_id, 1);
8388
rb_define_method(cls, "rb_is_const_id", symbol_spec_rb_is_const_id, 1);
8489
rb_define_method(cls, "rb_is_instance_id", symbol_spec_rb_is_instance_id, 1);

spec/ruby/optional/capi/symbol_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,19 @@
7171
end
7272
end
7373

74+
describe "rb_check_symbol_cstr" do
75+
it "returns a Symbol if a Symbol already exists for the given C string" do
76+
sym = :test_symbol
77+
@s.rb_check_symbol_cstr('test_symbol').should == sym
78+
end
79+
80+
it "returns nil if the Symbol does not exist yet and does not create it" do
81+
str = "symbol_does_not_exist_#{Object.new.object_id}_#{rand}"
82+
@s.rb_check_symbol_cstr(str).should == nil # does not create the Symbol
83+
@s.rb_check_symbol_cstr(str).should == nil
84+
end
85+
end
86+
7487
describe "rb_is_const_id" do
7588
it "returns true given a const-like symbol" do
7689
@s.rb_is_const_id(:Foo).should == true

0 commit comments

Comments
 (0)