test_bench-output 2.1.1.3 → 3.0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/test_bench/output/comment_style.rb +61 -0
- data/lib/test_bench/output/controls/comment_style.rb +21 -0
- data/lib/test_bench/output/controls/event.rb +2 -2
- data/lib/test_bench/output/controls/events/aborted.rb +9 -0
- data/lib/test_bench/output/controls/events/commented.rb +21 -0
- data/lib/test_bench/output/controls/events/context_finished.rb +9 -0
- data/lib/test_bench/output/controls/events/context_started.rb +9 -0
- data/lib/test_bench/output/controls/events/detailed.rb +21 -0
- data/lib/test_bench/output/controls/events/failed.rb +9 -0
- data/lib/test_bench/output/controls/events/file_executed.rb +9 -0
- data/lib/test_bench/output/controls/events/file_not_found.rb +9 -0
- data/lib/test_bench/output/controls/events/file_queued.rb +9 -0
- data/lib/test_bench/output/controls/events/skipped.rb +9 -0
- data/lib/test_bench/output/controls/events/test_finished.rb +9 -0
- data/lib/test_bench/output/controls/events/test_started.rb +9 -0
- data/lib/test_bench/output/controls/random.rb +2 -2
- data/lib/test_bench/output/controls/session.rb +17 -0
- data/lib/test_bench/output/controls/status.rb +7 -0
- data/lib/test_bench/output/controls/style.rb +14 -6
- data/lib/test_bench/output/controls/text.rb +16 -4
- data/lib/test_bench/output/controls.rb +21 -5
- data/lib/test_bench/output/detail_policy.rb +44 -0
- data/lib/test_bench/output/device/null.rb +3 -3
- data/lib/test_bench/output/device/substitute.rb +19 -33
- data/lib/test_bench/output/device.rb +73 -0
- data/lib/test_bench/output/get.rb +30 -0
- data/lib/test_bench/output/level.rb +51 -0
- data/lib/test_bench/output/output.rb +540 -36
- data/lib/test_bench/output/writer/style.rb +3 -3
- data/lib/test_bench/output/writer/substitute.rb +10 -23
- data/lib/test_bench/output/writer.rb +54 -147
- data/lib/test_bench/output.rb +10 -4
- metadata +49 -21
- data/lib/test_bench/output/controls/data.rb +0 -49
- data/lib/test_bench/output/controls/device.rb +0 -27
- data/lib/test_bench/output/controls/output.rb +0 -34
- data/lib/test_bench/output/controls/styling.rb +0 -27
- data/lib/test_bench/output/digest.rb +0 -113
- data/lib/test_bench/output/writer/buffer.rb +0 -57
- data/lib/test_bench/output/writer/defaults.rb +0 -11
@@ -1,208 +1,115 @@
|
|
1
1
|
module TestBench
|
2
|
-
|
2
|
+
class Output
|
3
3
|
class Writer
|
4
4
|
def device
|
5
5
|
@device ||= Device::Substitute.build
|
6
6
|
end
|
7
7
|
attr_writer :device
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
alias :styling :styling_policy
|
13
|
-
attr_writer :styling_policy
|
14
|
-
|
15
|
-
def digest
|
16
|
-
@digest ||= Digest.new
|
17
|
-
end
|
18
|
-
attr_writer :digest
|
19
|
-
|
20
|
-
def sequence
|
21
|
-
@sequence ||= 0
|
22
|
-
end
|
23
|
-
attr_writer :sequence
|
24
|
-
|
25
|
-
def column_sequence
|
26
|
-
@column_sequence ||= 0
|
27
|
-
end
|
28
|
-
attr_writer :column_sequence
|
29
|
-
|
30
|
-
def buffer
|
31
|
-
@buffer ||= Buffer.new
|
32
|
-
end
|
33
|
-
attr_writer :buffer
|
9
|
+
attr_accessor :styling
|
10
|
+
alias :styling? :styling
|
34
11
|
|
35
|
-
def
|
36
|
-
|
12
|
+
def indentation_depth
|
13
|
+
@indentation_depth ||= 0
|
37
14
|
end
|
15
|
+
attr_writer :indentation_depth
|
38
16
|
|
39
|
-
def self.build(
|
40
|
-
device ||= Defaults.device
|
41
|
-
|
17
|
+
def self.build(styling: nil, device: nil)
|
42
18
|
instance = new
|
43
|
-
instance.device = device
|
44
|
-
instance.styling_policy = styling
|
45
19
|
|
46
|
-
|
20
|
+
Device.configure(instance, device:)
|
21
|
+
|
22
|
+
device = instance.device
|
47
23
|
|
48
|
-
|
24
|
+
styling = styling(styling, device)
|
25
|
+
instance.styling = styling
|
49
26
|
|
50
27
|
instance
|
51
28
|
end
|
52
29
|
|
53
|
-
def self.configure(receiver,
|
30
|
+
def self.configure(receiver, styling: nil, device: nil, attr_name: nil)
|
54
31
|
attr_name ||= :writer
|
55
32
|
|
56
|
-
|
57
|
-
instance = writer
|
58
|
-
else
|
59
|
-
instance = build(device, styling:, inert_digest:)
|
60
|
-
end
|
61
|
-
|
33
|
+
instance = build(styling:, device:)
|
62
34
|
receiver.public_send(:"#{attr_name}=", instance)
|
63
35
|
end
|
64
36
|
|
65
|
-
def
|
66
|
-
|
37
|
+
def self.styling(styling_or_styling_policy, device)
|
38
|
+
case styling_or_styling_policy
|
39
|
+
in true | false => styling
|
40
|
+
return styling
|
41
|
+
in :detect | :on | :off => styling_policy
|
42
|
+
in nil
|
43
|
+
styling_policy = Defaults.styling_policy
|
44
|
+
end
|
45
|
+
|
46
|
+
case styling_policy
|
47
|
+
in :detect
|
48
|
+
device.tty?
|
49
|
+
in :on
|
50
|
+
true
|
51
|
+
in :off
|
52
|
+
false
|
53
|
+
end
|
67
54
|
end
|
68
55
|
|
69
|
-
def
|
70
|
-
|
56
|
+
def indent
|
57
|
+
indentation_width = 2 * indentation_depth
|
58
|
+
|
59
|
+
print(' ' * indentation_width)
|
71
60
|
end
|
72
|
-
attr_writer :tty
|
73
|
-
alias :tty? :tty
|
74
61
|
|
75
62
|
def puts(text=nil)
|
76
63
|
if not text.nil?
|
77
|
-
text
|
78
|
-
|
79
|
-
print(text)
|
64
|
+
print(text.chomp)
|
80
65
|
end
|
81
66
|
|
82
67
|
style(:reset)
|
83
68
|
|
84
|
-
|
85
|
-
write("\e[0K")
|
86
|
-
end
|
87
|
-
|
88
|
-
write("\n")
|
89
|
-
|
90
|
-
self.column_sequence = 0
|
69
|
+
print("\n")
|
91
70
|
end
|
92
71
|
|
93
72
|
def style(style, *additional_styles)
|
94
|
-
|
95
|
-
control_codes = [control_code]
|
73
|
+
styles = [style, *additional_styles]
|
96
74
|
|
97
|
-
|
98
|
-
|
99
|
-
control_codes << control_code
|
75
|
+
control_codes = styles.map do |style|
|
76
|
+
Style.control_code(style)
|
100
77
|
end
|
101
78
|
|
102
79
|
if styling?
|
103
|
-
|
80
|
+
control_sequence = "\e[#{control_codes.join(';')}m"
|
81
|
+
|
82
|
+
print(control_sequence)
|
104
83
|
end
|
105
84
|
|
106
85
|
self
|
107
86
|
end
|
108
87
|
|
109
88
|
def print(text)
|
110
|
-
self.column_sequence += text.length
|
111
|
-
|
112
89
|
write(text)
|
113
90
|
|
114
91
|
self
|
115
92
|
end
|
116
93
|
|
117
|
-
def write(
|
118
|
-
|
119
|
-
bytes_written = write!(data)
|
120
|
-
else
|
121
|
-
bytes_written = buffer.receive(data)
|
122
|
-
end
|
123
|
-
|
124
|
-
self.sequence += bytes_written
|
125
|
-
|
126
|
-
data = data[0...bytes_written]
|
127
|
-
digest.update(data)
|
128
|
-
|
129
|
-
bytes_written
|
130
|
-
end
|
131
|
-
|
132
|
-
def write!(data)
|
133
|
-
device.write(data)
|
134
|
-
|
135
|
-
data.bytesize
|
136
|
-
end
|
137
|
-
|
138
|
-
def device_tty?
|
139
|
-
device.tty?
|
140
|
-
end
|
141
|
-
|
142
|
-
def flush
|
143
|
-
buffer.flush(device)
|
144
|
-
end
|
145
|
-
|
146
|
-
def sync=(sync)
|
147
|
-
@sync = sync
|
148
|
-
|
149
|
-
if sync
|
150
|
-
flush
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
def written?(data=nil)
|
155
|
-
if data.nil?
|
156
|
-
sequence > 0
|
157
|
-
else
|
158
|
-
digest.digest?(data)
|
159
|
-
end
|
94
|
+
def write(text)
|
95
|
+
device.write(text)
|
160
96
|
end
|
161
97
|
|
162
|
-
def
|
163
|
-
|
98
|
+
def increase_indentation
|
99
|
+
self.indentation_depth += 1
|
164
100
|
end
|
165
101
|
|
166
|
-
def
|
167
|
-
|
102
|
+
def decrease_indentation
|
103
|
+
self.indentation_depth -= 1
|
168
104
|
end
|
169
105
|
|
170
|
-
module
|
171
|
-
|
172
|
-
|
173
|
-
def self.styling?(policy, console)
|
174
|
-
assure_styling(policy, console)
|
175
|
-
end
|
176
|
-
|
177
|
-
def self.assure_styling(policy, console=nil)
|
178
|
-
console ||= false
|
179
|
-
|
180
|
-
case policy
|
181
|
-
when on
|
182
|
-
true
|
183
|
-
when off
|
184
|
-
false
|
185
|
-
when detect
|
186
|
-
console ? true : false
|
187
|
-
else
|
188
|
-
raise Error, "Unknown styling policy #{policy.inspect}"
|
189
|
-
end
|
190
|
-
end
|
106
|
+
module Defaults
|
107
|
+
def self.styling_policy
|
108
|
+
env_styling_policy = ENV.fetch('TEST_BENCH_OUTPUT_STYLING', 'detect')
|
191
109
|
|
192
|
-
|
193
|
-
def self.off = :off
|
194
|
-
def self.detect = :detect
|
195
|
-
|
196
|
-
def self.default
|
197
|
-
policy = ENV.fetch('TEST_BENCH_OUTPUT_STYLING') do
|
198
|
-
return default!
|
199
|
-
end
|
200
|
-
|
201
|
-
policy.to_sym
|
202
|
-
end
|
110
|
+
styling_policy = env_styling_policy.to_sym
|
203
111
|
|
204
|
-
|
205
|
-
:detect
|
112
|
+
styling_policy
|
206
113
|
end
|
207
114
|
end
|
208
115
|
end
|
data/lib/test_bench/output.rb
CHANGED
@@ -1,14 +1,20 @@
|
|
1
|
-
require '
|
1
|
+
require 'import_constants'
|
2
2
|
|
3
|
-
require 'test_bench/
|
3
|
+
require 'test_bench/session'
|
4
4
|
|
5
5
|
require 'test_bench/output/device/null'
|
6
6
|
require 'test_bench/output/device/substitute'
|
7
|
+
require 'test_bench/output/device'
|
7
8
|
|
8
|
-
require 'test_bench/output/writer/buffer'
|
9
9
|
require 'test_bench/output/writer'
|
10
10
|
require 'test_bench/output/writer/style'
|
11
11
|
require 'test_bench/output/writer/substitute'
|
12
|
-
|
12
|
+
|
13
|
+
require 'test_bench/output/comment_style'
|
14
|
+
require 'test_bench/output/detail_policy'
|
15
|
+
|
16
|
+
require 'test_bench/output/level'
|
13
17
|
|
14
18
|
require 'test_bench/output/output'
|
19
|
+
|
20
|
+
require 'test_bench/output/get'
|
metadata
CHANGED
@@ -1,17 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: test_bench-output
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
8
|
-
autorequire:
|
7
|
+
- Brightworks Digital
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
12
10
|
dependencies:
|
13
11
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
12
|
+
name: import_constants
|
15
13
|
requirement: !ruby/object:Gem::Requirement
|
16
14
|
requirements:
|
17
15
|
- - ">="
|
@@ -25,7 +23,21 @@ dependencies:
|
|
25
23
|
- !ruby/object:Gem::Version
|
26
24
|
version: '0'
|
27
25
|
- !ruby/object:Gem::Dependency
|
28
|
-
name: test_bench-
|
26
|
+
name: test_bench-session
|
27
|
+
requirement: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '0'
|
32
|
+
type: :runtime
|
33
|
+
prerelease: false
|
34
|
+
version_requirements: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: test_bench-bootstrap
|
29
41
|
requirement: !ruby/object:Gem::Requirement
|
30
42
|
requirements:
|
31
43
|
- - ">="
|
@@ -38,41 +50,58 @@ dependencies:
|
|
38
50
|
- - ">="
|
39
51
|
- !ruby/object:Gem::Version
|
40
52
|
version: '0'
|
41
|
-
description:
|
42
|
-
email:
|
53
|
+
description: Test output writer for TestBench.
|
54
|
+
email: development@bright.works
|
43
55
|
executables: []
|
44
56
|
extensions: []
|
45
57
|
extra_rdoc_files: []
|
46
58
|
files:
|
47
59
|
- lib/test_bench
|
48
60
|
- lib/test_bench/output
|
61
|
+
- lib/test_bench/output/comment_style.rb
|
49
62
|
- lib/test_bench/output/controls
|
50
|
-
- lib/test_bench/output/controls/
|
51
|
-
- lib/test_bench/output/controls/device.rb
|
63
|
+
- lib/test_bench/output/controls/comment_style.rb
|
52
64
|
- lib/test_bench/output/controls/event.rb
|
53
|
-
- lib/test_bench/output/controls/
|
65
|
+
- lib/test_bench/output/controls/events
|
66
|
+
- lib/test_bench/output/controls/events/aborted.rb
|
67
|
+
- lib/test_bench/output/controls/events/commented.rb
|
68
|
+
- lib/test_bench/output/controls/events/context_finished.rb
|
69
|
+
- lib/test_bench/output/controls/events/context_started.rb
|
70
|
+
- lib/test_bench/output/controls/events/detailed.rb
|
71
|
+
- lib/test_bench/output/controls/events/failed.rb
|
72
|
+
- lib/test_bench/output/controls/events/file_executed.rb
|
73
|
+
- lib/test_bench/output/controls/events/file_not_found.rb
|
74
|
+
- lib/test_bench/output/controls/events/file_queued.rb
|
75
|
+
- lib/test_bench/output/controls/events/skipped.rb
|
76
|
+
- lib/test_bench/output/controls/events/test_finished.rb
|
77
|
+
- lib/test_bench/output/controls/events/test_started.rb
|
54
78
|
- lib/test_bench/output/controls/random.rb
|
79
|
+
- lib/test_bench/output/controls/session.rb
|
80
|
+
- lib/test_bench/output/controls/status.rb
|
55
81
|
- lib/test_bench/output/controls/style.rb
|
56
|
-
- lib/test_bench/output/controls/styling.rb
|
57
82
|
- lib/test_bench/output/controls/text.rb
|
58
83
|
- lib/test_bench/output/controls.rb
|
84
|
+
- lib/test_bench/output/detail_policy.rb
|
59
85
|
- lib/test_bench/output/device
|
60
86
|
- lib/test_bench/output/device/null.rb
|
61
87
|
- lib/test_bench/output/device/substitute.rb
|
62
|
-
- lib/test_bench/output/
|
88
|
+
- lib/test_bench/output/device.rb
|
89
|
+
- lib/test_bench/output/get.rb
|
90
|
+
- lib/test_bench/output/level.rb
|
63
91
|
- lib/test_bench/output/output.rb
|
64
92
|
- lib/test_bench/output/writer
|
65
|
-
- lib/test_bench/output/writer/buffer.rb
|
66
|
-
- lib/test_bench/output/writer/defaults.rb
|
67
93
|
- lib/test_bench/output/writer/style.rb
|
68
94
|
- lib/test_bench/output/writer/substitute.rb
|
69
95
|
- lib/test_bench/output/writer.rb
|
70
96
|
- lib/test_bench/output.rb
|
71
|
-
homepage:
|
97
|
+
homepage: http://test-bench.software
|
72
98
|
licenses:
|
73
99
|
- MIT
|
74
|
-
metadata:
|
75
|
-
|
100
|
+
metadata:
|
101
|
+
homepage_uri: http://test-bench.software
|
102
|
+
source_code_uri: https://github.com/test-bench-demo/test-bench-output
|
103
|
+
allowed_push_host: https://rubygems.org
|
104
|
+
namespace: TestBench::Output
|
76
105
|
rdoc_options: []
|
77
106
|
require_paths:
|
78
107
|
- lib
|
@@ -87,8 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
116
|
- !ruby/object:Gem::Version
|
88
117
|
version: '0'
|
89
118
|
requirements: []
|
90
|
-
rubygems_version: 3.
|
91
|
-
signing_key:
|
119
|
+
rubygems_version: 3.6.9
|
92
120
|
specification_version: 4
|
93
|
-
summary:
|
121
|
+
summary: Test output writer for TestBench
|
94
122
|
test_files: []
|
@@ -1,49 +0,0 @@
|
|
1
|
-
module TestBench
|
2
|
-
module Output
|
3
|
-
module Controls
|
4
|
-
module Data
|
5
|
-
def self.example
|
6
|
-
"some-data"
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.random
|
10
|
-
suffix = Random.string
|
11
|
-
|
12
|
-
"#{example}-#{suffix}"
|
13
|
-
end
|
14
|
-
|
15
|
-
module Digest
|
16
|
-
def self.example
|
17
|
-
'0123456789:;<=>'
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.digest
|
21
|
-
0x3031323334353637 + 0x0038393A3B3C3D3E
|
22
|
-
end
|
23
|
-
|
24
|
-
module Equivalent
|
25
|
-
module SameLength
|
26
|
-
def self.example
|
27
|
-
'0123456789:;<=>'
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.digest
|
31
|
-
Digest.digest
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
module DifferentLength
|
36
|
-
def self.example
|
37
|
-
[Digest.digest].pack('Q>')
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.digest
|
41
|
-
Digest.digest
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module TestBench
|
2
|
-
module Output
|
3
|
-
module Controls
|
4
|
-
module Device
|
5
|
-
def self.example
|
6
|
-
TestBench::Output::Device::Substitute.build
|
7
|
-
end
|
8
|
-
|
9
|
-
module TTY
|
10
|
-
def self.example(tty: nil)
|
11
|
-
tty = true if tty.nil?
|
12
|
-
|
13
|
-
device = Device.example
|
14
|
-
device.tty! if tty
|
15
|
-
device
|
16
|
-
end
|
17
|
-
|
18
|
-
module Non
|
19
|
-
def self.example
|
20
|
-
TTY.example(tty: false)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module TestBench
|
2
|
-
module Output
|
3
|
-
module Controls
|
4
|
-
module Output
|
5
|
-
def self.example_class(&block)
|
6
|
-
Class.new do
|
7
|
-
include TestBench::Output
|
8
|
-
|
9
|
-
class_exec(&block)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
class Example
|
14
|
-
include TestBench::Output
|
15
|
-
end
|
16
|
-
|
17
|
-
module Configure
|
18
|
-
class Example
|
19
|
-
include TestBench::Output
|
20
|
-
attr_accessor :some_attr
|
21
|
-
|
22
|
-
def configure(some_attr: nil, **arguments)
|
23
|
-
self.some_attr = some_attr
|
24
|
-
end
|
25
|
-
|
26
|
-
def configured?(some_attr)
|
27
|
-
self.some_attr == some_attr
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module TestBench
|
2
|
-
module Output
|
3
|
-
module Controls
|
4
|
-
module Styling
|
5
|
-
def self.example
|
6
|
-
Writer::Styling.on
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.other_example
|
10
|
-
Writer::Styling.off
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.random
|
14
|
-
stylings = [
|
15
|
-
Writer::Styling.on,
|
16
|
-
Writer::Styling.off,
|
17
|
-
Writer::Styling.detect
|
18
|
-
]
|
19
|
-
|
20
|
-
index = Random.integer % stylings.count
|
21
|
-
|
22
|
-
stylings[index]
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,113 +0,0 @@
|
|
1
|
-
module TestBench
|
2
|
-
module Output
|
3
|
-
class Digest
|
4
|
-
def last_digest
|
5
|
-
@last_digest ||= 0
|
6
|
-
end
|
7
|
-
attr_writer :last_digest
|
8
|
-
|
9
|
-
def buffer
|
10
|
-
@buffer ||= String.new(encoding: 'BINARY', capacity: Defaults.size_bytes * 2)
|
11
|
-
end
|
12
|
-
attr_writer :buffer
|
13
|
-
|
14
|
-
def self.digest(data)
|
15
|
-
instance = new
|
16
|
-
instance.update(data)
|
17
|
-
instance.digest
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.configure(receiver, inert: nil, attr_name: nil)
|
21
|
-
attr_name ||= :digest
|
22
|
-
inert = true if inert.nil?
|
23
|
-
|
24
|
-
if inert
|
25
|
-
instance = Null.new
|
26
|
-
else
|
27
|
-
instance = self.new
|
28
|
-
end
|
29
|
-
|
30
|
-
receiver.public_send(:"#{attr_name}=", instance)
|
31
|
-
end
|
32
|
-
|
33
|
-
def clone
|
34
|
-
cloned_digest = Digest.new
|
35
|
-
cloned_digest.last_digest = last_digest
|
36
|
-
cloned_digest.buffer << buffer
|
37
|
-
cloned_digest
|
38
|
-
end
|
39
|
-
|
40
|
-
def update(data)
|
41
|
-
digest_size_bytes = Defaults.size_bytes
|
42
|
-
|
43
|
-
(0..data.bytesize).step(digest_size_bytes).each do |position|
|
44
|
-
next_bytes = data.byteslice(position, digest_size_bytes)
|
45
|
-
next_bytes.force_encoding('BINARY')
|
46
|
-
|
47
|
-
buffer << next_bytes
|
48
|
-
|
49
|
-
if buffer.length >= digest_size_bytes
|
50
|
-
self.last_digest = digest
|
51
|
-
|
52
|
-
buffer.slice!(0, 8)
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
digest
|
57
|
-
end
|
58
|
-
|
59
|
-
def digest
|
60
|
-
buffer_bytes = buffer.unpack('C*')[0...8]
|
61
|
-
|
62
|
-
buffer_int64 = buffer_bytes.reduce(0) do |int64, byte|
|
63
|
-
(int64 << 8) | byte
|
64
|
-
end
|
65
|
-
|
66
|
-
(last_digest + buffer_int64) % (256 ** Defaults.size_bytes - 1)
|
67
|
-
end
|
68
|
-
alias :value :digest
|
69
|
-
alias :to_i :digest
|
70
|
-
|
71
|
-
def digest?(data)
|
72
|
-
other_digest = self.class.digest(data)
|
73
|
-
|
74
|
-
digest == other_digest
|
75
|
-
end
|
76
|
-
|
77
|
-
def format
|
78
|
-
self.class.format(value)
|
79
|
-
end
|
80
|
-
alias :to_s :format
|
81
|
-
|
82
|
-
def self.format(digest)
|
83
|
-
if digest < 0
|
84
|
-
# 0x8000...
|
85
|
-
sign_mask = 0x1 << (8 * Defaults.size_bytes - 1)
|
86
|
-
# 0x7FFF...
|
87
|
-
mask = (256 ** Defaults.size_bytes - 1) ^ sign_mask
|
88
|
-
|
89
|
-
digest_uint64 = ~digest & mask | sign_mask
|
90
|
-
else
|
91
|
-
digest_uint64 = digest
|
92
|
-
end
|
93
|
-
|
94
|
-
"0x%016X" % digest_uint64
|
95
|
-
end
|
96
|
-
|
97
|
-
class Null
|
98
|
-
def update(_data)
|
99
|
-
end
|
100
|
-
|
101
|
-
def digest?(_data)
|
102
|
-
false
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
module Defaults
|
107
|
-
def self.size_bytes
|
108
|
-
8
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
end
|