summaryrefslogtreecommitdiff
path: root/spec/ruby/core/symbol/casecmp_spec.rb
blob: 80ea51e9109fe93a6569d0d68cd39f84451086dd (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
134
135
136
137
138
139
140
141
142
143
144
# -*- encoding: utf-8 -*-
require_relative '../../spec_helper'

describe "Symbol#casecmp with Symbol" do
  it "compares symbols without regard to case" do
    :abcdef.casecmp(:abcde).should == 1
    :aBcDeF.casecmp(:abcdef).should == 0
    :abcdef.casecmp(:abcdefg).should == -1
    :abcdef.casecmp(:ABCDEF).should == 0
  end

  it "doesn't consider non-ascii characters equal that aren't" do
    # -- Latin-1 --
    upper_a_tilde  = "\xC3".b.to_sym
    upper_a_umlaut = "\xC4".b.to_sym
    lower_a_tilde  = "\xE3".b.to_sym
    lower_a_umlaut = "\xE4".b.to_sym

    lower_a_tilde.casecmp(lower_a_umlaut).should_not == 0
    lower_a_umlaut.casecmp(lower_a_tilde).should_not == 0
    upper_a_tilde.casecmp(upper_a_umlaut).should_not == 0
    upper_a_umlaut.casecmp(upper_a_tilde).should_not == 0

    # -- UTF-8 --
    upper_a_tilde  = :"Ã"
    lower_a_tilde  = :"ã"
    upper_a_umlaut = :"Ä"
    lower_a_umlaut = :"ä"

    lower_a_tilde.casecmp(lower_a_umlaut).should_not == 0
    lower_a_umlaut.casecmp(lower_a_tilde).should_not == 0
    upper_a_tilde.casecmp(upper_a_umlaut).should_not == 0
    upper_a_umlaut.casecmp(upper_a_tilde).should_not == 0
  end

  it "doesn't do case mapping for non-ascii characters" do
    # -- Latin-1 --
    upper_a_tilde  = "\xC3".b.to_sym
    upper_a_umlaut = "\xC4".b.to_sym
    lower_a_tilde  = "\xE3".b.to_sym
    lower_a_umlaut = "\xE4".b.to_sym

    upper_a_tilde.casecmp(lower_a_tilde).should == -1
    upper_a_umlaut.casecmp(lower_a_umlaut).should == -1
    lower_a_tilde.casecmp(upper_a_tilde).should == 1
    lower_a_umlaut.casecmp(upper_a_umlaut).should == 1

    # -- UTF-8 --
    upper_a_tilde  = :"Ã"
    lower_a_tilde  = :"ã"
    upper_a_umlaut = :"Ä"
    lower_a_umlaut = :"ä"

    upper_a_tilde.casecmp(lower_a_tilde).should == -1
    upper_a_umlaut.casecmp(lower_a_umlaut).should == -1
    lower_a_tilde.casecmp(upper_a_tilde).should == 1
    lower_a_umlaut.casecmp(upper_a_umlaut).should == 1
  end
end

describe "Symbol#casecmp" do
  it "returns nil if other is a String" do
    :abc.casecmp("abc").should be_nil
  end

  it "returns nil if other is an Integer" do
    :abc.casecmp(1).should be_nil
  end

  it "returns nil if other is an object" do
    obj = mock("string <=>")
    :abc.casecmp(obj).should be_nil
  end
end

describe 'Symbol#casecmp?' do
  it "compares symbols without regard to case" do
    :abcdef.casecmp?(:abcde).should == false
    :aBcDeF.casecmp?(:abcdef).should == true
    :abcdef.casecmp?(:abcdefg).should == false
    :abcdef.casecmp?(:ABCDEF).should == true
  end

  it "doesn't consider non-ascii characters equal that aren't" do
    # -- Latin-1 --
    upper_a_tilde  = "\xC3".b.to_sym
    upper_a_umlaut = "\xC4".b.to_sym
    lower_a_tilde  = "\xE3".b.to_sym
    lower_a_umlaut = "\xE4".b.to_sym

    lower_a_tilde.casecmp?(lower_a_umlaut).should_not == true
    lower_a_umlaut.casecmp?(lower_a_tilde).should_not == true
    upper_a_tilde.casecmp?(upper_a_umlaut).should_not == true
    upper_a_umlaut.casecmp?(upper_a_tilde).should_not == true

    # -- UTF-8 --
    upper_a_tilde  = :"Ã"
    lower_a_tilde  = :"ã"
    upper_a_umlaut = :"Ä"
    lower_a_umlaut = :"ä"

    lower_a_tilde.casecmp?(lower_a_umlaut).should_not == true
    lower_a_umlaut.casecmp?(lower_a_tilde).should_not == true
    upper_a_tilde.casecmp?(upper_a_umlaut).should_not == true
    upper_a_umlaut.casecmp?(upper_a_tilde).should_not == true
  end

  it "doesn't do case mapping for non-ascii and non-unicode characters" do
    # -- Latin-1 --
    upper_a_tilde  = "\xC3".b.to_sym
    upper_a_umlaut = "\xC4".b.to_sym
    lower_a_tilde  = "\xE3".b.to_sym
    lower_a_umlaut = "\xE4".b.to_sym

    upper_a_tilde.casecmp?(lower_a_tilde).should == false
    upper_a_umlaut.casecmp?(lower_a_umlaut).should == false
    lower_a_tilde.casecmp?(upper_a_tilde).should == false
    lower_a_umlaut.casecmp?(upper_a_umlaut).should == false
  end

  it 'does case mapping for unicode characters' do
    # -- UTF-8 --
    upper_a_tilde  = :"Ã"
    lower_a_tilde  = :"ã"
    upper_a_umlaut = :"Ä"
    lower_a_umlaut = :"ä"

    upper_a_tilde.casecmp?(lower_a_tilde).should == true
    upper_a_umlaut.casecmp?(lower_a_umlaut).should == true
    lower_a_tilde.casecmp?(upper_a_tilde).should == true
    lower_a_umlaut.casecmp?(upper_a_umlaut).should == true
  end

  it 'returns nil when comparing characters with different encodings' do
    # -- Latin-1 --
    upper_a_tilde = "\xC3".b.to_sym

    # -- UTF-8 --
    lower_a_tilde = :"ã"

    upper_a_tilde.casecmp?(lower_a_tilde).should == nil
    lower_a_tilde.casecmp?(upper_a_tilde).should == nil
  end
end