Skip to content

Commit 8d6be09

Browse files
bjfisheregon
authored andcommitted
Implement rb_str_locktmp and rb_str_unlocktmp
1 parent a4d3a4a commit 8d6be09

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

optional/capi/ext/string_spec.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,14 @@ static VALUE string_spec_rb_str_catf(VALUE self, VALUE mesg) {
577577
return rb_str_catf(mesg, "fmt %d %d number", 41, 6);
578578
}
579579

580+
static VALUE string_spec_rb_str_locktmp(VALUE self, VALUE str) {
581+
return rb_str_locktmp(str);
582+
}
583+
584+
static VALUE string_spec_rb_str_unlocktmp(VALUE self, VALUE str) {
585+
return rb_str_unlocktmp(str);
586+
}
587+
580588
void Init_string_spec(void) {
581589
VALUE cls = rb_define_class("CApiStringSpecs", rb_cObject);
582590
rb_define_method(cls, "rb_cstr2inum", string_spec_rb_cstr2inum, 2);
@@ -672,6 +680,8 @@ void Init_string_spec(void) {
672680
rb_define_method(cls, "rb_utf8_str_new_cstr", string_spec_rb_utf8_str_new_cstr, 0);
673681
rb_define_method(cls, "rb_str_vcatf", string_spec_rb_str_vcatf, 1);
674682
rb_define_method(cls, "rb_str_catf", string_spec_rb_str_catf, 1);
683+
rb_define_method(cls, "rb_str_locktmp", string_spec_rb_str_locktmp, 1);
684+
rb_define_method(cls, "rb_str_unlocktmp", string_spec_rb_str_unlocktmp, 1);
675685
}
676686

677687
#ifdef __cplusplus

optional/capi/string_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,4 +1209,31 @@ def inspect
12091209
str.should == "test fmt 41 6 number"
12101210
end
12111211
end
1212+
1213+
describe "rb_str_locktmp" do
1214+
it "raises an error when trying to lock an already locked string" do
1215+
str = "test"
1216+
@s.rb_str_locktmp(str).should == str
1217+
-> { @s.rb_str_locktmp(str) }.should raise_error(RuntimeError, 'temporal locking already locked string')
1218+
end
1219+
1220+
it "locks a string so that modifications would raise an error" do
1221+
str = "test"
1222+
@s.rb_str_locktmp(str).should == str
1223+
-> { str.upcase! }.should raise_error(RuntimeError, 'can\'t modify string; temporarily locked')
1224+
end
1225+
end
1226+
1227+
describe "rb_str_unlocktmp" do
1228+
it "unlocks a locked string" do
1229+
str = "test"
1230+
@s.rb_str_locktmp(str)
1231+
@s.rb_str_unlocktmp(str).should == str
1232+
str.upcase!.should == "TEST"
1233+
end
1234+
1235+
it "raises an error when trying to unlock an already unlocked string" do
1236+
-> { @s.rb_str_unlocktmp("test") }.should raise_error(RuntimeError, 'temporal unlocking already unlocked string')
1237+
end
1238+
end
12121239
end

0 commit comments

Comments
 (0)