cutest 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/cutest.gemspec CHANGED
@@ -1,11 +1,11 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cutest"
3
- s.version = "0.0.5"
3
+ s.version = "0.0.6"
4
4
  s.summary = "Forking tests."
5
5
  s.description = "Run tests in separate processes to avoid shared state."
6
6
  s.authors = ["Damian Janowski", "Michel Martens"]
7
7
  s.email = ["djanowski@dimaion.com", "michel@soveran.com"]
8
8
  s.homepage = "http://github.com/djanowski/cutest"
9
- s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/cutest.rb", "cutest.gemspec", "test/assert.rb", "test/assert_raise.rb", "test/prepare.rb", "test/setup.rb"]
9
+ s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/cutest.rb", "cutest.gemspec", "test/assert.rb", "test/assert_raise.rb", "test/prepare.rb", "test/scopes.rb", "test/setup.rb"]
10
10
  s.add_dependency "batch", "~> 0.0.1"
11
11
  end
data/lib/cutest.rb CHANGED
@@ -1,55 +1,7 @@
1
1
  require "batch"
2
2
 
3
- class AssertionFailed < StandardError; end
4
-
5
- def assert(value)
6
- flunk unless value
7
- end
8
-
9
- def assert_raise(expected = Exception)
10
- begin
11
- yield
12
- rescue => exception
13
- ensure
14
- flunk unless exception.kind_of?(expected)
15
- end
16
- end
17
-
18
- def flunk(caller = caller[1])
19
- ex = AssertionFailed.new(@_test)
20
- ex.set_backtrace(caller)
21
-
22
- file, line = ex.backtrace.shift.split(":")
23
- code = File.readlines(file)[line.to_i - 1]
24
-
25
- ex.message.replace(">> #{@_test}\n=> #{code.strip}\n #{file} +#{line}")
26
-
27
- raise ex
28
- end
29
-
30
- @_prepare = []
31
-
32
- def prepare(&block)
33
- @_prepare << block if block_given?
34
- @_prepare
35
- end
36
-
37
- @_setup = nil
38
-
39
- def setup(&block)
40
- @_setup = block if block_given?
41
- @_setup
42
- end
43
-
44
- def test(name = nil, &block)
45
- @_test = name
46
-
47
- prepare.each { |block| block.call }
48
- block.call(setup && setup.call)
49
- end
50
-
51
3
  class Cutest < Batch
52
- VERSION = "0.0.5"
4
+ VERSION = "0.0.6"
53
5
 
54
6
  def report_errors
55
7
  return if @errors.empty?
@@ -83,8 +35,109 @@ class Cutest < Batch
83
35
  Process.wait
84
36
 
85
37
  output = read.read
86
- raise AssertionFailed.new(output) unless output.empty?
38
+ raise Cutest::AssertionFailed.new(output) unless output.empty?
87
39
  read.close
88
40
  end
89
41
  end
42
+
43
+ class AssertionFailed < StandardError; end
44
+
45
+ class Scope
46
+ def initialize(&scope)
47
+ @scope = scope
48
+ end
49
+
50
+ def call
51
+ instance_eval(&@scope)
52
+ end
53
+ end
54
+ end
55
+
56
+ module Kernel
57
+ private
58
+
59
+ # Use Thread.current[:cutest] to store information about test preparation and
60
+ # setup.
61
+ Thread.current[:cutest] ||= { :prepare => [] }
62
+
63
+ # Shortcut to access Thread.current[:cutest].
64
+ def cutest
65
+ Thread.current[:cutest]
66
+ end
67
+
68
+ # Create a class where the block will be evaluated. Recomended to improve
69
+ # isolation between tests.
70
+ def scope(&blk)
71
+ Cutest::Scope.new(&blk).call
72
+ end
73
+
74
+ # Prepare the environment in order to run the tests. This method can be called
75
+ # many times, and each new block is appened to a list of preparation blocks.
76
+ # When a test is executed, all the preparation blocks are ran in the order they
77
+ # were declared. If called without a block, it returns the array of preparation
78
+ # blocks.
79
+ def prepare(&block)
80
+ cutest[:prepare] << block if block_given?
81
+ cutest[:prepare]
82
+ end
83
+
84
+ # Setup parameters for the tests. The block passed to setup is evaluated before
85
+ # running each test, and the result of the setup block is passed to the test as
86
+ # a parameter. If the setup and the tests are declared at the same level (in
87
+ # the global scope or in a sub scope), it is possible to use instance
88
+ # variables, but the parameter passing pattern is recommended to ensure there
89
+ # are no side effects.
90
+ #
91
+ # If the setup blocks are declared in the global scope and the tests are
92
+ # declared in sub scopes, the parameter passing usage is required.
93
+ #
94
+ # Setup blocks can be defined many times, but each new definition overrides the
95
+ # previous one. It is recommended to split the tests in many different files
96
+ # (the report is per file, not per assertion). Usually one setup block per file
97
+ # is enough, but nothing forbids having different scopes with different setup
98
+ # blocks.
99
+ def setup(&block)
100
+ cutest[:setup] = block if block_given?
101
+ cutest[:setup]
102
+ end
103
+
104
+ # Call all the prepare blocks and setup blocks before executing the test. Even
105
+ # though the assertions can live anywhere (it's not mandatory to put them
106
+ # inside test blocks), it is necessary to wrap them in test blocks in order to
107
+ # execute preparation and setup blocks.
108
+ def test(name = nil, &block)
109
+ @_test = name
110
+
111
+ prepare.each { |block| block.call }
112
+ block.call(setup && setup.call)
113
+ end
114
+
115
+ # Assert that value is not nil or false.
116
+ def assert(value)
117
+ flunk unless value
118
+ end
119
+
120
+ # Assert that the block doesn't raise the expected exception.
121
+ def assert_raise(expected = Exception)
122
+ begin
123
+ yield
124
+ rescue => exception
125
+ ensure
126
+ flunk unless exception.kind_of?(expected)
127
+ end
128
+ end
129
+
130
+ # Stop the tests and raise an error where the message is the last line executed
131
+ # before flunking.
132
+ def flunk(caller = caller[1])
133
+ ex = Cutest::AssertionFailed.new(@_test)
134
+ ex.set_backtrace(caller)
135
+
136
+ file, line = ex.backtrace.shift.split(":")
137
+ code = File.readlines(file)[line.to_i - 1]
138
+
139
+ ex.message.replace(">> #{@_test}\n=> #{code.strip}\n #{file} +#{line}")
140
+
141
+ raise ex
142
+ end
90
143
  end
data/test/assert.rb CHANGED
@@ -3,7 +3,7 @@ test "succeeds if the value is true" do
3
3
  end
4
4
 
5
5
  test "raises if the assertion fails" do
6
- assert_raise(AssertionFailed) do
6
+ assert_raise(Cutest::AssertionFailed) do
7
7
  assert false
8
8
  end
9
9
  end
data/test/assert_raise.rb CHANGED
@@ -6,7 +6,7 @@ end
6
6
 
7
7
 
8
8
  test "raises if the expectation is not met" do
9
- assert_raise(AssertionFailed) do
9
+ assert_raise(Cutest::AssertionFailed) do
10
10
  assert_raise(RuntimeError) do
11
11
  raise ArgumentError
12
12
  end
data/test/prepare.rb CHANGED
@@ -1,19 +1,25 @@
1
1
  prepare do
2
- @foo = []
2
+ $foo = []
3
3
  end
4
4
 
5
5
  prepare do
6
- @foo << true
6
+ $foo << true
7
7
  end
8
8
 
9
9
  test "all the prepare blocks are called" do
10
- assert @foo == [true]
10
+ assert $foo == [true]
11
11
  end
12
12
 
13
13
  prepare do
14
- @foo << false
14
+ $foo << false
15
15
  end
16
16
 
17
17
  test "and are cumulative" do
18
- assert @foo == [true, false]
18
+ assert $foo == [true, false]
19
+ end
20
+
21
+ module Foo
22
+ test "and run inside modules" do
23
+ assert $foo = [true, false]
24
+ end
19
25
  end
data/test/scopes.rb ADDED
@@ -0,0 +1,31 @@
1
+ @bar = true
2
+
3
+ prepare { $foo = false }
4
+
5
+ scope do
6
+ @foo = true
7
+
8
+ test "something" do
9
+ assert !$foo
10
+ assert defined?(@foo)
11
+ assert !defined?(@bar)
12
+ end
13
+ end
14
+
15
+ scope do
16
+ test "something" do
17
+ assert !$foo
18
+ assert !defined?(@foo)
19
+ assert !defined?(@bar)
20
+ end
21
+ end
22
+
23
+ scope do
24
+ @baz = true
25
+
26
+ scope do
27
+ test "something" do
28
+ assert !defined?(@baz)
29
+ end
30
+ end
31
+ end
data/test/setup.rb CHANGED
@@ -21,3 +21,9 @@ end
21
21
  test "only the most recently defined setup block is executed" do |value|
22
22
  assert "Hello world!" == value
23
23
  end
24
+
25
+ module Foo
26
+ test "works inside modules too" do |value|
27
+ assert "Hello world!" == value
28
+ end
29
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 5
9
- version: 0.0.5
8
+ - 6
9
+ version: 0.0.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Damian Janowski
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-15 00:00:00 -03:00
18
+ date: 2010-09-18 00:00:00 -03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -52,6 +52,7 @@ files:
52
52
  - test/assert.rb
53
53
  - test/assert_raise.rb
54
54
  - test/prepare.rb
55
+ - test/scopes.rb
55
56
  - test/setup.rb
56
57
  has_rdoc: true
57
58
  homepage: http://github.com/djanowski/cutest