Updated MSpec source to 46e80081.
[rbx.git] / spec / subtend / string_spec.rb
blob9f76ba6cc5a3e5c95c52b2aa0818d810d7d8b765
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
8   before :each do
9     @s = SubtendString.new
10   end
12   class ValidTostrTest
13     def to_str
14       "ruby"
15     end
16   end
18   class InvalidTostrTest
19     def to_str
20       []
21     end
22   end
23   
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"
27   end
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"
32   end
34   it "rb_str_new2 should raise ArgumentError if passed NULL" do
35     lambda { @s.rb_str_new2_with_null }.should raise_error(ArgumentError)
36   end
38   it "rb_str_dup should return a copy of the string" do
39     str1 = "hi"
40     str2 = @s.dup_string str1
41     str1.should == str2
42     str1.object_id.should_not == str2.object_id
43   end
44   
45   it "rb_str_append should append a string to another string" do
46     @s.rb_str_append("Hello", " Goodbye").should == "Hello Goodbye"
47   end
48   
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"
51   end
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?"
55   end
56   
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?"
59   end
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?"
63   end
64   
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
71   end
72   
73   it "rb_str_split should split strings over a splitter" do
74     @s.rb_str_split("Hello,Goodbye").should == ["Hello", "Goodbye"]
75   end
76   
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
80   end
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
85   end
86   
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]
90     end
91   end
93   it "RSTRING(str)->ptr should return the string on the object" do
94     @s.rb_rstring_see("foo").should == "foo"
95   end
97   it "Changing RSTRING(str)->ptr should change the string" do
98     t = "a"
99     @s.rb_rstring_assign_foo(t)
100     t.should == "foo"
101     $global_rstring_test = "b"
102     @s.rb_rstring_assign_global_foobar()
103     $global_rstring_test.should == "foobar"
104   end
106   it "Reducing RSTRING(str)->len should cut the string" do
107     t = "12345"
108     @s.rb_rstring_set_len(t, 3)
109     t.should == "123"
110   end
112   it "Changing RSTRING(str)->ptr and calling upcase! should change the string and upcase it" do
113     t = ""
114     @s.rb_rstring_assign_foo_and_upcase(t)
115     t.should == "FOO"
116   end
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)
123   end