more_core_extensions 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +7 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +63 -0
  7. data/Rakefile +6 -0
  8. data/lib/more_core_extensions/all.rb +5 -0
  9. data/lib/more_core_extensions/core_ext/array/duplicates.rb +11 -0
  10. data/lib/more_core_extensions/core_ext/array/inclusions.rb +33 -0
  11. data/lib/more_core_extensions/core_ext/array/random.rb +19 -0
  12. data/lib/more_core_extensions/core_ext/array/stretch.rb +49 -0
  13. data/lib/more_core_extensions/core_ext/array/tableize.rb +89 -0
  14. data/lib/more_core_extensions/core_ext/array.rb +5 -0
  15. data/lib/more_core_extensions/core_ext/hash/deletes.rb +19 -0
  16. data/lib/more_core_extensions/core_ext/hash/nested.rb +56 -0
  17. data/lib/more_core_extensions/core_ext/hash.rb +2 -0
  18. data/lib/more_core_extensions/core_ext/string/formats.rb +41 -0
  19. data/lib/more_core_extensions/core_ext/string/hex_dump.rb +55 -0
  20. data/lib/more_core_extensions/core_ext/string.rb +2 -0
  21. data/lib/more_core_extensions/version.rb +3 -0
  22. data/lib/more_core_extensions.rb +1 -0
  23. data/more_core_extensions.gemspec +27 -0
  24. data/spec/core_ext/array/duplicates_spec.rb +11 -0
  25. data/spec/core_ext/array/inclusions_spec.rb +33 -0
  26. data/spec/core_ext/array/stretch_spec.rb +96 -0
  27. data/spec/core_ext/array/tableize_spec.rb +158 -0
  28. data/spec/core_ext/hash/deletes_spec.rb +17 -0
  29. data/spec/core_ext/hash/nested_spec.rb +125 -0
  30. data/spec/core_ext/string/formats_spec.rb +63 -0
  31. data/spec/core_ext/string/hex_dump_spec.rb +86 -0
  32. data/spec/spec_helper.rb +21 -0
  33. metadata +175 -0
@@ -0,0 +1,96 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ describe Array do
4
+ STRETCH_CASES = [
5
+ # 2 parameter cases
6
+ # Message Test case Expected
7
+ "receiver same size as parameter", [[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]],
8
+
9
+ "receiver longer than parameter", [[1, 2, 3], [4, 5]], [[1, 2, 3], [4, 5, nil]],
10
+ "receiver shorter than parameter", [[1, 2], [4, 5, 6]], [[1, 2, nil], [4, 5, 6]],
11
+
12
+ "receiver with empty parameter", [[1, 2, 3], []], [[1, 2, 3], [nil, nil, nil]],
13
+ "receiver is empty with 1 parameter", [[], [4, 5, 6]], [[nil, nil, nil], [4, 5, 6]],
14
+
15
+ # 3 parameter cases
16
+ # Message Test Case Expected
17
+ "receiver longer than some parameters", [[1, 2, 3], [4, 5], [7, 8, 9]], [[1, 2, 3], [4, 5, nil], [7, 8, 9]],
18
+ "receiver shorter than parameters", [[1, 2], [4, 5, 6], [7, 8, 9]], [[1, 2, nil], [4, 5, 6], [7, 8, 9]],
19
+
20
+ "receiver longer than all parameters", [[1, 2, 3], [4, 5], [7, 8]], [[1, 2, 3], [4, 5, nil], [7, 8, nil]],
21
+ "receiver shorter than some parameters", [[1, 2], [4, 5, 6], [7, 8]], [[1, 2, nil], [4, 5, 6], [7, 8, nil]],
22
+
23
+ "receiver is empty with 2 parameters", [[], [], [7, 8, 9]], [[nil, nil, nil], [nil, nil, nil], [7, 8, 9]],
24
+ ]
25
+
26
+ context '.stretch' do
27
+ STRETCH_CASES.each_slice(3) do |msg, test_case, expected|
28
+ it "where #{msg}" do
29
+ result = Array.stretch(*test_case)
30
+ result.each_with_index do |r, i|
31
+ r.should_not equal(test_case[i])
32
+ r.should == expected[i]
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ context '.stretch!' do
39
+ STRETCH_CASES.each_slice(3) do |msg, test_case, expected|
40
+ it "where #{msg}" do
41
+ result = Array.stretch!(*test_case)
42
+ result.each_with_index do |r, i|
43
+ r.should equal(test_case[i])
44
+ r.should == expected[i]
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ context '#stretch' do
51
+ STRETCH_CASES.each_slice(3) do |msg, test_case, expected|
52
+ it "where #{msg}" do
53
+ receiver, params = test_case[0], test_case[1..-1]
54
+ result = receiver.stretch(*params)
55
+ result.should_not equal(receiver)
56
+ result.should == expected[0]
57
+ end
58
+ end
59
+ end
60
+
61
+ context '#stretch!' do
62
+ STRETCH_CASES.each_slice(3) do |msg, test_case, expected|
63
+ it "where #{msg}" do
64
+ receiver, params = test_case[0].dup, test_case[1..-1]
65
+ result = receiver.stretch!(*params)
66
+ result.should equal(receiver)
67
+ result.should == expected[0]
68
+ end
69
+ end
70
+ end
71
+
72
+ ZIP_STRETCHED_CASES = [
73
+ # 1 parameter tests
74
+ # Message Test case Expected
75
+ "receiver same size as parameter", [1, 2, 3], [[4, 5, 6]], [[1, 4], [2, 5], [3, 6]],
76
+ "receiver longer than parameter", [1, 2, 3], [[4, 5]], [[1, 4], [2, 5], [3, nil]],
77
+ "receiver shorter than parameter", [1, 2], [[4, 5, 6]], [[1, 4], [2, 5], [nil, 6]], # Different than zip
78
+
79
+ # 2 parameter tests
80
+ # Message Test case Expected
81
+ "receiver same size as all parameters", [1, 2, 3], [[4, 5, 6], [7, 8, 9]], [[1, 4, 7], [2, 5, 8], [3, 6, 9]],
82
+ "receiver longer than first parameter", [1, 2, 3], [[4, 5], [7, 8, 9]], [[1, 4, 7], [2, 5, 8], [3, nil, 9]],
83
+ "receiver shorter than all parameters", [1, 2], [[4, 5, 6], [7, 8, 9]], [[1, 4, 7], [2, 5, 8], [nil, 6, 9]], # Different than zip
84
+ "receiver shorter than first parameter", [1, 2], [[4, 5, 6], [7, 8]], [[1, 4, 7], [2, 5, 8], [nil, 6, nil]], # Different than zip
85
+ "receiver shorter than last parameter", [1, 2], [[4, 5], [7, 8, 9]], [[1, 4, 7], [2, 5, 8], [nil, nil, 9]], # Different than zip
86
+ "receiver is empty with 2 parameters", [], [[4, 5, 6], [7, 8, 9]], [[nil, 4, 7], [nil, 5, 8], [nil, 6, 9]], # Different than zip
87
+ ]
88
+
89
+ context '#zip_stretched' do
90
+ ZIP_STRETCHED_CASES.each_slice(4) do |msg, receiver, params, expected|
91
+ it "where #{msg}" do
92
+ receiver.zip_stretched(*params).should == expected
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,158 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ describe Array do
4
+ context '#tableize' do
5
+ context "on an Array of Arrays" do
6
+ it 'normal case' do
7
+ test = [["Col1", "Col2"], ["Val1", "Val2"], ["Value3", "Value4"]]
8
+ expected = <<-EOF
9
+ Col1 | Col2
10
+ --------+--------
11
+ Val1 | Val2
12
+ Value3 | Value4
13
+ EOF
14
+ test.tableize.should == expected
15
+ end
16
+
17
+ it 'with numeric column values right justified' do
18
+ test = [["Col1", "Col2"], ["Val1", 200], ["Value3", 30]]
19
+ expected = <<-EOF
20
+ Col1 | Col2
21
+ --------+------
22
+ Val1 | 200
23
+ Value3 | 30
24
+ EOF
25
+ test.tableize.should == expected
26
+ end
27
+
28
+ it 'with really long column value' do
29
+ test = [["Col1", "Col2"], ["Val1", "Val2"], ["Really Really Long Value3", "Value4"]]
30
+ expected = <<-EOF
31
+ Col1 | Col2
32
+ ---------------------------+--------
33
+ Val1 | Val2
34
+ Really Really Long Value3 | Value4
35
+ EOF
36
+ test.tableize.should == expected
37
+ end
38
+
39
+ it 'with really long column value and :max_width option' do
40
+ test = [["Col1", "Col2"], ["Val1", "Val2"], ["Really Really Long Value3", "Value4"]]
41
+ expected = <<-EOF
42
+ Col1 | Col2
43
+ ------------+--------
44
+ Val1 | Val2
45
+ Really Rea | Value4
46
+ EOF
47
+ test.tableize(:max_width => 10).should == expected
48
+ end
49
+
50
+ it 'with :header => false option' do
51
+ test = [["Col1", "Col2"], ["Val1", "Val2"], ["Value3", "Value4"]]
52
+ expected = <<-EOF
53
+ Col1 | Col2
54
+ Val1 | Val2
55
+ Value3 | Value4
56
+ EOF
57
+ test.tableize(:header => false).should == expected
58
+ end
59
+ end
60
+
61
+ context "on an Array of Hashes" do
62
+ before do
63
+ @str_case = [{"Col3" => "Val3", "Col2" => "Val2", "Col1" => "Val1"}, {"Col3" => "Value6", "Col2" => "Value5", "Col1" => "Value4"}]
64
+ @sym_case = [{:Col3 => "Val3", :Col2 => "Val2", :Col1 => "Val1"}, {:Col3 => "Value6", :Col2 => "Value5", :Col1 => "Value4"}]
65
+ end
66
+
67
+ it "normal case" do
68
+ expected = <<-EOF
69
+ Col1 | Col2 | Col3
70
+ --------+--------+--------
71
+ Val1 | Val2 | Val3
72
+ Value4 | Value5 | Value6
73
+ EOF
74
+
75
+ @str_case.tableize.should == expected
76
+ @sym_case.tableize.should == expected
77
+ end
78
+
79
+ context "with :columns option" do
80
+ before do
81
+ @expected = <<-EOF
82
+ Col3 | Col1 | Col2
83
+ --------+--------+--------
84
+ Val3 | Val1 | Val2
85
+ Value6 | Value4 | Value5
86
+ EOF
87
+ end
88
+
89
+ it "normal case" do
90
+ @str_case.tableize(:columns => ["Col3", "Col1", "Col2"]).should == @expected
91
+ @sym_case.tableize(:columns => [:Col3, :Col1, :Col2 ]).should == @expected
92
+ end
93
+
94
+ it "with only some values" do
95
+ expected = <<-EOF
96
+ Col3 | Col1
97
+ --------+--------
98
+ Val3 | Val1
99
+ Value6 | Value4
100
+ EOF
101
+
102
+ @str_case.tableize(:columns => ["Col3", "Col1"]).should == expected
103
+ @sym_case.tableize(:columns => [:Col3, :Col1 ]).should == expected
104
+ end
105
+
106
+ it "and :leading_columns option" do
107
+ @str_case.tableize(:columns => ["Col3", "Col1", "Col2"], :leading_columns => ["Col1"]).should == @expected
108
+ @sym_case.tableize(:columns => [:Col3, :Col1, :Col2 ], :leading_columns => [:Col1 ]).should == @expected
109
+ end
110
+
111
+ it "and :trailing_columns option" do
112
+ @str_case.tableize(:columns => ["Col3", "Col1", "Col2"], :trailing_columns => ["Col1"]).should == @expected
113
+ @sym_case.tableize(:columns => [:Col3, :Col1, :Col2 ], :trailing_columns => [:Col1 ]).should == @expected
114
+ end
115
+ end
116
+
117
+ it "with :leading_columns option" do
118
+ expected = <<-EOF
119
+ Col3 | Col2 | Col1
120
+ --------+--------+--------
121
+ Val3 | Val2 | Val1
122
+ Value6 | Value5 | Value4
123
+ EOF
124
+
125
+ @str_case.tableize(:leading_columns => ["Col3", "Col2"]).should == expected
126
+ @sym_case.tableize(:leading_columns => [:Col3, :Col2 ]).should == expected
127
+ end
128
+
129
+ it "with :trailing_columns option" do
130
+ expected = <<-EOF
131
+ Col1 | Col3 | Col2
132
+ --------+--------+--------
133
+ Val1 | Val3 | Val2
134
+ Value4 | Value6 | Value5
135
+ EOF
136
+
137
+ @str_case.tableize(:trailing_columns => ["Col3", "Col2"]).should == expected
138
+ @sym_case.tableize(:trailing_columns => [:Col3, :Col2 ]).should == expected
139
+ end
140
+
141
+ it "with both :leading_columns and :trailing_columns options" do
142
+ expected = <<-EOF
143
+ Col3 | Col1 | Col2
144
+ --------+--------+--------
145
+ Val3 | Val1 | Val2
146
+ Value6 | Value4 | Value5
147
+ EOF
148
+
149
+ @str_case.tableize(:leading_columns => ["Col3"], :trailing_columns => ["Col2"]).should == expected
150
+ @sym_case.tableize(:leading_columns => [:Col3 ], :trailing_columns => [:Col2 ]).should == expected
151
+ end
152
+ end
153
+
154
+ it 'with an invalid receiver' do
155
+ lambda { [1, 2, 3].tableize }.should raise_error(RuntimeError)
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,17 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ describe Hash do
4
+ it "#delete_nils" do
5
+ {}.delete_nils.should == {}
6
+ {:a => 1}.delete_nils.should == {:a => 1}
7
+ {:c => nil}.delete_nils.should == {}
8
+ {:a => 1, :b => [], :c => nil}.delete_nils.should == {:a => 1, :b => []}
9
+ end
10
+
11
+ it "#delete_blanks" do
12
+ {}.delete_blanks.should == {}
13
+ {:a => 1}.delete_blanks.should == {:a => 1}
14
+ {:c => nil}.delete_blanks.should == {}
15
+ {:a => 1, :b => [], :c => nil}.delete_blanks.should == {:a => 1}
16
+ end
17
+ end
@@ -0,0 +1,125 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ describe Hash do
4
+ let(:hash) do
5
+ {
6
+ "a" => 1,
7
+ "b" => {},
8
+ "c" => {"c1" => 2},
9
+ "d" => {"d1" => {"d2" => {"d3" => 3}}},
10
+ "e" => Hash.new(4),
11
+ "f" => Hash.new { |h, k| Hash.new }
12
+ }
13
+ end
14
+
15
+ it '#fetch_path' do
16
+ hash.fetch_path("a").should == 1
17
+ hash.fetch_path("b").should == {}
18
+ hash.fetch_path("b", "b1").should be_nil
19
+ hash.fetch_path("b", "b1", "b2").should be_nil
20
+ hash.fetch_path("c").should == {"c1" => 2}
21
+ hash.fetch_path("c", "c1").should == 2
22
+ hash.fetch_path("c", "c1", "c2").should be_nil
23
+ hash.fetch_path("d", "d1", "d2", "d3").should == 3
24
+ hash.fetch_path("d", "d1", "d2", "dx").should be_nil
25
+ hash.fetch_path("d", "d1", "d2", "d3", "d4").should be_nil
26
+ hash.fetch_path("e").should == {}
27
+ hash.fetch_path("e", "e1").should == 4
28
+ hash.fetch_path("e", "e1", "e2").should be_nil
29
+ hash.fetch_path("f").should == {}
30
+ hash.fetch_path("f", "f1").should == {}
31
+ hash.fetch_path("f", "f1", "f2").should be_nil
32
+
33
+ hash.fetch_path(nil).should be_nil
34
+ hash.fetch_path("d", nil, "d1").should be_nil
35
+ hash.fetch_path("e", nil).should == 4
36
+ hash.fetch_path("e", nil, "e1").should be_nil
37
+
38
+ lambda { hash.fetch_path }.should raise_error(ArgumentError)
39
+ end
40
+
41
+ context "#store_path" do
42
+ it "on an empty hash" do
43
+ h = {}
44
+ h.store_path("a", 1)
45
+ h.should == {"a" => 1}
46
+
47
+ h = {}
48
+ h.store_path("b", "b1", 2)
49
+ h.should == {"b" => {"b1" => 2}}
50
+ end
51
+
52
+ it "on an existing hash" do
53
+ hash.store_path("b", "b1", 2)
54
+ hash["b"].should == {"b1" => 2}
55
+ hash.store_path("c", "c1", 3)
56
+ hash["c"].should == {"c1" => 3}
57
+ end
58
+
59
+ it "on an existing item that is not a hash" do
60
+ hash.store_path("a", 2)
61
+ hash["a"].should == 2
62
+ hash.store_path("a", "a1", 3)
63
+ hash["a"].should == {"a1" => 3}
64
+ end
65
+
66
+ it "with an array of keys" do
67
+ h = {}
68
+ h.store_path(["d", "d1", "d2", "d3"], 3)
69
+ h.should == {"d" => {"d1" => {"d2" => {"d3" => 3}}}}
70
+ end
71
+
72
+ it "with a nil value" do
73
+ h = {}
74
+ h.store_path("a", "b", nil)
75
+ h.should == {"a" => {"b" => nil}}
76
+ end
77
+
78
+ it "with invalid values" do
79
+ lambda { {}.store_path }.should raise_error(ArgumentError)
80
+ lambda { {}.store_path(nil) }.should raise_error(ArgumentError)
81
+ end
82
+ end
83
+
84
+ it '#has_key_path?' do
85
+ hash.has_key_path?("a").should be_true
86
+ hash.has_key_path?("b").should be_true
87
+ hash.has_key_path?("b", "b1").should be_false
88
+ hash.has_key_path?("b", "b1", "b2").should be_false
89
+ hash.has_key_path?("c").should be_true
90
+ hash.has_key_path?("c", "c1").should be_true
91
+ hash.has_key_path?("c", "c1", "c2").should be_false
92
+ hash.has_key_path?("d", "d1", "d2", "d3").should be_true
93
+ hash.has_key_path?("d", "d1", "d2", "dx").should be_false
94
+ hash.has_key_path?("d", "d1", "d2", "d3", "d4").should be_false
95
+ hash.has_key_path?("e").should be_true
96
+ hash.has_key_path?("e", "e1").should be_false
97
+ hash.has_key_path?("e", "e1", "e2").should be_false
98
+ hash.has_key_path?("f").should be_true
99
+ hash.has_key_path?("f", "f1").should be_false
100
+ hash.has_key_path?("f", "f1", "f2").should be_false
101
+
102
+ hash.has_key_path?(nil).should be_false
103
+ hash.has_key_path?("d", nil, "d1").should be_false
104
+ hash.has_key_path?("e", nil).should be_false
105
+ hash.has_key_path?("e", nil, "e1").should be_false
106
+
107
+ lambda { hash.has_key_path? }.should raise_error(ArgumentError)
108
+ end
109
+
110
+ [:fetch_path, :has_key_path?, :store_path, :delete_path].each do |meth|
111
+ it "##{meth} will not modify arguments" do
112
+ args = (meth == :store_path ? [1] : [])
113
+
114
+ key = ["d", "d1", "d2", "d3"]
115
+ key2 = key.dup
116
+ hash.send(meth, key2, *args)
117
+ key2.should == key
118
+
119
+ key = ["e", "e1", "e2"]
120
+ key2 = key.dup
121
+ hash.send(meth, key2, *args)
122
+ key2.should == key
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,63 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ describe String do
4
+ it '#email?' do
5
+ "john@example.com".should be_email
6
+ "john.doe@example.com".should be_email
7
+ "john.doe@examplecom".should_not be_email
8
+ "john.doe@example-com".should_not be_email
9
+ "".should_not be_email
10
+ "foo".should_not be_email
11
+ end
12
+
13
+ it '#domain_name?' do
14
+ "example.com".should be_domain_name
15
+ "example..com".should_not be_domain_name
16
+ "john.doe@example.com".should_not be_domain_name
17
+ "".should_not be_domain_name
18
+ "foo".should_not be_domain_name
19
+ end
20
+
21
+ it '#ipv4?' do
22
+ "192.168.252.15".should be_ipv4
23
+
24
+ "392.168.252.15".should_not be_ipv4
25
+ "".should_not be_ipv4
26
+ "foo".should_not be_ipv4
27
+ "::1".should_not be_ipv4 # 127.0.0.1 in IPv6
28
+ "1762:0:0:0:0:B03:1:AF18".should_not be_ipv4 # Standard Notation
29
+ "1762:0:0:0:0:B03:127.32.67.15".should_not be_ipv4 # Mixed Notation
30
+ "1762::B03:1:AF18".should_not be_ipv4 # Compressed Notation
31
+ end
32
+
33
+ it '#ipv6?' do
34
+ "::1".should be_ipv6 # 127.0.0.1 in IPv6
35
+ "1762:0:0:0:0:B03:1:AF18".should be_ipv6 # Standard Notation
36
+ "1762:0:0:0:0:B03:127.32.67.15".should be_ipv6 # Mixed Notation
37
+ "1762::B03:1:AF18".should be_ipv6 # Compressed Notation
38
+
39
+ "192.168.252.15".should_not be_ipv6
40
+ "392.168.252.15".should_not be_ipv6
41
+ "".should_not be_ipv6
42
+ "foo".should_not be_ipv6
43
+ end
44
+
45
+ it "#ipaddress?" do
46
+ "192.168.252.15".should be_ipaddress
47
+ "::1".should be_ipaddress # 127.0.0.1 in IPv6
48
+ "1762:0:0:0:0:B03:1:AF18".should be_ipaddress # Standard Notation
49
+ "1762:0:0:0:0:B03:127.32.67.15".should be_ipaddress # Mixed Notation
50
+ "1762::B03:1:AF18".should be_ipaddress # Compressed Notation
51
+
52
+ "392.168.252.15".should_not be_ipaddress
53
+ "".should_not be_ipaddress
54
+ "foo".should_not be_ipaddress
55
+ end
56
+
57
+ it '#guid?' do
58
+ '01234567-89ab-cdef-abcd-ef0123456789'.should be_guid
59
+ '012ZZZ67-89ab-cdef-abcd-ef0123456789'.should_not be_guid
60
+ "".should_not be_guid
61
+ "foo".should_not be_guid
62
+ end
63
+ end
@@ -0,0 +1,86 @@
1
+ require_relative "../../spec_helper"
2
+
3
+ describe String do
4
+ context '#hex_dump' do
5
+ let(:str) { "This is a test of the emergency broadcast system. This is only a test." }
6
+
7
+ it "will handle exceptions" do
8
+ lambda { "".hex_dump(1, 2) }.should raise_error(ArgumentError)
9
+ lambda { "".hex_dump(:obj => STDOUT) }.should raise_error(ArgumentError)
10
+ lambda { "".hex_dump(:meth => :puts) }.should raise_error(ArgumentError)
11
+ end
12
+
13
+ it 'with empty string' do
14
+ "".hex_dump.should == ""
15
+ end
16
+
17
+ it 'with a short string' do
18
+ "This is a test.".hex_dump.should == "0x00000000 54 68 69 73 20 69 73 20 61 20 74 65 73 74 2e This is a test.\n"
19
+ end
20
+
21
+ it 'normal dump' do
22
+ str.hex_dump.should == <<-EOL
23
+ 0x00000000 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6f This is a test o
24
+ 0x00000010 66 20 74 68 65 20 65 6d 65 72 67 65 6e 63 79 20 f the emergency\040
25
+ 0x00000020 62 72 6f 61 64 63 61 73 74 20 73 79 73 74 65 6d broadcast system
26
+ 0x00000030 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 61 . This is only a
27
+ 0x00000040 20 74 65 73 74 2e test.
28
+ EOL
29
+ end
30
+
31
+ it 'passing object and method' do
32
+ str_out = ''
33
+ str.hex_dump(:obj => str_out, :meth => :<<)
34
+ str_out.should == <<-EOL
35
+ 0x00000000 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6f This is a test o
36
+ 0x00000010 66 20 74 68 65 20 65 6d 65 72 67 65 6e 63 79 20 f the emergency\040
37
+ 0x00000020 62 72 6f 61 64 63 61 73 74 20 73 79 73 74 65 6d broadcast system
38
+ 0x00000030 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 61 . This is only a
39
+ 0x00000040 20 74 65 73 74 2e test.
40
+ EOL
41
+ end
42
+
43
+ it 'passing :grouping => 8 option' do
44
+ str.hex_dump(:grouping => 8).should == <<-EOL
45
+ 0x00000000 54 68 69 73 20 69 73 20 This is\040
46
+ 0x00000008 61 20 74 65 73 74 20 6f a test o
47
+ 0x00000010 66 20 74 68 65 20 65 6d f the em
48
+ 0x00000018 65 72 67 65 6e 63 79 20 ergency\040
49
+ 0x00000020 62 72 6f 61 64 63 61 73 broadcas
50
+ 0x00000028 74 20 73 79 73 74 65 6d t system
51
+ 0x00000030 2e 20 54 68 69 73 20 69 . This i
52
+ 0x00000038 73 20 6f 6e 6c 79 20 61 s only a
53
+ 0x00000040 20 74 65 73 74 2e test.
54
+ EOL
55
+ end
56
+
57
+ it 'passing :newline => false option' do
58
+ str.hex_dump(:newline => false).should == "0x00000000 54 68 69 73 20 69 73 20 61 20 74 65 73 74 20 6f This is a test o0x00000010 66 20 74 68 65 20 65 6d 65 72 67 65 6e 63 79 20 f the emergency 0x00000020 62 72 6f 61 64 63 61 73 74 20 73 79 73 74 65 6d broadcast system0x00000030 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20 61 . This is only a0x00000040 20 74 65 73 74 2e test."
59
+ end
60
+
61
+ it 'dumping every possible character' do
62
+ expected = <<-EOL
63
+ 0x00000000 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f ................
64
+ 0x00000010 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f ................
65
+ 0x00000020 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f !\"\#$%&'()*+,-./
66
+ 0x00000030 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?
67
+ 0x00000040 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO
68
+ 0x00000050 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_
69
+ 0x00000060 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno
70
+ 0x00000070 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.
71
+ 0x00000080 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f ................
72
+ 0x00000090 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f ................
73
+ 0x000000a0 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af \240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257
74
+ 0x000000b0 b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf \260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277
75
+ 0x000000c0 c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf \300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317
76
+ 0x000000d0 d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df \320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337
77
+ 0x000000e0 e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef \340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357
78
+ 0x000000f0 f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff \360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377
79
+ EOL
80
+
81
+ str = ''
82
+ 0.upto(255) { |i| str << i.chr }
83
+ str.hex_dump.should == expected
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,21 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+
11
+ # Run specs in random order to surface order dependencies. If you find an
12
+ # order dependency and want to debug it, you can fix the order by providing
13
+ # the seed, which is printed after each run.
14
+ # --seed 1234
15
+ config.order = 'random'
16
+ end
17
+
18
+ require 'coveralls'
19
+ Coveralls.wear!
20
+
21
+ require 'more_core_extensions/all'