From b3c938a989548560997ae0bca4707b2faed0160d Mon Sep 17 00:00:00 2001 From: martinahansen Date: Fri, 28 May 2010 10:43:23 +0000 Subject: [PATCH] updating ruby code git-svn-id: http://biopieces.googlecode.com/svn/trunk@976 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/Maasha/lib/biopieces.rb | 7 ++- code_ruby/Maasha/test/test_biopieces.rb | 57 +++++++++++++++---------- code_ruby/Maasha/test/test_seq.rb | 2 +- 3 files changed, 41 insertions(+), 25 deletions(-) diff --git a/code_ruby/Maasha/lib/biopieces.rb b/code_ruby/Maasha/lib/biopieces.rb index b739ee7..75cb978 100644 --- a/code_ruby/Maasha/lib/biopieces.rb +++ b/code_ruby/Maasha/lib/biopieces.rb @@ -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 diff --git a/code_ruby/Maasha/test/test_biopieces.rb b/code_ruby/Maasha/test/test_biopieces.rb index 7a2ae14..1baa8e3 100755 --- a/code_ruby/Maasha/test/test_biopieces.rb +++ b/code_ruby/Maasha/test/test_biopieces.rb @@ -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 returns options['stream_in']=>[]" 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 returns options['stream_in']=>[]" 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 diff --git a/code_ruby/Maasha/test/test_seq.rb b/code_ruby/Maasha/test/test_seq.rb index 7eb8d23..91dd99f 100755 --- a/code_ruby/Maasha/test/test_seq.rb +++ b/code_ruby/Maasha/test/test_seq.rb @@ -1,6 +1,6 @@ #!/usr/bin/env ruby -require 'Maasha/lib/seq' +require 'seq' require 'test/unit' require 'pp' -- 2.39.5