Permalink
Browse files

Cleanup spec tests

  • Loading branch information...
1 parent e7ed6f4 commit 469b59739b639ccf02779827ab1a9844603f4039 @svenyurgensson committed Jul 4, 2012
Showing with 56 additions and 32 deletions.
  1. +1 −1 Rakefile
  2. +5 −2 lib/penetrator.rb
  3. +1 −0 lib/penetrator/inflector.rb
  4. +1 −0 lib/penetrator/version.rb
  5. +5 −2 penetrator.gemspec
  6. +43 −27 spec/core_spec.rb
View
@@ -8,5 +8,5 @@ task :default => :test
Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
- t.test_files = FileList['specs/**/*_spec.rb']
+ t.test_files = FileList['spec/**/*_spec.rb']
end
View
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require "penetrator/inflector"
require "penetrator/version"
@@ -10,8 +11,10 @@ def self.included(base)
module ClassMethods
def behave_like(trait_name, *args)
- trait_name = "#{Penetrator::Inflector.camelize(trait_name.to_s)}Trait"
- trait = Penetrator::Inflector.constantize(trait_name)
+ full_name = "#{Penetrator::Inflector.camelize(trait_name.to_s)}Trait"
+ trait = Penetrator::Inflector.constantize(full_name)
+ trait_args_var = "@@" + trait_name.to_s.downcase.gsub(/[^_a-z]+/, '_')+"_args"
+ self.class_variable_set(trait_args_var.to_sym, (args || nil))
include trait
end
end
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
# These methods are backported from Rails so modularity works with plain Ruby.
module Penetrator
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
module Penetrator
VERSION = "0.0.1"
end
View
@@ -4,8 +4,8 @@ require File.expand_path('../lib/penetrator/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Yury Batenko"]
gem.email = ["jurbat@gmail.com"]
- gem.description = %q{Implement traits for using with ruby classes to get rid of repetition in the code}
- gem.summary = %q{Implement traits for using with ruby classes to get rid of repetition in the code}
+ gem.description = %q{Implement traits behavior to get rid of code repetition}
+ gem.summary = %q{Implement traits behavior to get rid of code repetition}
gem.homepage = "http://github/svenyurgensson/penetrator"
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
@@ -14,5 +14,8 @@ Gem::Specification.new do |gem|
gem.name = "penetrator"
gem.require_paths = ["lib"]
gem.version = Penetrator::VERSION
+ if RUBY_VERSION < '1.9.0'
+ gem.add_development_dependency "minitest"
+ end
gem.add_development_dependency "mocha"
end
View
@@ -1,3 +1,4 @@
+# -*- encoding : utf-8 -*-
require 'penetrator'
require 'minitest/spec'
require 'minitest/autorun'
@@ -16,7 +17,7 @@ module FirstTrait
def test; end
end
- it 'have traits methods' do
+ it 'add trait methods' do
class Victim
behave_like 'first'
end
@@ -32,7 +33,7 @@ def inner_test; end
end
- it 'have traits methods' do
+ it 'add traits methods from nested modules' do
class Victim
behave_like 'outer/inner'
end
@@ -63,34 +64,26 @@ def test
end # methods chainings
-
-# TODO
describe 'callbacks' do
- module FirstTrait
- def test; end
+ module CallbackTrait
end
it 'have traits methods' do
+ class Victim; end
+ CallbackTrait.expects(:included).with(Victim)
class Victim
- behave_like 'first'
+ behave_like 'callback'
end
end
-
end # callbacks
-
-
-
describe 'visibility' do
module VisibilityTrait
- def public_method_from_trait
- end
+ def public_method_from_trait; end
protected
- def protected_method_from_trait
- end
+ def protected_method_from_trait; end
private
- def private_method_from_trait
- end
+ def private_method_from_trait; end
end
it 'allow trait to define methods with different visibility' do
@@ -107,18 +100,41 @@ class Victim
end # visibility
-
describe 'trait arguments' do
- module HaveArgsTrait
- def test; end
- end
+ module HaveArgsTrait; end
it 'receive trait arguments' do
- lambda {
- class Victim
- behave_like 'have_args', 'arg1', 'arg2'
- end
- }.wont_raise
+ class Victim; end
+ Victim.expects(:behave_like).with('have_args', 'arg1', 'arg2')
+
+ class Victim
+ behave_like 'have_args', 'arg1', 'arg2'
+ end
+
end
-end
+ it 'base holds arguments' do
+ class Victim
+ behave_like 'have_args', 'arg1', 'arg2'
+ end
+ Victim.class_variable_get(:@@have_args_args).must_equal ['arg1','arg2']
+ end
+
+
+ describe 'arguments defined before trait included' do
+ module HandyTrait
+ def self.included(base)
+ base.send :stub, base.class_variable_get(:@@handy_args)
+ end
+ end
+
+ class Victim; end
+ Victim.expects(:stub).with(['arg'])
+
+ class Victim
+ behave_like :handy, 'arg'
+ end
+ end # 'trait use arguments'
+
+
+end # trait argument

0 comments on commit 469b597

Please sign in to comment.