genspec 0.1.1 → 0.2.0.prerails3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/README.rdoc +79 -31
  2. data/Rakefile +69 -22
  3. data/VERSION +1 -1
  4. data/genspec.gemspec +31 -40
  5. data/lib/gen_spec.rb +1 -0
  6. data/lib/genspec.rb +23 -8
  7. data/lib/genspec/generator_example_group.rb +47 -52
  8. data/lib/genspec/matchers.rb +59 -0
  9. data/lib/genspec/matchers/base.rb +148 -0
  10. data/lib/genspec/matchers/generation_method_matcher.rb +88 -0
  11. data/lib/genspec/matchers/output_matcher.rb +34 -0
  12. data/lib/genspec/matchers/result_matcher.rb +28 -0
  13. data/lib/genspec/shell.rb +99 -0
  14. data/pkg/genspec-0.1.1.gem +0 -0
  15. data/pkg/genspec-0.2.0.pre1.gem +0 -0
  16. data/pkg/genspec-0.2.0.prerails3.1.gem +0 -0
  17. data/spec/generators/test_rails3_spec.rb +74 -0
  18. data/spec/rcov.opts +2 -0
  19. data/spec/rspec.opts +2 -0
  20. data/spec/spec_helper.rb +10 -3
  21. data/spec/support/generators/test_rails3/USAGE +8 -0
  22. data/spec/support/generators/{test → test_rails3}/templates/file +0 -0
  23. data/spec/support/generators/test_rails3/test_rails3_generator.rb +23 -0
  24. metadata +48 -40
  25. data/lib/genspec/generation_matchers.rb +0 -27
  26. data/lib/genspec/generation_matchers/generation_matcher.rb +0 -147
  27. data/lib/genspec/generation_matchers/result_matcher.rb +0 -42
  28. data/pkg/genspec-0.0.0.gem +0 -0
  29. data/pkg/genspec-0.1.0.gem +0 -0
  30. data/rdoc/classes/GenSpec.html +0 -124
  31. data/rdoc/classes/GenSpec/GenerationMatchers.html +0 -197
  32. data/rdoc/classes/GenSpec/GenerationMatchers/GenerationMatcher.html +0 -363
  33. data/rdoc/classes/GenSpec/GenerationMatchers/ResultMatcher.html +0 -241
  34. data/rdoc/classes/GenSpec/GeneratorExampleGroup.html +0 -285
  35. data/rdoc/created.rid +0 -1
  36. data/rdoc/files/README_rdoc.html +0 -261
  37. data/rdoc/files/lib/genspec/generation_matchers/generation_matcher_rb.html +0 -101
  38. data/rdoc/files/lib/genspec/generation_matchers/result_matcher_rb.html +0 -101
  39. data/rdoc/files/lib/genspec/generation_matchers_rb.html +0 -109
  40. data/rdoc/files/lib/genspec/generator_example_group_rb.html +0 -101
  41. data/rdoc/files/lib/genspec_rb.html +0 -114
  42. data/rdoc/fr_class_index.html +0 -31
  43. data/rdoc/fr_file_index.html +0 -32
  44. data/rdoc/fr_method_index.html +0 -46
  45. data/rdoc/index.html +0 -26
  46. data/rdoc/rdoc-style.css +0 -208
  47. data/spec/generators/test_spec.rb +0 -96
  48. data/spec/support/generators/test/test_generator.rb +0 -29
Binary file
Binary file
Binary file
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe :test_rails3 do
4
+ context "with no options or arguments" do
5
+ it "should generate a file called default_file" do
6
+ subject.should generate("default_file")
7
+ subject.should_not generate("some_other_file")
8
+
9
+ subject.should call_action(:create_file)
10
+ subject.should call_action(:create_file, "default_file")
11
+ subject.should_not call_action(:create_file, "some_other_file")
12
+
13
+ subject.should create_file
14
+ subject.should create_file('default_file')
15
+ subject.should_not create_file("some_other_file")
16
+ end
17
+
18
+ it "should generate a file with specific content" do
19
+ subject.should generate("default_file") { |content| content.should == "content!" }
20
+ subject.should generate("default_file") { |content| content.should_not == "!content" }
21
+ subject.should_not generate("some_other_file")
22
+ end
23
+
24
+ it "should generate a template called 'default_template'" do
25
+ subject.should generate(:template)
26
+ subject.should generate(:template, 'file', 'file_template')
27
+ end
28
+
29
+ it "should output 'create file'" do
30
+ subject.should output(/create\s+default_file/)
31
+ end
32
+
33
+ it "shoud generate a directory called 'a_directory'" do
34
+ subject.should generate(:empty_directory)
35
+ subject.should generate(:empty_directory, "a_directory")
36
+ subject.should generate("a_directory")
37
+ subject.should_not generate(:empty_directory, 'another_directory')
38
+ subject.should empty_directory("a_directory")
39
+ subject.should_not empty_directory("another_directory")
40
+ end
41
+
42
+ # if the other tests pass then it seems to be working properly, but let's make sure
43
+ # Rails-specific actions are also working. If they are, it's safe to say custom extensions
44
+ # will work fine too.
45
+ it 'should add_source "http://gems.github.com/"' do
46
+ subject.should add_source("http://gems.github.com/")
47
+ end
48
+ end
49
+
50
+ with_args '--help' do
51
+ it "should output usage banner with string" do
52
+ subject.should output("rails generate test_rails3 [ARGUMENT1] [options]")
53
+ end
54
+
55
+ it "should output usage banner with regexp" do
56
+ subject.should output(/rails generate test_rails3/)
57
+ end
58
+ end
59
+
60
+ context "with arguments without block" do
61
+ with_args :test_arg
62
+
63
+ it "should generate file 'test_arg'" do
64
+ subject.should generate('test_arg')
65
+ end
66
+ end
67
+
68
+ # FIXME: Uh, how best to write a spec around this? I'm actually trying to test #with_args with a block...
69
+ with_args :test_arg do
70
+ it "should generate file 'test_arg'" do
71
+ subject.should generate('test_arg')
72
+ end
73
+ end
74
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1,2 @@
1
+ -c
2
+ --format nested
data/spec/rspec.opts ADDED
@@ -0,0 +1,2 @@
1
+ -c
2
+ -f d
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,11 @@
1
- require File.expand_path("../../../../../config/environment", __FILE__)
1
+ # Having ./support in the load path means Rails will load the generators at
2
+ # ./support/generators/**/*_generator.rb and
3
+ # ./support/rails/generators/**/*_generator.rb
4
+ $LOAD_PATH.push File.join(File.dirname(__FILE__), "support")
5
+ require File.join(File.dirname(__FILE__),"../../../config/environment")
6
+ require File.join(File.dirname(__FILE__),"../lib/gen_spec")
2
7
 
3
- Rails::Generator::Base.append_sources Rails::Generator::PathSource.new(:test,
4
- File.expand_path("../support/generators", __FILE__))
8
+ if Rails::VERSION::MAJOR < 3
9
+ Rails::Generator::Base.append_sources Rails::Generator::PathSource.new(:test,
10
+ File.expand_path("../support/generators", __FILE__))
11
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate test Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,23 @@
1
+ class TestRails3Generator < Rails::Generators::Base
2
+ def self.source_root
3
+ File.expand_path('../templates', __FILE__)
4
+ end
5
+
6
+ argument :argument1, :type => :string, :default => "default_file"
7
+
8
+ def gen_file_with_arg
9
+ create_file argument1, "content!"
10
+ end
11
+
12
+ def gen_directory
13
+ empty_directory "a_directory"
14
+ end
15
+
16
+ def gen_template
17
+ template 'file', 'file_template'
18
+ end
19
+
20
+ def gen_gem_source
21
+ add_source "http://gems.github.com/"
22
+ end
23
+ end
metadata CHANGED
@@ -1,12 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genspec
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
4
+ hash: -3738369098
5
+ prerelease: true
5
6
  segments:
6
7
  - 0
8
+ - 2
9
+ - 0
10
+ - prerails3
7
11
  - 1
8
- - 1
9
- version: 0.1.1
12
+ version: 0.2.0.prerails3.1
10
13
  platform: ruby
11
14
  authors:
12
15
  - Colin MacKenzie IV
@@ -14,36 +17,44 @@ autorequire:
14
17
  bindir: bin
15
18
  cert_chain: []
16
19
 
17
- date: 2010-06-21 00:00:00 -04:00
20
+ date: 2010-07-01 00:00:00 -04:00
18
21
  default_executable:
19
22
  dependencies:
20
23
  - !ruby/object:Gem::Dependency
21
24
  name: rspec
22
25
  prerelease: false
23
26
  requirement: &id001 !ruby/object:Gem::Requirement
27
+ none: false
24
28
  requirements:
25
29
  - - ">="
26
30
  - !ruby/object:Gem::Version
31
+ hash: 4053856191
27
32
  segments:
33
+ - 2
34
+ - 0
28
35
  - 0
29
- version: "0"
36
+ - beta
37
+ - 14
38
+ version: 2.0.0.beta.14
30
39
  type: :runtime
31
40
  version_requirements: *id001
32
41
  - !ruby/object:Gem::Dependency
33
42
  name: sc-core-ext
34
43
  prerelease: false
35
44
  requirement: &id002 !ruby/object:Gem::Requirement
45
+ none: false
36
46
  requirements:
37
47
  - - ">="
38
48
  - !ruby/object:Gem::Version
49
+ hash: 29
39
50
  segments:
40
51
  - 1
41
52
  - 2
42
- - 0
43
- version: 1.2.0
53
+ - 1
54
+ version: 1.2.1
44
55
  type: :runtime
45
56
  version_requirements: *id002
46
- description: Just like rspec-rails uses the structure of your spec/ directory to infer which test is being run (controllers, helpers, lib, etc.), you just need to create a spec/generators directory and put your generator specs in there.
57
+ description: Simple, expressive Rails 3 generator testing for RSpec. For the Rails 2.3 version, use genspec 0.1.x.
47
58
  email: sinisterchipmunk@gmail.com
48
59
  executables: []
49
60
 
@@ -58,35 +69,26 @@ files:
58
69
  - Rakefile
59
70
  - VERSION
60
71
  - genspec.gemspec
72
+ - lib/gen_spec.rb
61
73
  - lib/genspec.rb
62
- - lib/genspec/generation_matchers.rb
63
- - lib/genspec/generation_matchers/generation_matcher.rb
64
- - lib/genspec/generation_matchers/result_matcher.rb
65
74
  - lib/genspec/generator_example_group.rb
66
- - pkg/genspec-0.0.0.gem
67
- - pkg/genspec-0.1.0.gem
68
- - rdoc/classes/GenSpec.html
69
- - rdoc/classes/GenSpec/GenerationMatchers.html
70
- - rdoc/classes/GenSpec/GenerationMatchers/GenerationMatcher.html
71
- - rdoc/classes/GenSpec/GenerationMatchers/ResultMatcher.html
72
- - rdoc/classes/GenSpec/GeneratorExampleGroup.html
73
- - rdoc/created.rid
74
- - rdoc/files/README_rdoc.html
75
- - rdoc/files/lib/genspec/generation_matchers/generation_matcher_rb.html
76
- - rdoc/files/lib/genspec/generation_matchers/result_matcher_rb.html
77
- - rdoc/files/lib/genspec/generation_matchers_rb.html
78
- - rdoc/files/lib/genspec/generator_example_group_rb.html
79
- - rdoc/files/lib/genspec_rb.html
80
- - rdoc/fr_class_index.html
81
- - rdoc/fr_file_index.html
82
- - rdoc/fr_method_index.html
83
- - rdoc/index.html
84
- - rdoc/rdoc-style.css
75
+ - lib/genspec/matchers.rb
76
+ - lib/genspec/matchers/base.rb
77
+ - lib/genspec/matchers/generation_method_matcher.rb
78
+ - lib/genspec/matchers/output_matcher.rb
79
+ - lib/genspec/matchers/result_matcher.rb
80
+ - lib/genspec/shell.rb
81
+ - pkg/genspec-0.1.1.gem
82
+ - pkg/genspec-0.2.0.pre1.gem
83
+ - pkg/genspec-0.2.0.prerails3.1.gem
85
84
  - spec/environment_spec.rb
86
- - spec/generators/test_spec.rb
85
+ - spec/generators/test_rails3_spec.rb
86
+ - spec/rcov.opts
87
+ - spec/rspec.opts
87
88
  - spec/spec_helper.rb
88
- - spec/support/generators/test/templates/file
89
- - spec/support/generators/test/test_generator.rb
89
+ - spec/support/generators/test_rails3/USAGE
90
+ - spec/support/generators/test_rails3/templates/file
91
+ - spec/support/generators/test_rails3/test_rails3_generator.rb
90
92
  has_rdoc: true
91
93
  homepage: http://www.thoughtsincomputation.com
92
94
  licenses: []
@@ -97,28 +99,34 @@ rdoc_options:
97
99
  require_paths:
98
100
  - lib
99
101
  required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
100
103
  requirements:
101
104
  - - ">="
102
105
  - !ruby/object:Gem::Version
106
+ hash: 3
103
107
  segments:
104
108
  - 0
105
109
  version: "0"
106
110
  required_rubygems_version: !ruby/object:Gem::Requirement
111
+ none: false
107
112
  requirements:
108
- - - ">="
113
+ - - ">"
109
114
  - !ruby/object:Gem::Version
115
+ hash: 25
110
116
  segments:
111
- - 0
112
- version: "0"
117
+ - 1
118
+ - 3
119
+ - 1
120
+ version: 1.3.1
113
121
  requirements: []
114
122
 
115
123
  rubyforge_project:
116
- rubygems_version: 1.3.6
124
+ rubygems_version: 1.3.7
117
125
  signing_key:
118
126
  specification_version: 3
119
- summary: Simple, expressive Rails generator testing for RSpec.
127
+ summary: Simple, expressive Rails 3 generator testing for RSpec. For the Rails 2.3 version, use genspec 0.1.x.
120
128
  test_files:
121
129
  - spec/environment_spec.rb
122
- - spec/generators/test_spec.rb
130
+ - spec/generators/test_rails3_spec.rb
123
131
  - spec/spec_helper.rb
124
- - spec/support/generators/test/test_generator.rb
132
+ - spec/support/generators/test_rails3/test_rails3_generator.rb
@@ -1,27 +0,0 @@
1
- require 'genspec/generation_matchers/generation_matcher'
2
- require 'genspec/generation_matchers/result_matcher'
3
-
4
- module GenSpec
5
- module GenerationMatchers
6
- # Valid types: :dependency, :class_collisions, :file, :template, :complex_template, :directory, :readme,
7
- # :migration_template, :route_resources
8
- def generate(kind, *args, &block)
9
- case kind.to_s
10
- when *GenSpec::GenerationMatchers::GenerationMatcher.generation_methods
11
- GenSpec::GenerationMatchers::GenerationMatcher.new(kind, *args, &block)
12
- else
13
- if kind.kind_of?(String)
14
- GenSpec::GenerationMatchers::ResultMatcher.new(kind, &block)
15
- else
16
- raise ArgumentError, "No generator matcher for #{kind.inspect}"
17
- end
18
- end
19
- end
20
-
21
- # This tests the content sent to the command line, instead of the generated product.
22
- # Useful for testing help messages, etc.
23
- def output(text_or_regexp)
24
- GenSpec::GenerationMatchers::GenerationMatcher.new(:output, text_or_regexp)
25
- end
26
- end
27
- end
@@ -1,147 +0,0 @@
1
- module GenSpec
2
- module GenerationMatchers
3
- class GenerationMatcher
4
- delegate :generation_methods, :to => 'self.class'
5
-
6
- def initialize(kind = nil, *args)
7
- raise ArgumentError, "Call with kind" unless kind
8
- @kind = kind.to_s
9
- @args = args
10
- @but_found = nil
11
- end
12
-
13
- def self.generation_methods
14
- @generation_methods ||= %w(dependency class_collisions file template complex_template directory readme
15
- migration_template route_resources)
16
- end
17
-
18
- def matches?(target)
19
- # if it's a string then it's an error message or some such from the generator.
20
- if target.kind_of?(Rails::Generator::GeneratorError)
21
- # if we're not checking output then why hold on to it? Raise it like the error it is!
22
- raise target unless @kind == 'output'
23
- @log = target.message
24
- else
25
- temporary_root(target) do
26
- swap_loggers(target) do
27
- replay(target)
28
- end
29
- end
30
- end
31
- match_content
32
- matched?
33
- end
34
-
35
- def failure_message
36
- "Expected to generate #{@kind}#{with_file}#{but_found}"
37
- end
38
-
39
- def negative_failure_message
40
- "Expected not to generate #{@kind}#{with_file}"
41
- end
42
-
43
- protected
44
-
45
- def replay(target)
46
- @create = Rails::Generator::Commands::Create.new(target)
47
- target.manifest.replay(self)
48
- after_replay(target)
49
- end
50
-
51
- # hook
52
- def after_replay(target)
53
- end
54
-
55
- def matched!
56
- @matched = true
57
- end
58
-
59
- def matched?
60
- !!@matched
61
- end
62
-
63
- private
64
- def temporary_root(target)
65
- # We could bear to split this into two methods, one called #suspend_logging or some such.
66
- original_root = target.instance_variable_get("@destination_root")
67
-
68
- Dir.mktmpdir do |dir|
69
- # need to copy a few files for some methods, ie route_resources
70
- Dir.mkdir(File.join(dir, "config"))
71
- FileUtils.cp File.join(RAILS_ROOT, "config/routes.rb"), File.join(dir, "config/routes.rb")
72
- target.instance_variable_set("@destination_root", dir)
73
- yield
74
- end
75
- ensure
76
- target.instance_variable_set("@destination_root", original_root)
77
- end
78
-
79
- def swap_loggers(target)
80
- @log = ""
81
- original_logger = Rails::Generator::Base.logger
82
- original_quiet = target.logger.quiet
83
- target.logger.quiet = true
84
- Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(StringIO.new(@log))
85
-
86
- yield
87
- rescue
88
- if original_logger != Rails::Generator::Base.logger
89
- Kernel::raise $!.class, "#{$!.message}\n#{@log}", $!.backtrace
90
- else
91
- Kernel::raise $!
92
- end
93
- ensure
94
- Rails::Generator::Base.logger = original_logger
95
- target.logger.quiet = original_quiet
96
- line = "Generator '#{target.spec.name}'"
97
- line << " with args #{target.args.inspect}" unless target.args.empty?
98
- Rails.logger.debug line
99
- Rails.logger.debug @log
100
- end
101
-
102
- def match_content
103
- # check for a match if #kind is 'output'
104
- if @kind == 'output'
105
- @but_found = @log.dup
106
- if (text = @args.first).kind_of?(String)
107
- if @log =~ /#{Regexp::escape text}/m
108
- matched!
109
- end
110
- elsif (regexp = @args.first).kind_of?(Regexp)
111
- if @log =~ regexp
112
- matched!
113
- end
114
- end
115
- end
116
- end
117
-
118
- def but_found
119
- if @but_found.nil?
120
- ""
121
- else
122
- ",\n but found #{@but_found.inspect}"
123
- end
124
- end
125
-
126
- def with_file
127
- !@args.empty? ? "\n with #{@args.inspect}" : ""
128
- end
129
-
130
- def method_missing(name, *args, &block)
131
- if generation_methods.include? name.to_s
132
- @create.send(name, *args, &block)
133
- if name.to_s == @kind && (@args.empty? || args == @args)
134
- matched!
135
- elsif name.to_s == @kind
136
- @but_found = args
137
- else
138
- nil
139
- end
140
- else super
141
- end
142
- #rescue
143
- # Kernel::raise $!.class, $!.message, caller
144
- end
145
- end
146
- end
147
- end