hexdump 0.3.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +5 -6
- data/.gitignore +1 -0
- data/.yardopts +1 -1
- data/ChangeLog.md +79 -6
- data/Gemfile +3 -0
- data/LICENSE.txt +1 -1
- data/README.md +500 -137
- data/benchmark.rb +29 -22
- data/gemspec.yml +2 -1
- data/hexdump.gemspec +1 -4
- data/lib/hexdump/chars.rb +46 -0
- data/lib/hexdump/core_ext/file.rb +68 -6
- data/lib/hexdump/core_ext/io.rb +2 -2
- data/lib/hexdump/core_ext/kernel.rb +5 -0
- data/lib/hexdump/core_ext/string.rb +2 -2
- data/lib/hexdump/core_ext/string_io.rb +2 -2
- data/lib/hexdump/core_ext.rb +5 -4
- data/lib/hexdump/format_string.rb +43 -0
- data/lib/hexdump/hexdump.rb +766 -75
- data/lib/hexdump/mixin.rb +192 -0
- data/lib/hexdump/module_methods.rb +132 -0
- data/lib/hexdump/numeric/binary.rb +55 -0
- data/lib/hexdump/numeric/char_or_int.rb +95 -0
- data/lib/hexdump/numeric/decimal.rb +56 -0
- data/lib/hexdump/numeric/exceptions.rb +11 -0
- data/lib/hexdump/numeric/hexadecimal.rb +59 -0
- data/lib/hexdump/numeric/octal.rb +55 -0
- data/lib/hexdump/numeric.rb +5 -0
- data/lib/hexdump/reader.rb +313 -0
- data/lib/hexdump/theme/ansi.rb +82 -0
- data/lib/hexdump/theme/rule.rb +159 -0
- data/lib/hexdump/theme.rb +61 -0
- data/lib/hexdump/type.rb +233 -0
- data/lib/hexdump/types.rb +108 -0
- data/lib/hexdump/version.rb +1 -1
- data/lib/hexdump.rb +14 -3
- data/spec/chars_spec.rb +76 -0
- data/spec/core_ext_spec.rb +10 -6
- data/spec/format_string_spec.rb +22 -0
- data/spec/hexdump_class_spec.rb +1708 -0
- data/spec/hexdump_module_spec.rb +23 -0
- data/spec/mixin_spec.rb +37 -0
- data/spec/numeric/binary_spec.rb +239 -0
- data/spec/numeric/char_or_int_spec.rb +210 -0
- data/spec/numeric/decimal_spec.rb +317 -0
- data/spec/numeric/hexadecimal_spec.rb +320 -0
- data/spec/numeric/octal_spec.rb +239 -0
- data/spec/reader_spec.rb +866 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/theme/ansi_spec.rb +242 -0
- data/spec/theme/rule_spec.rb +199 -0
- data/spec/theme_spec.rb +94 -0
- data/spec/type_spec.rb +317 -0
- data/spec/types_spec.rb +904 -0
- metadata +42 -12
- data/.gemtest +0 -0
- data/lib/hexdump/dumper.rb +0 -419
- data/lib/hexdump/extensions.rb +0 -2
- data/spec/dumper_spec.rb +0 -329
- data/spec/hexdump_spec.rb +0 -30
@@ -0,0 +1,239 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hexdump/numeric/octal'
|
3
|
+
require 'hexdump/type'
|
4
|
+
|
5
|
+
describe Hexdump::Numeric::Octal do
|
6
|
+
describe "#initialize" do
|
7
|
+
subject { described_class.new(type) }
|
8
|
+
|
9
|
+
context "when given a Type::Int type" do
|
10
|
+
context "and size is 1" do
|
11
|
+
let(:type) { Hexdump::Type::Int8.new }
|
12
|
+
|
13
|
+
it "must set #width to 3 + 1" do
|
14
|
+
expect(subject.width).to eq(3 + 1)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "and size is 2" do
|
19
|
+
let(:type) { Hexdump::Type::Int16.new }
|
20
|
+
|
21
|
+
it "must set #width to 6 + 1" do
|
22
|
+
expect(subject.width).to eq(6 + 1)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "and size is 4" do
|
27
|
+
let(:type) { Hexdump::Type::Int32.new }
|
28
|
+
|
29
|
+
it "must set #width to 11 + 1" do
|
30
|
+
expect(subject.width).to eq(11 + 1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "and size is 8" do
|
35
|
+
let(:type) { Hexdump::Type::Int64.new }
|
36
|
+
|
37
|
+
it "must set #width to 22 + 1" do
|
38
|
+
expect(subject.width).to eq(22 + 1)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "but the type has an unsupported size" do
|
43
|
+
class UnsupportedIntType < Hexdump::Type::Int
|
44
|
+
|
45
|
+
def initialize
|
46
|
+
super(size: 3)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
let(:type) { UnsupportedIntType.new }
|
52
|
+
|
53
|
+
it do
|
54
|
+
expect { described_class.new(type) }.to raise_error(NotImplementedError)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when given a Type::UInt type" do
|
60
|
+
context "and size is 1" do
|
61
|
+
let(:type) { Hexdump::Type::UInt8.new }
|
62
|
+
|
63
|
+
it "must set #width to 3" do
|
64
|
+
expect(subject.width).to eq(3)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "and size is 2" do
|
69
|
+
let(:type) { Hexdump::Type::UInt16.new }
|
70
|
+
|
71
|
+
it "must set #width to 6" do
|
72
|
+
expect(subject.width).to eq(6)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "and size is 4" do
|
77
|
+
let(:type) { Hexdump::Type::UInt32.new }
|
78
|
+
|
79
|
+
it "must set #width to 11" do
|
80
|
+
expect(subject.width).to eq(11)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "and size is 8" do
|
85
|
+
let(:type) { Hexdump::Type::UInt64.new }
|
86
|
+
|
87
|
+
it "must set #width to 22" do
|
88
|
+
expect(subject.width).to eq(22)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context "but the type has an unsupported size" do
|
93
|
+
class UnsupportedUIntType < Hexdump::Type::UInt
|
94
|
+
|
95
|
+
def initialize
|
96
|
+
super(size: 3)
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
let(:type) { UnsupportedUIntType.new }
|
102
|
+
|
103
|
+
it do
|
104
|
+
expect { described_class.new(type) }.to raise_error(NotImplementedError)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context "when given a Type::Float type" do
|
110
|
+
let(:type) { Hexdump::Type::Float32.new }
|
111
|
+
|
112
|
+
it do
|
113
|
+
expect {
|
114
|
+
described_class.new(type)
|
115
|
+
}.to raise_error(Hexdump::Numeric::IncompatibleTypeError,"cannot format floating-point numbers in octal")
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context "when given a non-Type object" do
|
120
|
+
let(:type) { Object.new }
|
121
|
+
|
122
|
+
it do
|
123
|
+
expect { described_class.new(type) }.to raise_error(TypeError)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "#%" do
|
129
|
+
subject { described_class.new(type) }
|
130
|
+
|
131
|
+
context "when the given type has size 1" do
|
132
|
+
let(:type) { Hexdump::Type::UInt8.new }
|
133
|
+
|
134
|
+
let(:value) { 0xf }
|
135
|
+
let(:octal) { '017' }
|
136
|
+
|
137
|
+
it "must return a octal string of length 8" do
|
138
|
+
expect(subject % value).to eq(octal)
|
139
|
+
end
|
140
|
+
|
141
|
+
context "and is signed" do
|
142
|
+
let(:type) { Hexdump::Type::Int8.new }
|
143
|
+
|
144
|
+
context "and the value is positive" do
|
145
|
+
it "must return a octal string of length 8 prefixed with a ' '" do
|
146
|
+
expect(subject % value).to eq(" #{octal}")
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context "and the value is negative" do
|
151
|
+
it "must return a octal string of length 8 prefixed with a '-'" do
|
152
|
+
expect(subject % -value).to eq("-#{octal}")
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
context "when the given type has size 2" do
|
159
|
+
let(:type) { Hexdump::Type::UInt16.new }
|
160
|
+
|
161
|
+
let(:value) { 0xff }
|
162
|
+
let(:octal) { '000377' }
|
163
|
+
|
164
|
+
it "must return a octal string of length 16" do
|
165
|
+
expect(subject % value).to eq(octal)
|
166
|
+
end
|
167
|
+
|
168
|
+
context "and is signed" do
|
169
|
+
let(:type) { Hexdump::Type::Int16.new }
|
170
|
+
|
171
|
+
context "and the value is positive" do
|
172
|
+
it "must return a octal string of length 16 prefixed with a ' '" do
|
173
|
+
expect(subject % value).to eq(" #{octal}")
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
context "and the value is negative" do
|
178
|
+
it "must return a octal string of length 16 prefixed with a '-'" do
|
179
|
+
expect(subject % -value).to eq("-#{octal}")
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context "when the given type has size 4" do
|
186
|
+
let(:type) { Hexdump::Type::UInt32.new }
|
187
|
+
|
188
|
+
let(:value) { 0xffff }
|
189
|
+
let(:octal) { '00000177777' }
|
190
|
+
|
191
|
+
it "must return a octal string of length 32" do
|
192
|
+
expect(subject % value).to eq(octal)
|
193
|
+
end
|
194
|
+
|
195
|
+
context "and is signed" do
|
196
|
+
let(:type) { Hexdump::Type::Int32.new }
|
197
|
+
|
198
|
+
context "and the value is positive" do
|
199
|
+
it "must return a octal string of length 32 prefixed with a ' '" do
|
200
|
+
expect(subject % value).to eq(" #{octal}")
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context "and the value is negative" do
|
205
|
+
it "must return a octal string of length 32 prefixed with a '-'" do
|
206
|
+
expect(subject % -value).to eq("-#{octal}")
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context "when the given type has size 8" do
|
213
|
+
let(:type) { Hexdump::Type::UInt64.new }
|
214
|
+
|
215
|
+
let(:value) { 0xffffffff }
|
216
|
+
let(:octal) { '0000000000037777777777' }
|
217
|
+
|
218
|
+
it "must return a octal string of length 64" do
|
219
|
+
expect(subject % value).to eq(octal)
|
220
|
+
end
|
221
|
+
|
222
|
+
context "and is signed" do
|
223
|
+
let(:type) { Hexdump::Type::Int64.new }
|
224
|
+
|
225
|
+
context "and the value is positive" do
|
226
|
+
it "must return a octal string of length 64 prefixed with a ' '" do
|
227
|
+
expect(subject % value).to eq(" #{octal}")
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
context "and the value is negative" do
|
232
|
+
it "must return a octal string of length 64 prefixed with a '-'" do
|
233
|
+
expect(subject % -value).to eq("-#{octal}")
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|