summaryrefslogtreecommitdiff
path: root/spec/ruby/optional/capi/symbol_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/optional/capi/symbol_spec.rb')
-rw-r--r--spec/ruby/optional/capi/symbol_spec.rb49
1 files changed, 48 insertions, 1 deletions
diff --git a/spec/ruby/optional/capi/symbol_spec.rb b/spec/ruby/optional/capi/symbol_spec.rb
index b6532f4a4e..12c93c9f27 100644
--- a/spec/ruby/optional/capi/symbol_spec.rb
+++ b/spec/ruby/optional/capi/symbol_spec.rb
@@ -1,5 +1,5 @@
# -*- encoding: utf-8 -*-
-require File.expand_path('../spec_helper', __FILE__)
+require_relative 'spec_helper'
load_extension('symbol')
@@ -8,6 +8,16 @@ describe "C-API Symbol function" do
@s = CApiSymbolSpecs.new
end
+ describe "SYMBOL_P" do
+ it "returns true for a Symbol" do
+ @s.SYMBOL_P(:foo).should == true
+ end
+
+ it "returns false for non-Symbols" do
+ @s.SYMBOL_P('bar').should == false
+ end
+ end
+
describe "rb_intern" do
it "converts a string to a symbol, uniquely" do
@s.rb_intern("test_symbol").should == :test_symbol
@@ -51,6 +61,10 @@ describe "C-API Symbol function" do
it "converts a symbol to a C char array" do
@s.rb_id2name(:test_symbol).should == "test_symbol"
end
+
+ it "returns (char*) NULL for (ID) 0" do
+ @s.rb_id2name_id_zero.should == nil
+ end
end
describe "rb_id2str" do
@@ -62,6 +76,10 @@ describe "C-API Symbol function" do
str = "test_symbol".encode(Encoding::UTF_16LE)
@s.rb_id2str(str.to_sym).encoding.should == Encoding::UTF_16LE
end
+
+ it "returns (VALUE) 0 = Qfalse for (ID) 0" do
+ @s.rb_id2str_id_zero.should == false
+ end
end
describe "rb_intern_str" do
@@ -71,6 +89,19 @@ describe "C-API Symbol function" do
end
end
+ describe "rb_check_symbol_cstr" do
+ it "returns a Symbol if a Symbol already exists for the given C string" do
+ sym = :test_symbol
+ @s.rb_check_symbol_cstr('test_symbol').should == sym
+ end
+
+ it "returns nil if the Symbol does not exist yet and does not create it" do
+ str = "symbol_does_not_exist_#{Object.new.object_id}_#{rand}"
+ @s.rb_check_symbol_cstr(str).should == nil # does not create the Symbol
+ @s.rb_check_symbol_cstr(str).should == nil
+ end
+ end
+
describe "rb_is_const_id" do
it "returns true given a const-like symbol" do
@s.rb_is_const_id(:Foo).should == true
@@ -130,4 +161,20 @@ describe "C-API Symbol function" do
@s.rb_sym2str(:bacon).should == "bacon"
end
end
+
+ describe "rb_to_symbol" do
+ it "returns a Symbol for a Symbol" do
+ @s.rb_to_symbol(:foo).should == :foo
+ end
+
+ it "returns a Symbol for a String" do
+ @s.rb_to_symbol("foo").should == :foo
+ end
+
+ it "coerces to Symbol using to_str" do
+ o = mock('o')
+ o.should_receive(:to_str).and_return("foo")
+ @s.rb_to_symbol(o).should == :foo
+ end
+ end
end