k3_testing 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7ce15918881263ce04d979e3770fd24c4ea47410
4
+ data.tar.gz: 932ec59f91b770a06d8101dae8270f09d502bc3a
5
+ SHA512:
6
+ metadata.gz: 5ecb58369e34bd076fa243321933bd8b72537b57a84b6cc0bfaee3e48ec52666d9c79fc6c19b12386f31fed02ff896cd28b48be50f9875dabe0b784cb543897b
7
+ data.tar.gz: 444387064027aa65916a900759872172c187102ea0d2cdb3cd7f68a8cbcb5a4d7583d78838d7baf9b8de2a7dc265d896719e8a6bc975cc8964741426cf98eae6
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in k3_testing.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 K3 Integrations, LLC
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/Readme.md ADDED
@@ -0,0 +1,89 @@
1
+ K3 Testing Helpers
2
+ ==================
3
+
4
+ Provides Capybara selectors for finding the element in your document that represents a specific record, attribute, or role.
5
+
6
+ Installation
7
+ ------------
8
+
9
+ Add this line to your application's `Gemfile`:
10
+
11
+ gem 'k3_testing'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ ActionView Helpers: Usage
18
+ -------------------------
19
+
20
+ The main helper this gem provides is `present_model`. This method allows you to represent an
21
+ ActiveRecord object as HTML and will add appropriate data-role attributes as needed.
22
+
23
+ The simplest way of calling, just represents the object and one of its
24
+ attributes...
25
+
26
+ <%= present_model @user, :name %>
27
+
28
+ Will render something like the following:
29
+
30
+ <div data-role="user.42.name">Joe Blow</div>
31
+
32
+ You can also pass extra html attributes with your call by specifying
33
+ `:html_options` as an extra parameter. If you don't want a div element,
34
+ you may specify `:as` as a parameter as well. For example:
35
+
36
+ <%= present_model @user, :name, :as => :span, :html_options => {:id => 'current_user'} %>
37
+
38
+ Will render something like the following:
39
+
40
+ <span data-role="user.42.name" id="current_user">Joe Blow</span>
41
+
42
+ You may also leave out the attribute and specify a block having a single
43
+ parameter. That parameter may be used to render attribute elements.
44
+ For example:
45
+
46
+ <%= present_model @user do |u| %>
47
+ <%= u.attr :name %> is
48
+ <%= u.attr :age %> years old.
49
+ <% end %>
50
+
51
+ Will render something like:
52
+
53
+ <div data-role="user.42">
54
+ <span data-role="user.42.name">Joe Blow</span> is
55
+ <span data-role="user.42.age">55</span> years old
56
+ </div>
57
+
58
+ Finally, the `attr` method can also take the options `:as` and
59
+ `:html_options`, or even take a block. For example:
60
+
61
+ <%= present_model @user, :as => :tr do |u| %>
62
+ <%= u.attr :name, :as => :td, :html_options => {:class => 'name'} %>
63
+ <%= u.attr :age, :as => :td do %>
64
+ <%= u.object.age %> years old
65
+ <% end %>
66
+ <% end %>
67
+
68
+ Will render something like:
69
+
70
+ <tr data-role="user.42">
71
+ <td data-role="user.42.name" class="name">Joe Blow</td>
72
+ <td data-role="user.42.name">55 years old</td>
73
+ </tr>
74
+
75
+
76
+ Capybara selectors:
77
+ -------------------
78
+
79
+ Add this line to your `features/support/env.rb` (assuming you're using cucumber):
80
+
81
+ require 'k3_testing/capybara/selectors'
82
+
83
+ Then you will be able to use the selectors like this:
84
+
85
+ @user = User.first
86
+ find @user
87
+ page.find('table#myTable').find(@user).text
88
+ page.find('table#myTable').has_selector?(@user)
89
+ within(@user) { page.should have_content('$100.000') }
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/k3_testing/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Tyler Rick"]
6
+ gem.email = ["tyler@k3integrations.com"]
7
+ gem.description = %q{Capybara selectors for finding the element in your document that represents a specific record, attribute, or role}
8
+ gem.summary = gem.description
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "k3_testing"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = K3Testing::Version
17
+ end
@@ -0,0 +1,27 @@
1
+ # Example usage:
2
+ # @user = User.first
3
+ # find @user
4
+ # page.find('table#myTable').find(@user).text
5
+ # page.find('table#myTable').has_selector?(@user)
6
+ # within(@user) { page.should have_content('$100.000') }
7
+ Capybara.add_selector(:active_record_object) do
8
+ xpath { |obj| XPath.descendant[XPath.attr('data-role') == K3Testing::PresentModel.role_for(obj)] }
9
+ #failure_message { |node, selector| "no element found for obj #{obj}" }
10
+ match { |value| value.is_a?(ActiveRecord::Base) }
11
+ end
12
+
13
+ # Example usage:
14
+ # @user = User.first
15
+ # find [@user, :name]
16
+ # within(:content) { find [@user, :name] }
17
+ Capybara.add_selector(:active_record_object_with_attribute) do
18
+ xpath { |params| XPath.descendant[XPath.attr('data-role') == K3Testing::PresentModel.role_for(*params)] }
19
+ #failure_message { |node, selector| "no element found for obj #{obj} and attribute #{attr_name}" }
20
+ match { |params| params.is_a?(Array) && params[0].is_a?(ActiveRecord::Base) }
21
+ end
22
+
23
+ # TODO: Use starts-with(string1,string2) XPath selector (apparable not available in xpath gem)
24
+ Capybara.add_selector(:with_role) do
25
+ xpath { |value| XPath.descendant[XPath.attr('data-role').contains value] }
26
+ failure_message { |node, selector| "no element found with role '#{selector.locator}'" }
27
+ end
@@ -0,0 +1,111 @@
1
+ module K3Testing
2
+ # PresentModel adds a helper method to assist with creating HTML with
3
+ # data-role attributes identifying it. This is especially useful for testing
4
+ # but can also help with Javascript as well.
5
+ module PresentModel
6
+ # This method allows you to represent an ActiveRecord object as HTML and
7
+ # will add appropriate data-role attributes as needed.
8
+ #
9
+ # ==== Examples
10
+ #
11
+ # The simplest way of calling, just represents the object and one of its
12
+ # attributes...
13
+ #
14
+ # <%= present_model @user, :name %>
15
+ #
16
+ # Will render something like the following:
17
+ #
18
+ # <div data-role="user.42.name">Joe Blow</div>
19
+ #
20
+ # You can also pass extra html attributes with your call by specifying
21
+ # +:html_options+ as an extra parameter. If you don't want a div element,
22
+ # you may specify +:as+ as a parameter as well. For example:
23
+ #
24
+ # <%= present_model @user, :name, :as => :span, :html_options => {:id => 'current_user'} %>
25
+ #
26
+ # Will render something like the following:
27
+ #
28
+ # <span data-role="user.42.name" id="current_user">Joe Blow</span>
29
+ #
30
+ # You may also leave out the attribute and specify a block having a single
31
+ # parameter. That parameter may be used to render attribute elements.
32
+ # For example:
33
+ #
34
+ # <%= present_model @user do |u| %>
35
+ # <%= u.attr :name %> is
36
+ # <%= u.attr :age %> years old.
37
+ # <% end %>
38
+ #
39
+ # Will render something like:
40
+ #
41
+ # <div data-role="user.42">
42
+ # <span data-role="user.42.name">Joe Blow</span> is
43
+ # <span data-role="user.42.age">55</span> years old
44
+ # </div>
45
+ #
46
+ # Finally, the +attr+ method can also take the options +:as+ and
47
+ # +:html_options+, or even take a block. For example:
48
+ #
49
+ # <%= present_model @user, :as => :tr do |u| %>
50
+ # <%= u.attr :name, :as => :td, :html_options => {:class => 'name'} %>
51
+ # <%= u.attr :age, :as => :td do %>
52
+ # <%= u.object.age %> years old
53
+ # <% end %>
54
+ # <% end %>
55
+ #
56
+ # Will render something like:
57
+ #
58
+ # <tr data-role="user.42">
59
+ # <td data-role="user.42.name" class="name">Joe Blow</td>
60
+ # <td data-role="user.42.name">55 years old</td>
61
+ # </tr>
62
+ #
63
+ def present_model(obj, *attr_and_options, &block)
64
+ options = attr_and_options.last.is_a?(Hash) ? attr_and_options.pop : {}
65
+ attr = attr_and_options.pop
66
+ tag = options[:as] || :div
67
+ presenter = Presenter.new(obj,self)
68
+ output = block.nil? ? obj.send(attr).to_s : capture(presenter, &block)
69
+ html_opts = (options[:html_options] || {}).merge(role_hash_for(obj, attr))
70
+ content_tag tag, output, html_opts
71
+ end
72
+
73
+ # Returns just the data-role by itself in a hash for cases where you don't need the full
74
+ # Presenter or can't use it.
75
+ #
76
+ # This can be used to give a data-role attribute to an element in HAML, for example:
77
+ # %li{role_hash_for(record)}
78
+ #
79
+ def role_hash_for(obj, attr=nil)
80
+ {'data-role' => PresentModel.role_for(obj, attr)}
81
+ end
82
+
83
+ # Returns just the name of the role by itself as a string.
84
+ #
85
+ def self.role_for(obj, attr=nil)
86
+ klass = obj.is_a?(Class) ? obj : obj.class
87
+ klass = klass.name.underscore
88
+ id = ".#{obj.id}" if obj.respond_to?(:id)
89
+ attr = ".#{attr}" unless attr.blank?
90
+ "#{klass}#{id}#{attr}"
91
+ end
92
+ end
93
+
94
+ class Presenter
95
+ attr_reader :object
96
+
97
+ def initialize(obj, parent)
98
+ @object = obj
99
+ @parent = parent
100
+ end
101
+
102
+ def attr(name, options={}, &block)
103
+ tag = options[:as] || :span
104
+ output = block.nil? ? object.send(name).to_s : @parent.capture(&block)
105
+ html_opts = (options[:html_options] || {}).merge('data-role' => "#{object.class.name.underscore}.#{object.id}.#{name.to_s}")
106
+ @parent.content_tag tag, output, html_opts
107
+ end
108
+ end
109
+ end
110
+
111
+ ActionView::Base.send :include, K3Testing::PresentModel
@@ -0,0 +1,3 @@
1
+ module K3Testing
2
+ Version = "0.0.1"
3
+ end
data/lib/k3_testing.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "k3_testing/version"
2
+ require "k3_testing/presenter"
3
+
4
+ module K3Testing
5
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: k3_testing
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tyler Rick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Capybara selectors for finding the element in your document that represents
14
+ a specific record, attribute, or role
15
+ email:
16
+ - tyler@k3integrations.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - Rakefile
25
+ - Readme.md
26
+ - k3_testing.gemspec
27
+ - lib/k3_testing.rb
28
+ - lib/k3_testing/capybara/selectors.rb
29
+ - lib/k3_testing/presenter.rb
30
+ - lib/k3_testing/version.rb
31
+ homepage: ''
32
+ licenses: []
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.0.0
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Capybara selectors for finding the element in your document that represents
54
+ a specific record, attribute, or role
55
+ test_files: []