lax 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,7 +24,7 @@ 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 concurrent testing (via threads - not currently threadsafe in JRuby).
27
+ * Support for (optional) concurrent testing (via threads - not currently known to be threadsafe in JRuby).
28
28
  * Code footprint so small, it's hardly there at all (< 200 SLOC).
29
29
  * Does not pollute your toplevel namespace or infect the entire Ruby object hierarchy with its code.
30
30
 
data/lib/lax/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lax
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/lax.rb CHANGED
@@ -1,18 +1,171 @@
1
1
  require 'lax/version'
2
2
  module Lax
3
- autoload :Tree, 'lax/tree'
4
- autoload :RakeTask, 'lax/rake_task'
5
- autoload :Runner, 'lax/runner'
6
- autoload :CB, 'lax/cb'
7
3
  class << self
8
4
  @@cases = []
9
- def test(c={})
10
- yield(group = Tree.new(Hash===c ? c : {obj: c}))
11
- @@cases += group.cases
5
+ def test(c={},&b)
6
+ @@cases += Tree.new(c).tap(&b).leaves
12
7
  end
13
8
 
14
9
  def go(runner_opts={})
15
- Runner.new(@@cases.shift(@@cases.size), runner_opts).go
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)
92
+ end
93
+
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}")
134
+ 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
148
+ end
149
+
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"]
160
+ end
161
+
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
16
169
  end
17
170
  end
18
171
  end
data/rakefile CHANGED
@@ -2,6 +2,6 @@ $LOAD_PATH << File.expand_path('../lib', __FILE__)
2
2
  require 'rake'
3
3
  require 'lax'
4
4
 
5
- Lax::RakeTask.new
5
+ Lax::Task.new
6
6
  task default: :test
7
7
 
data/test/case/cases.rb CHANGED
@@ -7,7 +7,7 @@ class TestCases
7
7
  end
8
8
  end
9
9
 
10
- Lax.test(TestCases.new) {|assert|
10
+ Lax.test(obj: TestCases.new) {|assert|
11
11
  assert.calling(:a_number).satisfies {|n| Fixnum===n}
12
12
  assert.calling(:an_exception).raises
13
13
  }
data/test/case/control.rb CHANGED
@@ -1,4 +1,4 @@
1
- Lax.test(1) {|that|
1
+ Lax.test(obj: 1) {|that|
2
2
  that.calling(:/).with(0).raises ZeroDivisionError
3
3
  that.calling(:**) {|exponentiation|
4
4
  exponentiation.with(1).satisfies {|n|n==1}
@@ -11,25 +11,24 @@ Lax.test { |assert|
11
11
  assert.returns(1) { |assert_equal_to_one|
12
12
  assert_equal_to_one.on(0).calling(:+).with 1
13
13
  assert_equal_to_one.on(1) {|identity_on_one|
14
- identity_on_one.calling(:*).with 1
14
+ identity_on_one.calling(:* ).with 1
15
15
  identity_on_one.calling(:**).with 2
16
- identity_on_one.calling(:+).with 0
16
+ identity_on_one.calling(:+ ).with 0
17
17
  }
18
18
  }
19
19
  }
20
20
 
21
- Lax.test {|claim|
22
- claim.calling(:object_id).satisfies(->(v){Fixnum===v}) {|_|
21
+ Lax.test(msg: :object_id) {|id|
22
+ id.satisfies(->(v){Fixnum===v}) {|_|
23
23
  _.on 1
24
24
  _.on 'asdf'
25
25
  _.on String
26
26
  _.on Lax
27
+ _.calling(:size).on([1,2,3])
27
28
  }
28
-
29
- claim.calling(:size).on([1,2,3]).satisfies {|n|n==3}
30
29
  }
31
30
 
32
- Lax.test(222) {|that|
31
+ Lax.test(obj: 222) {|that|
33
32
  that.it.returns 222
34
33
  }
35
34
 
@@ -1,6 +1,6 @@
1
1
  @array = []
2
2
 
3
- Lax.test(@array) {|that|
3
+ Lax.test(obj: @array) {|that|
4
4
  that.before {@array << 1}.calling(:size) {|_|
5
5
  _.returns 1
6
6
  _.returns 2
data/test/unit/test.rb CHANGED
@@ -2,7 +2,7 @@ Lax.test {|assert|
2
2
  assert.calling(:upcase).on('asdf').returns('ASDF')
3
3
  }
4
4
 
5
- Lax.test('asdf') {|test_that|
5
+ Lax.test(obj: 'asdf') {|test_that|
6
6
  test_that.satisfies {|n| n=='ASDF'}.calling(:upcase)
7
7
  }
8
8
 
data/test/unit/thread.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  before = proc {sleep 0.01}
2
- cases = Lax::Tree.new.tap {|tree|
2
+ cases = Lax::Tree.new(Lax::Case.new).tap {|tree|
3
3
  1.upto(100) do |n|
4
4
  tree.on(n).it.returns n
5
5
  end
6
- }.cases
6
+ }.leaves
7
7
 
8
- Lax.test(Lax::Runner.new(cases, threads: 100, before: before)) {|that|
8
+ Lax.test(obj: Lax::Runner.new(cases, threads: 100, before: before)) {|that|
9
9
  that.calling(:go).satisfies {|runner|
10
10
  runner.cases.size == 100 and
11
11
  runner.cases.all? {|c| c[:pass]}
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-04 00:00:00.000000000 Z
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A testing framework that tries to stay out of your way.
15
15
  email: feivel@sdf.org
@@ -19,11 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - lax.gemspec
21
21
  - lib/lax.rb
22
- - lib/lax/rake_task.rb
23
22
  - lib/lax/version.rb
24
- - lib/lax/runner.rb
25
- - lib/lax/cb.rb
26
- - lib/lax/tree.rb
27
23
  - README.md
28
24
  - LICENSE
29
25
  - rakefile
data/lib/lax/cb.rb DELETED
@@ -1,28 +0,0 @@
1
- module Lax
2
- class CB < Proc
3
- def <<(cb); CB.new {|e| self[cb[e]]} end
4
- def +(cb); CB.new {|e| self[e]; cb[e]} end
5
-
6
- StartTime = CB.new { @start = Time.now }
7
- StopTime = CB.new { @stop = Time.now }
8
-
9
- SimpleOut = CB.new do |tc|
10
- $stdout.write(tc[:pass] ? "\x1b[32m=\x1b[0m" : "\x1b[31m#\x1b[0m")
11
- end
12
-
13
- Summary = CB.new do |rn|
14
- puts "\nFinished #{(cs=rn.cases).size} tests" <<
15
- " in #{(@stop - @start).round 10} seconds" <<
16
- " with #{(fs=cs.select{|c|!c[:pass]}).size} failures"
17
- end
18
-
19
- FailList = CB.new do |rn|
20
- fs = rn.cases.select{|c|!c[:pass]}
21
- fs.each do |f|
22
- puts " #{Module===f[:obj] ? "#{f[:obj]}::" : "#{f[:obj].class}#"}#{f[:msg]}" <<
23
- "#{?(+[*f[:args]]*', '+?) if f[:args]} " << (f.has_key?(:value) ? "#=> #{f[:value]}" : "raised unhandled #{f[:xptn].class}")
24
- end
25
- end
26
- end
27
- end
28
-
data/lib/lax/rake_task.rb DELETED
@@ -1,38 +0,0 @@
1
- module Lax
2
- class RakeTask
3
- include Rake::DSL
4
- def initialize(opts = {})
5
- dir = opts.delete(:dir) || :test
6
- runner_opts = {
7
- start: CB::StartTime,
8
- after: CB::SimpleOut,
9
- finish: CB::StopTime + CB::Summary + CB::FailList
10
- }.merge opts
11
- make_tasks dir, runner_opts
12
- end
13
-
14
- private
15
- def make_tasks(dir, runner_opts)
16
- namespace dir do
17
- desc "[Lax] load all test files"
18
- task load: make_test_groups(dir)
19
- desc "[Lax] run all loaded tests"
20
- task(:run) { Lax.go runner_opts }
21
- end
22
- desc "[Lax] load and run all tests"
23
- task dir => ["#{dir}:load","#{dir}:run"]
24
- end
25
-
26
- def make_test_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 do
31
- Dir["#{group}/*.rb"].each {|file| load file }
32
- end
33
- "#{dir}:#{name}"
34
- end
35
- end
36
- end
37
- end
38
-
data/lib/lax/runner.rb DELETED
@@ -1,45 +0,0 @@
1
- module Lax
2
- # Runner for test cases. Handles callbacks, concurrency, etc.
3
- # TODO: DRb support!
4
- class Runner
5
- attr_reader :cases
6
- # Takes an array of test cases and an optional hash of options.
7
- def initialize(cases, opts={})
8
- @cases, @opts = cases, {threads: 1}.merge(opts)
9
- end
10
-
11
- def go
12
- (start=@opts[:start]) and start[self]
13
- todo = @cases.dup
14
- (1..@opts[:threads]).map do
15
- Thread.new {after run before todo.shift while todo.any?}
16
- end.each &:join
17
- (finish=@opts[:finish]) and finish[self]
18
- self
19
- end
20
-
21
- private
22
- def run(c)
23
- begin
24
- c[:pass] = c[:cond][c[:value]=c[:obj].__send__(c[:msg],*c[:args],&c[:blk])]
25
- rescue c[:xptn] => e
26
- c[:pass] = true
27
- rescue => e
28
- c[:pass] = false
29
- c[:xptn] = e
30
- end
31
- c
32
- end
33
-
34
- def before(c)
35
- [@opts,c].each {|h| h[:before] and h[:before][c]}
36
- c
37
- end
38
-
39
- def after(c)
40
- [c,@opts].each {|h| h[:after] and h[:after][c]}
41
- c
42
- end
43
- end
44
- end
45
-
data/lib/lax/tree.rb DELETED
@@ -1,60 +0,0 @@
1
- module Lax
2
- class Tree < Array
3
- attr_reader :tc
4
- def initialize(tc={})
5
- @tc=tc
6
- end
7
-
8
- def on(obj,&b)
9
- where({obj: obj},&b)
10
- end
11
-
12
-
13
- def calling(msg,&b)
14
- where({msg: msg},&b)
15
- end
16
-
17
- def with(*args,&b)
18
- where({args: args},&b)
19
- end
20
-
21
- def with_block(blk=nil,&b)
22
- blk ? where({blk: blk},&b) : where(blk: b)
23
- end
24
-
25
- def satisfies(cond=nil,&b)
26
- cond ? where({cond: cond},&b) : where(cond: b)
27
- end
28
-
29
- def before(bef=nil,&b)
30
- bef ? where({before: bef},&b) : where(before: b)
31
- end
32
-
33
- def after(aft=nil,&b)
34
- aft ? where({after: aft},&b) : where(after: b)
35
- end
36
-
37
- def raises(xptn=StandardError,&b)
38
- where({xptn: xptn},&b)
39
- end
40
-
41
- def it
42
- calling(:tap).with_block {}
43
- end
44
-
45
- def returns(v,&b)
46
- satisfies ->(e){e==v}, &b
47
- end
48
-
49
- def where(h)
50
- g=Tree.new tc.merge h
51
- yield g if block_given?
52
- push(g).last
53
- end
54
-
55
- def cases
56
- any?? map {|n| n.any? ? n.cases : n.tc}.flatten : [tc]
57
- end
58
- end
59
- end
60
-