File: test_command.rb

package info (click to toggle)
ruby-bio 2.0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,108 kB
  • sloc: ruby: 68,331; perl: 13; makefile: 11; sh: 1
file content (356 lines) | stat: -rw-r--r-- 11,507 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#
# test/unit/bio/test_command.rb - Unit test for Bio::Command
#
# Copyright::	Copyright (C) 2005-2008
#               Mitsuteru Nakao <n@bioruby.org>,
# 		Naohisa Goto <ng@bioruby.org>,
#		Toshiaki Katayama <k@bioruby.org>
# License::	The Ruby License
#
#  $Id:$
#

# loading helper routine for testing bioruby
require 'pathname'
load Pathname.new(File.join(File.dirname(__FILE__), ['..'] * 2,
                            'bioruby_test_helper.rb')).cleanpath.to_s

# libraries needed for the tests
require 'test/unit'
require 'bio/command'

module Bio
  class TestCommand < Test::Unit::TestCase

    def windows_platform?
      Bio::Command.module_eval { windows_platform? }
    end
    private :windows_platform?
    
    def test_command_constants
      assert(Bio::Command::UNSAFE_CHARS_UNIX)
      assert(Bio::Command::QUOTE_CHARS_WINDOWS)
      assert(Bio::Command::UNESCAPABLE_CHARS)
    end

    def test_escape_shell_windows
      str = "bio_ruby.123@456:789"
      assert_equal("bio_ruby.123@456:789",
                   Bio::Command.escape_shell_windows(str))
      str = "bio\'\"r u\"b\\y123@456:789"
      assert_equal("\"bio'\"\"r u\"\"b\\y123@456:789\"",
                   Bio::Command.escape_shell_windows(str))
    end

    def test_escape_shell_unix
      str = "bio_ruby.123@456:789"
      assert_equal("bio_ruby.123@456:789",
                   Bio::Command.escape_shell_unix(str))
      str = "bio\'\"r u\"b\\y123@456:789"
      assert_equal("bio\\'\\\"r\\ u\\\"b\\\\y123@456:789",
                   Bio::Command.escape_shell_unix(str))
    end

    def test_escape_shell
      str = "bio_ruby.123@456:789"
      assert_equal("bio_ruby.123@456:789",
                   Bio::Command.escape_shell(str))
      str = "bio\'\"r u\"b\\y123@456:789"
      if windows_platform?
        # mswin32, bccwin32, mingw32, etc.
        assert_equal("\"bio'\"\"r u\"\"b\\y123@456:789\"",
                     Bio::Command.escape_shell(str))
      else
        assert_equal("bio\\'\\\"r\\ u\\\"b\\\\y123@456:789",
                     Bio::Command.escape_shell(str))
      end
    end

    def test_make_command_line
      ary = [ "ruby", 
        "test.rb", "atgcatgc", "bio\'\"r u\"b\\y123@456:789" ]
      if windows_platform?
        # mswin32, bccwin32, mingw32, etc.
        assert_equal("ruby" + 
                       " test.rb atgcatgc" + 
                       " \"bio'\"\"r u\"\"b\\y123@456:789\"",
                     Bio::Command.make_command_line(ary))
      else
        assert_equal("ruby" + 
                       " test.rb atgcatgc" + 
                       " bio\\'\\\"r\\ u\\\"b\\\\y123@456:789",
                     Bio::Command.make_command_line(ary))
      end
    end

    def test_make_command_line_windows
      ary = [ "C:\\Program Files\\Ruby\\bin\\ruby.exe", 
        "test.rb", "atgcatgc", "bio\'\"r u\"b\\y123@456:789" ]
      assert_equal("\"C:\\Program Files\\Ruby\\bin\\ruby.exe\"" + 
                     " test.rb atgcatgc" + 
                     " \"bio'\"\"r u\"\"b\\y123@456:789\"",
                   Bio::Command.make_command_line_windows(ary))
    end

    def test_make_command_line_unix
      ary = [ "/usr/local/bin/ruby", 
        "test.rb", "atgcatgc", "bio\'\"r u\"b\\y123@456:789" ]
      assert_equal("/usr/local/bin/ruby" + 
                     " test.rb atgcatgc" +
                     " bio\\'\\\"r\\ u\\\"b\\\\y123@456:789",
                   Bio::Command.make_command_line_unix(ary))
    end

    def test_safe_command_line_array
      ary1 = [ 'test' ]
      assert_equal([ [ 'test', 'test' ] ],
                   Bio::Command.safe_command_line_array(ary1))
      ary1a = [ [ 'test/test1a', 'test' ] ]
      assert_equal(ary1a,
                   Bio::Command.safe_command_line_array(ary1a))
    end

    def test_safe_command_line_array_passthrough
      ary0 = []
      assert_equal(ary0,
                   Bio::Command.safe_command_line_array(ary0))
      ary2 = [ 'cmd', 'arg0' ]
      assert_equal(ary2,
                   Bio::Command.safe_command_line_array(ary2))
      ary2a = [ [ 'cmd', 'display name' ], 'arg0' ]
      assert_equal(ary2a,
                   Bio::Command.safe_command_line_array(ary2a))
      ary3 = [ 'cmd', 'arg0', 'arg1' ]
      assert_equal(ary3,
                   Bio::Command.safe_command_line_array(ary3))
    end

    def test_make_cgi_params_by_hash_in_symbol
      ary = [
             "type1=bp",
             "type2=bp",
             "downstream=",
             "upstream=",
             "format=fasta",
             "options=similarity",
             "options=gene",
             "action=export",
             "_format=Text",
             "output=txt",
             "submit=Continue+%3E%3E",
             "ab%3Dcd%26ef%3Dgh%23ij=pq%3D12%26rs%3D34%23tu",
            ]
      hash = {
        :type1 => 'bp',
        :type2 => 'bp',
        :downstream => '',
        :upstream => '',
        :format => 'fasta',
        :options => ['similarity', 'gene'],
        :action => 'export',
        :_format => 'Text',
        :output => 'txt',
        :submit => 'Continue >>',
        :"ab=cd&ef=gh#ij" => 'pq=12&rs=34#tu',
      }
      result = Bio::Command.make_cgi_params(hash)
      ary.each do |str|
        assert_match(Regexp.new(Regexp.escape(str)), result)
      end

      # round-trip test
      result_hash = {}
      CGI.parse(result).each do |k, v|
        v = case v.size
            when 0
              ''
            when 1
              v[0]
            else
              v
            end
        result_hash[k.intern] = v
      end
      assert_equal(hash, result_hash)
    end

    def test_make_cgi_params_by_hash_in_string
      ary = [
             "type1=bp",
             "type2=bp",
             "downstream=",
             "upstream=",
             "format=fasta",
             "options=similarity",
             "options=gene",
             "action=export",
             "_format=Text",
             "output=txt",
             "submit=Continue+%3E%3E",
             "ab%3Dcd%26ef%3Dgh%23ij=pq%3D12%26rs%3D34%23tu",
            ]
      hash = {
        "type1" => 'bp',
        "type2" => 'bp',
        "downstream" => '',
        "upstream" => '',
        "format" => 'fasta',
        "options" => ['similarity', 'gene'],
        "action" => 'export',
        "_format" => 'Text',
        "output" => 'txt',
        "submit" => 'Continue >>',
        'ab=cd&ef=gh#ij' => 'pq=12&rs=34#tu',
      }
      result = Bio::Command.make_cgi_params(hash)
      ary.each do |str|
        assert_match(Regexp.new(Regexp.escape(str)), result)
      end

      # round-trip test
      result_hash = {}
      CGI.parse(result).each do |k, v|
        v = case v.size
            when 0
              ''
            when 1
              v[0]
            else
              v
            end
        result_hash[k] = v
      end
      assert_equal(hash, result_hash)
    end

    def test_make_cgi_params_by_array_of_array
      ary = [
             "type1=bp",
             "type2=bp",
             "downstream=",
             "upstream=",
             "format=fasta",
             "options=similarity",
             "options=gene",
             "action=export",
             "_format=Text",
             "output=txt",
             "submit=Continue+%3E%3E",
             "ab%3Dcd%26ef%3Dgh%23ij=pq%3D12%26rs%3D34%23tu",
            ]
      array_of_array = [
        ["type1", 'bp'],
        ["type2", 'bp'], 
        ["downstream", ''],
        ["upstream", ''],
        ["format", 'fasta'],
        ["options", ['similarity', 'gene']],
        ["action", 'export'],
        ["_format", 'Text'],
        ["output", 'txt'],
        ["submit", 'Continue >>'],
        [ 'ab=cd&ef=gh#ij', 'pq=12&rs=34#tu' ],
      ]
      result = Bio::Command.make_cgi_params(array_of_array)
      # When array of array, order is guaranteed.
      assert_equal(ary.join('&'), result)

      # round-trip test
      result_array = []
      CGI.parse(result).each do |k, v|
        v = case v.size
            when 0
              ''
            when 1
              v[0]
            else
              v
            end
        result_array.push([ k, v ])
      end
      assert_equal(array_of_array.sort, result_array.sort)
    end

    def test_make_cgi_params_by_array_of_hash
      ary = [
             "type1=bp",
             "type2=bp",
             "downstream=",
             "upstream=",
             "format=fasta",
             "options=similarity",
             "options=gene",
             "action=export",
             "_format=Text",
             "output=txt",
             "submit=Continue+%3E%3E",
             "ab%3Dcd%26ef%3Dgh%23ij=pq%3D12%26rs%3D34%23tu",
            ]
      array_of_hash = [
                       {"type1" => 'bp'},
                       {"type2" => 'bp'},
                       {"downstream" => ''},
                       {"upstream" => ''},
                       {"format" => 'fasta'},
                       {"options" => ['similarity', 'gene']},
                       {"action" => 'export'},
                       {"_format" => 'Text'},
                       {"output" => 'txt'},
                       {"submit" => 'Continue >>'},
                       {'ab=cd&ef=gh#ij' => 'pq=12&rs=34#tu'},
                      ]
      result = Bio::Command.make_cgi_params(array_of_hash)
      # When array of hash, order is guaranteed.
      assert_equal(ary.join('&'), result)

      # round-trip test
      result_array = []
      CGI.parse(result).each do |k, v|
        v = case v.size
            when 0
              ''
            when 1
              v[0]
            else
              v
            end
        result_array.push({ k => v })
      end
      assert_equal(array_of_hash.sort { |x,y| x.keys[0] <=> y.keys[0] },
                   result_array.sort { |x,y| x.keys[0] <=> y.keys[0] })
    end

    def test_make_cgi_params_by_array_of_string
      str = "type1=bp&type2=bp&downstream=&upstream=&format=fasta&options=similarity&options=gene&action=export&_format=Text&output=txt&submit=Continue+%3E%3E&ab=cd%26ef%3Dgh%23ij%3Dpq%3D12%26rs%3D34%23tu"
      array_of_string = [
                         "type1=bp",
                         "type2=bp",
                         "downstream=",
                         "upstream=",
                         "format=fasta",
                         "options=similarity",
                         "options=gene",
                         "action=export",
                         "_format=Text",
                         "output=txt",
                         "submit=Continue >>",
                         # In the following case, 'ab' is regarded as
                         # the form key, and rest of the string is
                         # regarded as the value.
                         'ab=cd&ef=gh#ij=pq=12&rs=34#tu',
                        ]
      result = Bio::Command.make_cgi_params(array_of_string)
      assert_equal(str, result)
    end

    def test_make_cgi_params_by_string
      ##Before BioRuby 1.4.3.0001, only URI escaping was performed.
      #string = "type1=bp&type2=bp&downstream=&upstream=&format=fasta&options=similarity&options=gene&action=export&_format=Text&output=txt&submit=Continue%20%3E%3E"
      query = " type1=bp&type2=bp&downstream=&upstream=&format=fasta&options=similarity&options=gene&action=export&_format=Text&output=txt&submit=Continue >> "
      assert_raise(TypeError) {
        Bio::Command.make_cgi_params(query)
      }
    end

  end
end