File: matcher_spec.rb

package info (click to toggle)
ruby-generator-spec 0.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 176 kB
  • sloc: ruby: 538; makefile: 5
file content (299 lines) | stat: -rw-r--r-- 7,724 bytes parent folder | download | duplicates (3)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
require 'spec_helper'
require 'tmpdir'

TMP_ROOT = Pathname.new(Dir.tmpdir)

describe TestGenerator, 'using custom matcher' do
  include GeneratorSpec::TestCase
  destination TMP_ROOT
  arguments %w(test --test)

  before do
    delete_directory(TMP_ROOT)
    prepare_destination
    run_generator
  end

  specify do
    expect(destination_root).to have_structure {
      no_file 'test.rb'
      directory 'config' do
        directory 'initializers' do
          file 'test.rb' do
            contains '# Initializer'
          end
        end
      end
      directory 'db' do
        directory 'migrate' do
          file '123_create_tests.rb'
          migration 'create_tests' do
            contains 'class TestMigration'
          end
        end
      end
    }
  end

  it 'fails when it doesnt match' do
    expect {
      expect(destination_root).to have_structure {
        directory 'db' do
          directory 'migrate' do
            no_file '123_create_tests.rb'
          end
        end
      }
    }.to raise_error
  end
end

module GeneratorSpec
  module Matcher
    describe File do
      describe '#matches?' do
        before do
          delete_directory(TMP_ROOT)
        end

        let(:file) { File.new('test_file') }
        let(:location) { TMP_ROOT.join('test_file') }

        context 'with no contains' do
          it 'doesnt throw if the file exists' do
            write_file(location, '')
            expect {
              file.matches?(TMP_ROOT)
            }.to_not throw_symbol
          end

          it 'throws :failure if it doesnt exist' do
            expect {
              file.matches?(TMP_ROOT)
            }.to throw_symbol(:failure)
          end
        end

        context 'with contains' do
          before do
            write_file(location, 'class CreatePosts')
          end

          it 'doesnt throw if the content includes the string' do
            file.contains 'CreatePosts'
            expect {
              file.matches?(TMP_ROOT)
            }.to_not throw_symbol
          end

          it 'throws :failure if the contents dont include the string' do
            file.contains 'PostsMigration'
            expect {
              file.matches?(TMP_ROOT)
            }.to throw_symbol(:failure)
          end
        end
      end
    end

    describe Migration do
      describe '#matches?' do
        before do
          delete_directory(TMP_ROOT)
        end

        let(:migration) { Migration.new('create_posts') }
        let(:location) { TMP_ROOT.join('123456_create_posts.rb') }

        context 'with no contains' do
          it 'doesnt throw if the migration exists' do
            write_file(location, 'class CreatePosts')
            expect {
              migration.matches?(TMP_ROOT)
            }.to_not throw_symbol
          end

          it 'throws :failure if it doesnt exist' do
            expect {
              migration.matches?(TMP_ROOT)
            }.to throw_symbol(:failure)
          end
        end

        context 'with contains' do
          before do
            write_file(location, 'class CreatePosts')
          end

          it 'doesnt throw if the migration includes the given content' do
            migration.contains('CreatePosts')
            expect {
              migration.matches?(TMP_ROOT)
            }.to_not throw_symbol
          end

          it 'throws failure if the migration doesnt include the given content' do
            migration.contains('CreateNotes')
            expect {
              migration.matches?(TMP_ROOT)
            }.to throw_symbol(:failure)
          end
        end
      end
    end

    describe Directory do
      describe '#location' do
        it 'equals the full path' do
          expect(Directory.new('test').location('test_2')).to eq('test/test_2')
        end
      end

      describe '#directory' do
        context 'without a block' do
          it 'adds a directory name to the tree' do
            dir = Directory.new 'test' do
              directory 'dir'
            end
            expect(dir.tree['dir']).to eq(false)
          end
        end

        context 'with a block' do
          it 'add a directory object to the tree' do
            dir = Directory.new 'test' do
              directory 'dir' do
                directory 'test_2'
              end
            end
            expect(dir.tree['dir']).to be_an_instance_of(Directory)
            expect(dir.tree['dir'].tree['test_2']).to eq(false)
          end
        end
      end

      describe '#file' do
        it 'adds it to the tree' do
          dir = Directory.new 'test' do
            file 'test_file'
          end
          expect(dir.tree['test_file']).to be_an_instance_of(File)
        end
      end

      describe '#file' do
        it 'adds it to the tree' do
          dir = Directory.new 'test' do
            migration 'test_file'
          end
          expect(dir.tree['test_file']).to be_an_instance_of(Migration)
          expect(dir.tree['test_file'].instance_variable_get('@name')).to eq('test/test_file')
        end
      end

      describe '#matches?' do
        before do
          delete_directory(TMP_ROOT)
        end

        context 'with a directory name' do
          let(:dir) {
            Directory.new 'test' do
              directory 'test_2'
            end
          }

          it 'doesnt throw if the directory exists' do
            write_directory(TMP_ROOT.join('test/test_2'))
            expect {
              dir.matches?(TMP_ROOT)
            }.to_not throw_symbol
          end

          it 'throws :failure if it doesnt exist' do
            expect {
              dir.matches?(TMP_ROOT)
            }.to throw_symbol(:failure)
          end
        end

        context 'with a directory object' do
          let(:dir) {
            Directory.new 'test' do
              directory 'test_2' do
                file 'test_file'
              end
            end
          }

          before do
            delete_directory(TMP_ROOT)
            write_directory(TMP_ROOT.join('test/test_2'))
          end

          it 'doesnt throw if the file exists' do
            write_file(TMP_ROOT.join('test/test_2/test_file'), '')
            expect {
              dir.matches?(TMP_ROOT)
            }.to_not throw_symbol
          end

          it 'throws :failure if it doesnt exist' do
            expect {
              dir.matches?(TMP_ROOT)
            }.to throw_symbol(:failure)
          end
        end

        context '#no_file' do
          let(:dir) {
            Directory.new 'test' do
              no_file 'test_file'
            end
          }
          before do
            delete_directory(TMP_ROOT)
            write_directory(TMP_ROOT.join('test'))
          end

          it 'doesnt throw if the file exist' do
            expect {
              dir.matches?(TMP_ROOT)
            }.to_not throw_symbol
          end

          it 'throws if the file exists' do
            write_file(TMP_ROOT.join('test/test_file'), '')
            expect {
              dir.matches?(TMP_ROOT)
            }.to throw_symbol(:failure)
          end
        end
      end

    end

    describe Root do
      describe '#matches?' do
        before do
          delete_directory(TMP_ROOT)
        end

        let(:root) {
          Root.new 'test' do
            directory 'test_dir'
          end
        }

        it 'returns true on no failures' do
          write_directory(TMP_ROOT.join('test/test_dir'))
          expect(root.matches?(TMP_ROOT)).to be_truthy
        end

        it 'returns false on failures' do
          expect(root.matches?(TMP_ROOT)).to be_falsey
        end
      end
    end
  end
end