picotest 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ =begin
2
+
3
+ This file is part of the picotest project, http://github.com/tario/picotest
4
+
5
+ Copyright (c) 2012 Roberto Dario Seminara <robertodarioseminara@gmail.com>
6
+
7
+ picotest is free software: you can redistribute it and/or modify
8
+ it under the terms of the gnu general public license as published by
9
+ the free software foundation, either version 3 of the license, or
10
+ (at your option) any later version.
11
+
12
+ picotest is distributed in the hope that it will be useful,
13
+ but without any warranty; without even the implied warranty of
14
+ merchantability or fitness for a particular purpose. see the
15
+ gnu general public license for more details.
16
+
17
+ you should have received a copy of the gnu general public license
18
+ along with picotest. if not, see <http://www.gnu.org/licenses/>.
19
+
20
+ =end
21
+ class Undo
22
+ def initialize(*data,&blk)
23
+ @blk = blk
24
+ @data = data
25
+ end
26
+
27
+ def undo
28
+ @blk.call(*@data)
29
+ end
30
+ end
31
+
32
+ class Method
33
+ def mock(options = {})
34
+ lambda{|*x|
35
+ undo = Array.new
36
+
37
+ begin
38
+
39
+ options.each do |k,v|
40
+ case k.to_s
41
+ when /^\@/
42
+ undo << Undo.new(receiver.instance_variable_get(k)) {|x| receiver.instance_variable_set(k,x) }
43
+ receiver.instance_variable_set(k,v)
44
+ when /^\$/
45
+ undo << Undo.new(eval(k.to_s)) {|x| eval("#{k.to_s} = x") }
46
+ eval("#{k.to_s} = v")
47
+ else
48
+ undo << Undo.new {
49
+ eval "
50
+ class << receiver
51
+ remove_method :#{k}
52
+ end
53
+ "
54
+ }
55
+
56
+ if v.respond_to? :call
57
+ receiver.define_singleton_method(k) do |*x|
58
+ v.call(*x)
59
+ end
60
+ else
61
+ receiver.define_singleton_method(k) do |*x|
62
+ v
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ call(*x)
69
+
70
+ ensure
71
+ undo.each &:undo
72
+ end
73
+ }
74
+ end
75
+ end
76
+
77
+ class Class
78
+ def new_mock(options = {})
79
+ mock(self,options)
80
+ end
81
+ end
82
+
83
+ def mock(*args)
84
+ options = args.select{|x| Hash === x }.inject{|x,y| x.merge(y) } || {}
85
+ klass = args.find{|x| Class === x} || Object
86
+ o = klass.new
87
+ options.each do |k,v|
88
+ case k.to_s
89
+ when /^\@/
90
+ o.instance_variable_set(k,v)
91
+ else
92
+ if v.respond_to? :call
93
+ o.define_singleton_method(k) do |*x|
94
+ v.call(*x)
95
+ end
96
+ else
97
+ o.define_singleton_method(k) do |*x|
98
+ v
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ o
105
+ end
106
+
@@ -0,0 +1,3 @@
1
+ module Picotest
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ require "picotest"
2
+
3
+ suite( "a b" => ["a","b"]).test :split
4
+ suite( lambda{|x|x.join " "} => _set(["a","b"], ["x","y","z","1"], ["aaa","bbb"]) ).test :split
5
+
6
+ class X
7
+ def foo(data)
8
+ data.split(";")[2].to_f
9
+ end
10
+ end
11
+
12
+ suite( "aaa;xxx;1.0;999" => 1.0, ";;3.2" => 3.2,
13
+ lambda{|x| ";;#{x}"} => _set(*(1..20).map(&:to_f)),
14
+ lambda{|x| "aaa;xxx;#{x};111"} => _set(*(1..20).map(&:to_f))
15
+ ).test X.new.method(:foo)
16
+
17
+ suite(_set(-2,2) => 4).test lambda{|x|x*x}
18
+ suite(lambda{|x|x*x} => 2).test Math.method(:sqrt)
19
+ suite(4 => lambda{|y,x| y*y == x}).test Math.method(:sqrt)
20
+ suite(_set(1,2,3,4,5) => lambda{|y,x| (y*y - x).abs<0.00001}).test Math.method(:sqrt)
21
+ suite(_set(*(1..100) ) => lambda{|y,x| (y*y - x).abs<0.00001}).test Math.method(:sqrt)
22
+
23
+ class X
24
+ def sum(*x)
25
+ x.inject{|a,b| a+b}
26
+ end
27
+ end
28
+
29
+ suite( 1 => 1, [1,2] => 3, [1,2,3] => 6 ).test X.new.method(:sum)
30
+ suite( _set(6,[1,5],[2,4],[3,3],[4,2],[5,1]) => 6 ).test X.new.method(:sum)
31
+ suite(_set(*(1..100)) => lambda{|y,x| y == x},
32
+ _set([1,2],[3,4],[4,6],[5,6]) => lambda{|y,x1,x2| y == x1+x2},
33
+ _set([1,2,3],[3,4,5],[4,6,7],[5,6,7]) => lambda{|y,x1,x2,x3| y == x1+x2+x3}
34
+ ).test X.new.method(:sum)
35
+
36
+
37
+ class X
38
+ def foo(a)
39
+ raise ArgumentError unless a.kind_of? Numeric
40
+
41
+ a*2
42
+ end
43
+ end
44
+
45
+ suite(1 => 2, 2 => 4, 3 => 6, "00" => _raise(ArgumentError)).test(X.new.method(:foo))
46
+
@@ -0,0 +1,55 @@
1
+ require "picotest"
2
+ # mocking sample
3
+
4
+ class X
5
+ def foo(data)
6
+ data.split(";")[@fieldno].to_f
7
+ end
8
+ end
9
+
10
+ s = suite( "aaa;xxx;1.0;999" => 1.0, ";;3.2" => 3.2,
11
+ lambda{|x| ";;#{x}"} => _set(*(1..20).map(&:to_f)),
12
+ lambda{|x| "aaa;xxx;#{x};111"} => _set(*(1..20).map(&:to_f)) )
13
+
14
+ # to mock instance variable use mock method with a hash
15
+ s.test X.new.method(:foo).mock(:@fieldno => 2)
16
+
17
+ class X
18
+ attr_accessor :fieldno
19
+ def foo(data)
20
+ data.split(";")[fieldno].to_f
21
+ end
22
+ end
23
+
24
+ # it also can be use to mock method on the object owner of the method being tested
25
+ s.test X.new.method(:foo).mock(:fieldno => 2)
26
+
27
+
28
+ class X
29
+ attr_accessor :another_object
30
+ def foo(data)
31
+ data.split(";")[another_object.fieldno].to_f
32
+ end
33
+ end
34
+
35
+ # you can return mock objects using mock method again
36
+ s.test X.new.method(:foo).mock(:another_object => mock(:fieldno => 2) )
37
+
38
+ class X
39
+ attr_accessor :another_object
40
+ def foo(data)
41
+ another_object.invented_here_split(data,";")[@fieldno].to_f
42
+ end
43
+ end
44
+
45
+ # you can specify the implementatioin of methods using lambdas
46
+ s.test X.new.method(:foo).mock(
47
+ :another_object => mock(:invented_here_split => lambda{|str,sep| str.split(sep) }),
48
+ :@fieldno => 2 )
49
+
50
+ # Since :split.to_proc is equivalent to lambda{|str,sep| str.split(sep) }, you can also use:
51
+ s.test X.new.method(:foo).mock(
52
+ :another_object => mock(:invented_here_split => :split.to_proc),
53
+ :@fieldno => 2 )
54
+
55
+
@@ -0,0 +1,23 @@
1
+ require "picotest"
2
+ # mocking sample for especial classes
3
+
4
+ class X
5
+ def foo(a)
6
+ l = bar
7
+ raise RuntimeError unless l.instance_of? Proc
8
+ l.call(a)+1
9
+ end
10
+ end
11
+
12
+ s = suite(0 => 1, 1 => 3, 2 => 5, 3 => 7)
13
+
14
+ # to mock a method returning lambda, you should use nested lambdas
15
+
16
+ s.test X.new.method(:foo).mock(:bar =>
17
+ lambda{ # this lambda represents the mock of bar method
18
+ lambda{|x| # this is the lambda returned by the fake bar
19
+ x*2
20
+ }
21
+ }
22
+ )
23
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: picotest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dario Seminara
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-02 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email: robertodarioseminara@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README
20
+ files:
21
+ - samples/sample2.rb
22
+ - samples/sample1.rb
23
+ - samples/sample3.rb
24
+ - lib/picotest.rb
25
+ - lib/picotest/core.rb
26
+ - lib/picotest/autotest.rb
27
+ - lib/picotest/version.rb
28
+ - lib/picotest/picomock.rb
29
+ - lib/picotest/autotest/samples.rb
30
+ - LICENSE
31
+ - AUTHORS
32
+ - README
33
+ - Rakefile
34
+ - CHANGELOG
35
+ homepage: http://github.com/tario/picotest
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.10
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Test pico framework. One-line test suite
59
+ test_files: []