File: encoded_string_spec.rb

package info (click to toggle)
ruby-ttfunk 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,472 kB
  • sloc: ruby: 7,954; makefile: 7
file content (117 lines) | stat: -rw-r--r-- 3,629 bytes parent folder | download
1
2
3
4
5
6
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

require 'spec_helper'
require 'ttfunk/encoded_string'

RSpec.describe TTFunk::EncodedString do
  subject(:encoded_string) { described_class.new }

  describe '#<<' do
    it 'writes the given string' do
      encoded_string << 'foo'
      expect(encoded_string.string).to eq('foo')
    end

    it 'adds the given placeholder' do
      encoded_string << 'abc'
      encoded_string << TTFunk::Placeholder.new(:foo)
      placeholder = encoded_string.placeholders[:foo]
      expect(placeholder.position).to eq(3)
    end

    it 'allows shoveling on other instances of EncodedString' do
      other =
        described_class.new do |str|
          str << 'abc'
          str << TTFunk::Placeholder.new(:foo, length: 3)
        end

      expect(other.placeholders[:foo].position).to eq(3)

      encoded_string << 'def'
      encoded_string << other

      # carries over the placeholder and adjusts its position
      expect(encoded_string.placeholders[:foo].position).to eq(6)
    end

    it 'does not allow adding two placeholders with the same name' do
      encoded_string << TTFunk::Placeholder.new(:foo)
      expect { encoded_string << TTFunk::Placeholder.new(:foo) }.to raise_error(TTFunk::DuplicatePlaceholderError)
    end

    it 'adds padding bytes when adding a placeholder' do
      encoded_string << 'abc'
      encoded_string << TTFunk::Placeholder.new(:foo, length: 3)
      expect(encoded_string.__send__(:io).string).to eq("abc\0\0\0")
    end
  end

  describe '#concat' do
    it 'adds all arguments' do
      encoded_string = described_class.new
      encoded_string.concat("\00", "\01", "\02", TTFunk::Placeholder.new(:foo))

      expect(encoded_string.__send__(:io).string).to eq("\00\01\02\00")
      expect(encoded_string.placeholders[:foo]).to_not be_nil
    end

    it 'returns self' do
      empty_encoded_string = described_class.new
      encoded_string = empty_encoded_string.concat("\00", "\01", "\02")

      expect(encoded_string.string).to eq("\00\01\02")
      expect(encoded_string).to equal(empty_encoded_string)
    end
  end

  describe '#length' do
    it 'retrieves the number of bytes written' do
      encoded_string << 'foo'
      expect(encoded_string.length).to eq(3)
    end
  end

  describe '#string' do
    it 'retrieves the encoded string value' do
      encoded_string << 'foo'
      expect(encoded_string.string).to eq('foo')
    end

    it "raises an error if any placeholders haven't been resolved" do
      encoded_string << 'foo'
      encoded_string << TTFunk::Placeholder.new(:name)
      expect { encoded_string.string }.to raise_error(TTFunk::UnresolvedPlaceholderError)
    end
  end

  describe '#bytes' do
    it 'retrieves the encoded string bytes' do
      encoded_string << 'foo'
      expect(encoded_string.bytes).to eq([0x66, 0x6f, 0x6f])
    end

    it "raises an error if any placeholders haven't been resolved" do
      encoded_string << 'foo'
      encoded_string << TTFunk::Placeholder.new(:name)
      expect { encoded_string.bytes }.to raise_error(TTFunk::UnresolvedPlaceholderError)
    end
  end

  describe '#resolve_placeholder' do
    it 'replaces the placeholder bytes' do
      encoded_string << '123'
      encoded_string << TTFunk::Placeholder.new(:name, length: 3)
      encoded_string << '456def'
      encoded_string.resolve_placeholder(:name, 'ghi')
      expect(encoded_string.string).to eq('123ghi456def')
    end
  end

  describe '#align!' do
    it 'byte-aligns the string by padding it' do
      encoded_string << 'abc'
      expect(encoded_string.align!.string).to eq("abc\0")
    end
  end
end