File: casecmp_spec.rb

package info (click to toggle)
jruby 9.1.17.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 71,608 kB
  • sloc: ruby: 505,916; java: 237,875; xml: 31,161; ansic: 7,152; yacc: 4,605; sh: 887; makefile: 108; jsp: 48; tcl: 40
file content (74 lines) | stat: -rw-r--r-- 2,291 bytes parent folder | download
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
# -*- encoding: binary -*-
require File.expand_path('../../../spec_helper', __FILE__)

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"
    upper_a_umlaut = :"\xC4"
    lower_a_tilde  = :"\xE3"
    lower_a_umlaut = :"\xE4"

    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  = :"\xC3\x83"
    upper_a_umlaut = :"\xC3\x84"
    lower_a_tilde  = :"\xC3\xA3"
    lower_a_umlaut = :"\xC3\xA4"

    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"
    upper_a_umlaut = :"\xC4"
    lower_a_tilde  = :"\xE3"
    lower_a_umlaut = :"\xE4"

    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  = :"\xC3\x83"
    upper_a_umlaut = :"\xC3\x84"
    lower_a_tilde  = :"\xC3\xA3"
    lower_a_umlaut = :"\xC3\xA4"

    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 a Fixnum" 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