File: test_symbol.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (146 lines) | stat: -rw-r--r-- 3,834 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
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
145
146
require 'test/unit'


class TestSymbol < Test::Unit::TestCase
  IS19 = RUBY_VERSION =~ /1\.9/

  #
  # Check that two arrays contain the same "bag" of elements.
  # A mathematical bag differs from a "set" by counting the
  # occurences of each element. So as a bag [1,2,1] differs from
  # [2,1] (but is equal to [1,1,2]).
  #
  # The method only relies on the == operator to match objects
  # from the two arrays. The elements of the arrays may contain
  # objects that are not "Comparable".
  #
  # FIXME: This should be moved to common location.
  def assert_bag_equal(expected, actual)
    # For each object in "actual" we remove an equal object
    # from "expected". If we can match objects pairwise from the
    # two arrays we have two equal "bags". The method Array#index
    # uses == internally. We operate on a copy of "expected" to
    # avoid destructively changing the argument.
    #
    expected_left = expected.dup
    actual.each do |x|
      if j = expected_left.index(x)
        expected_left.slice!(j)
      end
    end
    assert( expected.length == actual.length && expected_left.length == 0,
           "Expected: #{expected.inspect}, Actual: #{actual.inspect}")
  end

  # v---------- test --------------v
  class Fred
    $f1 = :Fred
    def Fred
      $f3 = :Fred
    end
  end
  
  module Test
    Fred = 1
    $f2 = :Fred
  end
  
  # ^----------- test ------------^

  Fred.new.Fred

  def test_00sanity
    assert_equal($f1.__id__,$f2.__id__)
    assert_equal($f2.__id__,$f3.__id__)
  end

  @@unique_symbol_count = 0

  def gen_unique_symbol
    @@unique_symbol_count += 1
    "rubicon_unique_symbol_#{@@unique_symbol_count}".intern
  end

    def test_s_all_symbols
      assert_instance_of(Array, Symbol.all_symbols)
      Symbol.all_symbols.each do |sym|
        assert_instance_of(Symbol, sym)
      end
      assert_equal(Symbol.all_symbols, Symbol.all_symbols.uniq)

      symbols1 = Symbol.all_symbols
      s1 = gen_unique_symbol
      s2 = gen_unique_symbol
      symbols2 = Symbol.all_symbols
      assert_bag_equal([s1, s2], symbols2 - symbols1)
    end

  def test_VERY_EQUAL # '==='
    assert_equal(true,  :Fred   === :Fred)
    assert_equal(false, :Fred   === :Barney)
    assert_equal(true,  :Barney === :Barney)

    # don't match any non-Symbol
    assert_equal(false,  :Barney === ":Barney")
    assert_equal(false,  :Barney === "Barney")
    assert_equal(false,  :Barney === Object.new)
  end

  def test_id2name
    assert_equal("Fred",:Fred.id2name)
    assert_equal("Barney",:Barney.id2name)
    assert_equal("wilma",:wilma.id2name)
  end

  def test_inspect
    assert_equal(':hello', 'hello'.intern.inspect)
    assert_equal(':"hello world"', 'hello world'.intern.inspect)
    assert_equal(':"with \" char"', 'with " char'.intern.inspect)
    assert_equal(':"with \\\\ \" chars"', 'with \ " chars'.intern.inspect)
  end

  unless IS19
    def test_to_i
      assert_equal($f1.to_i,$f2.to_i)
      assert_equal($f2.to_i,$f3.to_i)
      assert(:wilma.to_i != :Fred.to_i)
      assert(:Barney.to_i != :wilma.to_i)
    end
  end

  def test_to_s
    assert_equal("Fred",:Fred.id2name)
    assert_equal("Barney",:Barney.id2name)
    assert_equal("wilma",:wilma.id2name)
  end

    def test_to_sym
      assert_equal(:Fred, :Fred.to_sym)
      assert_equal(:Barney, :Barney.to_sym)
      assert_equal(:wilma, :wilma.to_sym)
    end

  def test_type
    assert_equal(Symbol, :Fred.class)
    assert_equal(Symbol, :fubar.class)
  end

  def test_taint
    assert_same(:Fred, :Fred.taint)
    assert(! :Fred.tainted?)
  end

  def test_freeze
    assert_same(:Fred, :Fred.freeze)
    if IS19
      assert(:Fred.frozen?)
    else
      assert(! :Fred.frozen?)
    end
  end

  def test_dup
    assert_raise(TypeError) { :Fred.clone }
    assert_raise(TypeError) { :Fred.dup }
  end
end