sixarm_ruby_ramp 2.1.3 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +1 -0
- data.tar.gz.sig +1 -1
- data/CONTRIBUTING.md +28 -0
- data/{README.rdoc → README.md} +112 -42
- data/VERSION +1 -1
- data/lib/sixarm_ruby_ramp.rb +1 -1
- data/lib/sixarm_ruby_ramp/array.rb +39 -16
- data/lib/sixarm_ruby_ramp/hash.rb +1 -1
- data/lib/sixarm_ruby_ramp/kernel.rb +11 -5
- data/lib/sixarm_ruby_ramp/numeric.rb +49 -35
- data/lib/sixarm_ruby_ramp/process.rb +5 -20
- data/lib/sixarm_ruby_ramp/string.rb +3 -4
- data/lib/sixarm_ruby_ramp/xml.rb +0 -204
- data/test/sixarm_ruby_ramp_test.rb +2 -2
- data/test/sixarm_ruby_ramp_test/array_test.rb +330 -0
- data/test/sixarm_ruby_ramp_test/class_test.rb +87 -0
- data/test/sixarm_ruby_ramp_test/csv_test.rb +47 -0
- data/test/sixarm_ruby_ramp_test/date_test.rb +60 -0
- data/test/sixarm_ruby_ramp_test/enumerable_test.rb +303 -0
- data/test/sixarm_ruby_ramp_test/file_test.rb +17 -0
- data/test/sixarm_ruby_ramp_test/fixnum_test.rb +25 -0
- data/test/sixarm_ruby_ramp_test/hash_test.rb +209 -0
- data/test/sixarm_ruby_ramp_test/integer_test.rb +21 -0
- data/test/sixarm_ruby_ramp_test/io_test.rb +30 -0
- data/test/{sixarm_ruby_ramp → sixarm_ruby_ramp_test}/io_test.txt +0 -0
- data/test/sixarm_ruby_ramp_test/kernel_test.rb +55 -0
- data/test/sixarm_ruby_ramp_test/math_test.rb +19 -0
- data/test/sixarm_ruby_ramp_test/nil_test.rb +16 -0
- data/test/sixarm_ruby_ramp_test/numeric_test.rb +57 -0
- data/test/sixarm_ruby_ramp_test/object_test.rb +13 -0
- data/test/sixarm_ruby_ramp_test/process_test.rb +67 -0
- data/test/sixarm_ruby_ramp_test/string_test.rb +131 -0
- data/test/sixarm_ruby_ramp_test/symbol_test.rb +27 -0
- data/test/sixarm_ruby_ramp_test/time_test.rb +29 -0
- data/test/sixarm_ruby_ramp_test/xml_test.rb +10 -0
- data/test/sixarm_ruby_ramp_test/yaml_test.rb +8 -0
- metadata +100 -48
- metadata.gz.sig +0 -0
- data/CHANGELOG.txt +0 -53
- data/INSTALL.txt +0 -32
- data/LICENSE.txt +0 -25
- data/test/sixarm_ruby_ramp/xml_test_1.xml +0 -5
- data/test/sixarm_ruby_ramp/xml_test_2.xml +0 -5
- data/test/sixarm_ruby_ramp/xml_test_msword_clean.html +0 -1
- data/test/sixarm_ruby_ramp/xml_test_msword_dirty.html +0 -148
@@ -5,40 +5,54 @@
|
|
5
5
|
class Numeric
|
6
6
|
|
7
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
|
-
|
8
|
+
# @return 0 if the given flag is any of: nil, false, 0, [], {}
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# 3.if(true) => 3
|
12
|
+
# 3.if(false) => 0
|
13
|
+
#
|
14
|
+
def if(flag)
|
15
|
+
if [nil,false,0,[],{}].include?(flag) then 0 else self end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
# @return self if flag is nil, false, 0, [], {}; otherwise return 0.
|
20
|
+
#
|
21
|
+
# @example
|
22
|
+
# 3.unless(true) => 0
|
23
|
+
# 3.unless(false) => 3
|
24
|
+
#
|
25
|
+
def unless(flag)
|
26
|
+
if [nil,false,0,[],{}].include?(flag) then self else 0 end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
# @return self as a percent, i.e. * 100 then call round.
|
31
|
+
#
|
32
|
+
# @example
|
33
|
+
# x = 0.12345
|
34
|
+
# x.percent => 12
|
35
|
+
# x.percent(1) => 12.3
|
36
|
+
# x.percent(2) => 12.34
|
37
|
+
# x.percent(-1) => 10
|
38
|
+
#
|
39
|
+
def percent(ndigits=0)
|
40
|
+
(self * 100).round(ndigits)
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# @return self, truncated to a given precision
|
45
|
+
#
|
46
|
+
# @example
|
47
|
+
# x = 0.12345
|
48
|
+
# x.floor_precision(0) => 0.0
|
49
|
+
# x.floor_precision(1) => 0.1
|
50
|
+
# x.floor_precision(2) => 0.12
|
51
|
+
# x.floor_precision(3) => 0.123
|
52
|
+
#
|
53
|
+
def floor_precision(precision)
|
54
|
+
return self if is_a?(Float) && nan?
|
55
|
+
(self * (10 ** precision)).truncate.fdiv(10 ** precision)
|
56
|
+
end
|
43
57
|
|
44
58
|
end
|
@@ -47,29 +47,14 @@ module Process
|
|
47
47
|
# -
|
48
48
|
# OPTIMIZE: add dates, times
|
49
49
|
|
50
|
+
PS_TO_I = %w(c egid egroup uid fgid lwp ni nlwp pgid pid ppid rgid rss ruid sid sgid suid)
|
51
|
+
PS_TO_F = %w(cp pcpu pmem)
|
52
|
+
|
50
53
|
def self.pss(pid=Process.pid)
|
51
54
|
ps=self.ps(pid)
|
52
55
|
h=Hash[*self.ps_keys.zip(ps.split).flatten]
|
53
|
-
h[
|
54
|
-
h[
|
55
|
-
h['egid'] =h['egid'].to_i
|
56
|
-
h['egroup'] =h['egroup'].to_i
|
57
|
-
h['uid'] =h['uid'].to_i
|
58
|
-
h['fgid'] =h['fgid'].to_i
|
59
|
-
h['lwp'] =h['lwp'].to_i
|
60
|
-
h['ni'] =h['ni'].to_i
|
61
|
-
h['nlwp'] =h['nlwp'].to_i
|
62
|
-
h['pcpu'] =h['pcpu'].to_f
|
63
|
-
h['pgid'] =h['pgid'].to_i
|
64
|
-
h['pid'] =h['pid'].to_i
|
65
|
-
h['pmem'] =h['pmem'].to_f
|
66
|
-
h['ppid'] =h['ppid'].to_i
|
67
|
-
h['rgid'] =h['rgid'].to_i
|
68
|
-
h['rss'] =h['rss'].to_i
|
69
|
-
h['ruid'] =h['ruid'].to_i
|
70
|
-
h['sid'] =h['sid'].to_i
|
71
|
-
h['sgid'] =h['sgid'].to_i
|
72
|
-
h['suid'] =h['suid'].to_i
|
56
|
+
PS_TO_I.each{|x| h[x]=h[x].to_i}
|
57
|
+
PS_TO_F.each{|x| h[x]=h[x].to_f}
|
73
58
|
self.ps_aliases.each_pair{|key,val| h[key]=h[val]}
|
74
59
|
return h
|
75
60
|
end
|
@@ -111,7 +111,7 @@ class String
|
|
111
111
|
# @see String#increment
|
112
112
|
|
113
113
|
def decrement(step=1)
|
114
|
-
|
114
|
+
increment(-step)
|
115
115
|
end
|
116
116
|
|
117
117
|
|
@@ -188,9 +188,8 @@ class String
|
|
188
188
|
|
189
189
|
# Helpful constants
|
190
190
|
|
191
|
-
LOWERCASE_ENGLISH_CHARS =
|
192
|
-
UPPERCASE_ENGLISH_CHARS =
|
193
|
-
|
191
|
+
LOWERCASE_ENGLISH_CHARS = ('a'..'z').to_a
|
192
|
+
UPPERCASE_ENGLISH_CHARS = ('A'..'Z').to_a
|
194
193
|
|
195
194
|
##
|
196
195
|
#
|
data/lib/sixarm_ruby_ramp/xml.rb
CHANGED
@@ -1,204 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
require 'rexml/document'
|
3
|
-
|
4
|
-
# XML extensions
|
5
|
-
|
6
|
-
module XML
|
7
|
-
|
8
|
-
|
9
|
-
# Specify one or more directory patterns and pass each XML file in the matching directories to a block.
|
10
|
-
#
|
11
|
-
# See [Dir#glob](http://www.ruby-doc.org/core/classes/Dir.html#M002347) for pattern details.
|
12
|
-
#
|
13
|
-
# @example To load xml documents
|
14
|
-
# XML.load_dir('/tmp/*.xml'){|xml_document|
|
15
|
-
# #...whatever you want to do with each xml document
|
16
|
-
# }
|
17
|
-
#
|
18
|
-
# @example To load xml documents in files beginning in "foo" or "bar"
|
19
|
-
# XML.load_dir('/tmp/foo*.yaml','/tmp/bar*.xml','){|xml_document|
|
20
|
-
# #...whatever you want to do with the xml document
|
21
|
-
# }
|
22
|
-
|
23
|
-
def XML.load_dir(*dirpaths)
|
24
|
-
dirpaths=[*dirpaths.flatten]
|
25
|
-
dirpaths.each do |dirpath|
|
26
|
-
Dir[dirpath].sort.each do |filename|
|
27
|
-
File.open(filename) do |file|
|
28
|
-
doc = REXML::Document.new file
|
29
|
-
yield doc
|
30
|
-
end #file
|
31
|
-
end #dir
|
32
|
-
end #each
|
33
|
-
end #def
|
34
|
-
|
35
|
-
|
36
|
-
# Sugar to load elements from a file.
|
37
|
-
#
|
38
|
-
# @example
|
39
|
-
# XML.load_attributes('config.xml','userlist/user'){|element| pp element.attributes['first_name'] }
|
40
|
-
|
41
|
-
def XML.load_elements(dirpath,xpath)
|
42
|
-
XML.load_dir(dirpath){|doc|
|
43
|
-
doc.elements.each(xpath){|elem|
|
44
|
-
yield elem
|
45
|
-
}
|
46
|
-
}
|
47
|
-
end
|
48
|
-
|
49
|
-
|
50
|
-
# Sugar to load attributes from a file.
|
51
|
-
#
|
52
|
-
# @example
|
53
|
-
# XML.load_attributes('config.xml','userlist/user'){|attributes| pp attributes['first_name'] }
|
54
|
-
|
55
|
-
def XML.load_attributes(dirpath,xpath)
|
56
|
-
XML.load_elements(dirpath,xpath){|elem|
|
57
|
-
yield elem.attributes
|
58
|
-
}
|
59
|
-
end
|
60
|
-
|
61
|
-
# Sugar to load attributes hash from a file.
|
62
|
-
#
|
63
|
-
# @example
|
64
|
-
# XML.load_attributes('config.xml','userlist/user'){|attributes| pp attributes['first_name'] }
|
65
|
-
|
66
|
-
def XML.load_attributes_hash(dirpath,xpath)
|
67
|
-
XML.load_elements(dirpath,xpath){|elem|
|
68
|
-
yield elem.attributes.to_hash
|
69
|
-
}
|
70
|
-
end
|
71
|
-
|
72
|
-
|
73
|
-
# Santize dirty xml by removing unprintables, bad tags,
|
74
|
-
# comments, and generally anything else we might need
|
75
|
-
# to enable the XML parser to handle a dirty document.
|
76
|
-
#
|
77
|
-
# This method calls these in order:
|
78
|
-
# - XML.strip_unprintables
|
79
|
-
# - XML.strip_microsoft
|
80
|
-
# - XML.strip_comments
|
81
|
-
# - XML.strip_attributes
|
82
|
-
#
|
83
|
-
# @example
|
84
|
-
# # This example shows curly braces instead of angle braces because of HTML formatting
|
85
|
-
# s="{foo a=b c=d}{!--comment--}Hello{!-[if bar]}Microsoft{![endif]}World{/foo}"
|
86
|
-
# XML.strip_all(s) => "{foo}HelloWorld{/foo}"
|
87
|
-
#
|
88
|
-
# @return [String] the text, stripped of unprintables, Microsoft markup, comments, and attributes
|
89
|
-
|
90
|
-
def XML.strip_all(xml_text)
|
91
|
-
return XML.strip_attributes(XML.strip_comments(XML.strip_microsoft(XML.strip_unprintables(xml_text))))
|
92
|
-
end
|
93
|
-
|
94
|
-
|
95
|
-
# Strip out all attributes from the xml text's tags.
|
96
|
-
#
|
97
|
-
# @example
|
98
|
-
# s="<foo a=b c=d e=f>Hello</foo>"
|
99
|
-
# XML.strip_attributes(s) => "<foo>Hello</foo>"
|
100
|
-
#
|
101
|
-
# @return [String] the text, stripped of attributes
|
102
|
-
|
103
|
-
def XML.strip_attributes(xml_text)
|
104
|
-
return xml_text.gsub(/<(\/?\w+).*?>/im){"<#{$1}>"} # delete attributes
|
105
|
-
end
|
106
|
-
|
107
|
-
|
108
|
-
# Strip out all comments from the xml text.
|
109
|
-
#
|
110
|
-
# @example
|
111
|
-
# # This example shows curly braces instead of angle braces because of HTML formatting
|
112
|
-
# s="Hello{!--comment--}World"
|
113
|
-
# XML.strip_comments(s) => "HelloWorld"
|
114
|
-
#
|
115
|
-
# @return [String] the text, stripped of comments
|
116
|
-
|
117
|
-
def XML.strip_comments(xml_text)
|
118
|
-
return xml_text.gsub(/<!.*?>/im,'')
|
119
|
-
end
|
120
|
-
|
121
|
-
|
122
|
-
# Strip out all microsoft proprietary codes.
|
123
|
-
#
|
124
|
-
# @example
|
125
|
-
# s="Hello<!-[if foo]>Microsoft<![endif]->World"
|
126
|
-
# XML.strip_microsoft(s) => "HelloWorld"
|
127
|
-
#
|
128
|
-
# @return [String] the text, stripped of Microsoft markup
|
129
|
-
|
130
|
-
def XML.strip_microsoft(xml_text)
|
131
|
-
return xml_text.gsub(/<!-*\[if\b.*?<!\[endif\]-*>/im,'')
|
132
|
-
end
|
133
|
-
|
134
|
-
|
135
|
-
# Strip out all unprintable characters from the input string.
|
136
|
-
#
|
137
|
-
# @example
|
138
|
-
# s="Hello\XXXWorld" # where XXX is unprintable
|
139
|
-
# XML.strip_unprintables(s) => "HelloWorld"
|
140
|
-
#
|
141
|
-
# @return [String] the text, stripped of unprintables
|
142
|
-
|
143
|
-
def XML.strip_unprintables(xml_text)
|
144
|
-
return xml_text.gsub(/[^[:print:]]/, "")
|
145
|
-
end
|
146
|
-
|
147
|
-
end
|
148
|
-
|
149
|
-
|
150
|
-
# REXML::Attributes extensions
|
151
|
-
|
152
|
-
class REXML::Attributes
|
153
|
-
|
154
|
-
# @return a new hash of the attribute keys and values.
|
155
|
-
#
|
156
|
-
# @example
|
157
|
-
# attributes.to_hash => {"src"=>"pic.jpg", "height" => "100", "width" => "200"}
|
158
|
-
|
159
|
-
def to_hash
|
160
|
-
h=Hash.new
|
161
|
-
self.keys.each{|k| h[k]=self[k]}
|
162
|
-
h
|
163
|
-
end
|
164
|
-
|
165
|
-
end
|
166
|
-
|
167
|
-
|
168
|
-
# REXML::Document extensions
|
169
|
-
|
170
|
-
class REXML::Document
|
171
|
-
|
172
|
-
# Remove all attributes from the document's elements.
|
173
|
-
#
|
174
|
-
# @return the document.
|
175
|
-
#
|
176
|
-
# @see Element#remove_attributes
|
177
|
-
|
178
|
-
def remove_attributes
|
179
|
-
self.elements.each("//") { |elem| elem.attributes.each_attribute{|attribute| attribute.remove }}
|
180
|
-
self
|
181
|
-
end
|
182
|
-
|
183
|
-
end
|
184
|
-
|
185
|
-
|
186
|
-
# REXML::Element extensions
|
187
|
-
|
188
|
-
class REXML::Element
|
189
|
-
|
190
|
-
# Remove all attributes from the element.
|
191
|
-
#
|
192
|
-
# @return the element.
|
193
|
-
#
|
194
|
-
# @see Document#remove_attributes
|
195
|
-
|
196
|
-
def remove_attributes
|
197
|
-
self.attributes.each_attribute{|attribute| attribute.remove }
|
198
|
-
self
|
199
|
-
end
|
200
|
-
|
201
|
-
end
|
202
|
-
|
203
|
-
|
204
|
-
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# -*- coding: utf-8 -*-
|
2
|
-
require '
|
2
|
+
require 'minitest/autorun'
|
3
3
|
require 'simplecov'
|
4
4
|
SimpleCov.start
|
5
5
|
|
6
6
|
['array','class','csv','date','enumerable','file','fixnum','hash','integer','io','kernel','math','nil','numeric','object','process','string','symbol','time','xml','yaml'].map{|x|
|
7
|
-
require "
|
7
|
+
require "sixarm_ruby_ramp_test/#{x}_test.rb"
|
8
8
|
}
|
9
9
|
|
@@ -0,0 +1,330 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'minitest/autorun'
|
3
|
+
require 'csv'
|
4
|
+
require 'sixarm_ruby_ramp'
|
5
|
+
|
6
|
+
|
7
|
+
class ArrayTest < Minitest::Test
|
8
|
+
|
9
|
+
A=['a','b','c','d']
|
10
|
+
B=['w','x','y','z']
|
11
|
+
|
12
|
+
def test_join_with_no_ops
|
13
|
+
assert_equal('abcd',A.join())
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_join_with_1_op_does_infix
|
17
|
+
assert_equal('a*b*c*d', A.join('*'))
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_join_with_2_ops_does_prefix_suffix
|
21
|
+
assert_equal('[a][b][c][d]', A.join('[',']'))
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_join_with_3_opes_does_prefix_suffix_infix
|
25
|
+
assert_equal('[a]*[b]*[c]*[d]', A.join('[',']','*'))
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_join_with_too_many_ops_raises
|
29
|
+
assert_raises(ArgumentError){ A.join('','','','') }
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_join_prefix_suffix
|
33
|
+
assert_equal('[a][b][c][d]', A.join_prefix_suffix('[',']'))
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_join_prefix_suffix_infix
|
37
|
+
assert_equal('[a]*[b]*[c]*[d]', A.join_prefix_suffix_infix('[',']','*'))
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_size_with_one_item
|
41
|
+
assert_equal(true,[1].size?)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_size_with_two_items
|
45
|
+
assert_equal(true,[1,2].size?)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_size_with_empty
|
49
|
+
assert_equal(false,[].size?)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_size_with_nil
|
53
|
+
assert_equal(true,[nil].size?)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_size_with_one_empty
|
57
|
+
assert_equal(true,[[]].size?)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_rotate
|
61
|
+
a=A.dup
|
62
|
+
a.rotate!
|
63
|
+
assert_equal(['b','c','d','a'],a)
|
64
|
+
a.rotate!
|
65
|
+
assert_equal(['c','d','a','b'],a)
|
66
|
+
a.rotate!
|
67
|
+
assert_equal(['d','a','b','c'],a)
|
68
|
+
a.rotate!
|
69
|
+
assert_equal(['a','b','c','d'],a)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_rotate_with_empty
|
73
|
+
a=[]
|
74
|
+
a.rotate!
|
75
|
+
assert_equal([],a)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_choice
|
79
|
+
5.times {
|
80
|
+
c=A.choice
|
81
|
+
assert_equal(String,c.class)
|
82
|
+
assert(A.include?(c))
|
83
|
+
}
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_choices
|
87
|
+
5.times {
|
88
|
+
c=A.choices(2)
|
89
|
+
assert_equal(2,c.size)
|
90
|
+
assert(A.include?(c[0]))
|
91
|
+
assert(A.include?(c[1]))
|
92
|
+
c=A.choices(3)
|
93
|
+
assert_equal(3,c.size)
|
94
|
+
assert(A.include?(c[0]))
|
95
|
+
assert(A.include?(c[1]))
|
96
|
+
assert(A.include?(c[2]))
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_onto
|
101
|
+
assert_equal({'a'=>'w', 'b'=>'x', 'c'=>'y', 'd'=>'z'},A.onto(B))
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_onto_with_empty
|
105
|
+
assert_raises(ArgumentError){ A.onto([]) }
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_union
|
109
|
+
a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
|
110
|
+
assert_equal([1,2,3,4,5,6],a.union)
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_union_with_empty
|
114
|
+
assert_equal([],[].union)
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_intersect
|
118
|
+
a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
|
119
|
+
assert_equal([3,4],a.intersect)
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_intersect_with_empty
|
123
|
+
assert_equal([],[].intersect)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_shifted
|
127
|
+
a=['a','b','c']
|
128
|
+
assert_equal([ 'b','c'],a.shifted)
|
129
|
+
assert_equal(['a','b','c'],a.shifted(0))
|
130
|
+
assert_equal([ 'b','c'],a.shifted(1))
|
131
|
+
assert_equal([ 'c'],a.shifted(2))
|
132
|
+
assert_equal([ ],a.shifted(3))
|
133
|
+
assert_equal(nil ,a.shifted(4))
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_shifted_with_negative_count
|
137
|
+
assert_raises(ArgumentError){ [].shifted(-1) }
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_shifted_with_non_integer_count
|
141
|
+
assert_raises(ArgumentError){ [].shifted(0.123) }
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_shifted_with_non_numeric_count
|
145
|
+
assert_raises(ArgumentError){ [].shifted("") }
|
146
|
+
end
|
147
|
+
|
148
|
+
# alias: test_cdr must be idential to test_shifted
|
149
|
+
def test_cdr
|
150
|
+
a=['a','b','c']
|
151
|
+
assert_equal([ 'b','c'],a.cdr)
|
152
|
+
assert_equal(['a','b','c'],a.cdr(0))
|
153
|
+
assert_equal([ 'b','c'],a.cdr(1))
|
154
|
+
assert_equal([ 'c'],a.cdr(2))
|
155
|
+
assert_equal([ ],a.cdr(3))
|
156
|
+
assert_equal(nil ,a.cdr(4))
|
157
|
+
end
|
158
|
+
|
159
|
+
# alias: test_cdr must be idential to test_shifted
|
160
|
+
def test_cdr_with_negative_count
|
161
|
+
assert_raises(ArgumentError){ [].cdr(-1) }
|
162
|
+
end
|
163
|
+
|
164
|
+
# alias: test_cdr must be idential to test_shifted
|
165
|
+
def test_cdr_with_non_integer_count
|
166
|
+
assert_raises(ArgumentError){ [].cdr(0.123) }
|
167
|
+
end
|
168
|
+
|
169
|
+
# alias: test_cdr must be idential to test_shifted
|
170
|
+
def test_cdr_with_non_numeric_count
|
171
|
+
assert_raises(ArgumentError){ [].cdr("") }
|
172
|
+
end
|
173
|
+
|
174
|
+
# alias: test_rest must be idential to test_shifted
|
175
|
+
def test_rest
|
176
|
+
a=['a','b','c']
|
177
|
+
assert_equal([ 'b','c'],a.rest)
|
178
|
+
assert_equal(['a','b','c'],a.rest(0))
|
179
|
+
assert_equal([ 'b','c'],a.rest(1))
|
180
|
+
assert_equal([ 'c'],a.rest(2))
|
181
|
+
assert_equal([ ],a.rest(3))
|
182
|
+
assert_equal(nil ,a.rest(4))
|
183
|
+
end
|
184
|
+
|
185
|
+
# alias: test_rest must be idential to test_shifted
|
186
|
+
def test_rest_with_negative_count
|
187
|
+
assert_raises(ArgumentError){ [].rest(-1) }
|
188
|
+
end
|
189
|
+
|
190
|
+
# alias: test_rest must be idential to test_shifted
|
191
|
+
def test_rest_with_non_integer_count
|
192
|
+
assert_raises(ArgumentError){ [].rest(0.123) }
|
193
|
+
end
|
194
|
+
|
195
|
+
# alias: test_rest must be idential to test_shifted
|
196
|
+
def test_rest_with_non_numeric_count
|
197
|
+
assert_raises(ArgumentError){ [].rest("") }
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_shifted_bang
|
201
|
+
a=['a','b','c']; a.shifted!; assert_equal([ 'b','c'],a)
|
202
|
+
a=['a','b','c']; a.shifted!(0); assert_equal(['a','b','c'],a)
|
203
|
+
a=['a','b','c']; a.shifted!(1); assert_equal([ 'b','c'],a)
|
204
|
+
a=['a','b','c']; a.shifted!(2); assert_equal([ 'c'],a)
|
205
|
+
a=['a','b','c']; a.shifted!(3); assert_equal([ ],a)
|
206
|
+
a=['a','b','c']; a.shifted!(4); assert_equal([ ],a)
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_shifted_bang_with_negative_count
|
210
|
+
assert_raises(ArgumentError){ [].shifted!(-1) }
|
211
|
+
end
|
212
|
+
|
213
|
+
def test_shifted_bang_with_non_integer_count
|
214
|
+
assert_raises(ArgumentError){ [].shifted!(0.123) }
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_shifted_bang_with_non_numeric_count
|
218
|
+
assert_raises(ArgumentError){ [].shifted!("") }
|
219
|
+
end
|
220
|
+
|
221
|
+
# alias: test_cdr_bang must be identical to test_shifted_bang
|
222
|
+
def test_cdr_bang
|
223
|
+
a=['a','b','c']; a.cdr!; assert_equal([ 'b','c'],a)
|
224
|
+
a=['a','b','c']; a.cdr!(0); assert_equal(['a','b','c'],a)
|
225
|
+
a=['a','b','c']; a.cdr!(1); assert_equal([ 'b','c'],a)
|
226
|
+
a=['a','b','c']; a.cdr!(2); assert_equal([ 'c'],a)
|
227
|
+
a=['a','b','c']; a.cdr!(3); assert_equal([ ],a)
|
228
|
+
a=['a','b','c']; a.cdr!(4); assert_equal([ ],a)
|
229
|
+
end
|
230
|
+
|
231
|
+
# alias: test_cdr_bang must be identical to test_shifted_bang
|
232
|
+
def test_cdr_bang_with_negative_count
|
233
|
+
assert_raises(ArgumentError){ [].cdr!(-1) }
|
234
|
+
end
|
235
|
+
|
236
|
+
# alias: test_cdr_bang must be identical to test_shifted_bang
|
237
|
+
def test_cdr_bang_with_non_integer_count
|
238
|
+
assert_raises(ArgumentError){ [].cdr!(0.123) }
|
239
|
+
end
|
240
|
+
|
241
|
+
# alias: test_cdr_bang must be identical to test_shifted_bang
|
242
|
+
def test_cdr_bang_with_non_numeric_count
|
243
|
+
assert_raises(ArgumentError){ [].cdr!("") }
|
244
|
+
end
|
245
|
+
|
246
|
+
# alias: test_rest_bang must be identical to test_shifted_bang
|
247
|
+
def test_rest_bang
|
248
|
+
a=['a','b','c']; a.rest!; assert_equal([ 'b','c'],a)
|
249
|
+
a=['a','b','c']; a.rest!(0); assert_equal(['a','b','c'],a)
|
250
|
+
a=['a','b','c']; a.rest!(1); assert_equal([ 'b','c'],a)
|
251
|
+
a=['a','b','c']; a.rest!(2); assert_equal([ 'c'],a)
|
252
|
+
a=['a','b','c']; a.rest!(3); assert_equal([ ],a)
|
253
|
+
a=['a','b','c']; a.rest!(4); assert_equal([ ],a)
|
254
|
+
end
|
255
|
+
|
256
|
+
# alias: test_rest_bang must be identical to test_shifted_bang
|
257
|
+
def test_rest_bang_with_negative_count
|
258
|
+
assert_raises(ArgumentError){ [].rest!(-1) }
|
259
|
+
end
|
260
|
+
|
261
|
+
# alias: test_rest_bang must be identical to test_shifted_bang
|
262
|
+
def test_rest_bang_with_non_integer_count
|
263
|
+
assert_raises(ArgumentError){ [].rest!(0.123) }
|
264
|
+
end
|
265
|
+
|
266
|
+
# alias: test_rest_bang must be identical to test_shifted_bang
|
267
|
+
def test_rest_bang_with_non_numeric_count
|
268
|
+
assert_raises(ArgumentError){ [].rest!("") }
|
269
|
+
end
|
270
|
+
|
271
|
+
def test_car
|
272
|
+
a=['a','b','c']
|
273
|
+
assert_equal('a',a.car)
|
274
|
+
end
|
275
|
+
|
276
|
+
def test_shuffle
|
277
|
+
a=[1,2,3,4]
|
278
|
+
5.times {
|
279
|
+
b=a.shuffle
|
280
|
+
assert_equal(a.size,b.size)
|
281
|
+
a.each{|item| assert(b.include?(item)) }
|
282
|
+
}
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_shuffle_bang
|
286
|
+
a=[1,2,3,4]
|
287
|
+
b=a.dup
|
288
|
+
5.times {
|
289
|
+
b.shuffle!
|
290
|
+
assert_equal(a.size,b.size)
|
291
|
+
a.each{|item| assert(b.include?(item)) }
|
292
|
+
}
|
293
|
+
end
|
294
|
+
|
295
|
+
def test_to_csv_one_dimensional
|
296
|
+
assert_equal("a,b,c,d\n",A.to_csv)
|
297
|
+
end
|
298
|
+
|
299
|
+
def test_to_csv_with_empty
|
300
|
+
assert_equal("",[].to_csv)
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_to_csv_multi_dimensional
|
304
|
+
a=[["a","b","c"], ["d","e","f"],["g","h","i"]]
|
305
|
+
assert_equal("a,b,c\nd,e,f\ng,h,i\n",a.to_csv)
|
306
|
+
end
|
307
|
+
|
308
|
+
def test_to_tsv
|
309
|
+
a=[["a", "b"], ["c", "d"], ["e", "f"]]
|
310
|
+
assert_equal("a\tb\nc\td\ne\tf\n",a.to_tsv)
|
311
|
+
end
|
312
|
+
|
313
|
+
def test_to_tsv_with_empty
|
314
|
+
assert_equal("",[].to_tsv)
|
315
|
+
end
|
316
|
+
|
317
|
+
# alias: test_to_tdf must be identical to test_to_tsv
|
318
|
+
def test_to_tdf
|
319
|
+
a=[["a", "b"], ["c", "d"], ["e", "f"]]
|
320
|
+
assert_equal("a\tb\nc\td\ne\tf\n",a.to_tdf)
|
321
|
+
end
|
322
|
+
|
323
|
+
# alias: test_to_tdf must be identical to test_to_tsv
|
324
|
+
def test_to_tdf_with_empty
|
325
|
+
assert_equal("",[].to_tdf)
|
326
|
+
end
|
327
|
+
|
328
|
+
end
|
329
|
+
|
330
|
+
|