nayutaya-kagemusha 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY ADDED
@@ -0,0 +1,33 @@
1
+
2
+ == 0.0.8 2009-02-09
3
+
4
+ ...
5
+
6
+ == 0.0.7 2008-07-19
7
+
8
+ ...
9
+
10
+ == 0.0.6 2008-07-17
11
+
12
+ ...
13
+
14
+ == 0.0.5 2008-07-16
15
+
16
+ ...
17
+
18
+ == 0.0.4 2008-07-09
19
+
20
+ ...
21
+
22
+ == 0.0.3 2007-09-10
23
+
24
+ ...
25
+
26
+ == 0.0.2 2007-08-27
27
+
28
+ ...
29
+
30
+ == 0.0.1 2007-08-15
31
+
32
+ * 1 major enhancement:
33
+ * Initial release
data/HISTORY.ja ADDED
@@ -0,0 +1,43 @@
1
+
2
+ == 0.0.9 2009-06-29
3
+
4
+ * github.comへ移動
5
+
6
+ == 0.0.8 2009-02-09
7
+
8
+ * Ruby 1.9.1対応
9
+ * マジックコメントを追加
10
+ * ブロック引数のチェックが厳しくなった変更に対応
11
+ * メソッド一覧を取得するメソッド(methods等)がシンボルの配列を返すようになった変更に対応
12
+
13
+ == 0.0.7 2008-07-19
14
+
15
+ * lib/kagemusha/composite.rb、test/test_composite.rbがgemに格納されていなかったのを修正 :-(
16
+
17
+ == 0.0.6 2008-07-17
18
+
19
+ * Kagemusha::Compositeクラスを追加
20
+ * Kagemusha#+を追加
21
+ * private/protectedなインスタンスメソッドの置換に対応
22
+
23
+ == 0.0.5 2008-07-16
24
+
25
+ * Kernelモジュールのメソッドの復元に失敗する不具合を修正
26
+
27
+ == 0.0.4 2008-07-09
28
+
29
+ * インスタンスメソッド、クラスメソッドの復元に失敗する不具合を修正
30
+ * newgem 0.25.0で生成したパッケージに変更
31
+
32
+ == 0.0.3 2007-09-10
33
+
34
+ * インスタンスメソッド、クラスメソッドの追加/削除機能を追加
35
+
36
+ == 0.0.2 2007-08-27
37
+
38
+ * ブロックによる初期化機能を追加
39
+ * 各種スタイルでの記述例を追加
40
+
41
+ == 0.0.1 2007-08-15
42
+
43
+ * 初版
data/LICENSE ADDED
@@ -0,0 +1,54 @@
1
+
2
+ Kagemusha is copyrighted free software by Yuya Kato
3
+ <yuyakato at gmail dot com> You can redistribute it and/or modify it under
4
+ either the terms of the GPL or the conditions below:
5
+
6
+ 1. You may make and give away verbatim copies of the source form of the
7
+ software without restriction, provided that you duplicate all of the
8
+ original copyright notices and associated disclaimers.
9
+
10
+ 2. You may modify your copy of the software in any way, provided that
11
+ you do at least ONE of the following:
12
+
13
+ a) place your modifications in the Public Domain or otherwise make them
14
+ Freely Available, such as by posting said modifications to Usenet or an
15
+ equivalent medium, or by allowing the author to include your
16
+ modifications in the software.
17
+
18
+ b) use the modified software only within your corporation or
19
+ organization.
20
+
21
+ c) rename any non-standard executables so the names do not conflict with
22
+ standard executables, which must also be provided.
23
+
24
+ d) make other distribution arrangements with the author.
25
+
26
+ 3. You may distribute the software in object code or executable
27
+ form, provided that you do at least ONE of the following:
28
+
29
+ a) distribute the executables and library files of the software,
30
+ together with instructions (in the manual page or equivalent) on where
31
+ to get the original distribution.
32
+
33
+ b) accompany the distribution with the machine-readable source of the
34
+ software.
35
+
36
+ c) give non-standard executables non-standard names, with
37
+ instructions on where to get the original software distribution.
38
+
39
+ d) make other distribution arrangements with the author.
40
+
41
+ 4. You may modify and include the part of the software into any other
42
+ software (possibly commercial). But some files in the distribution
43
+ are not written by the author, so that they are not under this terms.
44
+
45
+ 5. The scripts and library files supplied as input to or produced as
46
+ output from the software do not automatically fall under the
47
+ copyright of the software, but belong to whomever generated them,
48
+ and may be sold commercially, and may be aggregated with this
49
+ software.
50
+
51
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
52
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
53
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
54
+ PURPOSE.
data/README ADDED
@@ -0,0 +1,57 @@
1
+
2
+ = Kagemusha - a library of testing mock-objects
3
+
4
+ http://github.com/nayutaya/kagemusha/
5
+
6
+ == DESCRIPTION:
7
+
8
+ Kagemusha is a library of helper functions
9
+ for testing Ruby scripts. It helps you generating
10
+ scoped mock-objects which overrides behavior of
11
+ the class restricted in given blocks, without
12
+ tainting a global area.
13
+
14
+ For example, if you override Date.today when you write
15
+ something about dates, then the code will taint global
16
+ areas to no purpose.
17
+
18
+ If you use Kagemusha, the test code will override
19
+ behavior of the class restricted in given block.
20
+
21
+ Example:
22
+ require "kagemusha"
23
+
24
+ musha = Kagemusha.new(Time)
25
+ musha.defs(:now) { self.local(2000, 1, 1) }
26
+ musha.def(:to_s) { self.strftime("%Y-%m-%d") }
27
+
28
+ musha.swap {
29
+ p Time.now #=> Sat Jan 01 00:00:00 +0900 2000
30
+ p Time.now.to_s #=> "2000-01-01"
31
+ }
32
+
33
+ Also, it has default useful mock-objects set Date, Time,
34
+ rand, and so on.
35
+
36
+ Example:
37
+ require "kagemusha/date"
38
+
39
+ musha = Kagemusha::Date.at(2000, 1, 1)
40
+ musha.swap {
41
+ p Date.today.to_s #=> "2000-01-01"
42
+ }
43
+
44
+ == SYNOPSIS:
45
+
46
+ see example directory.
47
+
48
+ == INSTALL:
49
+
50
+ The easiest way to get started with Kagemusha is to
51
+ install it via RubyGems. You can do this easily:
52
+
53
+ $ gem install nayutaya-kagemusha
54
+
55
+ == LICENSE:
56
+
57
+ Ruby's License
data/README.ja ADDED
@@ -0,0 +1,59 @@
1
+
2
+ 影武者 - テスト用モック作成支援ライブラリ
3
+ =========================================
4
+
5
+ これって何?
6
+ -----------
7
+
8
+ Rubyでテストコードを記述するための便利ライブラリです。
9
+
10
+ 例えば日付関係のテストを記述する際にはDate.todayなどの
11
+ 挙動を上書きするなどの手法がありますが、このやり方ですと
12
+ 無駄にグローバル領域を汚染してしまいます。
13
+
14
+ ブロックで指定された範囲内のみに限定して基本的なクラスの
15
+ 挙動を上書きするために、このライブラリを作りました。
16
+
17
+ 例:
18
+ require "kagemusha"
19
+
20
+ musha = Kagemusha.new(Time)
21
+ musha.defs(:now) { self.local(2000, 1, 1) }
22
+ musha.def(:to_s) { self.strftime("%Y-%m-%d") }
23
+
24
+ musha.swap {
25
+ p Time.now #=> Sat Jan 01 00:00:00 +0900 2000
26
+ p Time.now.to_s #=> "2000-01-01"
27
+ }
28
+
29
+ なお、よく使うと思われるDate、Time、randなどに対するモックを
30
+ いくつか標準で用意されています。
31
+
32
+ 例:
33
+ require "kagemusha/date"
34
+
35
+ musha = Kagemusha::Date.at(2000, 1, 1)
36
+ musha.swap {
37
+ p Date.today.to_s #=> "2000-01-01"
38
+ }
39
+
40
+
41
+ インストール
42
+ ------------
43
+
44
+ Gemでインストールしてください。
45
+
46
+ $ gem install nayutaya-kagemusha
47
+
48
+
49
+ 利用例
50
+ ------
51
+
52
+ exampleディレクトリを見てください。
53
+
54
+
55
+ サポート
56
+ --------
57
+
58
+ 何かありましたら下記までお願いします。
59
+ 加藤 勇也 (Yuya Kato) <yuyakato at gmail.com>
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+
2
+ require "rake/testtask"
3
+
4
+ task :default => [:test]
5
+
6
+ Rake::TestTask.new do |test|
7
+ test.libs << "test"
8
+ test.test_files = Dir.glob("test/test_*.rb")
9
+ test.verbose = true
10
+ end
11
+
12
+ desc "Generate gemspec file from template"
13
+ task :gemspec do
14
+ require "erb"
15
+ require "lib/kagemusha"
16
+
17
+ src = File.open("kagemusha.gemspec.erb", "rb") { |file| file.read }
18
+ erb = ERB.new(src, nil, "-")
19
+
20
+ version = Kagemusha::VERSION
21
+ date = Time.now.strftime("%Y-%m-%d")
22
+
23
+ files = Dir.glob("**/*").select { |s| File.file?(s) }
24
+ test_files = Dir.glob("test/**").select { |s| File.file?(s) }
25
+
26
+ File.open("kagemusha.gemspec", "wb") { |file|
27
+ file.write(erb.result(binding))
28
+ }
29
+ end
data/examples/date.rb ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ # $Id: date.rb 108 2009-02-09 06:04:39Z yuyakato $
3
+
4
+ require "rubygems"
5
+ require "kagemusha/date"
6
+
7
+ p Date.today.strftime("%Y-%m-%d") #=> today
8
+
9
+ # Normal Style.
10
+ musha = Kagemusha::Date.on(2007, 1, 1)
11
+ musha.swap {
12
+ p Date.today.strftime("%Y-%m-%d") #=> 2007-01-01
13
+ }
14
+
15
+ p Date.today.strftime("%Y-%m-%d") #=> today
16
+
17
+ # Block Style.
18
+ Kagemusha::Date.on(2007, 1, 1) {
19
+ p Date.today.strftime("%Y-%m-%d") #=> 2007-01-01
20
+ }
21
+
22
+ p Date.today.strftime("%Y-%m-%d") #=> today
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ # $Id: datetime.rb 108 2009-02-09 06:04:39Z yuyakato $
3
+
4
+ require "rubygems"
5
+ require "kagemusha/datetime"
6
+
7
+ p Date.today.strftime("%Y-%m-%d") #=> today
8
+ p Time.now #=> now
9
+ puts "---"
10
+
11
+ # Normal Style.
12
+ musha = Kagemusha::DateTime.at(1984, 5, 11, 5, 15, 30)
13
+ musha.swap {
14
+ p Date.today.strftime("%Y-%m-%d") #=> 1984-05-11
15
+ p Time.now #=> 1984-05-11 05:15:30
16
+ puts "---"
17
+ }
18
+
19
+ p Date.today.strftime("%Y-%m-%d") #=> today
20
+ p Time.now #=> now
21
+ puts "---"
22
+
23
+ # Block Style.
24
+ Kagemusha::DateTime.at(1984, 5, 11, 5, 15, 30) {
25
+ p Date.today.strftime("%Y-%m-%d") #=> 1984-05-11
26
+ p Time.now #=> 1984-05-11 05:15:30
27
+ puts "---"
28
+ }
29
+
30
+ p Date.today.strftime("%Y-%m-%d") #=> today
31
+ p Time.now #=> now
32
+ puts "---"
data/examples/first.rb ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ # $Id: first.rb 108 2009-02-09 06:04:39Z yuyakato $
3
+
4
+ require "rubygems"
5
+ require "kagemusha"
6
+
7
+ musha = Kagemusha.new(Time)
8
+ musha.defs(:now) { self.local(1984, 5, 11) }
9
+ musha.def(:+) { |other| other }
10
+
11
+ p Time.now #=> now
12
+ p Time.now + 60 * 60 * 24 #=> now + 24 hours
13
+ puts "---"
14
+
15
+ musha.swap {
16
+ p Time.now #=> 1984-05-11 00:00:00
17
+ p Time.now + 60 * 60 * 24 #=> 86400
18
+ puts "---"
19
+ }
20
+
21
+ p Time.now #=> now
22
+ p Time.now + 60 * 60 * 24 #=> now + 24 hours
23
+ puts "---"
data/examples/rand.rb ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ # $Id: rand.rb 108 2009-02-09 06:04:39Z yuyakato $
3
+
4
+ require "rubygems"
5
+ require "kagemusha/rand"
6
+
7
+ p rand(100) #=> rand
8
+ p rand(100) #=> rand
9
+ puts "---"
10
+
11
+ # Normal Style.
12
+ musha = Kagemusha::Rand.always(1)
13
+ musha.swap {
14
+ p rand(100) #=> 1
15
+ p rand(100) #=> 1
16
+ puts "---"
17
+ }
18
+
19
+ p rand(100) #=> rand
20
+ p rand(100) #=> rand
21
+ puts "---"
22
+
23
+ # Block Style.
24
+ Kagemusha::Rand.always(1) {
25
+ p rand(100) #=> 1
26
+ p rand(100) #=> 1
27
+ puts "---"
28
+ }
29
+
30
+ p rand(100) #=> rand
31
+ p rand(100) #=> rand
32
+ puts "---"
data/examples/style.rb ADDED
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ # $Id: style.rb 117 2009-02-09 08:09:27Z yuyakato $
3
+
4
+ require "rubygems"
5
+ require "kagemusha"
6
+
7
+ def one_plus_one
8
+ return 1 + 1
9
+ end
10
+
11
+ p one_plus_one #=> 2
12
+
13
+ # Normal Style.
14
+ musha = Kagemusha.new(Fixnum)
15
+ musha.def(:+) { |x| 1 }
16
+ musha.swap {
17
+ p one_plus_one #=> 1
18
+ }
19
+
20
+ p one_plus_one #=> 2
21
+
22
+ # Block Style.
23
+ Kagemusha.new(Fixnum) { |m|
24
+ m.def(:+) { |x| 1 }
25
+ m.swap {
26
+ p one_plus_one #=> 1
27
+ }
28
+ }
29
+
30
+ p one_plus_one #=> 2
31
+
32
+ # Chain Style.
33
+ Kagemusha.new(Fixnum).
34
+ def(:+) { |x| 1 }.
35
+ swap {
36
+ p one_plus_one #=> 1
37
+ }
38
+
39
+ p one_plus_one #=> 2
data/examples/time.rb ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ # $Id: time.rb 108 2009-02-09 06:04:39Z yuyakato $
3
+
4
+ require "rubygems"
5
+ require "kagemusha/time"
6
+
7
+ p Time.now #=> now
8
+
9
+ # Normal Style.
10
+ musha = Kagemusha::Time.at(2007, 1, 1)
11
+ musha.swap {
12
+ p Time.now #=> 2007-01-01 00:00:00
13
+ }
14
+
15
+ p Time.now #=> now
16
+
17
+ # Block Style.
18
+ Kagemusha::Time.at(2007, 1, 1) {
19
+ p Time.now #=> 2007-01-01 00:00:00
20
+ }
21
+
22
+ p Time.now #=> now
data/kagemusha.gemspec ADDED
@@ -0,0 +1,67 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.specification_version = 2
4
+ s.required_rubygems_version = Gem::Requirement.new(">= 0")
5
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
6
+
7
+ s.name = "kagemusha"
8
+ s.version = "0.0.9"
9
+ s.date = "2009-06-29"
10
+
11
+ s.authors = ["Yuya Kato"]
12
+ s.email = "yuyakato@gmail.com"
13
+
14
+ s.summary = "kagemusha"
15
+ s.description = "kagemusha"
16
+ s.homepage = "http://github.com/nayutaya/kagemusha/"
17
+
18
+ s.rubyforge_project = nil
19
+ s.has_rdoc = false
20
+ s.require_paths = ["lib"]
21
+
22
+ s.files = [
23
+ "examples/date.rb",
24
+ "examples/datetime.rb",
25
+ "examples/first.rb",
26
+ "examples/rand.rb",
27
+ "examples/style.rb",
28
+ "examples/time.rb",
29
+ "HISTORY",
30
+ "HISTORY.ja",
31
+ "kagemusha.gemspec",
32
+ "kagemusha.gemspec.erb",
33
+ "lib/kagemusha/composite.rb",
34
+ "lib/kagemusha/core.rb",
35
+ "lib/kagemusha/date.rb",
36
+ "lib/kagemusha/datetime.rb",
37
+ "lib/kagemusha/rand.rb",
38
+ "lib/kagemusha/time.rb",
39
+ "lib/kagemusha/version.rb",
40
+ "lib/kagemusha.rb",
41
+ "LICENSE",
42
+ "Rakefile",
43
+ "README",
44
+ "README.ja",
45
+ "test/test_bugs.rb",
46
+ "test/test_complex_cases.rb",
47
+ "test/test_composite.rb",
48
+ "test/test_date.rb",
49
+ "test/test_datetime.rb",
50
+ "test/test_helper.rb",
51
+ "test/test_kagemusha.rb",
52
+ "test/test_rand.rb",
53
+ "test/test_time.rb",
54
+ ]
55
+ s.test_files = [
56
+ "test/test_bugs.rb",
57
+ "test/test_complex_cases.rb",
58
+ "test/test_composite.rb",
59
+ "test/test_date.rb",
60
+ "test/test_datetime.rb",
61
+ "test/test_helper.rb",
62
+ "test/test_kagemusha.rb",
63
+ "test/test_rand.rb",
64
+ "test/test_time.rb",
65
+ ]
66
+ s.extra_rdoc_files = []
67
+ end
@@ -0,0 +1,33 @@
1
+
2
+ Gem::Specification.new do |s|
3
+ s.specification_version = 2
4
+ s.required_rubygems_version = Gem::Requirement.new(">= 0")
5
+ s.required_ruby_version = Gem::Requirement.new(">= 1.8.6")
6
+
7
+ s.name = "kagemusha"
8
+ s.version = <%= version.dump %>
9
+ s.date = <%= date.dump %>
10
+
11
+ s.authors = ["Yuya Kato"]
12
+ s.email = "yuyakato@gmail.com"
13
+
14
+ s.summary = "kagemusha"
15
+ s.description = "kagemusha"
16
+ s.homepage = "http://github.com/nayutaya/kagemusha/"
17
+
18
+ s.rubyforge_project = nil
19
+ s.has_rdoc = false
20
+ s.require_paths = ["lib"]
21
+
22
+ s.files = [
23
+ <%- files.each { |path| -%>
24
+ <%= path.dump %>,
25
+ <%- } -%>
26
+ ]
27
+ s.test_files = [
28
+ <%- test_files.each { |path| -%>
29
+ <%= path.dump %>,
30
+ <%- } -%>
31
+ ]
32
+ s.extra_rdoc_files = []
33
+ end
@@ -0,0 +1,42 @@
1
+ # coding: utf-8
2
+
3
+ #==============================================================================#
4
+ # $Id: composite.rb 108 2009-02-09 06:04:39Z yuyakato $
5
+ #==============================================================================#
6
+
7
+ class Kagemusha
8
+ class Composite
9
+ def initialize(*mocks)
10
+ @mocks = []
11
+ mocks.each { |mock| self << mock }
12
+ end
13
+
14
+ attr_reader :mocks
15
+
16
+ def size
17
+ return @mocks.size
18
+ end
19
+
20
+ def add(mock)
21
+ raise(ArgumentError) unless mock.kind_of?(Kagemusha) || mock.kind_of?(Kagemusha::Composite)
22
+ @mocks << mock
23
+ return self
24
+ end
25
+ alias << add
26
+
27
+ def concat(mock)
28
+ return self.class.new(*self.mocks) << mock
29
+ end
30
+ alias + concat
31
+
32
+ def swap(&block)
33
+ src = (0...self.size).to_a.reverse.inject("return yield") { |memo, index|
34
+ "@mocks[#{index}].swap { #{memo} }"
35
+ }
36
+ return eval(src, &block)
37
+ end
38
+ end
39
+ end
40
+
41
+ #==============================================================================#
42
+ #==============================================================================#