1 require File.dirname(__FILE__) + '/../spec_helper'
2 require File.dirname(__FILE__) + '/subtend_helper'
4 compile_extension('subtend_string')
5 require File.dirname(__FILE__) + '/ext/subtend_string'
7 describe "SubtendString" do
18 class InvalidTostrTest
24 it "rb_str_new should return a new string object" do
25 # Hardcoded to pass const char * = "hello"
26 @s.say_hello.should == "hello"
29 it "rb_str_new2 should return a new string object, figuring out the length itself" do
30 # Hardcoded to pass const char * = "hello\0invisible"
31 @s.say_hello2.should == "hello"
34 it "rb_str_new2 should raise ArgumentError if passed NULL" do
35 lambda { @s.rb_str_new2_with_null }.should raise_error(ArgumentError)
38 it "rb_str_dup should return a copy of the string" do
40 str2 = @s.dup_string str1
42 str1.object_id.should_not == str2.object_id
45 it "rb_str_append should append a string to another string" do
46 @s.rb_str_append("Hello", " Goodbye").should == "Hello Goodbye"
49 it "rb_str_plus should return a new string from two other strings" do
50 @s.rb_str_plus("Hello", " Goodbye").should == "Hello Goodbye"
53 it "rb_str_buf_cat should concat C strings to ruby strings" do
54 @s.phrase_as_question("Your house is on fire").should == "Your house is on fire?"
57 it "rb_str_cat should concat C strings to ruby strings" do
58 @s.cat_as_question("Your house is on fire").should == "Your house is on fire?"
61 it "rb_str_cat2 should concat C strings to ruby strings" do
62 @s.cat2_as_question("Your house is on fire").should == "Your house is on fire?"
65 it "rb_str_cmp should compare two strings" do
66 @s.rb_str_cmp("xxx", "xxxx").should == -1
67 @s.rb_str_cmp("xxxx", "xxx").should == 1
68 @s.rb_str_cmp("xxx", "yyy").should == -1
69 @s.rb_str_cmp("yyy", "xxx").should == 1
70 @s.rb_str_cmp("ppp", "ppp").should == 0
73 it "rb_str_split should split strings over a splitter" do
74 @s.rb_str_split("Hello,Goodbye").should == ["Hello", "Goodbye"]
77 it "rb_str2inum should return a string representation as a number" do
78 @s.rb_str2inum("10", 10).should == 10
79 @s.rb_str2inum("A", 16).should == 10
82 it "rb_str2inum should return a string representation as a number" do
83 @s.rb_cstr2inum(10).should == 10
84 @s.rb_cstr2inum(16).should == 16
87 it "rb_str_substr should return a substring" do
88 "hello".length.times do |time|
89 @s.rb_str_substr("hello", 0, time + 1).should == "hello"[0..time]
93 it "RSTRING(str)->ptr should return the string on the object" do
94 @s.rb_rstring_see("foo").should == "foo"
97 it "Changing RSTRING(str)->ptr should change the string" do
99 @s.rb_rstring_assign_foo(t)
101 $global_rstring_test = "b"
102 @s.rb_rstring_assign_global_foobar()
103 $global_rstring_test.should == "foobar"
106 it "Reducing RSTRING(str)->len should cut the string" do
108 @s.rb_rstring_set_len(t, 3)
112 it "Changing RSTRING(str)->ptr and calling upcase! should change the string and upcase it" do
114 @s.rb_rstring_assign_foo_and_upcase(t)
118 it "rb_str_to_str should try to coerce to String, otherwise raise a TypeError" do
119 @s.rb_str_to_str("foo").should == "foo"
120 @s.rb_str_to_str(ValidTostrTest.new).should == "ruby"
121 lambda { @s.rb_str_to_str(0) }.should raise_error(TypeError)
122 lambda { @s.rb_str_to_str(InvalidTostrTest.new) }.should raise_error(TypeError)