File: test_case.rb

package info (click to toggle)
jruby 1.5.1-1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 46,252 kB
  • ctags: 72,039
  • sloc: ruby: 398,155; java: 169,482; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (79 lines) | stat: -rw-r--r-- 1,128 bytes parent folder | download | duplicates (5)
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
require 'test/unit'

class TestCase < Test::Unit::TestCase
  def test_case_with_no_expression
    x = nil
    case
    when true
      x = 1
    when false
      x = 2
    end
    assert_equal(1, x)

    x = nil
    case
    when false
      x = 1
    when true
      x = 2
    end
    assert_equal(2, x)
  end

  def test_case_with_ranges
    x = nil
    case 10
    when 1..3
      x = 'a'
    when 4..8
      x = 'b'
    when 9..22
      x = 'c'
    else
      x = 'd'
    end
    assert_equal('c', x)
  end

  def test_case_with_else
    x = nil
    case 10
    when 1
      x = 'a'
    when 100
      x = 'b'
    else
      x = 'c'
    end
    assert_equal('c', x)
  end

  def test_case_no_match_returns_nil
    x = case nil
    when String then "HEH1"
    end
    assert_equal(nil, x)

    x = case "FOO"
    when Proc then "HEH1"
    end
    assert_equal(nil, x)
  end

  def test_case_return_value
    x = case "HEH"
    when Proc then "BAD"
    else "GOOD"
    end
    assert_equal("GOOD", x)
  end
  
  def test_case_when_splats_single
    assert_nothing_raised {
      case 1
      when *1
      end
    }
  end
end