]> git.donarmstrong.com Git - biopieces.git/commitdiff
updating ruby code
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Fri, 28 May 2010 10:43:23 +0000 (10:43 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Fri, 28 May 2010 10:43:23 +0000 (10:43 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@976 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/Maasha/lib/biopieces.rb
code_ruby/Maasha/test/test_biopieces.rb
code_ruby/Maasha/test/test_seq.rb

index b739ee7d4c1a1ea6d78368e6ce50e8b36f42d419..75cb9784639605aa0cc11d927222cc94c1bb2e17 100644 (file)
@@ -23,8 +23,11 @@ class Biopieces
   REGEX_STRING = /^(string|file|file!|dir|dir!|genome)$/
 
   # Initialize a Biopiece and write the status to file.
-  def initialize(no_status=nil)
+  # Options are for testing purposes only.
+  def initialize(no_status=nil,input=STDIN,output=STDOUT)
     status_set unless no_status
+    @input  = input
+    @output = output
   end
 
   # Check the integrity of a list of casts, followed by parsion options from argv
@@ -412,7 +415,7 @@ class Biopieces
     if @options["stream_out"]
       stream = write(@options["stream_out"], @options["compress"])
     else
-      stream = $stdout
+      stream = @output
     end
 
     stream
index 7a2ae14e1d40f4ac2481344f19030ffc84f0ab49..1baa8e3e800f3581977241520f22013126b6ae77 100755 (executable)
@@ -7,12 +7,15 @@ require 'biopieces'
 require 'pp'
 
 TYPES       = %w[flag string list int uint float file file! files files! dir dir! genome]
+DUMMY_FILE  = __FILE__
 SCRIPT_PATH = "write_fasta.rb"
 
 class OptionTest < Test::Unit::TestCase
 
   def setup
-    @bp = Biopieces.new(true)
+    @input  = StringIO.new
+    @output = StringIO.new
+    @bp     = Biopieces.new(true, @input, @output)
   end
 
   # >>>>>>>>>>>>>>>>>>>> Testing Options.new <<<<<<<<<<<<<<<<<<<<
@@ -197,40 +200,40 @@ class OptionTest < Test::Unit::TestCase
 #   end
 
   test "Biopieces#parse with --stream_in <arg> returns options['stream_in']=>[<arg>]" do
-    argv = ["--stream_in", __FILE__]
+    argv = ["--stream_in", DUMMY_FILE]
     options = @bp.parse(argv,[],SCRIPT_PATH)
-    assert_equal([__FILE__], options["stream_in"])
+    assert_equal([DUMMY_FILE], options["stream_in"])
   end
 
   test "Biopieces#parse with -I <arg> returns options['stream_in']=>[<arg>]" do
-    argv    = ["-I", __FILE__]
+    argv    = ["-I", DUMMY_FILE]
     casts   = []
     options = @bp.parse(argv, casts, SCRIPT_PATH)
-    assert_equal([__FILE__], options["stream_in"])
+    assert_equal([DUMMY_FILE], options["stream_in"])
   end
 
   test "Biopieces#parse use cast default value if no argument given" do
-    argv    = ["-I", __FILE__]
+    argv    = ["-I", DUMMY_FILE]
     casts   = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
     options = @bp.parse(argv, casts, SCRIPT_PATH)
     assert_equal(options["foo"], "bar")
   end
 
   test "Biopieces#parse don't use default value if argument given" do
-    argv    = ["--foo", "bleh", "-I", __FILE__]
+    argv    = ["--foo", "bleh", "-I", DUMMY_FILE]
     casts   = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>false, :default=>"bar", :allowed=>nil, :disallowed=>nil}]
     options = @bp.parse(argv, casts, SCRIPT_PATH)
     assert_equal(options["foo"], "bleh")
   end
 
   test "Biopieces#parse with mandatory cast and no argument raises" do
-    argv  = ["-I", __FILE__]
+    argv  = ["-I", DUMMY_FILE]
     casts = [{:long=>"foo", :short=>"f", :type=>"string", :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}]
     assert_raise(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
   end
 
   test "Biopieces#parse with mandatory cast and argument don't raise" do
-    argv  = ["--foo", "bar", "-I", __FILE__]
+    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) { @bp.parse(argv,casts,SCRIPT_PATH) }
   end
@@ -247,7 +250,7 @@ class OptionTest < Test::Unit::TestCase
 
   test "Biopieces#parse with type cast int don't raise" do
     [0,-1,1,327649123746293746374276347824].each do |val|
-      argv  = ["--foo", "#{val}", "-I", __FILE__]
+      argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
       casts = [{:long=>"foo", :short=>"f", :type=>"int", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
       assert_nothing_raised(ArgumentError) { @bp.parse(argv,casts,SCRIPT_PATH) }
     end
@@ -257,59 +260,59 @@ class OptionTest < Test::Unit::TestCase
 
   test "Biopieces#parse with type cast uint don't raise" do
     [0,1,327649123746293746374276347824].each do |val|
-      argv  = ["--foo", "#{val}", "-I", __FILE__]
+      argv  = ["--foo", "#{val}", "-I", DUMMY_FILE]
       casts = [{:long=>"foo", :short=>"f", :type=>"uint", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
       assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
     end
   end
 
   test "Biopieces#parse with file! cast and file don't exists raises" do
-    argv  = ["--foo", "bleh", "-I", __FILE__]
+    argv  = ["--foo", "bleh", "-I", DUMMY_FILE]
     casts = [{:long=>"foo", :short=>"f", :type=>"file!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
     assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
   end
 
   test "Biopieces#parse with file! cast and existing file don't raise" do
-    argv  = ["--foo", __FILE__, "-I", __FILE__]
+    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) { @bp.parse(argv, casts, SCRIPT_PATH) }
   end
 
   test "Biopieces#parse with files! cast and a file don't exists raises" do
-    argv  = ["--foo", __FILE__ + ",bleh", "-I", __FILE__]
+    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) { @bp.parse(argv, casts, SCRIPT_PATH) }
   end
 
   test "Biopieces#parse with files! cast and files exists don't raise" do
-    argv  = ["--foo", __FILE__ + "," + __FILE__, "-I", __FILE__]
+    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) { @bp.parse(argv, casts, SCRIPT_PATH) }
   end
 
   # TODO replace the absolute part below the file location with File.dirname(__FILE__)
   test "Biopieces#parse with glob argument expands correctly" do
-    argv    = ["--foo", "/Users/maasha/unit_test/foo*,/Users/maasha/unit_test/my_dir/*.fna", "-I", __FILE__]
+    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 = @bp.parse(argv, casts, SCRIPT_PATH)
     assert_equal(options["foo"], ["/Users/maasha/unit_test/foo.fna", "/Users/maasha/unit_test/my_dir/bar.fna"])
   end
 
   test "Biopieces#parse with dir! cast and dir don't exists raises" do
-    argv  = ["--foo", "bleh", "-I", __FILE__]
+    argv  = ["--foo", "bleh", "-I", DUMMY_FILE]
     casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
     assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
   end
 
   test "Biopieces#parse with dir! cast and dir exists don't raise" do
-    argv  = ["--foo", "/", "-I", __FILE__]
+    argv  = ["--foo", "/", "-I", DUMMY_FILE]
     casts = [{:long=>"foo", :short=>"f", :type=>"dir!", :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}]
     assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
   end
 
   test "Biopieces#parse with allowed cast and not allowed value raises" do
     ["bleh", "2", "3.3"].each do |val|
-      argv  = ["--foo", "#{val}", "-I", __FILE__]
+      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}]
       assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
     end
@@ -317,7 +320,7 @@ class OptionTest < Test::Unit::TestCase
 
   test "Biopieces#parse with allowed cast and allowed values don't raise" do
     ["0", "-1", "0.0", "1", "bar"].each do |val|
-      argv  = ["--foo", "#{val}", "-I", __FILE__]
+      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}]
       assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
     end
@@ -325,7 +328,7 @@ class OptionTest < Test::Unit::TestCase
 
   test "Biopieces#parse with disallowed cast and disallowed value raises" do
     ["0", "-1", "0.0", "1", "bar"].each do |val|
-      argv  = ["--foo", "#{val}", "-I", __FILE__]
+      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"}]
       assert_raise(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
     end
@@ -333,9 +336,19 @@ class OptionTest < Test::Unit::TestCase
 
   test "Biopieces#parse with disallowed cast and allowed values don't raise" do
     ["bleh", "2", "3.3"].each do |val|
-      argv  = ["--foo", "#{val}", "-I", __FILE__]
+      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"}]
       assert_nothing_raised(ArgumentError) { @bp.parse(argv, casts, SCRIPT_PATH) }
     end
   end
+
+  test "Biopieces#puts outputs correctly" do
+    argv    = []
+    casts   = []
+    @bp.expects(:print_usage_and_exit).with()
+    options = @bp.parse(argv, casts, SCRIPT_PATH)
+    record  = {"foo" => "bar"}
+
+    assert_equal("foo: bar\n---\n", @bp.puts(record))
+  end
 end
index 7eb8d2326d67793f5b79521a974f1e9840d2a547..91dd99fe86b02f9912f4450f2b4f363608459669 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env ruby
 
-require 'Maasha/lib/seq'
+require 'seq'
 require 'test/unit'
 require 'pp'