impasta 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 413356f9dae985577832481dc36aee8ca5a7a81d
4
+ data.tar.gz: 1b3ad9cc11a1937b783ce0d616d90c49e6f8906a
5
+ SHA512:
6
+ metadata.gz: 13c28c0051abb53493e155d713814b0b0ef04d2a2f4b736181e61b620b6c2409930f4b8f3692efbe2e607bf90c33adfa34cbe892ef56223ca19e4a3ccef3b373
7
+ data.tar.gz: ec5de3514157b92a0deee8d047291ef7ff50c556d9ebba1b2c170549c62b731975b8da492ba57a3aec351a9c4872ef61263ebd5af203462f80e6a3da4220e7b9
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ # gem ignores
21
+ Gemfile.lock
22
+ *.gem
@@ -0,0 +1 @@
1
+ impasta
@@ -0,0 +1 @@
1
+ ruby-2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in impasta.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Anthony M. Cook
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ Impasta
2
+ =======
3
+
4
+ > **im-past-a** (noun)
5
+ > 1. A piece of pasta found with other pasta noodles that do not share its type.
6
+ > **Formed from "imposter" and "pasta."**
7
+ > 2. A test spy that can impersonate a given class and/or track methods passed to it.
8
+
9
+ *protip: definition #2 is the one we're talking about*
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ gem 'impasta'
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install impasta
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( http://github.com/acook/impasta/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'impasta/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'impasta'
8
+ spec.version = Impasta::VERSION
9
+ spec.authors = ['Anthony M. Cook']
10
+ spec.email = ['me@anthonymcook.com']
11
+ spec.summary = %q{Test spy for Ruby.}
12
+ spec.description = %q{A test spy that can impersonate a given class and/or track methods passed to it.}
13
+ spec.homepage = 'https://github.com/acook/impasta'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split(?\x0)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ end
@@ -0,0 +1,139 @@
1
+ require 'impasta/version'
2
+
3
+ class Impasta
4
+ def initialize klass = nil, instantiate = true
5
+ @__impasta_caller__ = caller
6
+
7
+ if klass.is_a? Module then
8
+ @__impasta_class__ = klass
9
+ if klass.is_a?(Class) && instantiate then
10
+ @__impasta_instance__ = klass.new
11
+ @__impasta_name__ = "#<#{self.class}:#{@__impasta_instance__}>"
12
+ else
13
+ @__impasta_name__ = "#<#{self.class}:(#{klass.class})#{klass}>"
14
+ end
15
+ elsif klass.is_a? String then
16
+ @__impasta_name__ = "#<#{self.class}:#{klass}>"
17
+ @__impasta_nick__ = klass
18
+ elsif klass.nil? then
19
+ @__impasta_name__ = "#<#{self.class}:#{@__impasta_caller__.first}>"
20
+ else
21
+ @__impasta_instance__ = klass
22
+ @__impasta_class__ = klass.class
23
+
24
+ @__impasta_name__ = "#<#{self.class}:#{@__impasta_instance__}>"
25
+ end
26
+ end
27
+
28
+ def method_missing name, *args, &block
29
+ (@__impasta_methods__ ||= Array.new) << [name, args, block]
30
+
31
+ if @__impasta_instance__.nil? && @__impasta_klass__.nil? then
32
+ self
33
+ elsif @__impasta_instance__ && @__impasta_instance__.respond_to?(name) then
34
+ self
35
+ elsif @__impasta_klass__ && @__impasta_klass__.method_defined?(name) then
36
+ self
37
+ else
38
+ begin
39
+ super
40
+ rescue NoMethodError => error
41
+ raise ImpastaNoMethodError.new self, error
42
+ end
43
+ end
44
+ end
45
+
46
+ def to_s
47
+ @__impasta_name__
48
+ end
49
+
50
+ def impasta_inspect
51
+ impasta_dump.map do |var|
52
+ text << "#{var}: #{instance_variable_get(var)}\n" if var.to_s =~ /impasta/
53
+ text
54
+ end
55
+ end
56
+
57
+ def impasta_dump
58
+ instance_variables.inject(Hash.new) do |dump, var|
59
+ name = var.to_s.match(/@__impasta_(.*)__/)
60
+ dump[name.captures.first.to_sym] = instance_variable_get(var) if name
61
+ dump
62
+ end
63
+ end
64
+
65
+ class ImpastaNoMethodError < NoMethodError
66
+ def initialize impasta, parent_exception
67
+ @impasta, @parent_exception = impasta, parent_exception
68
+ @custom_message = "invalid message `#{method_info}' for #{object_info}"
69
+ end
70
+ attr :impasta, :parent_exception, :custom_message
71
+
72
+ def custom_backtrace
73
+ parent_exception.backtrace[1..-1]
74
+ end
75
+
76
+ alias_method :super_message, :message
77
+ alias_method :message, :custom_message
78
+ alias_method :super_backtrace, :backtrace
79
+ alias_method :backtrace, :custom_backtrace
80
+
81
+ def object_info
82
+ if object.is_a?(Class) then
83
+ object.name
84
+ elsif object.is_a?(String)
85
+ "Imposter object `#{object}' defined at `#{definition}'"
86
+ else
87
+ "instance of `#{object.class} < #{object.class.superclass}'"
88
+ end
89
+ end
90
+
91
+ def method_info
92
+ if method_name then
93
+ info = "`#{method_name}'"
94
+ info << " with args: #{args}" if args
95
+ if block then
96
+ info << args ? ' with' : ' and'
97
+ info << " block: #{block.inspect}"
98
+ end
99
+ info
100
+ else
101
+ parent_exception.message.gsub /impasta/, dump(:class).to_s
102
+ end
103
+ end
104
+
105
+ def method_name
106
+ bad_message.first
107
+ end
108
+
109
+ def block_info
110
+ block.inspect
111
+ end
112
+
113
+ def block
114
+ bad_message.last
115
+ end
116
+
117
+ def bad_message
118
+ accessed_methods.last
119
+ end
120
+
121
+ def definition
122
+ dump(:caller).first
123
+ end
124
+
125
+ def accessed_methods
126
+ dump(:methods) || Array.new
127
+ end
128
+
129
+ def object
130
+ dump(:instance) || dump(:class) || dump(:nick) || dump(:name)
131
+ end
132
+
133
+ def dump key = nil
134
+ @dump ||= impasta.impasta_dump
135
+ key ? @dump[key] : @dump
136
+ end
137
+ end
138
+
139
+ end
@@ -0,0 +1,3 @@
1
+ class Impasta
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,36 @@
1
+ require 'rspec'
2
+ require 'impasta'
3
+
4
+ describe Impasta do
5
+ context 'named impasta' do
6
+ subject(:imp){ Impasta.new 'whatever' }
7
+
8
+ it 'tracks passed in messsages' do
9
+ imp.foo
10
+ imp.bar
11
+
12
+ expect(imp.impasta_dump[:methods].map{|name,_,_| name}).to eq([:foo,:bar])
13
+ end
14
+ end
15
+
16
+ context 'has source object' do
17
+ subject(:imp){ Impasta.new Array }
18
+
19
+ it 'dissallows methods unknown to the source object' do
20
+ expect{ imp.nonexistant_method }.to raise_error
21
+ end
22
+
23
+ it 'captures errors and makes them debuggable' do
24
+ begin
25
+ imp.nonexistant_method
26
+ rescue Impasta::ImpastaNoMethodError => error
27
+ expect(error.parent_exception).to be # this is the original error, should you need it
28
+ expect(error.definition).to be # this is where you defined the Impasta object
29
+ expect(error.accessed_methods).to be # this will be a list of any of the methods called on it
30
+ expect(error.object).to be # this is what Impasta thinks the object should be
31
+ expect(error.method_name).to be # this is the method that wasn't found
32
+ end
33
+ end
34
+
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: impasta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Anthony M. Cook
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A test spy that can impersonate a given class and/or track methods passed
56
+ to it.
57
+ email:
58
+ - me@anthonymcook.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".ruby-gemset"
65
+ - ".ruby-version"
66
+ - Gemfile
67
+ - LICENSE
68
+ - README.md
69
+ - Rakefile
70
+ - impasta.gemspec
71
+ - lib/impasta.rb
72
+ - lib/impasta/version.rb
73
+ - spec/impasta_spec.rb
74
+ homepage: https://github.com/acook/impasta
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Test spy for Ruby.
98
+ test_files:
99
+ - spec/impasta_spec.rb