Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 106 lines (85 sloc) 1.804 kB
db9f1ed @clarkware Initial import
authored
1 #!/usr/bin/env ruby
2
3 require 'test/unit'
4
5 class ConditionalExpressionTest < Test::Unit::TestCase
6
7 def test_boolean_true
8 assert(true)
9 assert(1)
10 assert(0)
11 assert("")
12 end
13
14 def test_boolean_false
15 assert(!nil)
16 assert(!false)
17 end
18
19 def test_and
20 assert(true && true)
21 end
22
23 def test_or
24 assert(true || false)
25 end
26
27 def test_not
28 assert(!false)
29 end
30
31 def test_defined?
32 assert_equal(nil, defined? dummy)
33 assert_equal("expression", defined? 1)
34 assert_equal("method", defined? puts)
35 assert_equal("constant", defined? String)
36 assert_equal("global-variable", defined? $_)
37 assert_equal("assignment", defined? a=1)
38 end
39
40 def test_equal_value
41 a = "a"
42 b = "a"
43 assert(a == b)
44 end
45
46 def test_equal?
47 a = "a"
48 b = "a"
49 assert_equal(false, a.equal?(b))
50 end
51
52 def test_eql?
53 a = "a"
54 b = "a"
55 assert_equal(true, a.eql?(b))
56 end
57
58 def test_if
59 assert_equal("Yes", if_greater_than_five(6))
60 assert_equal("No", if_greater_than_five(5))
61 end
62
63 def if_greater_than_five(x)
64 if x > 5 then "Yes"
65 else "No"
66 end
67 end
68
69 def test_unless
70 assert_equal("Yes", unless_greater_than_five(6))
71 assert_equal("No", unless_greater_than_five(5))
72 end
73
74 def unless_greater_than_five(x)
75 unless x > 5 then "No"
76 else "Yes"
77 end
78 end
79
80 def test_if_modifier
81 x = 6
82 y = x if x > 5
83 assert_equal(6, y)
84 end
85
86 def test_unless_modifier
87 x = 6
88 y = x unless x > 5
89 assert_equal(nil, y)
90 end
91
92 def test_case
93 assert_equal(false, leap_year?(1970))
94 assert_equal(true, leap_year?(400))
95 end
96
97 def leap_year?(year)
98 leap = case year
99 when year % 400 == 0: true
100 when year % 100 == 0: false
101 else year % 4 == 0
102 end
103 end
104
105 end
Something went wrong with that request. Please try again.