Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial import

  • Loading branch information...
commit db9f1ede13c70a2f5477a9a0b86d06cf8addc5a7 0 parents
Mike Clark authored August 21, 2008
21  README
... ...
@@ -0,0 +1,21 @@
  1
+= Ruby Learning Tests
  2
+
  3
+Unit tests I wrote way back when I started learning Ruby,
  4
+as described on my blog at:
  5
+
  6
+  http://clarkware.com/cgi/blosxom/2005/03/18#RLT1
  7
+
  8
+== Usage
  9
+
  10
+To run all the tests, use:
  11
+
  12
+  rake
  13
+  
  14
+== Write Your Own!
  15
+
  16
+This project can be found on GitHub at the following URL.
  17
+
  18
+  http://github.com/clarkware/ruby-learning-tests/
  19
+
  20
+If you would like to write your own tests, please fork the
  21
+repository and have a lot of fun!
19  Rakefile
... ...
@@ -0,0 +1,19 @@
  1
+# Rakefile         -*- ruby -*-
  2
+
  3
+require 'rake/testtask'
  4
+
  5
+task :default => [:test]
  6
+
  7
+desc "Run all the tests"
  8
+Rake::TestTask.new("test") do |t|
  9
+  t.pattern = '*_test.rb'
  10
+end
  11
+task :test
  12
+
  13
+task :task_info do |t|
  14
+  puts t.name
  15
+end
  16
+
  17
+task :environment_variable do
  18
+  puts ENV['HOME']
  19
+end
148  array_test.rb
... ...
@@ -0,0 +1,148 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class ArrayTest < Test::Unit::TestCase
  6
+
  7
+  def test_new_array_literal
  8
+    a = [3.14, "pie", 99]
  9
+    assert_equal(Array, a.class)
  10
+    assert_equal(3, a.length)   
  11
+    assert_equal(3.14, a[0]) 
  12
+    assert_equal("pie", a[1]) 
  13
+    assert_equal(99, a[2]) 
  14
+    assert_nil(a[3])
  15
+  end
  16
+
  17
+  def test_new_array
  18
+    a = Array.new
  19
+    assert_equal(Array, a.class)
  20
+    a[0] = 3.14
  21
+    a[1] = "pie"
  22
+    a[2] = 99
  23
+    assert_equal(3, a.length)
  24
+    assert_equal([3.14, "pie", 99], a)
  25
+  end
  26
+
  27
+  def test_new_array_with_block
  28
+    a = Array.new(3) { |i| i + 2 }
  29
+    assert_equal([2, 3, 4], a)
  30
+  end
  31
+
  32
+  def test_array_of_words
  33
+    a = %w{ ireland scotland england }
  34
+    assert_equal(["ireland", "scotland", "england"], a)
  35
+  end
  36
+  
  37
+  def test_index_negative
  38
+    a = [ 1, 3, 5 ]
  39
+    assert_equal(5, a[-1])
  40
+    assert_equal(3, a[-2])
  41
+    assert_equal(1, a[-3])
  42
+    assert_nil(a[-99])
  43
+  end
  44
+  
  45
+  def test_index_by_start_and_count
  46
+    a = [ 1, 3, 5, 7 ]
  47
+    assert_equal([1], a[0, 1])
  48
+    assert_equal([1, 3], a[0, 2])
  49
+    assert_equal([3, 5, 7], a[1, 3])
  50
+    assert_equal([7], a[3, 1])
  51
+    assert_equal([3, 5], a[-3, 2])
  52
+  end
  53
+  
  54
+  def test_index_by_range
  55
+    a = [ 1, 3, 5, 7 ]
  56
+    assert_equal([1, 3, 5], a[0..2])
  57
+    assert_equal([1, 3], a[0...2])
  58
+    assert_equal([3], a[1..1])
  59
+    assert_equal([], a[1...1])
  60
+    assert_equal([3, 5, 7], a[-3..-1])
  61
+  end
  62
+  
  63
+  def test_single_index_change
  64
+    a = [ 1, 3, 5, 7, 9 ]
  65
+
  66
+    a[1] = "bat"
  67
+    assert_equal([1, "bat", 5, 7, 9], a)
  68
+
  69
+    a[-3] = "cat"
  70
+    assert_equal([1, "bat", "cat", 7, 9], a)
  71
+
  72
+    a[3] = [9, 8]
  73
+    assert_equal([1, "bat", "cat", [9, 8], 9], a)
  74
+
  75
+    a[6] = 99
  76
+    assert_equal([1, "bat", "cat", [9, 8], 9, nil, 99], a)
  77
+  end
  78
+  
  79
+  def test_range_change
  80
+    a = [ 1, 3, 5, 7, 9 ]
  81
+
  82
+    a[2, 2] = 'cat'
  83
+    assert_equal([1, 3, "cat", 9], a)
  84
+
  85
+    a[2, 0] = 'dog'
  86
+    assert_equal([1, 3, "dog", "cat", 9], a)
  87
+
  88
+    a[1, 1] = [9, 8, 7]
  89
+    assert_equal([1, 9, 8, 7, "dog", "cat", 9], a)
  90
+
  91
+    a[0..3] = []
  92
+    assert_equal(["dog", "cat", 9], a)
  93
+
  94
+    a[5..6] = 99, 98
  95
+    assert_equal(["dog", "cat", 9, nil, nil, 99, 98], a)
  96
+  end
  97
+  
  98
+  def test_clear
  99
+    a = [1, 2, 3]
  100
+    assert_equal([], a.clear())
  101
+  end
  102
+  
  103
+  def test_push_pop_shift
  104
+    a = Array.new
  105
+    a.push("A")
  106
+    a.push("B")
  107
+    a.push("C")
  108
+    assert_equal(["A", "B", "C"], a)
  109
+    assert_equal("A", a.shift)
  110
+    assert_equal("C", a.pop)
  111
+    assert_equal("B", a.pop)
  112
+    assert_equal(nil, a.pop)
  113
+  end
  114
+  
  115
+  def test_each
  116
+    a = ["A", "B", "C"]
  117
+    s = ""
  118
+    a.each { |element| s += element } 
  119
+    assert_equal("ABC", s)
  120
+  end
  121
+
  122
+  def test_sort
  123
+    a = [3, 1, 7, 0]
  124
+    assert_equal([0, 1, 3, 7], a.sort)
  125
+  end
  126
+    
  127
+  def test_reverse
  128
+    a = [3, 1, 7, 0]
  129
+    assert_equal([0, 7, 1, 3], a.reverse)
  130
+  end
  131
+
  132
+  def test_join
  133
+    a = ["a", "b", "c"]
  134
+    assert_equal("abc", a.join)
  135
+    assert_equal("a-b-c", a.join("-"))
  136
+  end
  137
+  
  138
+  def test_array_of_hashes
  139
+    a = Array.new(size=2) { Hash.new(0) }
  140
+    assert_equal([{}, {}], a)
  141
+  end
  142
+  
  143
+  def test_array_of_arrays
  144
+  	a = Array.new(size=2) { Array.new(count=1, obj="A") }
  145
+  	assert_equal([["A"], ["A"]], a)
  146
+  end
  147
+  
  148
+end
80  assignment_test.rb
... ...
@@ -0,0 +1,80 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class AssignmentTest < Test::Unit::TestCase
  6
+  
  7
+  def test_chained
  8
+    a = b = 1 + 2
  9
+    assert_equal(3, a)
  10
+    assert_equal(3, b)
  11
+    assert_equal(3, a = b = 3)
  12
+  end
  13
+
  14
+  def test_parallel
  15
+    a = 1
  16
+    b = 2
  17
+    a, b = b, a
  18
+    assert_equal(2, a)
  19
+    assert_equal(1, b)
  20
+  end
  21
+
  22
+  def test_more_lvalues_than_rvalues
  23
+    a, b = 1
  24
+    assert_equal(1, a)
  25
+    assert_equal(nil, b)
  26
+  end
  27
+
  28
+  def test_more_rvalues_than_lvalues
  29
+    a, b = 1, 2, 3
  30
+    assert_equal(1, a)
  31
+    assert_equal(2, b)
  32
+  end
  33
+
  34
+  def test_one_lvalue_many_rvalues
  35
+    a = 1, 2, 3
  36
+    assert_equal([1, 2, 3], a)
  37
+  end
  38
+
  39
+  def test_array
  40
+    a = [1, 2, 3]
  41
+    b, c = a
  42
+    assert_equal(1, b)
  43
+    assert_equal(2, c)
  44
+
  45
+    b, c = 99, a
  46
+    assert_equal(99, b)
  47
+    assert_equal([1, 2, 3], c)
  48
+  end
  49
+
  50
+  def test_collapse_array
  51
+    a = [1, 2, 3]
  52
+    b, *c = a
  53
+    assert_equal(1, b)
  54
+    assert_equal([2, 3], c)
  55
+  end
  56
+
  57
+  def test_expand_array
  58
+    a = [1, 2, 3]
  59
+    b, c = 99, *a
  60
+    assert_equal(99, b)
  61
+    assert_equal(1, c)
  62
+  end
  63
+
  64
+  def test_nested
  65
+    a, (b, c), d = 1, 2, 3, 4
  66
+    assert_equal(1, a)
  67
+    assert_equal(2, b)
  68
+    assert_equal(nil, c)
  69
+    assert_equal(3, d)
  70
+  end
  71
+
  72
+  def test_nested_array
  73
+    a, (b, c), d = 1, [2, 3, 4], 5
  74
+    assert_equal(1, a)
  75
+    assert_equal(2, b)
  76
+    assert_equal(3, c)
  77
+    assert_equal(5, d)
  78
+  end
  79
+
  80
+end
24  benchmark.rb
... ...
@@ -0,0 +1,24 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require "benchmark"
  4
+
  5
+Benchmark.bm(label_width = 10) do |test|
  6
+
  7
+  loops = 50000
  8
+
  9
+  test.report("for:") do
  10
+    y = 0
  11
+    (1..loops).each { |x| y += x }
  12
+  end
  13
+
  14
+  test.report("times:") do
  15
+    y = 0
  16
+    loops.times { |x| y += x }
  17
+  end
  18
+
  19
+  test.report("upto:") do 
  20
+    y = 0
  21
+    1.upto(loops) { |x| y += x }
  22
+  end
  23
+
  24
+end
86  block_test.rb
... ...
@@ -0,0 +1,86 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class BlockTest < Test::Unit::TestCase
  6
+  	
  7
+  def test_call_block_twice
  8
+    x = 0
  9
+    call_block_twice { x += 1 }
  10
+    assert_equal(2, x)
  11
+  end
  12
+  
  13
+  def call_block_twice
  14
+    yield
  15
+    yield
  16
+  end
  17
+    
  18
+  def test_call_block_with_one_parameter
  19
+    x = 0
  20
+    call_block_with_one_param(1) { |y| x = y  }
  21
+    assert_equal(1, x)
  22
+  end
  23
+  
  24
+  def call_block_with_one_param(y)
  25
+    yield y
  26
+  end
  27
+  
  28
+  def test_call_block_with_two_params
  29
+    z = 0
  30
+    call_block_with_two_params(1, 2) { |x, y| z = x + y }
  31
+    assert_equal(3, z)
  32
+  end
  33
+
  34
+  def call_block_with_two_params(x, y)
  35
+    yield x, y
  36
+  end
  37
+
  38
+  def test_call_block_with_unchanged_local_variable
  39
+    x = 0
  40
+    call_block_with_unchanged_local { |y| x = y = y+1 }
  41
+    assert_equal(2, x)
  42
+  end
  43
+
  44
+  def call_block_with_unchanged_local
  45
+    y = 1
  46
+    yield y
  47
+    assert_equal(1, y)
  48
+  end
  49
+  
  50
+  def test_block_given
  51
+    b = take_block("no block")
  52
+    assert_equal(false, b)
  53
+    
  54
+    b = take_block("block") { |param| param }
  55
+    assert_equal(true, b)
  56
+  end
  57
+  
  58
+  def take_block(param)
  59
+    if block_given?
  60
+      yield true
  61
+    else
  62
+      false
  63
+    end
  64
+  end
  65
+  
  66
+  def test_proc
  67
+    y = 0
  68
+    call_proc(1) { |x| y = x }
  69
+    assert_equal(1, y)
  70
+  end
  71
+  
  72
+  def call_proc(x, &action)
  73
+    action.call(x)
  74
+  end
  75
+  
  76
+  def test_closure
  77
+    p = n_times(2)
  78
+    assert_equal(4, p.call(2))
  79
+    assert_equal(6, p.call(3))
  80
+  end
  81
+  
  82
+  def n_times(thing)
  83
+    return proc { |n| thing * n }
  84
+  end
  85
+  
  86
+end
81  class_access_test.rb
... ...
@@ -0,0 +1,81 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class ClassAccessTest < Test::Unit::TestCase
  6
+  
  7
+  def test_private_method
  8
+    begin
  9
+      c = Parent.new
  10
+      c.only_me
  11
+      flunk("Should raise NoMethodError")
  12
+    rescue NoMethodError
  13
+      assert(true)
  14
+    end
  15
+  end
  16
+
  17
+  def test_protected_method
  18
+    begin
  19
+      c = Parent.new
  20
+      c.me_and_subclasses
  21
+      flunk("Should raise NoMethodError")
  22
+    rescue NoMethodError
  23
+      assert(true)
  24
+    end
  25
+  end
  26
+
  27
+  def test_child_calls_parent_private
  28
+    c = Child.new
  29
+    c.call_parent_private
  30
+    assert(true)
  31
+  end
  32
+
  33
+  def test_child_calls_parent_protected
  34
+    c = Child.new
  35
+    c.call_parent_protected
  36
+    assert(true)
  37
+  end
  38
+ 
  39
+end
  40
+
  41
+
  42
+class Parent
  43
+  
  44
+  public
  45
+
  46
+    def everybody
  47
+    end
  48
+
  49
+    def everybody_2
  50
+    end
  51
+
  52
+  protected
  53
+
  54
+    def me_and_subclasses
  55
+    end
  56
+
  57
+  private
  58
+
  59
+    def only_me
  60
+    end
  61
+
  62
+  # 
  63
+  # Alternative access control declarations:
  64
+  #
  65
+  public :everybody, :everybody_2
  66
+  protected :me_and_subclasses
  67
+  private :only_me
  68
+   
  69
+end
  70
+
  71
+class Child < Parent
  72
+
  73
+  def call_parent_private
  74
+    only_me
  75
+  end
  76
+
  77
+  def call_parent_protected
  78
+    me_and_subclasses 
  79
+  end
  80
+
  81
+end
48  class_inheritance_test.rb
... ...
@@ -0,0 +1,48 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class InheritanceTest < Test::Unit::TestCase
  6
+  
  7
+  def setup
  8
+    @song = Song.new("Ruby Tuesday", "Matz", 123)
  9
+    @karaokeSong = KaraokeSong.new("Ruby Tuesday", "Matz", 123, "Goodbye...")
  10
+  end
  11
+  
  12
+  def test_superclass
  13
+    assert_equal("Ruby Tuesday-Matz (123)", @song.to_s)
  14
+  end
  15
+
  16
+  def test_subclass
  17
+    assert_equal(KaraokeSong, @karaokeSong.class)
  18
+    assert_equal("Ruby Tuesday-Matz (123) [Goodbye...]", @karaokeSong.to_s)
  19
+  end
  20
+  
  21
+end
  22
+
  23
+class Song
  24
+  
  25
+  def initialize(name, artist, duration)
  26
+    @name = name
  27
+    @artist = artist
  28
+    @duration = duration
  29
+  end
  30
+  
  31
+  def to_s
  32
+    "#@name-#@artist (#@duration)"
  33
+  end
  34
+  
  35
+end
  36
+
  37
+class KaraokeSong < Song
  38
+  
  39
+  def initialize(name, artist, duration, lyrics)
  40
+    super(name, artist, duration)
  41
+    @lyrics = lyrics
  42
+  end
  43
+  
  44
+  def to_s
  45
+    super + " [#@lyrics]"
  46
+  end
  47
+  
  48
+end
46  class_private_constructor_test.rb
... ...
@@ -0,0 +1,46 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class PrivateConstructorTest < Test::Unit::TestCase
  6
+  
  7
+  def test_create_square_with_area
  8
+    s = Square.with_area(4)
  9
+    assert_equal(2, s.side())
  10
+  end
  11
+
  12
+  def test_create_square_with_diagonal
  13
+    s = Square.with_diagonal(2.828427)
  14
+    assert_in_delta(2, s.side(), 0.1)
  15
+  end
  16
+
  17
+  def test_create_square_with_side
  18
+    s = Square.with_side(2)
  19
+    assert_equal(2, s.side())
  20
+  end
  21
+  
  22
+end
  23
+
  24
+class Square
  25
+  
  26
+  private_class_method :new
  27
+  
  28
+  attr_reader :side
  29
+  
  30
+  def initialize(side)
  31
+    @side = side
  32
+  end
  33
+  
  34
+  def Square.with_area(area)
  35
+    new(Math.sqrt(area))
  36
+  end
  37
+  
  38
+  def Square.with_diagonal(diag)
  39
+    new(diag / Math.sqrt(2))
  40
+  end
  41
+  
  42
+  def Square.with_side(side)
  43
+    new(side)
  44
+  end
  45
+  
  46
+end
93  class_test.rb
... ...
@@ -0,0 +1,93 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class ClassTest < Test::Unit::TestCase
  6
+  
  7
+  def setup
  8
+    @song = Song.new("Ruby Tuesday", "Matz", 123)
  9
+  end
  10
+  
  11
+  def test_class
  12
+    assert_equal(Song, @song.class)  
  13
+    assert(@song.object_id > 0)
  14
+    assert_equal("Ruby Tuesday-Matz (123)", @song.to_s)
  15
+  end
  16
+  
  17
+  def test_read_only_attributes
  18
+    assert_equal("Ruby Tuesday", @song.name)
  19
+    assert_equal("Matz", @song.artist)
  20
+  end
  21
+  
  22
+  def test_read_write_attributes
  23
+    assert_equal(123, @song.duration)
  24
+    @song.duration = 456
  25
+    assert_equal(456, @song.duration)
  26
+  end
  27
+  
  28
+  def test_class_method
  29
+    assert_equal(0, Song.total_plays)
  30
+    assert_equal(1, @song.play)
  31
+    assert_equal(1, Song.total_plays)
  32
+    
  33
+    anotherSong = Song.new("Title", "Artist", 10)
  34
+    assert_equal(1, anotherSong.play)
  35
+    assert_equal(2, Song.total_plays)
  36
+  end
  37
+
  38
+  def test_dup
  39
+    song2 = @song.dup
  40
+    assert_not_equal(song2, @song)
  41
+    assert_not_equal(song2.object_id, @song.object_id)
  42
+  end
  43
+
  44
+  def test_freeze
  45
+    begin
  46
+      song2 = @song
  47
+      @song.freeze
  48
+      song2.duration = 123
  49
+      flunk("Should raise TypeError")
  50
+    rescue TypeError => error
  51
+      assert_equal("can't modify frozen object", error.message)
  52
+    end
  53
+  end
  54
+  
  55
+  class Song
  56
+  
  57
+    attr_reader :name, :artist, :duration
  58
+    attr_writer :duration
  59
+  
  60
+    @@totalPlays = 0
  61
+  
  62
+    def initialize(name, artist, duration)
  63
+      @name = name
  64
+      @artist = artist
  65
+      @duration = duration
  66
+      @plays = 0
  67
+    end
  68
+
  69
+    def duration
  70
+      @duration
  71
+    end
  72
+ 
  73
+    def duration=(duration)
  74
+      @duration = duration
  75
+    end
  76
+  
  77
+    def play
  78
+      @plays += 1
  79
+      @@totalPlays += 1
  80
+      return @plays
  81
+    end
  82
+  
  83
+    def self.total_plays
  84
+      @@totalPlays
  85
+    end
  86
+
  87
+    def to_s
  88
+      "#@name-#@artist (#@duration)"
  89
+    end
  90
+    
  91
+  end  
  92
+  
  93
+end
105  conditional_expression_test.rb
... ...
@@ -0,0 +1,105 @@
  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
89  control_structures_test.rb
... ...
@@ -0,0 +1,89 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class ControlStructuresTest < Test::Unit::TestCase
  6
+  
  7
+  def test_while
  8
+    x = 0
  9
+    while x < 10
  10
+      x += 1
  11
+    end
  12
+    assert_equal(10, x)
  13
+  end
  14
+  
  15
+  def test_while_modifier
  16
+    x = 0
  17
+    x += 1 while x < 10
  18
+    assert_equal(10, x)
  19
+  end
  20
+
  21
+  def test_until
  22
+    x = 0
  23
+    until x == 10
  24
+      x += 1
  25
+    end
  26
+    assert_equal(10, x)
  27
+  end
  28
+  
  29
+  def test_until_modifier
  30
+    x = 0
  31
+    x += 1 until x == 10
  32
+    assert_equal(10, x)
  33
+  end
  34
+  
  35
+  def test_for
  36
+    numbers = [1, 2, 3]
  37
+    x = 0
  38
+    for i in numbers
  39
+      x += i
  40
+    end
  41
+    assert_equal(6, x)
  42
+  end
  43
+  
  44
+  # test above translates into...
  45
+
  46
+  def test_for_iterator
  47
+    numbers = [1, 2, 3]
  48
+    x = 0
  49
+    numbers.each { |i| x += i }
  50
+    assert_equal(6, x)
  51
+  end
  52
+
  53
+  def test_for_upto
  54
+    x = 0
  55
+    1.upto(3) { |i| x += i }
  56
+    assert_equal(6, x)
  57
+  end
  58
+
  59
+  def test_loop
  60
+    x = 0
  61
+    loop do
  62
+      x += 1
  63
+      break if x > 4
  64
+    end
  65
+    assert_equal(5, x)
  66
+  end
  67
+
  68
+  def test_loop_next
  69
+    numbers = []
  70
+    x = 0
  71
+    loop do
  72
+      x += 1
  73
+      next if x < 3
  74
+      numbers << x
  75
+      break if x > 4
  76
+    end
  77
+    assert_equal([3, 4, 5], numbers)
  78
+  end
  79
+
  80
+  def test_retry
  81
+    numbers = []
  82
+    for i in 1..3
  83
+      numbers << i
  84
+      retry if i == 3 and numbers.length < 6
  85
+    end
  86
+    assert_equal([1, 2, 3, 1, 2, 3], numbers)
  87
+  end
  88
+
  89
+end
40  enumerable_test.rb
... ...
@@ -0,0 +1,40 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class EnumerableTest < Test::Unit::TestCase
  6
+
  7
+  def test_find
  8
+    a = ["A", "B", "C"]
  9
+    result = a.find { |element| element == "B" }
  10
+    assert_equal("B", result)
  11
+  end
  12
+  
  13
+  def test_each_with_index
  14
+    a = ["A", "B", "C"]
  15
+    s = ""
  16
+    a.each_with_index { |element, index| s += element + index.to_s} 
  17
+    assert_equal("A0B1C2", s)
  18
+  end
  19
+  
  20
+  def test_collect
  21
+    a = ["H", "A", "L"]
  22
+    collected = a.collect { |element| element.succ }
  23
+    assert_equal(["I", "B", "M"], collected)
  24
+  end
  25
+
  26
+  def test_sort
  27
+    a = [3, 1, 7, 0]
  28
+    assert_equal([0, 1, 3, 7], a.sort)
  29
+  end
  30
+
  31
+  def test_inject
  32
+    a = [ 1, 3, 5, 7 ]
  33
+    result = a.inject(0) { |sum, element| sum + element }
  34
+    assert_equal(16, result)
  35
+
  36
+    result = a.inject { |sum, element| sum + element }
  37
+    assert_equal(16, result)
  38
+  end
  39
+  	
  40
+end
65  environment_test.rb
... ...
@@ -0,0 +1,65 @@
  1
+#!/usr/bin/env ruby 
  2
+
  3
+require 'test/unit'
  4
+
  5
+class EnvironmentTest < Test::Unit::TestCase
  6
+  
  7
+  def test_command_line_args
  8
+    assert_not_nil(program_name = $0)
  9
+    assert_equal(Array, ARGV.class)
  10
+  end
  11
+  
  12
+  def test_load_path
  13
+    load_path = $:
  14
+    assert_equal(Array, load_path.class)
  15
+    assert(load_path.length > 0)
  16
+  end
  17
+  
  18
+  def test_loaded_files
  19
+    loaded_files = $"
  20
+    assert_equal(Array, loaded_files.class)
  21
+    assert(loaded_files.length > 0)
  22
+  end
  23
+  
  24
+  def test_prepend_load_path
  25
+    parent_dir = File.join(File.dirname(__FILE__), '..')
  26
+    $:.unshift parent_dir
  27
+    assert_equal(parent_dir, $:.first)
  28
+  end
  29
+  
  30
+  def test_rescue_from_exit
  31
+    begin
  32
+      exit 1
  33
+      flunk("Should raise SystemExit")
  34
+    rescue SystemExit => ex
  35
+      assert_equal("exit", ex.message)
  36
+    end    
  37
+  end
  38
+  
  39
+  def test_read_environment_variable
  40
+    assert_equal('mike', ENV['USER']) 
  41
+    assert_equal(`echo $HOME`.chomp, ENV['HOME'])
  42
+  end
  43
+  
  44
+  def test_update_environment_variable
  45
+    assert_equal('mike', ENV['USER']) 
  46
+    ENV['USER'] = 'joe-bob' 
  47
+    assert_equal('joe-bob', ENV['USER'])    
  48
+  end
  49
+  
  50
+  def test_environment_variable_changes_propogate_to_child_processes
  51
+    assert_equal('mike', ENV['USER']) 
  52
+    fork do 
  53
+      assert_equal('mike', ENV['USER']) 
  54
+      ENV['USER'] = 'joe-bob' 
  55
+      fork do 
  56
+        assert_equal('joe-bob', ENV['USER'])
  57
+      end 
  58
+      Process.wait 
  59
+      assert_equal('joe-bob', ENV['USER']) 
  60
+    end 
  61
+    Process.wait 
  62
+    assert_equal('mike', ENV['USER']) 
  63
+  end
  64
+
  65
+end
138  exception_test.rb
... ...
@@ -0,0 +1,138 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class ExceptionTest < Test::Unit::TestCase
  6
+
  7
+  def test_no_exception
  8
+    assert_nothing_raised { true }
  9
+  end
  10
+  
  11
+  def test_raise_runtime_error
  12
+    assert_raise(RuntimeError) { raise "Bomb" }
  13
+  end
  14
+  
  15
+  def test_handle_runtime_error
  16
+    begin
  17
+      raise "Bomb"
  18
+      flunk "Should raise RuntimeError"
  19
+    rescue
  20
+      assert_equal(RuntimeError, $!.class)
  21
+      assert_equal("Bomb", $!.message)
  22
+    end
  23
+  end
  24
+
  25
+  def test_reraise_runtime_error
  26
+    begin
  27
+      begin
  28
+        raise "Bomb"
  29
+        flunk "Should raise RuntimeError"
  30
+      rescue
  31
+        raise
  32
+      end
  33
+      flunk "Should raise RuntimeError"
  34
+    rescue
  35
+      assert_equal(RuntimeError, $!.class)
  36
+      assert_equal("Bomb", $!.message)      
  37
+    end
  38
+  end
  39
+
  40
+  def test_raise_runtime_error_explicit
  41
+    begin
  42
+      raise RuntimeError, "Bomb"
  43
+      flunk "Should raise RuntimeError"
  44
+    rescue
  45
+      assert_equal(RuntimeError, $!.class)
  46
+      assert_equal("Bomb", $!.message)
  47
+    end
  48
+  end
  49
+
  50
+  def test_custom_exception
  51
+    begin
  52
+      raise CustomException.new(13), "Bomb"
  53
+      flunk "Should raise CustomException"
  54
+    rescue CustomException => custom
  55
+      assert_equal(CustomException, custom.class)
  56
+      assert_equal("Bomb", custom.message)
  57
+      assert_equal(13, custom.code)
  58
+    end
  59
+  end
  60
+  
  61
+  def test_multiple_rescues
  62
+    begin
  63
+      raise CustomException.new(), "Bomb"
  64
+      flunk "Should raise CustomException"
  65
+    rescue CustomException => ex
  66
+      assert_equal(CustomException, ex.class)
  67
+    rescue RuntimeError => ex
  68
+      flunk "Should raise CustomException"
  69
+    end
  70
+  end
  71
+  
  72
+  def test_multiple_exceptions_one_rescue
  73
+    begin
  74
+      raise CustomException.new(), "Bomb"
  75
+      flunk "Should raise CustomException"
  76
+    rescue RuntimeError, CustomException => ex
  77
+      assert_equal(CustomException, ex.class)
  78
+    end
  79
+  end
  80
+  
  81
+  def test_ensure_with_exception_raised
  82
+    raised = false
  83
+    ensured = false
  84
+    begin
  85
+      raise RuntimeError
  86
+    rescue RuntimeError
  87
+      raised = true
  88
+    ensure
  89
+      ensured = true 
  90
+    end	
  91
+    assert(raised)
  92
+    assert(ensured)
  93
+  end
  94
+    
  95
+  def test_ensure_without_exception_raised
  96
+    ensured = false
  97
+    begin
  98
+    rescue RuntimeError
  99
+    ensure
  100
+      ensured = true 
  101
+    end
  102
+    assert(ensured)
  103
+  end
  104
+
  105
+  def test_retry
  106
+    times = 2
  107
+    attempts = 0
  108
+    begin
  109
+      attempts += 1
  110
+      raise RuntimeError
  111
+    rescue RuntimeError
  112
+      retry if attempts < times 
  113
+    end	
  114
+    assert_equal(2, attempts)
  115
+  end
  116
+
  117
+  def test_catch_throw
  118
+    x = 0
  119
+    catch (:done)  do
  120
+      for i in 1..10
  121
+        x = i
  122
+        throw :done unless i < 5
  123
+      end
  124
+      flunk "Should never get here"
  125
+    end
  126
+    assert_equal(5, x)
  127
+  end
  128
+    
  129
+end
  130
+
  131
+class CustomException < RuntimeError
  132
+
  133
+  attr :code
  134
+ 
  135
+  def initialize(code = 0)
  136
+    @code = code
  137
+  end
  138
+end
120  file_test.rb
... ...
@@ -0,0 +1,120 @@
  1
+#!/usr/bin/env ruby
  2
+
  3
+require 'test/unit'
  4
+
  5
+class IOTest < Test::Unit::TestCase
  6
+  
  7
+  TEST_FILE = "testfile"
  8
+
  9
+  def teardown
  10
+    File.delete(TEST_FILE) if File.exist?(TEST_FILE)
  11
+  end
  12
+  
  13
+  def test_new_file
  14
+    f = File.new(TEST_FILE, "w")
  15
+    assert(File.exist?(TEST_FILE))
  16
+    assert_equal(TEST_FILE, f.path)
  17
+    f.close()
  18
+    assert(f.closed?)
  19
+  end
  20
+  
  21
+  def test_create_and_open_file
  22
+    file = nil
  23
+    File.open(TEST_FILE, "w") do |file|
  24
+      assert_equal(TEST_FILE, file.path)
  25
+    end
  26
+    assert(file.closed?)
  27
+  end
  28
+  
  29
+  def test_readline
  30
+    File.open(TEST_FILE, "w+") do |file|
  31
+      file.print("ABC\nDEF")
  32
+      file.rewind()
  33
+      assert_equal("ABC\n", file.readline())
  34
+      assert_equal("DEF", file.readline())
  35
+    end
  36
+  end
  37
+  
  38
+  def test_each_line
  39
+    File.open(TEST_FILE, "w+") do |file|
  40
+      file.print("ABC\nDEF\n")
  41
+      file.rewind()
  42
+      lines = []
  43
+      file.each_line do |line|
  44
+        lines << line
  45
+      end
  46
+      assert_equal(["ABC\n", "DEF\n"], lines)	
  47
+    end
  48
+  end
  49
+
  50
+  def test_each_line_with_separator
  51
+    File.open(TEST_FILE, "w+") do |file|
  52
+      file.print("ABC.DEF.")
  53
+      file.rewind()
  54
+      lines = []
  55
+      file.each_line('.') do |line|
  56
+        lines << line
  57
+      end
  58
+      assert_equal(["ABC.", "DEF."], lines)	
  59
+    end
  60
+  end
  61
+
  62
+  def test_each_byte
  63
+    File.open(TEST_FILE, "w+") do |file|
  64
+      file.print("ABC")
  65
+      file.rewind()
  66
+      bytes = []
  67
+      file.each_byte { |x| bytes << x } 
  68
+      assert_equal([65, 66, 67], bytes)
  69
+    end
  70
+  end
  71
+  
  72
+  def test_for_each
  73
+    File.open(TEST_FILE, "w") do |file|
  74
+      file.print("ABC\nDEF\n")
  75
+      file.rewind()
  76
+    end
  77
+
  78
+    lines = []
  79
+    IO.foreach(TEST_FILE) do |line|
  80
+      lines << line
  81
+    end
  82
+    assert_equal(["ABC\n", "DEF\n"], lines)	
  83
+  end