Skip to content

Commit 34f26fc

Browse files
committed
Add EncodedString#concat
An array-like API to add a bunch of things at once
1 parent 362c9d6 commit 34f26fc

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/ttfunk/encoded_string.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require 'stringio'
4+
require_relative 'placeholder'
45

56
module TTFunk
67
class UnresolvedPlaceholderError < StandardError
@@ -27,12 +28,19 @@ def <<(obj)
2728
add_placeholder(placeholder.dup, placeholder.position + io.length)
2829
end
2930

30-
self << obj.unresolved_string
31+
io << obj.unresolved_string
3132
end
3233

3334
self
3435
end
3536

37+
def concat(*objs)
38+
objs.each do |obj|
39+
self << obj
40+
end
41+
self
42+
end
43+
3644
def align!(width = 4)
3745
if (length % width).positive?
3846
self << "\0" * (width - length % width)

spec/ttfunk/encoded_string_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,24 @@
4949
end
5050
end
5151

52+
describe '#concat' do
53+
it 'adds all arguments' do
54+
encoded_string = described_class.new
55+
encoded_string.concat("\00", "\01", "\02", TTFunk::Placeholder.new(:foo))
56+
57+
expect(encoded_string.__send__(:io).string).to eq("\00\01\02\00")
58+
expect(encoded_string.placeholders[:foo]).to_not be_nil
59+
end
60+
61+
it 'returns self' do
62+
empty_encoded_string = described_class.new
63+
encoded_string = empty_encoded_string.concat("\00", "\01", "\02")
64+
65+
expect(encoded_string.string).to eq("\00\01\02")
66+
expect(encoded_string).to equal(empty_encoded_string)
67+
end
68+
end
69+
5270
describe '#length' do
5371
it 'retrieves the number of bytes written' do
5472
encoded_string << 'foo'
@@ -71,6 +89,21 @@
7189
end
7290
end
7391

92+
describe '#bytes' do
93+
it 'retrieves the encoded string bytes' do
94+
encoded_string << 'foo'
95+
expect(encoded_string.bytes).to eq([0x66, 0x6f, 0x6f])
96+
end
97+
98+
it "raises an error if any placeholders haven't been resolved" do
99+
encoded_string << 'foo'
100+
encoded_string << TTFunk::Placeholder.new(:name)
101+
expect { encoded_string.bytes }.to(
102+
raise_error(TTFunk::UnresolvedPlaceholderError)
103+
)
104+
end
105+
end
106+
74107
describe '#resolve_placeholder' do
75108
it 'replaces the placeholder bytes' do
76109
encoded_string << '123'

0 commit comments

Comments
 (0)