lax 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,30 +1,29 @@
1
1
  lax
2
2
  ===
3
- A bright smidgen of a testing framework that is not the boss of you.
3
+ An insouciant smidgen of a testing framework that is not the boss of you.
4
4
  ```ruby
5
- Lax.test { |assert|
6
- assert.calling(:/).on(1).with(0).raises ZeroDivisionError
5
+ Lax.test {
6
+ calling(:/).on(1).with(0).raises ZeroDivisionError
7
7
 
8
- assert.satisfies(->(n) { n == 1 }) { |returns_one|
9
- returns_one.calling(:+).on(0).with 1
8
+ returns(1) {
9
+ calling(:+).on(0).with 1
10
10
 
11
- returns_one.on(1) { |id_on_one|
12
- id_on_one.calling(:+).with 0
13
- id_on_one.calling(:*).with 1
11
+ on(1) {
12
+ calling(:+).with 0
13
+ calling(:*).with 1
14
14
 
15
15
  0.upto(10) { |n|
16
- id_on_one.calling(:**).with n
16
+ calling(:**).with n
17
17
  }
18
18
  }
19
19
  }
20
20
  }
21
- Lax.go
21
+ Lax.test!
22
22
  ```
23
23
  yes but why
24
24
  -----------
25
25
  * Everything about a test is independently scopeable - methods, arguments, receivers, blocks, expectations, hooks, and any metadata you might care to attach. Testing that one method call satisfies three conditions is as natural as testing that one condition is satisfied by three different method calls. Write tests in whatever way makes sense.
26
26
  * No hardcoded constraints on terminal output, handling of failed tests, w/e - it's all done with user-configurable hooks.
27
- * Support for (optional) concurrent testing (via threads - not currently known to be threadsafe in JRuby).
28
27
  * Code footprint so small, it's hardly there at all (< 200 SLOC).
29
28
  * Does not pollute your toplevel namespace or infect the entire Ruby object hierarchy with its code.
30
29
 
data/lax.gemspec CHANGED
@@ -11,6 +11,6 @@ Gem::Specification.new do |spec|
11
11
  spec.test_files = FileList['rakefile','test/**/*.rb']
12
12
  spec.license = 'MIT/X11'
13
13
  spec.homepage = 'http://github.com/gwentacle/lax'
14
- spec.summary = 'A more forgiving testing framework.'
15
- spec.description = 'A testing framework that tries to stay out of your way.'
14
+ spec.summary = 'An insouciant smidgen of a testing framework.'
15
+ spec.description = 'A lightweight testing framework that is not the boss of you.'
16
16
  end
data/lib/lax/case.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Lax
2
+ class Case < Hash
3
+ def test
4
+ Lax.call self[:before], self
5
+ self[:pass] = begin
6
+ self[:cond][self[:value]=self[:obj].__send__(self[:msg],*self[:args],&self[:blk])]
7
+ rescue self[:xptn] => e
8
+ true
9
+ rescue => e
10
+ self[:xptn] = e
11
+ false
12
+ end
13
+ Lax.call self[:after], self
14
+ end
15
+ end
16
+ end
17
+
data/lib/lax/hook.rb ADDED
@@ -0,0 +1,24 @@
1
+ module Lax
2
+ class Hook < Proc
3
+ def <<(cb); Hook.new {|e| self[cb[e]]} end
4
+ def +(cb); Hook.new {|e| self[e]; cb[e]} end
5
+
6
+ StartTime = Hook.new { @start = Time.now }
7
+ StopTime = Hook.new { @stop = Time.now }
8
+ PassFail = Hook.new {|tc| $stdout.write(tc[:pass] ? "\x1b[32m=\x1b[0m" : "\x1b[31m#\x1b[0m")}
9
+
10
+ Summary = Hook.new do |cases|
11
+ puts "\nFinished #{cases.size} tests" <<
12
+ " in #{(@stop - @start).round 10} seconds" <<
13
+ " with #{(cases.reject{|c|c[:pass]}).size} failures"
14
+ end
15
+
16
+ Failures = Hook.new do |cases|
17
+ cases.reject {|c|c[:pass]}.each do |f|
18
+ puts " #{Module===f[:obj] ? "#{f[:obj]}::" : "#{f[:obj].class}#"}#{f[:msg]}" <<
19
+ "#{?(+[*f[:args]]*', '+?) if f[:args]} " << (f.has_key?(:value) ? "#=> #{f[:value]}" : "raised unhandled #{f[:xptn].class}")
20
+ end
21
+ end
22
+ end
23
+ end
24
+
data/lib/lax/task.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'rake'
2
+ module Lax
3
+ class Task
4
+ include Rake::DSL
5
+ def initialize(opts = {})
6
+ dir = opts.delete(:dir) || :test
7
+ make_tasks dir, {
8
+ start: Hook::StartTime,
9
+ after: Hook::PassFail,
10
+ finish: Hook::StopTime + Hook::Summary + Hook::Failures
11
+ }.merge(opts)
12
+ end
13
+
14
+ private
15
+ def make_tasks(dir, opts)
16
+ namespace dir do
17
+ desc "[Lax] load all test files"
18
+ task load: make_groups(dir)
19
+ desc "[Lax] run all loaded tests"
20
+ task(:run) { Lax.test! opts }
21
+ end
22
+ desc "[Lax] load and run all tests"
23
+ task dir => ["#{dir}:load","#{dir}:run"]
24
+ end
25
+
26
+ def make_groups(dir)
27
+ FileList["#{dir}/**/*"].select {|f| File.directory? f}.map do |group|
28
+ name = group.sub(/^#{dir}\//,'').gsub(/\//,?:)
29
+ desc "[Lax] load files in #{group}"
30
+ task(name) { Dir["#{group}/*.rb"].each {|file| load file} }
31
+ [dir,name]*?:
32
+ end
33
+ end
34
+ end
35
+ end
36
+
data/lib/lax/tree.rb ADDED
@@ -0,0 +1,60 @@
1
+ module Lax
2
+ class Tree < Array
3
+ attr_reader :tc
4
+ def initialize(tc={})
5
+ @tc = Case.new.merge tc
6
+ end
7
+
8
+ def on(obj,&b)
9
+ where({obj: obj},&b)
10
+ end
11
+
12
+ def calling(msg,&b)
13
+ where({msg: msg},&b)
14
+ end
15
+
16
+ def with(*args,&b)
17
+ where({args: args},&b)
18
+ end
19
+
20
+ def with_block(blk=nil,&b)
21
+ blk ? where({blk: blk},&b) : where(blk: b)
22
+ end
23
+
24
+ def satisfies(cond=nil,&b)
25
+ cond ? where({cond: cond},&b) : where(cond: Lax.preproc(b))
26
+ end
27
+
28
+ def before(bef=nil,&b)
29
+ bef ? where({before: bef},&b) : where(before: b)
30
+ end
31
+
32
+ def after(aft=nil,&b)
33
+ aft ? where({after: aft},&b) : where(after: b)
34
+ end
35
+
36
+ def raises(xptn=StandardError,&b)
37
+ where({xptn: xptn},&b)
38
+ end
39
+
40
+ def it
41
+ calling(:tap).with_block {}
42
+ end
43
+
44
+ def returns(v,&b)
45
+ satisfies ->(e){e==v}, &b
46
+ end
47
+
48
+ def where(h,&b)
49
+ t=Tree.new tc.merge h
50
+ Lax.preproc(b)[t] if b
51
+ push(t).last
52
+ end
53
+
54
+ def leaves
55
+ any?? map(&:leaves).flatten : [tc]
56
+ end
57
+
58
+ end
59
+ end
60
+
data/lib/lax/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lax
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/lax.rb CHANGED
@@ -1,171 +1,35 @@
1
1
  require 'lax/version'
2
2
  module Lax
3
+ autoload :Case, 'lax/case'
4
+ autoload :Tree, 'lax/tree'
5
+ autoload :Hook, 'lax/hook'
6
+ autoload :Task, 'lax/task'
7
+
3
8
  class << self
4
9
  @@cases = []
5
10
  def test(c={},&b)
6
- @@cases += Tree.new(c).tap(&b).leaves
7
- end
8
-
9
- def go(runner_opts={})
10
- Runner.new(@@cases, runner_opts).go
11
- end
12
- end
13
-
14
- class Case < Hash
15
- def run
16
- self[:before] and self[:before][self]
17
- self[:pass] = begin
18
- self[:cond][self[:value]=self[:obj].__send__(self[:msg],*self[:args],&self[:blk])]
19
- rescue self[:xptn] => e
20
- true
21
- rescue => e
22
- self[:xptn] = e
23
- false
24
- end
25
- self[:after] and self[:after][self]
26
- end
27
- end
28
-
29
- class Tree < Array
30
- attr_reader :tc
31
- def initialize(tc={})
32
- @tc = Case.new.merge tc
33
- end
34
-
35
- def on(obj,&b)
36
- where({obj: obj},&b)
37
- end
38
-
39
- def calling(msg,&b)
40
- where({msg: msg},&b)
41
- end
42
-
43
- def with(*args,&b)
44
- where({args: args},&b)
45
- end
46
-
47
- def with_block(blk=nil,&b)
48
- blk ? where({blk: blk},&b) : where(blk: b)
49
- end
50
-
51
- def satisfies(cond=nil,&b)
52
- cond ? where({cond: cond},&b) : where(cond: b)
53
- end
54
-
55
- def before(bef=nil,&b)
56
- bef ? where({before: bef},&b) : where(before: b)
57
- end
58
-
59
- def after(aft=nil,&b)
60
- aft ? where({after: aft},&b) : where(after: b)
61
- end
62
-
63
- def raises(xptn=StandardError,&b)
64
- where({xptn: xptn},&b)
65
- end
66
-
67
- def it
68
- calling(:tap).with_block {}
69
- end
70
-
71
- def returns(v,&b)
72
- satisfies ->(e){e==v}, &b
73
- end
74
-
75
- def where(h)
76
- g=Tree.new tc.merge h
77
- yield g if block_given?
78
- push(g).last
79
- end
80
-
81
- def leaves
82
- any?? map(&:leaves).flatten : [tc]
83
- end
84
- end
85
-
86
- # Runner for test cases. Handles callbacks, concurrency, etc.
87
- class Runner
88
- attr_reader :cases
89
- # Takes an array of test cases and an optional hash of options.
90
- def initialize(cases, opts={})
91
- @cases, @opts = cases, {threads: 1}.merge(opts)
11
+ preproc(b)[t=Tree.new(c)]
12
+ t.leaves.tap {|cs|@@cases+=cs}
92
13
  end
93
14
 
94
- def go
95
- @opts[:start][self] if @opts[:start]
96
- todo = cases.dup
97
- (1..@opts[:threads]).map do
98
- Thread.new {run todo.shift while todo.any?}
99
- end.each &:join
100
- @opts[:finish][self] if @opts[:finish]
101
- self
102
- end
103
-
104
- private
105
- def run(c)
106
- @opts[:before][c] if @opts[:before]
107
- c.run
108
- @opts[:after][c] if @opts[:after]
109
- end
110
- end
111
-
112
- class Hook < Proc
113
- def <<(cb); Hook.new {|e| self[cb[e]]} end
114
- def +(cb); Hook.new {|e| self[e]; cb[e]} end
115
-
116
- StartTime = Hook.new do |rn|
117
- rn.extend(Module.new {attr_accessor :start, :stop}).start = Time.now
118
- end
119
-
120
- StopTime = Hook.new {|rn| rn.stop = Time.now }
121
-
122
- SimpleOut = Hook.new {|tc| $stdout.write(tc[:pass] ? "\x1b[32m=\x1b[0m" : "\x1b[31m#\x1b[0m")}
123
-
124
- Summary = Hook.new do |rn|
125
- puts "\nFinished #{(cs=rn.cases).size} tests" <<
126
- " in #{(rn.stop - rn.start).round 10} seconds" <<
127
- " with #{(cs.reject{|c|c[:pass]}).size} failures"
128
- end
129
-
130
- FailList = Hook.new do |rn|
131
- rn.cases.reject {|c|c[:pass]}.each do |f|
132
- puts " #{Module===f[:obj] ? "#{f[:obj]}::" : "#{f[:obj].class}#"}#{f[:msg]}" <<
133
- "#{?(+[*f[:args]]*', '+?) if f[:args]} " << (f.has_key?(:value) ? "#=> #{f[:value]}" : "raised unhandled #{f[:xptn].class}")
15
+ def test!(opts={})
16
+ cases = opts[:cases] || @@cases
17
+ call opts[:start], cases
18
+ cases.each do |c|
19
+ call opts[:before], c
20
+ c.test
21
+ call opts[:after], c
134
22
  end
135
- end
136
- end
137
-
138
- class Task
139
- include Rake::DSL
140
- def initialize(opts = {})
141
- dir = opts.delete(:dir) || :test
142
- runner_opts = {
143
- start: Hook::StartTime,
144
- after: Hook::SimpleOut,
145
- finish: Hook::StopTime + Hook::Summary + Hook::FailList
146
- }.merge opts
147
- make_tasks dir, runner_opts
23
+ call opts[:finish], cases
24
+ cases
148
25
  end
149
26
 
150
- private
151
- def make_tasks(dir, runner_opts)
152
- namespace dir do
153
- desc "[Lax] load all test files"
154
- task load: make_groups(dir)
155
- desc "[Lax] run all loaded tests"
156
- task(:run) { Lax.go runner_opts }
157
- end
158
- desc "[Lax] load and run all tests"
159
- task dir => ["#{dir}:load","#{dir}:run"]
27
+ def call(p, *as)
28
+ p[*as] if p
160
29
  end
161
30
 
162
- def make_groups(dir)
163
- FileList["#{dir}/**/*"].select {|f| File.directory? f}.map do |group|
164
- name = group.sub(/^#{dir}\//,'').gsub(/\//,?:)
165
- desc "[Lax] load files in #{group}"
166
- task(name) { Dir["#{group}/*.rb"].each {|file| load file} }
167
- [dir,name]*?:
168
- end
31
+ def preproc(p)
32
+ p.parameters.any?? p : ->(o) { o.instance_exec &p }
169
33
  end
170
34
  end
171
35
  end
data/rakefile CHANGED
@@ -1,5 +1,4 @@
1
1
  $LOAD_PATH << File.expand_path('../lib', __FILE__)
2
- require 'rake'
3
2
  require 'lax'
4
3
 
5
4
  Lax::Task.new
data/test/case/cases.rb CHANGED
@@ -1,14 +1,5 @@
1
- class TestCases
2
- def a_number
3
- rand(0..100)
4
- end
5
- def an_exception
6
- QWERTY.uiop[]
7
- end
8
- end
9
-
10
- Lax.test(obj: TestCases.new) {|assert|
11
- assert.calling(:a_number).satisfies {|n| Fixnum===n}
12
- assert.calling(:an_exception).raises
1
+ Lax.test {|assert|
2
+ assert.on(rand(0..100)).it.satisfies {|n| Fixnum===n}
3
+ assert.on(->{QWERTY.uiop[]}).calling(:call).raises
13
4
  }
14
5
 
data/test/case/control.rb CHANGED
@@ -6,26 +6,24 @@ Lax.test(obj: 1) {|that|
6
6
  }
7
7
  }
8
8
 
9
- Lax.test { |assert|
10
- assert.calling(:/).on(1).with(0).raises ZeroDivisionError
11
- assert.returns(1) { |assert_equal_to_one|
12
- assert_equal_to_one.on(0).calling(:+).with 1
13
- assert_equal_to_one.on(1) {|identity_on_one|
14
- identity_on_one.calling(:* ).with 1
15
- identity_on_one.calling(:**).with 2
16
- identity_on_one.calling(:+ ).with 0
9
+ Lax.test {
10
+ calling(:/).on(1).with(0).raises ZeroDivisionError
11
+ returns(1) {
12
+ on(0).calling(:+).with 1
13
+ on(1) {
14
+ calling(:* ).with 1
15
+ calling(:**).with 2
16
+ calling(:+ ).with 0
17
17
  }
18
18
  }
19
19
  }
20
20
 
21
- Lax.test(msg: :object_id) {|id|
22
- id.satisfies(->(v){Fixnum===v}) {|_|
23
- _.on 1
24
- _.on 'asdf'
25
- _.on String
26
- _.on Lax
27
- _.calling(:size).on([1,2,3])
28
- }
21
+ Lax.test(msg: :object_id, cond: ->(v){Fixnum===v}) {
22
+ on 1
23
+ on 'asdf'
24
+ on String
25
+ on Lax
26
+ calling(:size).on([1,2,3])
29
27
  }
30
28
 
31
29
  Lax.test(obj: 222) {|that|
@@ -0,0 +1,5 @@
1
+ Lax.test {
2
+ on(2) { calling(:+) { with(3) { satisfies {odd?} } } }
3
+ on(2).calling(:+).with(3).satisfies &:odd?
4
+ }
5
+
data/test/unit/test.rb CHANGED
@@ -6,4 +6,3 @@ Lax.test(obj: 'asdf') {|test_that|
6
6
  test_that.satisfies {|n| n=='ASDF'}.calling(:upcase)
7
7
  }
8
8
 
9
-
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,9 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-05 00:00:00.000000000 Z
12
+ date: 2012-12-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: A testing framework that tries to stay out of your way.
14
+ description: A lightweight testing framework that is not the boss of you.
15
15
  email: feivel@sdf.org
16
16
  executables: []
17
17
  extensions: []
@@ -20,14 +20,18 @@ files:
20
20
  - lax.gemspec
21
21
  - lib/lax.rb
22
22
  - lib/lax/version.rb
23
+ - lib/lax/task.rb
24
+ - lib/lax/case.rb
25
+ - lib/lax/tree.rb
26
+ - lib/lax/hook.rb
23
27
  - README.md
24
28
  - LICENSE
25
29
  - rakefile
26
30
  - test/case/control.rb
27
31
  - test/case/cases.rb
32
+ - test/unit/implicit_receiver.rb
28
33
  - test/unit/callbacks.rb
29
34
  - test/unit/test.rb
30
- - test/unit/thread.rb
31
35
  homepage: http://github.com/gwentacle/lax
32
36
  licenses:
33
37
  - MIT/X11
@@ -52,11 +56,11 @@ rubyforge_project:
52
56
  rubygems_version: 1.8.23
53
57
  signing_key:
54
58
  specification_version: 3
55
- summary: A more forgiving testing framework.
59
+ summary: An insouciant smidgen of a testing framework.
56
60
  test_files:
57
61
  - rakefile
58
62
  - test/case/control.rb
59
63
  - test/case/cases.rb
64
+ - test/unit/implicit_receiver.rb
60
65
  - test/unit/callbacks.rb
61
66
  - test/unit/test.rb
62
- - test/unit/thread.rb
data/test/unit/thread.rb DELETED
@@ -1,14 +0,0 @@
1
- before = proc {sleep 0.01}
2
- cases = Lax::Tree.new(Lax::Case.new).tap {|tree|
3
- 1.upto(100) do |n|
4
- tree.on(n).it.returns n
5
- end
6
- }.leaves
7
-
8
- Lax.test(obj: Lax::Runner.new(cases, threads: 100, before: before)) {|that|
9
- that.calling(:go).satisfies {|runner|
10
- runner.cases.size == 100 and
11
- runner.cases.all? {|c| c[:pass]}
12
- }
13
- }
14
-