aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/blank_test.rb
blob: a14edf440b61c2ebb991fcad8260ab1096ea70cb (plain) (blame)
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
require File.dirname(__FILE__) + '/../abstract_unit'

class EmptyTrue
  def empty?() true; end
end

class EmptyFalse
  def empty?() false; end
end

class EmptyStripNotEmpty
  def empty?() true; end
  def strip() 'foo'; end
end

class EmptyStripEmpty
  def empty?() true; end
  def strip() ''; end
end

class NotEmptyStripNotEmpty
  def empty?() false; end
  def strip() 'foo'; end
end

class NotEmptyStripEmpty
  def empty?() false; end
  def strip() ''; end
end

class BlankTest < Test::Unit::TestCase
  BLANK = [ EmptyTrue.new, EmptyStripNotEmpty.new, EmptyStripEmpty.new,
            NotEmptyStripEmpty.new, nil, false, '', '   ', "  \n\t  \r ",
            [], {} ]
  NOT   = [ EmptyFalse.new, NotEmptyStripNotEmpty.new, Object.new, true,
            0, 1, 'a', [nil], { nil => 0 } ]
  
  class EmptyObject
    def empty?
      true
    end
    alias :strip :empty?
  end
  class NoStripObject < EmptyObject; undef :strip; end
  class NoEmptyStripObject < NoStripObject; undef :empty?; end

  def test_blank
    BLANK.each { |v| assert v.blank?  }
    NOT.each   { |v| assert !v.blank? }
    assert EmptyObject.new.blank?
    assert NoStripObject.new.blank?
    assert !NoEmptyStripObject.new.blank?
  end
end