aboutsummaryrefslogtreecommitdiffstats
path: root/spec/rubyspec/optional/capi/symbol_spec.rb
blob: b6532f4a4e8dc820021175ad5f3868c3692ee2c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- encoding: utf-8 -*-
require File.expand_path('../spec_helper', __FILE__)

load_extension('symbol')

describe "C-API Symbol function" do
  before :each do
    @s = CApiSymbolSpecs.new
  end

  describe "rb_intern" do
    it "converts a string to a symbol, uniquely" do
      @s.rb_intern("test_symbol").should == :test_symbol
      @s.rb_intern_c_compare("test_symbol", :test_symbol).should == true
    end
  end

  describe "rb_intern2" do
    it "converts a string to a symbol, uniquely, for a string of given length" do
      @s.rb_intern2("test_symbol", 4).should == :test
      @s.rb_intern2_c_compare("test_symbol", 4, :test).should == true
    end
  end

  describe "rb_intern3" do
    it "converts a multibyte symbol with the encoding" do
      sym = @s.rb_intern3("Ω", 2, Encoding::UTF_8)
      sym.encoding.should == Encoding::UTF_8
      sym.should == :Ω
      @s.rb_intern3_c_compare("Ω", 2, Encoding::UTF_8, :Ω).should == true
    end

    it "converts an ascii compatible symbol with the ascii encoding" do
      sym = @s.rb_intern3("foo", 3, Encoding::UTF_8)
      sym.encoding.should == Encoding::US_ASCII
      sym.should == :foo
    end

    it "should respect the symbol encoding via rb_intern3" do
      :Ω.to_s.encoding.should == Encoding::UTF_8
    end
  end

  describe "rb_intern_const" do
    it "converts a string to a Symbol" do
      @s.rb_intern_const("test").should == :test
    end
  end

  describe "rb_id2name" do
    it "converts a symbol to a C char array" do
      @s.rb_id2name(:test_symbol).should == "test_symbol"
    end
  end

  describe "rb_id2str" do
    it "converts a symbol to a Ruby string" do
      @s.rb_id2str(:test_symbol).should == "test_symbol"
    end

    it "creates a string with the same encoding as the symbol" do
      str = "test_symbol".encode(Encoding::UTF_16LE)
      @s.rb_id2str(str.to_sym).encoding.should == Encoding::UTF_16LE
    end
  end

  describe "rb_intern_str" do
    it "converts a Ruby String to a Symbol" do
      str = "test_symbol"
      @s.rb_intern_str(str).should == :test_symbol
    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
    end

    it "returns false given an ivar-like symbol" do
      @s.rb_is_const_id(:@foo).should == false
    end

    it "returns false given a cvar-like symbol" do
      @s.rb_is_const_id(:@@foo).should == false
    end

    it "returns false given an undecorated symbol" do
      @s.rb_is_const_id(:foo).should == false
    end
  end

  describe "rb_is_instance_id" do
    it "returns false given a const-like symbol" do
      @s.rb_is_instance_id(:Foo).should == false
    end

    it "returns true given an ivar-like symbol" do
      @s.rb_is_instance_id(:@foo).should == true
    end

    it "returns false given a cvar-like symbol" do
      @s.rb_is_instance_id(:@@foo).should == false
    end

    it "returns false given an undecorated symbol" do
      @s.rb_is_instance_id(:foo).should == false
    end
  end

  describe "rb_is_class_id" do
    it "returns false given a const-like symbol" do
      @s.rb_is_class_id(:Foo).should == false
    end

    it "returns false given an ivar-like symbol" do
      @s.rb_is_class_id(:@foo).should == false
    end

    it "returns true given a cvar-like symbol" do
      @s.rb_is_class_id(:@@foo).should == true
    end

    it "returns false given an undecorated symbol" do
      @s.rb_is_class_id(:foo).should == false
    end
  end

  describe "rb_sym2str" do
    it "converts a Symbol to a String" do
      @s.rb_sym2str(:bacon).should == "bacon"
    end
  end
end