isunit 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+ gem 'rake'
5
+ gem 'rspec'
6
+ gem 'mocha'
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ isunit (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.3)
10
+ metaclass (0.0.1)
11
+ mocha (0.10.0)
12
+ metaclass (~> 0.0.1)
13
+ rake (0.9.2.2)
14
+ rspec (2.7.0)
15
+ rspec-core (~> 2.7.0)
16
+ rspec-expectations (~> 2.7.0)
17
+ rspec-mocks (~> 2.7.0)
18
+ rspec-core (2.7.1)
19
+ rspec-expectations (2.7.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.7.0)
22
+
23
+ PLATFORMS
24
+ ruby
25
+
26
+ DEPENDENCIES
27
+ isunit!
28
+ mocha
29
+ rake
30
+ rspec
@@ -0,0 +1,2 @@
1
+ IsUnit ensures your unit tests don't have external dependencies or dependencies on other classes
2
+ unless explicitly stated making your code clean and easy to refactor.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "is_unit/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "isunit"
7
+ s.version = IsUnit::VERSION
8
+ s.authors = ["Sathish"]
9
+ s.email = ["sathish316@gmail.com"]
10
+ s.homepage = "http://www.github.com/sathish316/is_unit"
11
+ s.summary = %q{IsUnit ensures your unit tests don't have external dependencies or dependencies on other classes unless explicitly stated making your code clean and easy to refactor}
12
+ s.description = %q{IsUnit ensures your unit tests don't have external dependencies or dependencies on other classes unless explicitly stated making your code clean and easy to refactor}
13
+ s.rubyforge_project = "is_unit"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # specify any dependencies here; for example:
21
+ # s.add_development_dependency "rspec"
22
+ # s.add_runtime_dependency "rest-client"
23
+ end
@@ -0,0 +1,31 @@
1
+ require 'not_a_unit_test_error'
2
+ require 'rspec/mocks'
3
+
4
+ module IsUnit
5
+ def self.included(klass)
6
+ klass.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def is_unit(class_under_test, all_classes, &block)
11
+ classes_to_stub = all_classes - [class_under_test]
12
+
13
+ self.before(:each) do
14
+ classes_to_stub.each do |klass|
15
+ klass.stubs(:new).returns(object=klass.new)
16
+ klass.public_instance_methods(false).each do |method_name|
17
+ object.stubs(method_name).raises(NotAUnitTestError.new(class_under_test))
18
+ end
19
+ end
20
+ end
21
+
22
+ yield
23
+
24
+ self.after(:each) do
25
+ classes_to_stub.each do |klass|
26
+ klass.stubs(:new).returns(klass.new)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module IsUnit
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ class NotAUnitTestError < RuntimeError
2
+ def initialize(klass)
3
+ @klass = klass
4
+ end
5
+
6
+ def to_s
7
+ "#{@klass}Spec is not a unit test"
8
+ end
9
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ require 'model/game'
3
+ require 'model/player'
4
+ require 'model/dice'
5
+
6
+ describe Dice do
7
+ it "should throw random positive number" do
8
+ dice = Dice.new
9
+ dice.throw_number.should > 0
10
+ end
11
+
12
+ it "should throw random positive number less than 6" do
13
+ dice = Dice.new
14
+ dice.throw_number.should <= 6
15
+ end
16
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Game do
4
+ include IsUnit
5
+
6
+ describe "unit tests" do
7
+ is_unit(Game, all_classes) do
8
+ describe "by stubbing dependencies" do
9
+ it "should choose winner" do
10
+ player_1 = Player.new(Dice.new)
11
+ player_2 = Player.new(Dice.new)
12
+ game = Game.new(player_1, player_2)
13
+
14
+ player_1.expects(:play).returns(5)
15
+ player_2.expects(:play).returns(3)
16
+
17
+ game.play
18
+
19
+ game.winner.should == player_1
20
+ end
21
+ end
22
+
23
+ describe "with dependencies error" do
24
+ it "should play by throwing a number on dice" do
25
+ begin
26
+ player_1 = Player.new(Dice.new)
27
+ player_2 = Player.new(Dice.new)
28
+
29
+ game = Game.new(player_1, player_2)
30
+
31
+ game.play
32
+
33
+ game.winner.should_not be_nil
34
+ rescue => e
35
+ e.class.should == NotAUnitTestError
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ describe Game do
44
+ describe "integration tests" do
45
+ it "should play by throwing a number on dice" do
46
+ player_1 = Player.new(Dice.new)
47
+ player_2 = Player.new(Dice.new)
48
+
49
+ game = Game.new(player_1, player_2)
50
+
51
+ game.play
52
+
53
+ game.winner.should_not be_nil
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ class Dice
2
+
3
+ def throw_number
4
+ rand(6) + 1
5
+ end
6
+ end
@@ -0,0 +1,15 @@
1
+ class Game
2
+ attr_reader :winner
3
+
4
+ def initialize(player_1, player_2)
5
+ @player_1 = player_1
6
+ @player_2 = player_2
7
+ end
8
+
9
+ def play
10
+ points_1 = @player_1.play
11
+ points_2 = @player_2.play
12
+ @winner = points_1 >= points_2 ? @player_1 : @player_2
13
+ end
14
+
15
+ end
@@ -0,0 +1,9 @@
1
+ class Player
2
+ def initialize(dice)
3
+ @dice = dice
4
+ end
5
+
6
+ def play
7
+ @dice.throw_number
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Player do
4
+ include IsUnit
5
+
6
+ is_unit(Player, all_classes) do
7
+ describe "by stubbing dependencies" do
8
+ it "should play by throwing a number on dice" do
9
+ dice = Dice.new
10
+ player = Player.new(dice)
11
+
12
+ dice.expects(:throw_number).returns(5)
13
+
14
+ player.play.should == 5
15
+ end
16
+ end
17
+
18
+ describe "with dependencies error" do
19
+ it "should play by throwing a number on dice" do
20
+ begin
21
+ dice = Dice.new
22
+ player = Player.new(dice)
23
+
24
+ number = player.play
25
+ number.should > 0
26
+ number.should <= 6
27
+ rescue => e
28
+ e.class.should == NotAUnitTestError
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ describe Player do
36
+ describe "integration test" do
37
+ it "should play by throwing a number on dice" do
38
+ dice = Dice.new
39
+ player = Player.new(dice)
40
+
41
+ number = player.play
42
+ number.should > 0
43
+ number.should <= 6
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,12 @@
1
+ require 'bundler/setup'
2
+ require 'rspec/mocks'
3
+ require 'is_unit'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :mocha
7
+ end
8
+
9
+ #TODO: Load using paths
10
+ def all_classes
11
+ [Game, Player, Dice]
12
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: isunit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sathish
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-04 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: IsUnit ensures your unit tests don't have external dependencies or dependencies
15
+ on other classes unless explicitly stated making your code clean and easy to refactor
16
+ email:
17
+ - sathish316@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - README.markdown
25
+ - Rakefile
26
+ - is_unit.gemspec
27
+ - lib/is_unit.rb
28
+ - lib/is_unit/version.rb
29
+ - lib/not_a_unit_test_error.rb
30
+ - spec/dice_spec.rb
31
+ - spec/game_spec.rb
32
+ - spec/model/dice.rb
33
+ - spec/model/game.rb
34
+ - spec/model/player.rb
35
+ - spec/player_spec.rb
36
+ - spec/spec_helper.rb
37
+ homepage: http://www.github.com/sathish316/is_unit
38
+ licenses: []
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project: is_unit
57
+ rubygems_version: 1.8.10
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: IsUnit ensures your unit tests don't have external dependencies or dependencies
61
+ on other classes unless explicitly stated making your code clean and easy to refactor
62
+ test_files:
63
+ - spec/dice_spec.rb
64
+ - spec/game_spec.rb
65
+ - spec/model/dice.rb
66
+ - spec/model/game.rb
67
+ - spec/model/player.rb
68
+ - spec/player_spec.rb
69
+ - spec/spec_helper.rb