generator-spec 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.markdown +144 -0
  5. data/Rakefile +48 -0
  6. data/VERSION +1 -0
  7. data/generator-spec.gemspec +106 -0
  8. data/lib/generators/demo/demo_generator.rb +91 -0
  9. data/lib/rspec_for_generators.rb +11 -0
  10. data/lib/rspec_for_generators/fixtures/routes.rb +2 -0
  11. data/lib/rspec_for_generators/generator_spec_helper.rb +95 -0
  12. data/lib/rspec_for_generators/main.rb +18 -0
  13. data/lib/rspec_for_generators/matchers/content/have_block.rb +47 -0
  14. data/lib/rspec_for_generators/matchers/content/have_call.rb +42 -0
  15. data/lib/rspec_for_generators/matchers/content/have_class.rb +32 -0
  16. data/lib/rspec_for_generators/matchers/content/have_method.rb +41 -0
  17. data/lib/rspec_for_generators/matchers/content/have_module.rb +32 -0
  18. data/lib/rspec_for_generators/matchers/content/include_module.rb +32 -0
  19. data/lib/rspec_for_generators/matchers/content/inherit_from.rb +31 -0
  20. data/lib/rspec_for_generators/matchers/file/generate_directory.rb +71 -0
  21. data/lib/rspec_for_generators/matchers/file/generate_file.rb +71 -0
  22. data/lib/rspec_for_generators/matchers/file/generate_migration.rb +50 -0
  23. data/lib/rspec_for_generators/matchers/helpers/content.rb +18 -0
  24. data/lib/rspec_for_generators/matchers/helpers/file.rb +26 -0
  25. data/lib/rspec_for_generators/matchers/helpers/migration.rb +102 -0
  26. data/lib/rspec_for_generators/matchers/migration/have_column.rb +13 -0
  27. data/lib/rspec_for_generators/matchers/migration/have_index.rb +9 -0
  28. data/lib/rspec_for_generators/matchers/migration/have_migration.rb +5 -0
  29. data/lib/rspec_for_generators/matchers/migration/have_table.rb +18 -0
  30. data/lib/rspec_for_generators/matchers/migration/have_tbl_column.rb +25 -0
  31. data/lib/rspec_for_generators/matchers/migration/have_up_down.rb +13 -0
  32. data/lib/rspec_for_generators/rails_helpers/all.rb +6 -0
  33. data/lib/rspec_for_generators/rails_helpers/rails_app.rb +53 -0
  34. data/lib/rspec_for_generators/rails_helpers/rails_controller.rb +36 -0
  35. data/lib/rspec_for_generators/rails_helpers/rails_helper.rb +36 -0
  36. data/lib/rspec_for_generators/rails_helpers/rails_mailer.rb +29 -0
  37. data/lib/rspec_for_generators/rails_helpers/rails_model.rb +27 -0
  38. data/lib/rspec_for_generators/rails_helpers/rails_orm.rb +77 -0
  39. data/lib/rspec_for_generators/rails_helpers/rails_view.rb +29 -0
  40. data/lib/rspec_for_generators/rails_spec_helper.rb +35 -0
  41. data/lib/rspec_for_generators/rspec_test_case.rb +26 -0
  42. data/rspec_for_generators.gemspec +96 -0
  43. data/spec/rspec_for_generators/rspec_for_generators_spec.rb +41 -0
  44. data/spec/spec.opts +1 -0
  45. data/spec/spec_helper.rb +6 -0
  46. data/wiki/Custom Rails 3 Generators.textile +110 -0
  47. data/wiki/rails_view_helpers.textile +167 -0
  48. metadata +205 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Kristian Mandrup
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,144 @@
1
+ # RSpec 2 library for specifying and testing generators
2
+
3
+ This project contains RSpec 2 matchers, helpers and various utilities to assist in writing Rails 3 generator specs.
4
+
5
+ Rails 3 has a Rails::Generators::TestCase class for use with Test-Unit, to help test generators, containing specific assertion methods that can be used to assert generator behavior. Theres was no RSpec 2 equivalent, so I wrapped Rails::Generators::TestCase for use with RSpec 2 and created some RSpec 2 matchers that mimic the assertion methods
6
+ of TestCase and also added some extra goodies to the mix. It should now be pretty easy to test your Rails 3 generators with RSpec 2 :)
7
+
8
+ Please advice if you find any issues or have suggestions for improvements. The code is not as pretty as it could be, so feel free to refactor and improve it to your hearts desire!
9
+
10
+ ## Install
11
+
12
+ <code>gem install rspec_for_generators</code>
13
+
14
+ The gem is a jewel (using jeweler), so to install the gem from the code - simply use the jeweler rake task:
15
+
16
+ <code>rake install</code>
17
+
18
+ ## Usage
19
+
20
+ The following demonstrates an example usage. There are many more options and DSL convenience methods.
21
+
22
+ ### Configuration
23
+
24
+ First setup the *spec_helper.rb*. Here is an example configuration.
25
+
26
+ <pre># spec/spec_helper.rb
27
+
28
+ require 'rspec'
29
+ require 'rspec_for_generators'
30
+
31
+ # configure it like this to use default settings
32
+ RSpec::Generator.configure_root_dir __FILE__
33
+
34
+ # or customize the location of the temporary Rails 3 app dir used
35
+ RSpec::Generator.configure_root_dir '~/my/rails/folder', :custom
36
+ </pre>
37
+
38
+ ### Specs for generators
39
+
40
+ I recommend having a separate spec for each generator. A generator spec can include *this* file to ensure all generators are loaded.
41
+ Put a *require_generator* statement in each spec file, to ensure that particular generator is loaded and thus available for the spec.
42
+
43
+ <pre>require_generator :canable</pre>
44
+
45
+ This will load the generator : `generators/canabale_generator.rb`
46
+
47
+ If the generator is namespaced, use a nested approach like so:
48
+
49
+ <pre>require_generators :canable => ['model', 'user']</pre>
50
+
51
+ This will load the generators: `generators/canable/model_generator.rb` and `generators/canable/user_generator.rb`
52
+
53
+ If you have multiple generators in a shared namespace, the following approach can be used.
54
+ Create a file that loads all the generators in the namespace. Include this file for each spec that tests a particular generator in that namespace:
55
+
56
+ <pre># spec/generators/canable.rb
57
+ require_generators :canable => ['model', 'user']</pre>
58
+
59
+ <pre># spec/generators/canable/model_generator_spec.rb
60
+ ...
61
+ # load generators to spec
62
+ require 'generators/canable'
63
+
64
+ describe 'model_generator' do
65
+ ...
66
+ end
67
+ </pre>
68
+
69
+ <pre># spec/generators/canable/user_generator_spec.rb
70
+ ...
71
+ # load generators to spec
72
+ require 'generators/canable'
73
+
74
+ describe 'user_generator' do
75
+ ...
76
+ end
77
+ </pre>
78
+
79
+
80
+
81
+ ### Example of full generator spec
82
+
83
+ <pre># spec/generators/model_generator_spec.rb
84
+
85
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
86
+
87
+ # list of generators to spec are loaded
88
+ require 'generators/canable'
89
+
90
+ describe 'model_generator' do
91
+ # include Rails model helpers for ActiveRecord
92
+ # available: MongoMapper, Mongoid and DataMapper
93
+
94
+ include RSpec::Rails::Orm::ActiveRecord
95
+
96
+ before :each do
97
+ # define generator to test
98
+ RSpec::Generator.setup_generator 'model_generator' do
99
+ tests Canable::Generators::ModelGenerator
100
+ end
101
+ # ensure clean state before run
102
+ remove_model 'account'
103
+ end
104
+
105
+ after :each do
106
+ # ensure clean state after run
107
+ remove_model 'account'
108
+ end
109
+
110
+ it "should not work without an existing Account model file" do
111
+ RSpec::Generator.with_generator do |g|
112
+ name = 'account'
113
+ g.run_generator %w{account}
114
+ g.should_not generate_file name, :model
115
+ end
116
+ end
117
+
118
+ it "should decorate an existing Account model file with 'include Canable:Ables'" do
119
+ RSpec::Generator.with_generator do |g|
120
+ name = 'account'
121
+ create_model name
122
+ g.run_generator %w{account}
123
+ g.should generate_file name, :model do |content|
124
+ content.should have_class name do |klass|
125
+ klass.should include_module 'Canable::Ables'
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end</pre>
131
+
132
+ ## Note on Patches/Pull Requests
133
+
134
+ * Fork the project.
135
+ * Make your feature addition or bug fix.
136
+ * Add tests for it. This is important so I don't break it in a
137
+ future version unintentionally.
138
+ * Commit, do not mess with rakefile, version, or history.
139
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
140
+ * Send me a pull request. Bonus points for topic branches.
141
+
142
+ ## Copyright
143
+
144
+ Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gem|
4
+ gem.name = "generator-spec"
5
+ gem.summary = %Q{RSpec 2 library for writing generator specs}
6
+ gem.description = %Q{RSpec 2 library to assist in writing generator specs}
7
+ gem.email = "kmandrup@gmail.com"
8
+ gem.homepage = "http://github.com/kristianmandrup/rspec_for_generators"
9
+ gem.authors = ["Kristian Mandrup"]
10
+ gem.add_development_dependency "rspec", ">= 2.0.0.beta19"
11
+ gem.add_dependency "rspec", ">= 2.0.0.beta.19"
12
+ gem.add_dependency "rake", ">= 0.8.7"
13
+ gem.add_dependency "rails", ">= 3.0.0.rc"
14
+ gem.add_dependency "test-unit", ">= 2.0.9"
15
+ gem.add_dependency "require_all", ">= 1.1.0"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ # require 'spec/rake/spectask'
24
+ # Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ # spec.libs << 'lib' << 'spec'
26
+ # spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ # end
28
+ #
29
+ # Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ # spec.libs << 'lib' << 'spec'
31
+ # spec.pattern = 'spec/**/*_spec.rb'
32
+ # spec.rcov = true
33
+ # end
34
+ #
35
+ # task :spec => :check_dependencies
36
+ #
37
+ # task :default => :spec
38
+ #
39
+ # require 'rake/rdoctask'
40
+ # Rake::RDocTask.new do |rdoc|
41
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+ #
43
+ # rdoc.rdoc_dir = 'rdoc'
44
+ # rdoc.title = "rspec_for_generators #{version}"
45
+ # rdoc.rdoc_files.include('README*')
46
+ # rdoc.rdoc_files.include('lib/**/*.rb')
47
+ # end
48
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.3
@@ -0,0 +1,106 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{generator-spec}
8
+ s.version = "0.3.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Kristian Mandrup"]
12
+ s.date = %q{2010-08-04}
13
+ s.description = %q{RSpec 2 library to assist in writing generator specs}
14
+ s.email = %q{kmandrup@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.markdown"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.markdown",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "generator-spec.gemspec",
27
+ "lib/generators/demo/demo_generator.rb",
28
+ "lib/rspec_for_generators.rb",
29
+ "lib/rspec_for_generators/fixtures/routes.rb",
30
+ "lib/rspec_for_generators/generator_spec_helper.rb",
31
+ "lib/rspec_for_generators/main.rb",
32
+ "lib/rspec_for_generators/matchers/content/have_block.rb",
33
+ "lib/rspec_for_generators/matchers/content/have_call.rb",
34
+ "lib/rspec_for_generators/matchers/content/have_class.rb",
35
+ "lib/rspec_for_generators/matchers/content/have_method.rb",
36
+ "lib/rspec_for_generators/matchers/content/have_module.rb",
37
+ "lib/rspec_for_generators/matchers/content/include_module.rb",
38
+ "lib/rspec_for_generators/matchers/content/inherit_from.rb",
39
+ "lib/rspec_for_generators/matchers/file/generate_directory.rb",
40
+ "lib/rspec_for_generators/matchers/file/generate_file.rb",
41
+ "lib/rspec_for_generators/matchers/file/generate_migration.rb",
42
+ "lib/rspec_for_generators/matchers/helpers/content.rb",
43
+ "lib/rspec_for_generators/matchers/helpers/file.rb",
44
+ "lib/rspec_for_generators/matchers/helpers/migration.rb",
45
+ "lib/rspec_for_generators/matchers/migration/have_column.rb",
46
+ "lib/rspec_for_generators/matchers/migration/have_index.rb",
47
+ "lib/rspec_for_generators/matchers/migration/have_migration.rb",
48
+ "lib/rspec_for_generators/matchers/migration/have_table.rb",
49
+ "lib/rspec_for_generators/matchers/migration/have_tbl_column.rb",
50
+ "lib/rspec_for_generators/matchers/migration/have_up_down.rb",
51
+ "lib/rspec_for_generators/rails_helpers/all.rb",
52
+ "lib/rspec_for_generators/rails_helpers/rails_app.rb",
53
+ "lib/rspec_for_generators/rails_helpers/rails_controller.rb",
54
+ "lib/rspec_for_generators/rails_helpers/rails_helper.rb",
55
+ "lib/rspec_for_generators/rails_helpers/rails_mailer.rb",
56
+ "lib/rspec_for_generators/rails_helpers/rails_model.rb",
57
+ "lib/rspec_for_generators/rails_helpers/rails_orm.rb",
58
+ "lib/rspec_for_generators/rails_helpers/rails_view.rb",
59
+ "lib/rspec_for_generators/rails_spec_helper.rb",
60
+ "lib/rspec_for_generators/rspec_test_case.rb",
61
+ "rspec_for_generators.gemspec",
62
+ "spec/rspec_for_generators/rspec_for_generators_spec.rb",
63
+ "spec/spec.opts",
64
+ "spec/spec_helper.rb",
65
+ "wiki/Custom Rails 3 Generators.textile",
66
+ "wiki/rails_view_helpers.textile"
67
+ ]
68
+ s.homepage = %q{http://github.com/kristianmandrup/rspec_for_generators}
69
+ s.rdoc_options = ["--charset=UTF-8"]
70
+ s.require_paths = ["lib"]
71
+ s.rubygems_version = %q{1.3.7}
72
+ s.summary = %q{RSpec 2 library for writing generator specs}
73
+ s.test_files = [
74
+ "spec/rspec_for_generators/rspec_for_generators_spec.rb",
75
+ "spec/spec_helper.rb"
76
+ ]
77
+
78
+ if s.respond_to? :specification_version then
79
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
80
+ s.specification_version = 3
81
+
82
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
83
+ s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta19"])
84
+ s.add_runtime_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
85
+ s.add_runtime_dependency(%q<rake>, [">= 0.8.7"])
86
+ s.add_runtime_dependency(%q<rails>, [">= 3.0.0.rc"])
87
+ s.add_runtime_dependency(%q<test-unit>, [">= 2.0.9"])
88
+ s.add_runtime_dependency(%q<require_all>, [">= 1.1.0"])
89
+ else
90
+ s.add_dependency(%q<rspec>, [">= 2.0.0.beta19"])
91
+ s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
92
+ s.add_dependency(%q<rake>, [">= 0.8.7"])
93
+ s.add_dependency(%q<rails>, [">= 3.0.0.rc"])
94
+ s.add_dependency(%q<test-unit>, [">= 2.0.9"])
95
+ s.add_dependency(%q<require_all>, [">= 1.1.0"])
96
+ end
97
+ else
98
+ s.add_dependency(%q<rspec>, [">= 2.0.0.beta19"])
99
+ s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
100
+ s.add_dependency(%q<rake>, [">= 0.8.7"])
101
+ s.add_dependency(%q<rails>, [">= 3.0.0.rc"])
102
+ s.add_dependency(%q<test-unit>, [">= 2.0.9"])
103
+ s.add_dependency(%q<require_all>, [">= 1.1.0"])
104
+ end
105
+ end
106
+
@@ -0,0 +1,91 @@
1
+ class DemoGenerator < Rails::Generators::Base
2
+ desc "Adds Canable::Ables permission system to Model"
3
+
4
+ argument :name, :type => :string, :default => 'User', :desc => 'Name of model to make Canable:Able', :required => false
5
+
6
+ def self.source_root
7
+ @source_root ||= File.expand_path("../../templates", __FILE__)
8
+ end
9
+
10
+ def make_canable_able
11
+ if File.exist?(model_file_name)
12
+ @model_file_txt = File.open(model_file_name).read
13
+ after_txt = find_after_txt(model_file_txt)
14
+ inject_into_file(model_file_name, canable_include_txt, :after => after_txt) if after_txt
15
+ else
16
+ say "#{model_file_name} does not exist. Please create it first before you can make it Canable:Able", :red
17
+ end
18
+ end
19
+
20
+ def post_log
21
+ say "Your model #{model_class_name} is now Canable:Able. Please define your permission login in #{model_file_name}", :green
22
+ end
23
+
24
+ protected
25
+
26
+ attr_accessor :model_file_txt
27
+
28
+ def find_after_txt model_file_txt
29
+ # after first include statement
30
+ match = (model_file_txt =~ /(include\s+\S+)/)
31
+ return $1 if match
32
+
33
+ # or after class definition if no includes
34
+ match = model_file_txt =~ /(class\s+#{model_class_name}\s+<\s+ActiveRecord::Base)/ # first try match AR model
35
+ match = model_file_txt =~ /(class\s+#{model_class_name})/ if !match
36
+ $1 || nil
37
+ end
38
+
39
+ def model_class_name
40
+ name.to_s.camelize
41
+ end
42
+
43
+ def model_file_name
44
+ File.join(Rails.root, "app/models/#{name}.rb")
45
+ end
46
+
47
+ def canable_include_txt
48
+ %Q{
49
+ include Canable::Ables
50
+ #{add_userstamps}
51
+
52
+ # permission logic
53
+ #{add_methods}
54
+ }
55
+ end
56
+
57
+ def has_userstamps?
58
+ model_file_txt =~ /userstamps!/
59
+ end
60
+
61
+ def add_userstamps
62
+ if !has_userstamps?
63
+ "userstamps! # adds creator and updater\n" if options[:userstamps]
64
+ end
65
+ end
66
+
67
+ def has_method? name
68
+ model_file_txt =~ /(def #{name}_by\?)/
69
+ end
70
+
71
+ def add_methods
72
+ methods = []
73
+ %w{creatable destroyable updatable viewable}.each do |name|
74
+ if !has_method?(name)
75
+ method = add_method(name)
76
+ methods << method if method
77
+ end
78
+ end
79
+ methods.join("\n ")
80
+ end
81
+
82
+ def add_method(name)
83
+ if options[name.to_sym]
84
+ %Q{
85
+ def #{name}_by?(user)
86
+ true
87
+ end
88
+ }
89
+ end
90
+ end
91
+ end