generator_spec 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,4 +23,35 @@ Add 'generator_spec' to Gemfile and use just like you would test generators in t
23
23
  it "creates a test initializer" do
24
24
  assert_file "config/initializers/test.rb", "# Initializer"
25
25
  end
26
+ end
27
+
28
+ An RSpec file matching DSL is also provided, taken with permission from [beard](https://github.com/carlhuda/beard/blob/master/spec/support/matcher.rb) by [carlhuda](https://github.com/carlhuda).
29
+
30
+ describe TestGenerator, "using custom matcher" do
31
+ include GeneratorSpec::GeneratorExampleGroup
32
+
33
+ destination File.expand_path("../../tmp", __FILE__)
34
+
35
+ before do
36
+ prepare_destination
37
+ run_generator
38
+ end
39
+
40
+ specify do
41
+ destination_root.should have_structure {
42
+ no_file "test.rb"
43
+ directory "config" do
44
+ directory "initializers" do
45
+ file "test.rb" do
46
+ contains "# Initializer"
47
+ end
48
+ end
49
+ end
50
+ directory "db" do
51
+ directory "migrate" do
52
+ file "123_create_tests.rb"
53
+ end
54
+ end
55
+ }
56
+ end
26
57
  end
@@ -1,6 +1,7 @@
1
1
  require "rspec/rails"
2
2
  require "rails/generators/test_case"
3
3
  require "fileutils"
4
+ require "generator_spec/matcher"
4
5
 
5
6
  # Adapted from Rails::Generators::TestCase
6
7
  # https://github.com/rails/rails/blob/master/railties/lib/rails/generators/test_case.rb
@@ -9,6 +10,7 @@ module GeneratorSpec
9
10
  module GeneratorExampleGroup
10
11
  extend ActiveSupport::Concern
11
12
  include RSpec::Rails::RailsExampleGroup
13
+ include Matcher
12
14
  include FileUtils
13
15
 
14
16
  included do
@@ -0,0 +1,108 @@
1
+ module GeneratorSpec
2
+ module Matcher
3
+ # Author: Yahuda Katz
4
+ # Taken (with permission) from beard:
5
+ # https://github.com/carlhuda/beard
6
+
7
+ class File
8
+ def initialize(name, &block)
9
+ @contents = []
10
+ @name = name
11
+
12
+ if block_given?
13
+ instance_eval(&block)
14
+ end
15
+ end
16
+
17
+ def contains(text)
18
+ @contents << text
19
+ end
20
+
21
+ def matches?(root)
22
+ unless root.join(@name).exist?
23
+ throw :failure, root.join(@name)
24
+ end
25
+
26
+ contents = ::File.read(root.join(@name))
27
+
28
+ @contents.each do |string|
29
+ unless contents.include?(string)
30
+ throw :failure, [root.join(@name), string, contents]
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ class Directory
37
+ attr_reader :tree
38
+
39
+ def initialize(root = nil, &block)
40
+ @tree = {}
41
+ @negative_tree = []
42
+ @root = root
43
+ instance_eval(&block) if block_given?
44
+ end
45
+
46
+ def directory(name, &block)
47
+ @tree[name] = block_given? && Directory.new(location(name), &block)
48
+ end
49
+
50
+ def file(name, &block)
51
+ @tree[name] = File.new(location(name), &block)
52
+ end
53
+
54
+ def no_file(name)
55
+ @negative_tree << name
56
+ end
57
+
58
+ def location(name)
59
+ [@root, name].compact.join("/")
60
+ end
61
+
62
+ def matches?(root)
63
+ @tree.each do |file, value|
64
+ unless value
65
+ unless root.join(location(file)).exist?
66
+ throw :failure, "#{root}/#{location(file)}"
67
+ end
68
+ else
69
+ value.matches?(root)
70
+ end
71
+ end
72
+
73
+ @negative_tree.each do |file|
74
+ if root.join(location(file)).exist?
75
+ throw :failure, [:not, "unexpected #{root}/#{location(file)}"]
76
+ end
77
+ end
78
+
79
+ nil
80
+ end
81
+ end
82
+
83
+ class Root < Directory
84
+ def failure_message
85
+ if @failure.is_a?(Array) && @failure[0] == :not
86
+ "Structure should not have had #{@failure[1]}, but it did"
87
+ elsif @failure.is_a?(Array)
88
+ "Structure should have #{@failure[0]} with #{@failure[1]}. It had:\n#{@failure[2]}"
89
+ else
90
+ "Structure should have #{@failure}, but it didn't"
91
+ end
92
+ end
93
+
94
+ def matches?(root)
95
+ root = Pathname.new(root) unless root.is_a?(Pathname)
96
+ @failure = catch :failure do
97
+ super
98
+ end
99
+
100
+ !@failure
101
+ end
102
+ end
103
+
104
+ def have_structure(&block)
105
+ Root.new(&block)
106
+ end
107
+ end
108
+ end
@@ -1,3 +1,3 @@
1
1
  module GeneratorSpec
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,30 @@
1
+ require "spec_helper"
2
+
3
+ describe TestGenerator, "using custom matcher" do
4
+ include GeneratorSpec::GeneratorExampleGroup
5
+
6
+ destination File.expand_path("../../tmp", __FILE__)
7
+
8
+ before do
9
+ prepare_destination
10
+ run_generator
11
+ end
12
+
13
+ specify do
14
+ destination_root.should have_structure {
15
+ no_file "test.rb"
16
+ directory "config" do
17
+ directory "initializers" do
18
+ file "test.rb" do
19
+ contains "# Initializer"
20
+ end
21
+ end
22
+ end
23
+ directory "db" do
24
+ directory "migrate" do
25
+ file "123_create_tests.rb"
26
+ end
27
+ end
28
+ }
29
+ end
30
+ end
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: generator_spec
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 27
4
5
  prerelease:
5
- version: 0.0.1
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
6
11
  platform: ruby
7
12
  authors:
8
13
  - Steve Hodgkiss
@@ -21,6 +26,10 @@ dependencies:
21
26
  requirements:
22
27
  - - ~>
23
28
  - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
24
33
  version: "3.0"
25
34
  type: :runtime
26
35
  version_requirements: *id001
@@ -32,6 +41,9 @@ dependencies:
32
41
  requirements:
33
42
  - - ">="
34
43
  - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
35
47
  version: "0"
36
48
  type: :runtime
37
49
  version_requirements: *id002
@@ -54,8 +66,10 @@ files:
54
66
  - generator_spec.gemspec
55
67
  - lib/generator_spec.rb
56
68
  - lib/generator_spec/generator_example_group.rb
69
+ - lib/generator_spec/matcher.rb
57
70
  - lib/generator_spec/version.rb
58
71
  - spec/generator_spec/generator_example_group_spec.rb
72
+ - spec/generator_spec/matcher_spec.rb
59
73
  - spec/spec_helper.rb
60
74
  - spec/support/test_generator/templates/initializer.rb
61
75
  - spec/support/test_generator/templates/migration.rb
@@ -74,22 +88,29 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
88
  requirements:
75
89
  - - ">="
76
90
  - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
77
94
  version: "0"
78
95
  required_rubygems_version: !ruby/object:Gem::Requirement
79
96
  none: false
80
97
  requirements:
81
98
  - - ">="
82
99
  - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
83
103
  version: "0"
84
104
  requirements: []
85
105
 
86
106
  rubyforge_project: generator_spec
87
- rubygems_version: 1.5.2
107
+ rubygems_version: 1.5.3
88
108
  signing_key:
89
109
  specification_version: 3
90
110
  summary: Test Rails generators with RSpec
91
111
  test_files:
92
112
  - spec/generator_spec/generator_example_group_spec.rb
113
+ - spec/generator_spec/matcher_spec.rb
93
114
  - spec/spec_helper.rb
94
115
  - spec/support/test_generator/templates/initializer.rb
95
116
  - spec/support/test_generator/templates/migration.rb