bioinform 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -17
- data/Gemfile +4 -4
- data/LICENSE +21 -21
- data/README.md +29 -29
- data/Rakefile +5 -12
- data/bioinform.gemspec +21 -21
- data/lib/bioinform/data_models/collection.rb +2 -0
- data/lib/bioinform/data_models/{iupac.rb → old_style_models_TO_BE_REMOVED/iupac.rb} +1 -1
- data/lib/bioinform/data_models/{iupac_word.rb → old_style_models_TO_BE_REMOVED/iupac_word.rb} +0 -0
- data/lib/bioinform/data_models/{positional_count_matrix.rb → old_style_models_TO_BE_REMOVED/positional_count_matrix.rb} +0 -0
- data/lib/bioinform/data_models/{positional_matrix.rb → old_style_models_TO_BE_REMOVED/positional_matrix.rb} +3 -5
- data/lib/bioinform/data_models/{positional_probability_matrix.rb → old_style_models_TO_BE_REMOVED/positional_probability_matrix.rb} +0 -0
- data/lib/bioinform/data_models/{positional_weight_matrix.rb → old_style_models_TO_BE_REMOVED/positional_weight_matrix.rb} +0 -0
- data/lib/bioinform/data_models/parser.rb +41 -0
- data/lib/bioinform/data_models/parsers/array_parser.rb +17 -0
- data/lib/bioinform/data_models/parsers/hash_parser.rb +19 -0
- data/lib/bioinform/data_models/parsers/string_fantom_parser.rb +21 -0
- data/lib/bioinform/data_models/parsers/string_parser.rb +45 -0
- data/lib/bioinform/data_models/parsers.rb +4 -0
- data/lib/bioinform/data_models/pcm.rb +7 -0
- data/lib/bioinform/data_models/pm.rb +195 -0
- data/lib/bioinform/data_models/ppm.rb +8 -0
- data/lib/bioinform/data_models/pwm.rb +23 -0
- data/lib/bioinform/data_models.rb +5 -5
- data/lib/bioinform/support/callable_symbol.rb +33 -4
- data/lib/bioinform/support/collect_hash.rb +7 -0
- data/lib/bioinform/support/curry_except_self.rb +2 -2
- data/lib/bioinform/support/deep_dup.rb +5 -0
- data/lib/bioinform/support/delete_many.rb +14 -0
- data/lib/bioinform/support/has_keys.rb +14 -0
- data/lib/bioinform/support/inverf.rb +13 -0
- data/lib/bioinform/support/partial_sums.rb +6 -0
- data/lib/bioinform/support/{same.rb → same_by.rb} +1 -1
- data/lib/bioinform/support.rb +13 -5
- data/lib/bioinform/version.rb +3 -3
- data/lib/bioinform.rb +8 -7
- data/spec/data_models/parser_spec.rb +46 -0
- data/spec/data_models/parsers/array_parser_spec.rb +53 -0
- data/spec/data_models/parsers/hash_parser_spec.rb +60 -0
- data/spec/data_models/parsers/string_fantom_parser_spec.rb +38 -0
- data/spec/data_models/parsers/string_parser_spec.rb +112 -0
- data/spec/data_models/pm_spec.rb +369 -0
- data/spec/data_models/pwm_spec.rb +25 -0
- data/spec/spec_helper.rb +30 -0
- data/spec/support/callable_symbol_spec.rb +66 -0
- data/spec/support/collect_hash_spec.rb +15 -0
- data/spec/support/curry_except_self_spec.rb +9 -0
- data/spec/support/delete_many_spec.rb +44 -0
- data/spec/support/has_keys_spec.rb +48 -0
- data/spec/support/inverf_spec.rb +19 -0
- data/spec/support/multiline_squish_spec.rb +11 -0
- data/spec/support/partial_sums_spec.rb +9 -0
- data/spec/support/same_by_spec.rb +36 -0
- metadata +60 -21
- data/lib/bioinform/support/pmap.rb +0 -10
- data/lib/bioinform/support/ptap.rb +0 -7
- data/spec/callable_symbol_spec.rb +0 -37
- data/spec/pmap_test.rb +0 -24
- data/spec/positional_matrix_spec.rb +0 -169
- data/spec/ptap_spec.rb +0 -17
- data/spec/same_spec.rb +0 -19
@@ -1,11 +1,40 @@
|
|
1
1
|
require 'bioinform/support/curry_except_self'
|
2
2
|
|
3
|
+
# Useful extension for &:symbol - syntax to make it possible to pass arguments for method in block
|
4
|
+
# ['abc','','','def','ghi'].tap(&:delete.('')) # ==> ['abc','def','ghi']
|
5
|
+
# [1,2,3].map(&:to_s.(2)) # ==> ['1','10','11']
|
6
|
+
# ['abc','cdef','xy','z','wwww'].select(&:size.() == 4) # ==> ['cdef', 'wwww']
|
7
|
+
# ['abc','aaA','AaA','z'].count(&:upcase.().succ == 'AAB') # ==> 2
|
8
|
+
# [%w{1 2 3 4 5},%w{6 7 8 9}].map(&:join.().length) # ==> [5,4]
|
3
9
|
class Symbol
|
4
|
-
def call(*args)
|
5
|
-
obj=
|
6
|
-
|
7
|
-
@
|
10
|
+
def call(*args, &block)
|
11
|
+
obj=BasicObject.new.instance_exec(self,args,block) do |meth,params,block|
|
12
|
+
@postprocess_meth = [meth]
|
13
|
+
@postprocess_args = [params]
|
14
|
+
@postprocess_block = [block]
|
15
|
+
self
|
8
16
|
end
|
17
|
+
|
18
|
+
def obj.to_proc
|
19
|
+
# proc/lambda are methods that cannot be used in BasicObject. It's possible to use ->(slf){...} syntax since ruby-1.9.3 p194 but it's too modern and not widely spreaded yet
|
20
|
+
::Proc.new do |slf|
|
21
|
+
@postprocess_meth.zip(@postprocess_args,@postprocess_block).inject(slf) do |result,(call_meth,call_args,call_block)|
|
22
|
+
result.send(call_meth, *call_args,&call_block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def obj.method_missing(meth,*args,&block)
|
28
|
+
@postprocess_meth << meth
|
29
|
+
@postprocess_args << args
|
30
|
+
@postprocess_block << block
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def obj.==(other)
|
35
|
+
method_missing(:==, other)
|
36
|
+
end
|
37
|
+
|
9
38
|
obj
|
10
39
|
end
|
11
40
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Array
|
2
|
+
def delete_at_many(*indices)
|
3
|
+
indices.uniq.sort.reverse.each{|ind| delete_at ind}
|
4
|
+
end
|
5
|
+
def delete_many(*elements)
|
6
|
+
elements.each{|el| delete el}
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Hash
|
11
|
+
def delete_many(*keys)
|
12
|
+
keys.each{|el| delete el}
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Hash
|
2
|
+
def has_all_keys?(*keys)
|
3
|
+
keys.all?{|key| has_key? key}
|
4
|
+
end
|
5
|
+
def has_any_key?(*keys)
|
6
|
+
keys.any?{|key| has_key? key}
|
7
|
+
end
|
8
|
+
def has_none_key?(*keys)
|
9
|
+
keys.none?{|key| has_key? key}
|
10
|
+
end
|
11
|
+
def has_one_key?(*keys)
|
12
|
+
keys.one?{|key| has_key? key}
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Math
|
2
|
+
def self.inverf(x)
|
3
|
+
sign = x < 0 ? -1 : 1
|
4
|
+
x = x.abs
|
5
|
+
a = 8 / (3*Math::PI) * (Math::PI-3) / (4-Math::PI)
|
6
|
+
part0 = ( 2/(Math::PI*a) + (Math.log(1-x*x)) / 2 )**2
|
7
|
+
part = -2 / (Math::PI * a) - Math.log(1-x*x)/2 + Math.sqrt(-1/a * Math.log(1-x*x) + part0)
|
8
|
+
sign * Math.sqrt(part)
|
9
|
+
end
|
10
|
+
def inverf(x)
|
11
|
+
Math.inverf(x)
|
12
|
+
end
|
13
|
+
end
|
data/lib/bioinform/support.rb
CHANGED
@@ -1,7 +1,15 @@
|
|
1
|
-
require '
|
1
|
+
require 'active_support/core_ext/string/filters'
|
2
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
3
|
+
|
2
4
|
require 'bioinform/support/callable_symbol'
|
3
|
-
require 'bioinform/support/
|
4
|
-
require 'bioinform/support/
|
5
|
-
require 'bioinform/support/
|
5
|
+
require 'bioinform/support/collect_hash'
|
6
|
+
require 'bioinform/support/curry_except_self'
|
7
|
+
require 'bioinform/support/delete_many'
|
8
|
+
require 'bioinform/support/has_keys'
|
9
|
+
require 'bioinform/support/multiline_squish'
|
10
|
+
require 'bioinform/support/same_by'
|
6
11
|
require 'bioinform/support/yaml_dump_file'
|
7
|
-
require 'bioinform/support/
|
12
|
+
require 'bioinform/support/inverf'
|
13
|
+
require 'bioinform/support/deep_dup'
|
14
|
+
|
15
|
+
require 'bioinform/support/partial_sums'
|
data/lib/bioinform/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Bioinform
|
2
|
-
VERSION = "0.0
|
3
|
-
end
|
1
|
+
module Bioinform
|
2
|
+
VERSION = "0.1.0"
|
3
|
+
end
|
data/lib/bioinform.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
require 'bioinform/version'
|
2
|
-
require 'bioinform/support'
|
3
|
-
require 'bioinform/data_models'
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
require 'bioinform/version'
|
2
|
+
require 'bioinform/support'
|
3
|
+
require 'bioinform/data_models'
|
4
|
+
require 'bioinform/data_models/parsers'
|
5
|
+
|
6
|
+
module Bioinform
|
7
|
+
# Your code goes here...
|
8
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'bioinform/data_models/parser'
|
3
|
+
|
4
|
+
module Bioinform
|
5
|
+
describe PM::Parser do
|
6
|
+
include PM::Parser::Helpers
|
7
|
+
|
8
|
+
before :each do
|
9
|
+
parser_stub :ParserBad, false, { matrix: [[0,0,0,0],[1,1,1,1]], name: 'Bad' }
|
10
|
+
parser_stub :ParserGood, true, { matrix: [[1,1,1,1],[1,1,1,1]], name: 'Good' }
|
11
|
+
end
|
12
|
+
after :each do
|
13
|
+
parser_subclasses_cleanup
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when subklass created' do
|
17
|
+
it 'PM::Parser.subclasses should contain all subclasses' do
|
18
|
+
PM::Parser.subclasses.should include ParserBad
|
19
|
+
PM::Parser.subclasses.should include ParserGood
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#initialize' do
|
24
|
+
it 'should save argument `input`'do
|
25
|
+
parser = ParserGood.new('my stub input')
|
26
|
+
parser.input.should == 'my stub input'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#parse' do
|
31
|
+
it 'should raise an error unless reimplemented' do
|
32
|
+
parser = PM::Parser.new('my stub input')
|
33
|
+
expect{ parser.parse }.to raise_error
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'in a subclass' do
|
37
|
+
it '!!! USELESS TEST - see TODO and make LINT for subclasses!!! should return hash with key `matrix`' do
|
38
|
+
parser = ParserGood.new('my stub input')
|
39
|
+
parse_result = parser.parse
|
40
|
+
parse_result.should be_kind_of Hash
|
41
|
+
parse_result.should have_key :matrix
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'bioinform/data_models/parsers/array_parser'
|
3
|
+
|
4
|
+
module Bioinform
|
5
|
+
describe ArrayParser do
|
6
|
+
before :each do
|
7
|
+
@matrix = [[1,4,7,10], [2,5,8,11], [3,6,9,12]]
|
8
|
+
|
9
|
+
@valid_input = [[1,4,7,10], [2,5,8,11], [3,6,9,12]]
|
10
|
+
@valid_input_transposed = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]]
|
11
|
+
|
12
|
+
@invalid_array_of_hashes = [{A: 1, C: 4, G: 7, T: 10}, {A: 2, C: 5, G: 8, T: 11}, {A: 3, C: 6, G: 9, T: 12}]
|
13
|
+
@invalid_input_hash = {A: [1,2,3], C: [4,5,6], G: [7,8,9], T: [10,11,12]}
|
14
|
+
|
15
|
+
@invalid_input_array_different_size = [[1,4,7,10], [2,5,8,11], [3,6]]
|
16
|
+
@invalid_input_array_transposed_different_size = [[1,2,3],[4,5,6],[7,8,9],[10]]
|
17
|
+
|
18
|
+
@invalid_not_hash_string = "1 2 3\n4 5 6\n7 8 9\n10 11 12"
|
19
|
+
@invalid_not_hash_string_transposed = "1 2 3 4\n5 6 7 8\n9 10 11 12"
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#can_parse?' do
|
23
|
+
it 'should be true for a valid array' do
|
24
|
+
ArrayParser.new(@valid_input).can_parse?.should be_true
|
25
|
+
ArrayParser.new(@valid_input_transposed).can_parse?.should be_true
|
26
|
+
end
|
27
|
+
it 'should be false for invalid input' do
|
28
|
+
ArrayParser.new(@invalid_array_of_hashes).can_parse?.should be_false
|
29
|
+
ArrayParser.new(@invalid_input_hash).can_parse?.should be_false
|
30
|
+
|
31
|
+
ArrayParser.new(@invalid_input_array_different_size).can_parse?.should be_false
|
32
|
+
ArrayParser.new(@invalid_input_array_transposed_different_size).can_parse?.should be_false
|
33
|
+
|
34
|
+
ArrayParser.new(@invalid_not_hash_string).can_parse?.should be_false
|
35
|
+
ArrayParser.new(@invalid_not_hash_string_transposed).can_parse?.should be_false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
describe '#parse' do
|
39
|
+
it 'should raise an ArgumentError for invalid input' do
|
40
|
+
expect{ ArrayParser.new(@invalid_array_of_hashes).parse }.to raise_error ArgumentError
|
41
|
+
expect{ ArrayParser.new(@invalid_input_hash).parse }.to raise_error ArgumentError
|
42
|
+
expect{ ArrayParser.new(@invalid_input_array_different_size).parse }.to raise_error ArgumentError
|
43
|
+
expect{ ArrayParser.new(@invalid_input_array_transposed_different_size).parse }.to raise_error ArgumentError
|
44
|
+
expect{ ArrayParser.new(@invalid_not_hash_string).parse }.to raise_error ArgumentError
|
45
|
+
expect{ ArrayParser.new(@invalid_not_hash_string_transposed).parse }.to raise_error ArgumentError
|
46
|
+
end
|
47
|
+
it 'should return hash with `matrix` key for valid input' do
|
48
|
+
ArrayParser.new(@valid_input).parse.should == {matrix: @matrix}
|
49
|
+
ArrayParser.new(@valid_input_transposed).parse.should == {matrix: @matrix}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'bioinform/data_models/parsers/hash_parser'
|
3
|
+
|
4
|
+
module Bioinform
|
5
|
+
describe HashParser do
|
6
|
+
before :each do
|
7
|
+
@matrix = [[1,4,7,10], [2,5,8,11], [3,6,9,12]]
|
8
|
+
|
9
|
+
@valid_input_symbolic_keys = {A: [1,2,3], C: [4,5,6], G: [7,8,9], T: [10,11,12]}
|
10
|
+
@valid_input_string_keys = {'A' => [1,2,3], 'C' => [4,5,6], 'G' => [7,8,9], 'T' => [10,11,12]}
|
11
|
+
@valid_array_of_hashes = [{A: 1, C: 4, G: 7, T: 10}, {A: 2, C: 5, G: 8, T: 11}, {A: 3, C: 6, G: 9, T: 12}]
|
12
|
+
|
13
|
+
@invalid_array_of_hashes_missing_keys = [{A: 1, C: 4, G: 7, T: 10}, {C: 5, T: 11}, {A: 3, C: 6, G: 9, T: 12}]
|
14
|
+
|
15
|
+
@invalid_input_different_length = {A: [1,2,3], C: [4,5,6], G: [7,8,9], T: [10]}
|
16
|
+
@invalid_input_not_all_keys = {A: [1,2,3], C: [4,5,6], G: [7,8,9]}
|
17
|
+
|
18
|
+
@invalid_not_hash_array = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]]
|
19
|
+
@invalid_not_hash_array_transposed = [[1,4,7,10], [2,5,8,11], [3,6,9,12]]
|
20
|
+
|
21
|
+
@invalid_not_hash_string = "1 2 3\n4 5 6\n7 8 9\n10 11 12"
|
22
|
+
@invalid_not_hash_string_transposed = "1 2 3 4\n5 6 7 8\n9 10 11 12"
|
23
|
+
end
|
24
|
+
describe '#can_parse?' do
|
25
|
+
it 'should be true for a valid hash or array of hashes' do
|
26
|
+
HashParser.new(@valid_input_symbolic_keys).can_parse?.should be_true
|
27
|
+
HashParser.new(@valid_input_string_keys).can_parse?.should be_true
|
28
|
+
HashParser.new(@valid_array_of_hashes).can_parse?.should be_true
|
29
|
+
end
|
30
|
+
it 'should be false for invalid input' do
|
31
|
+
HashParser.new(@invalid_array_of_hashes_missing_keys).can_parse?.should be_false
|
32
|
+
HashParser.new(@invalid_input_different_length).can_parse?.should be_false
|
33
|
+
HashParser.new(@invalid_input_not_all_keys).can_parse?.should be_false
|
34
|
+
|
35
|
+
HashParser.new(@invalid_not_hash_array).can_parse?.should be_false
|
36
|
+
HashParser.new(@invalid_not_hash_array_transposed).can_parse?.should be_false
|
37
|
+
|
38
|
+
HashParser.new(@invalid_not_hash_string).can_parse?.should be_false
|
39
|
+
HashParser.new(@invalid_not_hash_string_transposed).can_parse?.should be_false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
describe '#parse' do
|
43
|
+
it 'should raise an ArgumentError for invalid input' do
|
44
|
+
expect{ HashParser.new(@invalid_input_different_length).parse }.to raise_error ArgumentError
|
45
|
+
expect{ HashParser.new(@invalid_input_not_all_keys).parse }.to raise_error ArgumentError
|
46
|
+
|
47
|
+
expect{ HashParser.new(@invalid_not_hash_array).parse }.to raise_error ArgumentError
|
48
|
+
expect{ HashParser.new(@invalid_not_hash_array_transposed).parse }.to raise_error ArgumentError
|
49
|
+
|
50
|
+
expect{ HashParser.new(@invalid_not_hash_string).parse }.to raise_error ArgumentError
|
51
|
+
expect{ HashParser.new(@invalid_not_hash_string_transposed).parse }.to raise_error ArgumentError
|
52
|
+
end
|
53
|
+
it 'should return hash with `matrix` key for valid input' do
|
54
|
+
HashParser.new(@valid_input_symbolic_keys).parse.should == {matrix: @matrix}
|
55
|
+
HashParser.new(@valid_input_string_keys).parse.should == {matrix: @matrix}
|
56
|
+
HashParser.new(@valid_array_of_hashes).parse.should == {matrix: @matrix}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'bioinform/data_models/parsers/string_fantom_parser'
|
3
|
+
|
4
|
+
module Bioinform
|
5
|
+
describe StringFantomParser do
|
6
|
+
before :each do
|
7
|
+
@matrix = [[0.0, 1878368.0, 0.0, 0.0],
|
8
|
+
[0.0, 0.0, 0.0, 1878368.0],
|
9
|
+
[469592.0, 469592.0, 469592.0, 469592.0],
|
10
|
+
[0.0, 1878368.0, 0.0, 0.0],
|
11
|
+
[1878368.0, 0.0, 0.0, 0.0],
|
12
|
+
[0.0, 0.0, 1878368.0, 0.0]]
|
13
|
+
|
14
|
+
@good_input = <<-EOS
|
15
|
+
NA motif_CTNCAG
|
16
|
+
P0 A C G T
|
17
|
+
P1 0 1878368 0 0
|
18
|
+
P2 0 0 0 1878368
|
19
|
+
P3 469592 469592 469592 469592
|
20
|
+
P4 0 1878368 0 0
|
21
|
+
P5 1878368 0 0 0
|
22
|
+
P6 0 0 1878368 0
|
23
|
+
EOS
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#can_parse?' do
|
27
|
+
it 'should parse particular kind of string' do
|
28
|
+
StringFantomParser.new(@good_input).can_parse?.should be_true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#parse' do
|
33
|
+
it 'should parse particular kind of string' do
|
34
|
+
StringFantomParser.new(@good_input).parse.should == { matrix: @matrix, name: 'motif_CTNCAG'}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'bioinform/data_models/parsers/string_parser'
|
3
|
+
|
4
|
+
module Bioinform
|
5
|
+
describe StringParser do
|
6
|
+
before :each do
|
7
|
+
@matrix = [[1.23, 4.56, 7.8, 9.0],[9.0, -8.7, 6.54, -3210.0]]
|
8
|
+
|
9
|
+
@input_with_name = <<-EOS
|
10
|
+
Testmatrix
|
11
|
+
1.23 4.56 7.8 9.0
|
12
|
+
9 -8.7 6.54 -3210
|
13
|
+
EOS
|
14
|
+
|
15
|
+
@input_with_name_with_introduction_sign = <<-EOS
|
16
|
+
> Testmatrix
|
17
|
+
1.23 4.56 7.8 9.0
|
18
|
+
9 -8.7 6.54 -3210
|
19
|
+
EOS
|
20
|
+
|
21
|
+
@input_with_name_with_special_characters = <<-EOS
|
22
|
+
Testmatrix_first:subname+sub-subname
|
23
|
+
1.23 4.56 7.8 9.0
|
24
|
+
9 -8.7 6.54 -3210
|
25
|
+
EOS
|
26
|
+
|
27
|
+
@input_with_tabs_and_multiple_spaces = <<-EOS
|
28
|
+
> \tTestmatrix
|
29
|
+
1.23 \t 4.56 7.8\t\t 9.0
|
30
|
+
9 -8.7 6.54 -3210
|
31
|
+
EOS
|
32
|
+
|
33
|
+
@input_with_windows_crlf = <<-EOS
|
34
|
+
> Testmatrix\r
|
35
|
+
1.23 4.56 7.8 9.0
|
36
|
+
9 -8.7 6.54 -3210\r
|
37
|
+
EOS
|
38
|
+
|
39
|
+
@input_with_leading_and_finishing_spaces_and_newlines = <<-EOS
|
40
|
+
\n\n
|
41
|
+
\t1.23 4.56 7.8 9.0
|
42
|
+
9 -8.7 6.54 -3210
|
43
|
+
\t\r\n\t\n
|
44
|
+
EOS
|
45
|
+
|
46
|
+
@input_without_name = <<-EOS
|
47
|
+
1.23 4.56 7.8 9.0
|
48
|
+
9 -8.7 6.54 -3210
|
49
|
+
EOS
|
50
|
+
|
51
|
+
@input_transposed = <<-EOS
|
52
|
+
1.23 9
|
53
|
+
4.56 -8.7
|
54
|
+
7.8 6.54
|
55
|
+
9.0 -3210
|
56
|
+
EOS
|
57
|
+
|
58
|
+
@bad_input_not_numeric = <<-EOS
|
59
|
+
1.23 4.56 aaa 9.0
|
60
|
+
9 -8.7 6.54 -3210
|
61
|
+
EOS
|
62
|
+
|
63
|
+
@bad_input_different_row_size = <<-EOS
|
64
|
+
1.23 4.56 7.8
|
65
|
+
9 -8.7 6.54 -3210
|
66
|
+
EOS
|
67
|
+
|
68
|
+
@bad_input_not_4_rows_and_cols = <<-EOS
|
69
|
+
1.23 4.56 7.8
|
70
|
+
9 -8.7 6.54
|
71
|
+
10 2 10
|
72
|
+
4 5 6
|
73
|
+
1 1 1
|
74
|
+
EOS
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '#can_parse?' do
|
78
|
+
it 'should return true for valid input string' do
|
79
|
+
StringParser.new(@input_with_name).can_parse?.should be_true
|
80
|
+
StringParser.new(@input_with_name_with_introduction_sign).can_parse?.should be_true
|
81
|
+
StringParser.new(@input_with_name_with_special_characters).can_parse?.should be_true
|
82
|
+
StringParser.new(@input_with_tabs_and_multiple_spaces).can_parse?.should be_true
|
83
|
+
StringParser.new(@input_with_windows_crlf).can_parse?.should be_true
|
84
|
+
StringParser.new(@input_with_leading_and_finishing_spaces_and_newlines).can_parse?.should be_true
|
85
|
+
StringParser.new(@input_without_name).can_parse?.should be_true
|
86
|
+
StringParser.new(@input_transposed).can_parse?.should be_true
|
87
|
+
end
|
88
|
+
it 'should return false for invalid input string' do
|
89
|
+
StringParser.new(@bad_input_not_numeric).can_parse?.should be_false
|
90
|
+
StringParser.new(@bad_input_different_row_size).can_parse?.should be_false
|
91
|
+
StringParser.new(@bad_input_not_4_rows_and_cols).can_parse?.should be_false
|
92
|
+
end
|
93
|
+
end
|
94
|
+
describe '#parse' do
|
95
|
+
it 'should return a hash with matrix and possibly name keys for valid input string' do
|
96
|
+
StringParser.new(@input_with_name).parse.should == {matrix: @matrix, name: 'Testmatrix'}
|
97
|
+
StringParser.new(@input_with_name_with_introduction_sign).parse.should == {matrix: @matrix, name: 'Testmatrix'}
|
98
|
+
StringParser.new(@input_with_name_with_special_characters).parse.should == {matrix: @matrix, name: 'Testmatrix_first:subname+sub-subname'}
|
99
|
+
StringParser.new(@input_with_tabs_and_multiple_spaces).parse.should == {matrix: @matrix, name: 'Testmatrix'}
|
100
|
+
StringParser.new(@input_with_windows_crlf).parse.should == {matrix: @matrix, name: 'Testmatrix'}
|
101
|
+
StringParser.new(@input_with_leading_and_finishing_spaces_and_newlines).parse.should == {matrix: @matrix}
|
102
|
+
StringParser.new(@input_without_name).parse.should == {matrix: @matrix}
|
103
|
+
StringParser.new(@input_transposed).parse.should == {matrix: @matrix}
|
104
|
+
end
|
105
|
+
it 'should raise an error for invalid input string' do
|
106
|
+
expect{ StringParser.new(@bad_input_not_numeric).parse }.to raise_error ArgumentError
|
107
|
+
expect{ StringParser.new(@bad_input_different_row_size).parse }.to raise_error ArgumentError
|
108
|
+
expect{ StringParser.new(@bad_input_not_4_rows_and_cols).parse }.to raise_error ArgumentError
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|