should_pricot 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/Manifest +92 -0
  3. data/README.rdoc +45 -0
  4. data/Rakefile +36 -0
  5. data/lib/should_pricot.rb +19 -0
  6. data/lib/should_pricot/count_assertion.rb +12 -0
  7. data/lib/should_pricot/element_assertion.rb +39 -0
  8. data/should_pricot.gemspec +31 -0
  9. data/tasks/should_pricot_tasks.rake +4 -0
  10. data/test/fixtures/posts.yml +4 -0
  11. data/test/fixtures/users.yml +9 -0
  12. data/test/functional/users_controller_test.rb +16 -0
  13. data/test/should_pricot_test.rb +6 -0
  14. data/test/test-app/app/controllers/application_controller.rb +10 -0
  15. data/test/test-app/app/controllers/users_controller.rb +40 -0
  16. data/test/test-app/app/helpers/application_helper.rb +3 -0
  17. data/test/test-app/app/models/post.rb +3 -0
  18. data/test/test-app/app/models/user.rb +7 -0
  19. data/test/test-app/app/views/users/edit.html.erb +0 -0
  20. data/test/test-app/app/views/users/index.html.erb +9 -0
  21. data/test/test-app/app/views/users/new.html.erb +0 -0
  22. data/test/test-app/config/boot.rb +110 -0
  23. data/test/test-app/config/database.yml +4 -0
  24. data/test/test-app/config/environment.rb +12 -0
  25. data/test/test-app/config/environments/test.rb +0 -0
  26. data/test/test-app/config/initializers/funktional.rb +8 -0
  27. data/test/test-app/config/initializers/new_rails_defaults.rb +21 -0
  28. data/test/test-app/config/routes.rb +5 -0
  29. data/test/test-app/db/migrate/001_create_users.rb +18 -0
  30. data/test/test-app/db/migrate/002_create_posts.rb +13 -0
  31. data/test/test-app/public/404.html +30 -0
  32. data/test/test-app/public/422.html +30 -0
  33. data/test/test-app/public/500.html +30 -0
  34. data/test/test-app/script/console +3 -0
  35. data/test/test-app/script/generate +3 -0
  36. data/test/test-app/vendor/plugins/funktional/MIT-LICENSE +20 -0
  37. data/test/test-app/vendor/plugins/funktional/README.rdoc +289 -0
  38. data/test/test-app/vendor/plugins/funktional/Rakefile +23 -0
  39. data/test/test-app/vendor/plugins/funktional/init.rb +8 -0
  40. data/test/test-app/vendor/plugins/funktional/install.rb +1 -0
  41. data/test/test-app/vendor/plugins/funktional/lib/funktional.rb +41 -0
  42. data/test/test-app/vendor/plugins/funktional/lib/funktional/assertion.rb +9 -0
  43. data/test/test-app/vendor/plugins/funktional/lib/funktional/assigned_assertion.rb +39 -0
  44. data/test/test-app/vendor/plugins/funktional/lib/funktional/attribute_test_helper.rb +27 -0
  45. data/test/test-app/vendor/plugins/funktional/lib/funktional/context.rb +126 -0
  46. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/assigned_should_block.rb +32 -0
  47. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/collector.rb +21 -0
  48. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/count_should_block.rb +21 -0
  49. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/delegating_should_block.rb +30 -0
  50. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/element_should_block.rb +29 -0
  51. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/flashed_should_block.rb +17 -0
  52. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/should_block.rb +60 -0
  53. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/should_create_block.rb +17 -0
  54. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/should_delete_block.rb +17 -0
  55. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/should_not_block.rb +20 -0
  56. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/should_not_create_block.rb +17 -0
  57. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/should_not_delete_block.rb +17 -0
  58. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/should_not_send_email_block.rb +18 -0
  59. data/test/test-app/vendor/plugins/funktional/lib/funktional/context/stack_recorder.rb +34 -0
  60. data/test/test-app/vendor/plugins/funktional/lib/funktional/email_assertion.rb +50 -0
  61. data/test/test-app/vendor/plugins/funktional/lib/funktional/flashed_assertion.rb +12 -0
  62. data/test/test-app/vendor/plugins/funktional/lib/funktional/model_assertions.rb +108 -0
  63. data/test/test-app/vendor/plugins/funktional/lib/funktional/random_characters.rb +11 -0
  64. data/test/test-app/vendor/plugins/funktional/lib/funktional/recursive_assertion.rb +23 -0
  65. data/test/test-app/vendor/plugins/funktional/lib/funktional/route_checker.rb +49 -0
  66. data/test/test-app/vendor/plugins/funktional/lib/funktional/setup.rb +11 -0
  67. data/test/test-app/vendor/plugins/funktional/lib/funktional/test_class_methods.rb +28 -0
  68. data/test/test-app/vendor/plugins/funktional/lib/funktional/test_instance_methods.rb +131 -0
  69. data/test/test-app/vendor/plugins/funktional/tasks/should_b_tasks.rake +4 -0
  70. data/test/test-app/vendor/plugins/funktional/test/fixtures/posts.yml +4 -0
  71. data/test/test-app/vendor/plugins/funktional/test/fixtures/users.yml +5 -0
  72. data/test/test-app/vendor/plugins/funktional/test/test-app/app/controllers/application_controller.rb +10 -0
  73. data/test/test-app/vendor/plugins/funktional/test/test-app/app/helpers/application_helper.rb +3 -0
  74. data/test/test-app/vendor/plugins/funktional/test/test-app/app/models/post.rb +3 -0
  75. data/test/test-app/vendor/plugins/funktional/test/test-app/app/models/user.rb +7 -0
  76. data/test/test-app/vendor/plugins/funktional/test/test-app/config/boot.rb +110 -0
  77. data/test/test-app/vendor/plugins/funktional/test/test-app/config/database.yml +4 -0
  78. data/test/test-app/vendor/plugins/funktional/test/test-app/config/environment.rb +12 -0
  79. data/test/test-app/vendor/plugins/funktional/test/test-app/config/environments/test.rb +0 -0
  80. data/test/test-app/vendor/plugins/funktional/test/test-app/config/initializers/funktional.rb +8 -0
  81. data/test/test-app/vendor/plugins/funktional/test/test-app/config/initializers/new_rails_defaults.rb +21 -0
  82. data/test/test-app/vendor/plugins/funktional/test/test-app/config/routes.rb +4 -0
  83. data/test/test-app/vendor/plugins/funktional/test/test-app/db/migrate/001_create_users.rb +18 -0
  84. data/test/test-app/vendor/plugins/funktional/test/test-app/db/migrate/002_create_posts.rb +13 -0
  85. data/test/test-app/vendor/plugins/funktional/test/test-app/public/404.html +30 -0
  86. data/test/test-app/vendor/plugins/funktional/test/test-app/public/422.html +30 -0
  87. data/test/test-app/vendor/plugins/funktional/test/test-app/public/500.html +30 -0
  88. data/test/test-app/vendor/plugins/funktional/test/test-app/script/console +3 -0
  89. data/test/test-app/vendor/plugins/funktional/test/test-app/script/generate +3 -0
  90. data/test/test-app/vendor/plugins/funktional/test/test_helper.rb +27 -0
  91. data/test/test-app/vendor/plugins/funktional/test/unit/user_test.rb +12 -0
  92. data/test/test-app/vendor/plugins/funktional/uninstall.rb +1 -0
  93. data/test/test_helper.rb +30 -0
  94. metadata +170 -0
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the should_b plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the should_b plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ShouldB'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,8 @@
1
+ if RAILS_ENV.eql? 'test'
2
+ require File.join(File.dirname(__FILE__), "lib", "funktional")
3
+ end
4
+
5
+ if RAILS_ENV.eql? 'test' or $0 == 'irb'
6
+ require 'funktional/random_characters'
7
+ Integer.send :include, Funktional::RandomCharacters
8
+ end
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,41 @@
1
+ Rails.backtrace_cleaner.add_silencer { |line| line =~ /funktional/ }
2
+
3
+ require 'test_help'
4
+
5
+ require 'funktional/test_instance_methods'
6
+ require 'funktional/test_class_methods'
7
+
8
+ require 'funktional/assertion'
9
+ require 'funktional/assigned_assertion'
10
+ require 'funktional/email_assertion'
11
+ require 'funktional/flashed_assertion'
12
+ require 'funktional/model_assertions'
13
+ require 'funktional/route_checker'
14
+ require 'funktional/attribute_test_helper'
15
+
16
+ require 'funktional/context'
17
+ require 'funktional/context/stack_recorder'
18
+ require 'funktional/context/should_block'
19
+ require 'funktional/context/should_not_block'
20
+ require 'funktional/context/delegating_should_block'
21
+ require 'funktional/context/should_create_block'
22
+ require 'funktional/context/should_not_create_block'
23
+ require 'funktional/context/should_not_delete_block'
24
+ require 'funktional/context/should_not_send_email_block'
25
+ require 'funktional/context/should_delete_block'
26
+ require 'funktional/context/element_should_block'
27
+ require 'funktional/context/count_should_block'
28
+ require 'funktional/context/flashed_should_block'
29
+ require 'funktional/context/assigned_should_block'
30
+ require 'funktional/context/collector'
31
+
32
+ require 'funktional/setup'
33
+
34
+ ActiveRecord::Base.send(:include, Funktional::ModelAssertions)
35
+
36
+ ActiveSupport::TestCase.send(:include, Funktional::TestInstanceMethods)
37
+ ActiveSupport::TestCase.send :extend, Funktional::TestClassMethods
38
+ ActiveSupport::TestCase.send(:include, Funktional::AttributeTestHelper)
39
+
40
+ ActiveSupport::TestCase.send(:include, Funktional::Setup)
41
+
@@ -0,0 +1,9 @@
1
+ module Funktional
2
+ class Assertion
3
+ protected
4
+
5
+ def method_missing(method, *args)
6
+ Funktional.test_instance.send method, *args
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ module Funktional
2
+ class AssignedAssertion
3
+ def initialize(klass_or_symbol)
4
+ if klass_or_symbol.is_a? Symbol
5
+ @symbol = klass_or_symbol
6
+ else
7
+ @klass = klass_or_symbol
8
+ @symbol = get_symbol_from_klass
9
+ end
10
+
11
+ @test = Funktional.test_instance
12
+ @assigned = @test.assigns(@symbol)
13
+
14
+ @test.assert_not_nil @assigned, "No [#{@symbol}] assigned"
15
+
16
+ if @klass
17
+ @test.assert @assigned.is_a?(@klass), type_safety_failed_msg
18
+ end
19
+ end
20
+
21
+ def should_be(expected_value)
22
+ @test.assert_equal expected_value, @assigned
23
+ end
24
+
25
+ protected
26
+
27
+ def method_missing(method, *args)
28
+ RecursiveAssertion.new(@assigned, method)
29
+ end
30
+
31
+ def get_symbol_from_klass
32
+ @klass.to_s.tableize.singularize.to_sym
33
+ end
34
+
35
+ def type_safety_failed_msg
36
+ "assigned [#{@symbol}] is not a [#{@klass}]"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,27 @@
1
+ module Funktional
2
+ module AttributeTestHelper
3
+
4
+ def missing_attrib(to_remove)
5
+ copy_of_attrib = attrib.clone
6
+ found = copy_of_attrib.delete to_remove
7
+ raise Exception, 'attribute marked for removal is missing' unless found
8
+
9
+ return copy_of_attrib
10
+ end
11
+
12
+ def blank_attrib(to_blank)
13
+ attrib.merge(to_blank => '')
14
+ end
15
+
16
+ def replace_attrib(to_replace)
17
+ key = to_replace.keys.first
18
+ value = to_replace.values.first
19
+
20
+ attrib.merge(key => value)
21
+ end
22
+
23
+ def add_attrib(addition)
24
+ attrib.merge(addition)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,126 @@
1
+ # borrowing heavily from shoulda contexts.
2
+
3
+ module Funktional
4
+ class Context
5
+
6
+ attr_accessor :name
7
+ attr_accessor :parent
8
+ attr_accessor :sub_contexts
9
+ attr_accessor :setup_blocks
10
+ attr_accessor :teardown_blocks
11
+ attr_accessor :should_blocks
12
+
13
+ def initialize(name, parent, &blk)
14
+ Funktional.add_context(self)
15
+ self.name = name
16
+ self.parent = parent
17
+ self.setup_blocks = []
18
+ self.teardown_blocks = []
19
+ self.should_blocks = []
20
+ self.sub_contexts = []
21
+
22
+ merge_block(&blk)
23
+ Funktional.remove_context
24
+ end
25
+
26
+ def merge_block(&blk)
27
+ blk.bind(self).call
28
+ end
29
+
30
+ def context(name, &blk)
31
+ self.sub_contexts << Context.new(name, self, &blk)
32
+ end
33
+
34
+ def setup(&blk)
35
+ self.setup_blocks << blk
36
+ end
37
+
38
+ alias :before :setup
39
+
40
+ def teardown(&blk)
41
+ self.teardown_blocks << blk
42
+ end
43
+
44
+ alias :after :teardown
45
+
46
+ def should(options, &blk)
47
+ self.should_blocks << ShouldBlock.build(options, self, &blk)
48
+ end
49
+
50
+ def should_not(options, &blk)
51
+ self.should_blocks << ShouldNotBlock.build(options, self, &blk)
52
+ end
53
+
54
+ def element(selector)
55
+ recorder = StackRecorder.new(selector)
56
+ self.should_blocks << ElementShouldBlock.new(recorder, self)
57
+
58
+ return recorder
59
+ end
60
+
61
+ def count(selector)
62
+ recorder = StackRecorder.new(selector)
63
+ self.should_blocks << CountShouldBlock.new(recorder, self)
64
+
65
+ return recorder
66
+ end
67
+
68
+ def assigned(target)
69
+ recorder = StackRecorder.new(target)
70
+ self.should_blocks << AssignedShouldBlock.new(recorder, self)
71
+
72
+ return recorder
73
+ end
74
+
75
+ def flashed(kind)
76
+ recorder = StackRecorder.new(kind)
77
+ self.should_blocks << FlashedShouldBlock.new(recorder, self)
78
+
79
+ return recorder
80
+ end
81
+
82
+ def full_name
83
+ parent_name = parent.full_name if sub_context?
84
+ return [parent_name, name].join(" ").strip
85
+ end
86
+
87
+ def sub_context?
88
+ parent.is_a?(self.class)
89
+ end
90
+
91
+ def test_unit_class
92
+ sub_context? ? parent.test_unit_class : parent
93
+ end
94
+
95
+ def run_all_setup_blocks(binding)
96
+ run_parent_setup_blocks(binding)
97
+ run_current_setup_blocks(binding)
98
+ end
99
+
100
+ def run_parent_setup_blocks(binding)
101
+ self.parent.run_all_setup_blocks(binding) if sub_context?
102
+ end
103
+
104
+ def run_current_setup_blocks(binding)
105
+ setup_blocks.each do |setup_block|
106
+ setup_block.bind(binding).call
107
+ end
108
+ end
109
+
110
+ def run_all_teardown_blocks(binding)
111
+ teardown_blocks.reverse.each do |teardown_block|
112
+ teardown_block.bind(binding).call
113
+ end
114
+ self.parent.run_all_teardown_blocks(binding) if sub_context?
115
+ end
116
+
117
+ def build
118
+ self.should_blocks.map(&:to_test)
119
+ sub_contexts.each { |context| context.build }
120
+ end
121
+
122
+ def method_missing(method, *args, &blk)
123
+ test_unit_class.send(method, *args, &blk)
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,32 @@
1
+ module Funktional
2
+ class AssignedShouldBlock < ShouldBlock
3
+
4
+ def initialize(recorder, context)
5
+ @recorder, @context = recorder, context
6
+ @should_name = "the assigned(#{@recorder.__target})"
7
+
8
+ @blk = lambda do
9
+ result = nil
10
+
11
+ recorder.__each_called do |meth, args|
12
+ if result.nil?
13
+ result = assigned(recorder.__target).send(meth, *args)
14
+ else
15
+ result.send(meth, *args)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def test_name_parts
22
+ call_chain = ''
23
+
24
+ @recorder.__each_called do |meth, args|
25
+ call_chain << ".#{meth}" unless meth.eql? :should_be
26
+ end
27
+ @should_name << "#{call_chain} should be [#{@recorder.__last_value}]"
28
+
29
+ return ["test:", "#{@context.full_name},", "#{@should_name}"]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ module Funktional
2
+ class << self
3
+ attr_accessor :contexts
4
+
5
+ def contexts
6
+ @contexts ||= []
7
+ end
8
+
9
+ def current_context
10
+ self.contexts.last
11
+ end
12
+
13
+ def add_context(context)
14
+ self.contexts.push(context)
15
+ end
16
+
17
+ def remove_context
18
+ self.contexts.pop
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Funktional
2
+ class CountShouldBlock < ShouldBlock
3
+
4
+ def initialize(recorder, context)
5
+ @recorder, @context = recorder, context
6
+ @should_name = "count(#{@recorder.__target})"
7
+
8
+ @blk = lambda do
9
+ count(recorder.__target).send(recorder.__meth(0)[:name], *recorder.__meth(0)[:args])
10
+ end
11
+ end
12
+
13
+ def test_name_parts
14
+ parts = ["test:", "#{@context.full_name},", "#{@should_name}"]
15
+ parts << @recorder.__meth(0)[:name].to_s.gsub('_', ' ')
16
+ parts << "[#{@recorder.__meth(0)[:args]}]"
17
+
18
+ return parts
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ module Funktional
2
+ class DelegatingShouldBlock < ShouldBlock
3
+
4
+ def initialize(options, context, &blk)
5
+ @should_name = build_should_name(options, &blk)
6
+ @blk = proc { should(options, &blk) }
7
+ @context = context
8
+ end
9
+
10
+ def build_should_name(options, &blk)
11
+ case options.keys.first
12
+ when :render
13
+ "render the [#{options[:render]}] template"
14
+ when :render_404
15
+ "render the [#{options[:render_404]}] template with a [404] status code"
16
+ when :redirect_to
17
+ "redirect to the [#{options[:redirect_to]}] url"
18
+ when :assign_new
19
+ "assign a new [#{options[:assign_new]}]"
20
+ when :route, :method
21
+ args = (options.length > 1) ? options : options.values.first
22
+ RouteChecker.build(args, &blk).__test_name
23
+ when :send_email
24
+ "send an email #{options[:send_email].inspect}"
25
+ else
26
+ raise NoMethodError, "#{options.inspect} is not recognised"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ module Funktional
2
+ class ElementShouldBlock < ShouldBlock
3
+
4
+ def initialize(recorder, context)
5
+ @recorder, @context = recorder, context
6
+ @should_name = "element(#{@recorder.__target})"
7
+
8
+ @blk = lambda do
9
+ element(recorder.__target).send(recorder.__meth(0)[:name], *recorder.__meth(0)[:args])
10
+ end
11
+ end
12
+
13
+ def test_name_parts
14
+ parts = ["test:", "#{@context.full_name},", "#{@should_name}"]
15
+ parts << @recorder.__meth(0)[:name].to_s.gsub('_', ' ')
16
+
17
+ if called?(:should_be) or called?(:should_contain)
18
+ parts << "[#{@recorder.__meth(0)[:args]}]"
19
+ end
20
+ return parts
21
+ end
22
+
23
+ private
24
+
25
+ def called?(name)
26
+ @recorder.__meth(0)[:name].eql? name
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module Funktional
2
+ class FlashedShouldBlock < ShouldBlock
3
+
4
+ def initialize(recorder, context)
5
+ @recorder, @context = recorder, context
6
+ @should_name = "flash[:#{@recorder.__target}]"
7
+
8
+ @blk = lambda do
9
+ flashed(recorder.__target).send(recorder.__meth(0)[:name], *recorder.__meth(0)[:args])
10
+ end
11
+ end
12
+
13
+ def test_name_parts
14
+ ["test:", "#{@context.full_name},", @should_name, "should be", "[#{@recorder.__meth(0)[:args]}]"]
15
+ end
16
+ end
17
+ end