#!/usr/bin/env ruby
# -*- ruby -*-

# Copyright 2000, 2001 by Jim Weirich (jweirich@one.net).
# All rights reserved.

# Permission is granted for use, copying, modification, distribution,
# and distribution of modified versions of this work as long as the
# above copyright notice is included.

require 'runit/testcase'
require 'runit/cui/testrunner'

require 'gem/inject'

$last_suite = RUNIT::TestSuite.new

# --------------------------------------------------------------------

class TestInject < RUNIT::TestCase
  def test_sum
    assert_equals 3, [1,2].inject(0) { |sum,item| item+sum }
    assert_equals 5, [2,3].inject(0) { |sum,item| item+sum }
    assert_equals 55,
      [1,2,3,4,5,6,7,8,9,10].inject(0) { |sum,item| item+sum }
  end

  def test_prod
    assert_equals 24, (1..4).inject(1) { |prod,item| item*prod }
  end

  def test_merge
    assert_equals [1,2,3,5], [[1,2],[2,3],[1,5]].inject([]) {
      |result,item| result | item
    }
  end
end

$last_suite.add_test (TestInject.suite)



# --------------------------------------------------------------------

class TestWhenMissing < RUNIT::TestCase
  def test_when_missing
    assert_equal 1, (1).when_missing { 0 }
    assert_equal 0, (nil).when_missing { 0 }
  end
end

$last_suite.add_test (TestWhenMissing.suite)


# --------------------------------------------------------------------

if __FILE__ == $0 then
  RUNIT::CUI::TestRunner.quiet_mode = true
  RUNIT::CUI::TestRunner.run ($last_suite)
end
