]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/test/maasha/test_biopieces.rb
rewrite of FASTQ internals
[biopieces.git] / code_ruby / test / maasha / test_biopieces.rb
index 86da311043ab64837ee4e86d7b6d4dfec34b87a9..892cbd54581596c26f97db298fec62d8f96ed4df 100755 (executable)
@@ -1,6 +1,7 @@
 #!/usr/bin/env ruby
+$:.unshift File.join(File.dirname(__FILE__), '..', '..')
 
-# Copyright (C) 2007-2010 Martin A. Hansen.
+# Copyright (C) 2007-2011 Martin A. Hansen.
 
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -25,9 +26,9 @@
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 require 'test/unit'
+require 'test/helper'
 require 'maasha/biopieces'
 require 'stringio'
-require 'pp'
 
 Biopieces::TEST     = true
 OptionHandler::TEST = true
@@ -39,13 +40,13 @@ SCRIPT_PATH = "write_fasta"
 class BiopiecesTest < Test::Unit::TestCase
   # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
 
-  def test_Biopieces_options_parse_with_all_cast_keys_dont_raise
+  test "Biopieces.options_parse with all cast keys dont raise" do
     argv  = []
     casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
     assert_nothing_raised(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
   end
 
-  def test_Biopieces_options_parse_with_illegal_long_cast_values_raises
+  test "Biopieces.options_parse with illegal long cast values raises" do
     [nil, true, false, 1, 0, "a"].each do |long|
       argv  = []
       casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -53,7 +54,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_legal_long_cast_values_dont_raise
+  test "Biopieces.options_parse with legal long cast values dont raise" do
     ["foo", "!!", "0123"].each do |long|
       argv  = []
       casts = [{:long=>long, :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -61,7 +62,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_illegal_short_cast_values_raises
+  test "Biopieces.options_parse with illegal short cast values raises" do
     [nil, true, false, "foo"].each do |short|
       argv  = []
       casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -69,7 +70,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_legal_short_cast_values_dont_raise
+  test "Biopieces.options_parse with legal short cast values dont raise" do
     ["!", "1", "a"].each do |short|
       argv  = []
       casts = [{:long=>"foo", :short=>short, :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -77,7 +78,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_illegal_type_cast_values_raises
+  test "Biopieces.options_parse with illegal type cast values raises" do
     [nil, true, false, "foo", 12, 0].each do |type|
       argv  = []
       casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -85,7 +86,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_legal_type_cast_values_dont_raise
+  test "Biopieces.options_parse with legal type cast values dont raise" do
     TYPES.each do |type|
       argv  = []
       casts = [{:long=>"foo", :short=>"f", :type=>type, :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -93,7 +94,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_illegal_mandatory_cast_values_raises
+  test "Biopieces.options_parse with illegal mandatory cast values raises" do
     ["yes", 12, 0, nil].each do |mandatory|
       argv  = []
       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -101,7 +102,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_legal_mandatory_cast_values_dont_raise
+  test "Biopieces.options_parse with legal mandatory cast values dont raise" do
     [true, false].each do |mandatory|
       argv  = [ "--foo", "1" ]
       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>mandatory, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -109,7 +110,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_illegal_default_cast_values_raises
+  test "Biopieces.options_parse with illegal default cast values raises" do
     [true, false, [], {}].each do |default|
       argv  = []
       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
@@ -117,7 +118,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_legal_default_cast_values_dont_raise
+  test "Biopieces.options_parse with legal default cast values dont raise" do
     [nil, 0, 1, -1].each do |default|
       argv  = []
       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>default, :allowed=>nil, :disallowed=>nil}]
@@ -125,7 +126,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_illegal_allowed_cast_values_raises
+  test "Biopieces.options_parse with illegal allowed cast values raises" do
     [true, false, {}, [], 0, 0.1].each do |allowed|
       argv  = []
       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
@@ -133,7 +134,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_legal_allowed_cast_values_dont_raise
+  test "Biopieces.options_parse with legal allowed cast values dont raise" do
     ["foo,bar,0",nil].each do |allowed|
       argv  = []
       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>allowed, :disallowed=>nil}]
@@ -141,7 +142,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_illegal_disallowed_cast_values_raises
+  test "Biopieces.options_parse with illegal disallowed cast values raises" do
     [true, false, {}, [], 0, 0.1].each do |disallowed|
       argv  = []
       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
@@ -149,7 +150,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_legal_disallowed_cast_values_dont_raise
+  test "Biopieces.options_parse with legal disallowed cast values dont raise" do
     ["foo,bar,0",nil].each do |disallowed|
       argv  = []
       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>disallowed}]
@@ -157,7 +158,7 @@ class BiopiecesTest < Test::Unit::TestCase
     end
   end
 
-  def test_Biopieces_options_parse_with_duplicate_long_cast_values_raises
+  test "Biopieces.options_parse with duplicate long cast values raises" do
     argv  = []
     casts = []
     casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
@@ -165,7 +166,7 @@ class BiopiecesTest < Test::Unit::TestCase
     assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
   end
 
-  def test_Biopieces_options_parse_with_duplicate_short_cast_values_raises
+  test "Biopieces.options_parse with duplicate short cast values raises" do
     argv  = []
     casts = []
     casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
@@ -173,7 +174,7 @@ class BiopiecesTest < Test::Unit::TestCase
     assert_raise(CastError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
   end
 
-  def test_Biopieces_options_parse_without_duplicate_long_and_short_cast_values_dont_raise
+  test "Biopieces.options_parse without duplicate long and short cast values dont raise" do
     argv  = []
     casts = []
     casts << {:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
@@ -183,80 +184,63 @@ class BiopiecesTest < Test::Unit::TestCase
 
   # >>>>>>>>>>>>>>>>>>>> Testing Options.parse <<<<<<<<<<<<<<<<<<<<
 
-  def test_Biopieces_options_parse_with_empty_argv_and_missing_wiki_file_raises
+  test "Biopieces.options_parse with empty argv and missing wiki file raises" do
     argv  = []
     casts = []
     assert_raise(RuntimeError) { Biopieces.options_parse(argv,casts, "foo") }
   end
 
-  def test_Biopieces_options_parse_with_empty_argv_and_existing_wiki_file_dont_raise
+  test "Biopieces.options_parse with empty argv and existing wiki file dont raise" do
     argv  = []
     casts = []
     assert_nothing_raised { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
   end
 
-  def test_Biopieces_options_parse_with_help_in_argv_and_existing_wiki_output_long_usage
+  test "Biopieces.options_parse with help in argv and existing wiki output long usage" do
     argv = ["--help"]
     assert_nothing_raised { Biopieces.options_parse(argv,[],SCRIPT_PATH) }
   end
 
-#  # FIXME This one fails because any argument to a flag is ignored and the flag value is set to true. Should it raise?
-#  test "Options.parse with type cast flag with an argument raises
-#    casts = [{:long=>"foo", :short=>"f", :type=>"flag", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
-#    opt_parser = Options.new(casts)
-#    assert_raise(ArgumentError) { opt_parser.parse(["--foo", "bar"],SCRIPT_PATH) }
-#   end
-
-  def test_Biopieces_options_parse_with_stream_in_argv_returns_correct_options
+  test "Biopieces.options_parse with stream in argv returns correct options" do
     argv = ["--stream_in", DUMMY_FILE]
     options = Biopieces.options_parse(argv,[],SCRIPT_PATH)
     assert_equal(DUMMY_FILE, options[:stream_in])
   end
  
-   def test_Biopieces_options_parse_with_I_argv_returns_correct_options
+   test "Biopieces.options_parse with I argv returns correct options" do
      argv    = ["-I", DUMMY_FILE]
      casts   = []
      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
      assert_equal(DUMMY_FILE, options[:stream_in])
    end
  
-   def test_Biopieces_options_parse_use_cast_default_value_if_no_argument_given
+   test "Biopieces.options_parse use cast default value if no argument given" do
      argv    = ["-I", DUMMY_FILE]
      casts   = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
      assert_equal(options[:foo], "bar")
    end
  
-   def test_Biopieces_options_parse_dont_use_default_value_if_argument_given
+   test "Biopieces.options_parse dont use default value if argument given" do
      argv    = ["--foo", "bleh", "-I", DUMMY_FILE]
      casts   = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
      options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
-     assert_equal(options[:foo], "bleh".to_sym)
+     assert_equal(options[:foo], "bleh")
    end
  
-   def test_Biopieces_options_parse_with_mandatory_cast_and_no_argument_raises
+   test "Biopieces.options_parse with mandatory cast and no argument raises" do
      argv  = ["-I", DUMMY_FILE]
      casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
      assert_raise(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
    end
  
-   def test_Biopieces_options_parse_with_mandatory_cast_and_argument_dont_raise
+   test "Biopieces.options_parse with mandatory cast and argument dont raise" do
      argv  = ["--foo", "bar", "-I", DUMMY_FILE]
      casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv,casts,SCRIPT_PATH) }
    end
  
- #  # This one results in an error: "OptionParser::InvalidArgument: invalid argument: --foo bar"
-#  # So it appears that this is tested in OptionParser already.
-#  test "Options.parse with type cast int and non-int value raises" do
-#    ["bar" ].each do |val| # what about nil, false, true, [], {}, 0.1 ?
-#      casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
-#      opt_parser = Options.new(casts)
-#      assert_raise(ArgumentError) { opt_parser.parse(["--foo", "#{val}"],SCRIPT_PATH) }
-#    end
-#  end
-
-   def test_Biopieces_options_parse_with_type_cast_int_dont_raise
+   test "Biopieces.options_parse with type cast int dont raise" do
      [0,-1,1,327649123746293746374276347824].each do |val|
        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
        casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -264,9 +248,7 @@ class BiopiecesTest < Test::Unit::TestCase
      end
    end
  
-   # TODO similar test for uint as "test "Options.parse with type cast int and non-int value raises" do"
-   def test_Biopieces_options_parse_with_type_cast_uint_dont_raise
+   test "Biopieces.options_parse with type cast uint dont raise" do
      [0,1,327649123746293746374276347824].each do |val|
        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
        casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
@@ -274,52 +256,51 @@ class BiopiecesTest < Test::Unit::TestCase
      end
    end
  
-   def test_Biopieces_options_parse_with_file_cast_and_file_dont_exists_raises
+   test "Biopieces.options_parse with file cast and file dont exists raises" do
      argv  = ["--foo", "bleh", "-I", DUMMY_FILE]
      casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
      assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
    end
  
-   def test_Biopieces_options_parse_with_file_cast_and_existing_file_dont_raise
+   test "Biopieces.options_parse with file cast and existing file dont raise" do
      argv  = ["--foo", DUMMY_FILE, "-I", DUMMY_FILE]
      casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
    end
  
-   def test_Biopieces_options_parse_with_files_cast_and_a_file_dont_exists_raises
+   test "Biopieces.options_parse with files cast and a file dont exists raises" do
      argv  = ["--foo", DUMMY_FILE + ",bleh", "-I", DUMMY_FILE]
      casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
      assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
    end
  
-   def test_Biopieces_options_parse_with_files_cast_and_files_exists_dont_raise
+   test "Biopieces.options_parse with files cast and files exists dont raise" do
      argv  = ["--foo", DUMMY_FILE + "," + DUMMY_FILE, "-I", DUMMY_FILE]
      casts = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
    end
  
-#   # TODO replace the absolute part below the file location with File.dirname(__FILE__)
-#   def test_Biopieces_options_parse_with_glob_argument_expands_correctly
-#     flunk("This test is flawed and need fixing")
-#     argv    = ["--foo", "/Users/maasha/unit_test/foo*,/Users/maasha/unit_test/my_dir/*.fna", "-I", DUMMY_FILE]
-#     casts   = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
-#     options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
-#     assert_equal(["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"], options[:foo])
-#   end
+   test "Biopieces.options_parse with glob argument expands correctly" do
+     path = File.join(ENV['BP_DIR'], "bp_test")
+     argv    = ["--foo", "#{path}/te*,#{path}/lib/*.sh", "-I", DUMMY_FILE]
+     casts   = [{:long=>"foo", :short=>"f", :type=>"files!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
+     options = Biopieces.options_parse(argv, casts, SCRIPT_PATH)
+     assert_equal(["#{path}/test_all", "#{path}/lib/test.sh"], options[:foo])
+   end
  
-   def test_Biopieces_options_parse_with_dir_cast_and_dir_dont_exists_raises
+   test "Biopieces.options_parse with dir cast and dir dont exists raises" do
      argv  = ["--foo", "bleh", "-I", DUMMY_FILE]
      casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
      assert_raise(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
    end
  
-   def test_Biopieces_options_parse_with_dir_cast_and_dir_exists_dont_raise
+   test "Biopieces.options_parse with dir cast and dir exists dont raise" do
      argv  = ["--foo", "/", "-I", DUMMY_FILE]
      casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
      assert_nothing_raised(ArgumentError) { Biopieces.options_parse(argv, casts, SCRIPT_PATH) }
    end
  
-   def test_Biopieces_options_parse_with_allowed_cast_and_not_allowed_value_raises
+   test "Biopieces.options_parse with allowed cast and not allowed value raises" do
      ["bleh", "2", "3.3"].each do |val|
        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
@@ -327,7 +308,7 @@ class BiopiecesTest < Test::Unit::TestCase
      end
    end
  
-   def test_Biopieces_options_parse_with_allowed_cast_and_allowed_values_dont_raise
+   test "Biopieces.options_parse with allowed cast and allowed values dont raise" do
      ["0", "-1", "0.0", "1", "bar"].each do |val|
        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>"0,-1,0.0,1,bar", :disallowed=>nil}]
@@ -335,7 +316,7 @@ class BiopiecesTest < Test::Unit::TestCase
      end
    end
  
-   def test_Biopieces_options_parse_with_disallowed_cast_and_disallowed_value_raises
+   test "Biopieces.options_parse with disallowed cast and disallowed value raises" do
      ["0", "-1", "0.0", "1", "bar"].each do |val|
        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]
@@ -343,7 +324,7 @@ class BiopiecesTest < Test::Unit::TestCase
      end
    end
  
-   def test_Biopieces_options_parse_with_disallowed_cast_and_allowed_values_dont_raise
+   test "Biopieces.options_parse with disallowed cast and allowed values dont raise" do
      ["bleh", "2", "3.3"].each do |val|
        argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
        casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>"0,-1,0.0,1,bar"}]