#!/usr/bin/env ruby require 'ftools' require 'runit/testcase' require 'runit/cui/testrunner' require 'runit/testsuite' $:.unshift '../lib' $:.unshift './lib' require 'library.rb' $verbose = false class Library class TestVersion < RUNIT::TestCase def setup end def test_a_initialize v = nil assert_exception(ArgumentError) do v = Version.new end assert_exception(RuntimeError) do v = Version.new 'foo.rb' end end def test_b_initialize v = nil assert_no_exception do v = Version.new 3 end assert_equals 3, v.current assert_equals 0, v.revision assert_equals 0, v.age assert_no_exception do v = Version.new 3, 2 end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 0, v.age assert_no_exception do v = Version.new 3, 2, 1 end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 1, v.age end def test_c_initialize v = nil assert_exception(RuntimeError) do v = Version.new 'foo.rb.3.' end assert_no_exception do v = Version.new 'foo.rb.3' end assert_equals 3, v.current assert_equals 0, v.revision assert_equals 0, v.age assert_exception(RuntimeError) do v = Version.new 'foo.rb.3.2.' end assert_no_exception do v = Version.new 'foo.rb.3.2' end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 0, v.age assert_exception(RuntimeError) do v = Version.new 'foo.rb.3.2.1.' end assert_no_exception do v = Version.new 'foo.rb.3.2.1' end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 1, v.age assert_no_exception do v = Version.new 'foo.rb.3,2' end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 0, v.age assert_no_exception do v = Version.new 'foo.rb.3,2,1' end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 1, v.age end def test_d_initialize v = nil assert_exception(RuntimeError) do v = Version.new '3.' end assert_no_exception do v = Version.new '3' end assert_equals 3, v.current assert_equals 0, v.revision assert_equals 0, v.age assert_exception(RuntimeError) do v = Version.new '3.2.' end assert_no_exception do v = Version.new '3.2' end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 0, v.age assert_exception(RuntimeError) do v = Version.new '3.2.1.' end assert_no_exception do v = Version.new '3.2.1' end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 1, v.age assert_no_exception do v = Version.new '3,2' end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 0, v.age assert_no_exception do v = Version.new '3,2,1' end assert_equals 3, v.current assert_equals 2, v.revision assert_equals 1, v.age end def test_e_spaceship a = Version.new 0,0,0 b = Version.new 0,1,0 assert_equals (0, (a <=> a)) assert_equals (-1, (a <=> b)) assert_equals (1, (b <=> a)) a = Version.new 0,0,0 b = Version.new 1,0,0 assert_equals (-1, (a <=> b)) assert_equals (1, (b <=> a)) a = Version.new 1,0,0 b = Version.new 1,0,1 assert_equals (-1, (a <=> b)) assert_equals (1, (b <=> a)) end def test_f_spaceship a = Version.new 0,0,0 b = Version.new '0.1.0' assert_equals (0, (a <=> a)) assert_equals (-1, (a <=> b)) assert_equals (1, (b <=> a)) a = Version.new 0,0,0 b = Version.new '1.0.0' assert_equals (-1, (a <=> b)) assert_equals (1, (b <=> a)) a = Version.new 1,0,0 b = Version.new '1.0.1' assert_equals (-1, (a <=> b)) assert_equals (1, (b <=> a)) end def test_g_caseequality a = Version.new 0,0,0 b = Version.new 0,1,0 assert ((a === a)) assert ((a === b)) assert ((b === a)) a = Version.new 0,0,0 b = Version.new 1,0,1 c = Version.new 1,1,1 assert ((b === a)) assert ((c === a)) assert ((c === b)) assert ((b === c)) end def test_h_caseequality a = Version.new 0,0,0 b = Version.new '0,1,0' assert ((a === a)) assert ((a === b)) assert ((b === a)) a = Version.new 0,0,0 b = Version.new '1,0,1' c = Version.new '1,1,1' assert ((b === a)) assert ((c === a)) assert ((c === b)) assert ((b === c)) assert (!(a === b)) assert (!(a === c)) end def test_i_next a = Version.new 0,0,0 b = a.next assert ((b == '0,1,0')) b = a.next true assert ((b == '0,1,0')) b = a.next false assert ((b == a)) b = a.next true, true assert ((b == '1,0,0')) b = a.next true, true, true assert ((b == '1,0,1')) end def test_j_version_parse filename, version = Version.parse 'foo.rb.0.0.0' assert (filename == 'foo.rb') assert (version == '0.0.0') filename, version = Version.parse 'foo.rb.0,0,0' assert (filename == 'foo.rb') assert (version == '0.0.0') end def teardown end end end class TestLibrary < RUNIT::TestCase @@libraries = [] def setup @@libraries.clear end def teardown @@libraries.map{|r| File.rm_f(r)} end # emulate releasing a new library - for testing only! def release library @@libraries << library File.open(library, 'w') do |f| f.puts "def libversion;'#{library}';end" end library end def test_a_load lib0 = release './foo.rb.0.0.0' loaded = Library.load 'foo.rb', '0' assert (lib0 == loaded) end def test_b_load lib0 = release './foo.rb.0.0.0' lib1 = release './foo.rb.0.1.0' loaded = Library.load 'foo.rb', '0' assert (lib1 == loaded) end def test_c_load lib0 = release './foo.rb.1.1.0' lib1 = release './foo.rb.3.7.2' assert_exception(RuntimeError) do loaded = Library.load 'foo.rb', '0' end lib2 = release './foo.rb.3.7.3' loaded = Library.load 'foo.rb', '0' assert (lib2 == loaded) end end RUNIT::CUI::TestRunner.run(Library::TestVersion.suite) RUNIT::CUI::TestRunner.run(Library::TestLibrary.suite)