File: buffer_spec.rb

package info (click to toggle)
ruby-unparser 0.6.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 936 kB
  • sloc: ruby: 7,691; sh: 6; makefile: 4
file content (156 lines) | stat: -rw-r--r-- 3,532 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require 'spec_helper'

describe Unparser::Buffer do
  describe '#append' do
    subject { object.append(string) }

    let(:object) { described_class.new }
    let(:string) { 'foo' }

    specify do
      expect { subject }.to change { object.content }.from('').to('foo')
    end

    it 'should prefix with indentation if line is empty' do
      object.append('foo')
      object.nl
      object.indent
      object.append('bar')
      object.append('baz')
      expect(object.content).to eql("foo\n  barbaz")
    end

    it_should_behave_like 'a command method'
  end

  describe '#append_without_prefix' do
    subject { object.append_without_prefix(string) }

    let(:object) { described_class.new }
    let(:string) { 'foo' }

    specify do
      expect { subject }.to change { object.content }.from('').to('foo')
    end

    it 'should not prefix with indentation' do
      object.append_without_prefix('foo')
      object.nl
      object.indent
      object.append_without_prefix('bar')
      object.append_without_prefix('baz')
      expect(object.content).to eql("foo\nbarbaz")
    end

    it_should_behave_like 'a command method'
  end

  describe '#content' do
    subject { object.content }

    let(:object) { described_class.new }

    shared_examples_for 'buffer content' do
      it 'contains expected content' do
        should eql(expected_content)
      end

      it { should be_frozen }

      it 'returns fresh string copies' do
        first  = object.content
        second = object.content
        expect(first).to eql(second)
        expect(first).not_to be(second)
      end
    end

    context 'with empty buffer' do
      let(:expected_content) { '' }

      it_should_behave_like 'buffer content'
    end

    context 'with filled buffer' do
      before do
        object.append('foo')
      end

      let(:expected_content) { 'foo' }

      it_behaves_like 'buffer content'
    end
  end

  describe '#fresh_line?' do
    let(:object) { described_class.new }

    it 'should return true while buffer is empty' do
      expect(object.fresh_line?).to eql(true)
    end

    it 'should return false after content has been appended' do
      object.append('foo')
      expect(object.fresh_line?).to eql(false)
    end

    it 'should return true after a nl has been appended' do
      object.append('foo')
      object.nl
      expect(object.fresh_line?).to eql(true)
    end
  end

  describe '#indent' do
    let(:object) { described_class.new }

    subject { object.indent }

    it 'should indent with two spaces' do
      object.append('foo')
      object.nl
      object.indent
      object.append('bar')
      object.nl
      object.indent
      object.append('baz')
      expect(object.content).to eql("foo\n  bar\n    baz")
    end

    it_should_behave_like 'a command method'
  end

  describe '#nl' do
    let(:object) { described_class.new }

    subject { object.nl }

    it 'writes a newline' do
      object.append('foo')
      subject
      object.append('bar')
      expect(object.content).to eql("foo\nbar")
    end

    it_should_behave_like 'a command method'
  end

  describe '#unindent' do
    let(:object) { described_class.new }

    subject { object.unindent }

    it 'unindents two chars' do
      object.append('foo')
      object.nl
      object.indent
      object.append('bar')
      object.nl
      object.unindent
      object.append('baz')
      expect(object.content).to eql("foo\n  bar\nbaz")
    end

    it_should_behave_like 'a command method'
  end
end