tbpgr_utils 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +67 -9
- data/lib/attributes_initializable.rb +1 -1
- data/lib/ghostable.rb +2 -2
- data/lib/open_classes/kernel.rb +98 -98
- data/lib/open_classes/object.rb +51 -51
- data/lib/tbpgr_utils/version.rb +1 -1
- data/lib/templatable.rb +3 -3
- data/lib/template_methodable.rb +66 -0
- data/lib/test_toolbox/kernel.rb +46 -46
- data/spec/attributes_initializable_spec.rb +5 -5
- data/spec/ghostable_spec.rb +9 -9
- data/spec/open_classes/kernel_spec.rb +173 -173
- data/spec/templatable_spec.rb +3 -3
- data/spec/template_methodable_spec.rb +107 -0
- data/spec/test_toolbox/kernel_spec.rb +15 -15
- metadata +13 -10
data/README.md
CHANGED
@@ -33,6 +33,7 @@ Or install it yourself as:
|
|
33
33
|
|TbpgrUtils Object#my_methods|return public/protected/private self define methods|
|
34
34
|
|TbpgrUtils String#justify_table|justify pipe format table string|
|
35
35
|
|Templatable module|get result from template + placeholder|
|
36
|
+
|TemplateMethodable module|for Template Method Pattern|
|
36
37
|
|
37
38
|
### Array#together
|
38
39
|
~~~ruby
|
@@ -350,21 +351,78 @@ line1:hoge-sample
|
|
350
351
|
line2:hige-sample
|
351
352
|
~~~
|
352
353
|
|
354
|
+
## TemplateMethodable
|
355
|
+
sample usage
|
356
|
+
|
357
|
+
~~~ruby
|
358
|
+
require "template_methodable"
|
359
|
+
# sample BaseClass
|
360
|
+
class BaseDeveloper
|
361
|
+
include TemplateMethodable
|
362
|
+
must_impl :easy_coding, :difficult_coding, :normal_coding
|
363
|
+
module DIFFICILTY
|
364
|
+
EASY = 1
|
365
|
+
NORMAL = 2
|
366
|
+
DIFFICILT = 3
|
367
|
+
end
|
368
|
+
def coding(difficulty)
|
369
|
+
ret = []
|
370
|
+
ret << "start coding"
|
371
|
+
case difficulty
|
372
|
+
when DIFFICILTY::EASY
|
373
|
+
ret << easy_coding
|
374
|
+
when DIFFICILTY::NORMAL
|
375
|
+
ret << normal_coding
|
376
|
+
when DIFFICILTY::DIFFICILT
|
377
|
+
ret << difficult_coding
|
378
|
+
else
|
379
|
+
fail 'error'
|
380
|
+
end
|
381
|
+
ret << "finish coding"
|
382
|
+
ret.join("\n")
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
# sample valid Concrete Class. it raises NotImplementedError.
|
387
|
+
class StarDeveloper < BaseDeveloper
|
388
|
+
def easy_coding
|
389
|
+
"complete 1 minutes"
|
390
|
+
end
|
391
|
+
def normal_coding
|
392
|
+
"complete 10 minutes"
|
393
|
+
end
|
394
|
+
def difficult_coding
|
395
|
+
"complete 59 minutes"
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
# sample invalid Concrete Class. if call NormalDeveloper#difficult_coding, it raises NotImplementedError.
|
400
|
+
class NormalDeveloper < BaseDeveloper
|
401
|
+
def easy_coding
|
402
|
+
"complete 10 minutes"
|
403
|
+
end
|
404
|
+
def normal_coding
|
405
|
+
"complete 100 minutes"
|
406
|
+
end
|
407
|
+
end
|
408
|
+
~~~
|
409
|
+
|
353
410
|
## Relation
|
354
411
|
if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
355
412
|
|
356
413
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
357
414
|
|
358
415
|
## History
|
359
|
-
* version 0.0.
|
360
|
-
* version 0.0.
|
361
|
-
* version 0.0.
|
362
|
-
* version 0.0.
|
363
|
-
* version 0.0.
|
364
|
-
* version 0.0.
|
365
|
-
* version 0.0.
|
366
|
-
* version 0.0.
|
367
|
-
* version 0.0.
|
416
|
+
* version 0.0.10 : add TemplateMethodable module.
|
417
|
+
* version 0.0.9 : add TestToolbox module. add Kernel#capture_stdout, Kernel#dp_line
|
418
|
+
* version 0.0.8 : add Kernel#bulk_define_methods
|
419
|
+
* version 0.0.7 : add Kernel#print_eval, Kernel#puts_eval
|
420
|
+
* version 0.0.6 : add Ghostable
|
421
|
+
* version 0.0.5 : add Templatable
|
422
|
+
* version 0.0.4 : AttributesInitializable::ClassMethods.attr_accessor_init
|
423
|
+
* version 0.0.3 : add Object#any_of?
|
424
|
+
* version 0.0.2 : loop all arrays by block.
|
425
|
+
* version 0.0.1 : first release.
|
368
426
|
|
369
427
|
## Contributing
|
370
428
|
|
@@ -28,7 +28,7 @@ module AttributesInitializable
|
|
28
28
|
instance_eval do
|
29
29
|
define_method :initialize do |values = nil, &block|
|
30
30
|
return block.call self if block
|
31
|
-
symbols.each {|symbol|
|
31
|
+
symbols.each { |symbol|method("#{symbol.to_s}=").call values[symbol] }
|
32
32
|
end
|
33
33
|
end
|
34
34
|
end
|
data/lib/ghostable.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require
|
2
|
+
require 'active_support/concern'
|
3
3
|
|
4
4
|
module Ghostable
|
5
5
|
extend ActiveSupport::Concern
|
@@ -7,7 +7,7 @@ module Ghostable
|
|
7
7
|
module ClassMethods
|
8
8
|
def ghost_method(pattern, basename, &ghost_block)
|
9
9
|
define_method basename do |method_name, *args, &block|
|
10
|
-
self.class.class_eval {ghost_block.call(method_name, *args, &block)}
|
10
|
+
self.class.class_eval { ghost_block.call(method_name, *args, &block) }
|
11
11
|
end
|
12
12
|
|
13
13
|
@patterns ||= {}
|
data/lib/open_classes/kernel.rb
CHANGED
@@ -1,98 +1,98 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Kernel
|
4
|
-
module Kernel
|
5
|
-
# Print code + eval result
|
6
|
-
#
|
7
|
-
# print_eval 8/4, binding # => 8/4 # => 2
|
8
|
-
# message = 'msg';print_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"
|
9
|
-
def print_eval(code, binding)
|
10
|
-
print exec_eval code, binding
|
11
|
-
end
|
12
|
-
|
13
|
-
# Puts code + eval result
|
14
|
-
#
|
15
|
-
# puts_eval 8/4, binding # => 8/4 # => 2\n
|
16
|
-
# message = 'msg';puts_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"\n
|
17
|
-
def puts_eval(code, binding)
|
18
|
-
puts exec_eval code, binding
|
19
|
-
end
|
20
|
-
|
21
|
-
# define methods to classes. methods have simple return value.
|
22
|
-
#
|
23
|
-
# bulk_define_methods [NilClass, FalseClass], :blank?, true
|
24
|
-
# bulk_define_methods [TrueClass, Numeric], "blank?", false
|
25
|
-
#
|
26
|
-
# puts nil.blank? # => true
|
27
|
-
# puts false.blank? # => true
|
28
|
-
# puts true.blank? # => false
|
29
|
-
# puts 1.blank? # => false
|
30
|
-
#
|
31
|
-
# bulk_define_methods [NilClass, FalseClass], [:blank?, :present?], [true, false]
|
32
|
-
# bulk_define_methods [TrueClass, Numeric], [:blank?, :present?], [false, true]
|
33
|
-
#
|
34
|
-
# puts nil.blank? # => true
|
35
|
-
# puts nil.present? # => false
|
36
|
-
# puts false.blank? # => true
|
37
|
-
# puts false.present? # => false
|
38
|
-
# puts true.blank? # => false
|
39
|
-
# puts true.present? # => true
|
40
|
-
# puts 1.blank? # => false
|
41
|
-
# puts 1.present? # => true
|
42
|
-
#
|
43
|
-
# bulk_define_methods NilClass, :blank?, true is same as following code
|
44
|
-
#
|
45
|
-
# class NilClass
|
46
|
-
# def blank?
|
47
|
-
# true
|
48
|
-
# end
|
49
|
-
# end
|
50
|
-
def bulk_define_methods(classes, methods, constants)
|
51
|
-
validate_bulk_define_classes!(classes)
|
52
|
-
validate_bulk_define_methods!(methods)
|
53
|
-
size = get_classes_size(classes)
|
54
|
-
tmp_classes = fill_same_values(classes, size)
|
55
|
-
tmp_methods = fill_same_values(methods, size)
|
56
|
-
tmp_constants = fill_same_values(constants, size)
|
57
|
-
tmp_classes.each do |klass|
|
58
|
-
define_method_constants(klass, tmp_methods, tmp_constants)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
private
|
63
|
-
def get_classes_size(classes)
|
64
|
-
size = classes.is_a?(Array) ? classes.size : 1
|
65
|
-
end
|
66
|
-
|
67
|
-
def fill_same_values(ary, size)
|
68
|
-
return ary if ary.is_a?(Array)
|
69
|
-
tmp = []
|
70
|
-
size.times {tmp << ary}
|
71
|
-
tmp
|
72
|
-
end
|
73
|
-
|
74
|
-
def validate_bulk_define_classes!(classes)
|
75
|
-
return if classes.class.any_of?(Array, Class)
|
76
|
-
fail TypeError, "invalid type #{a.class}. you have to use Array or Class or String or Symbol"
|
77
|
-
end
|
78
|
-
|
79
|
-
def validate_bulk_define_methods!(methods)
|
80
|
-
return if methods.class.any_of?(Array, String, Symbol)
|
81
|
-
fail TypeError, "invalid type #{a.class}. you have to use Array or Class or String or Symbol"
|
82
|
-
end
|
83
|
-
|
84
|
-
def define_method_constants(klass, methods, constants)
|
85
|
-
methods.each_with_index do |m, i|
|
86
|
-
klass.class_eval do
|
87
|
-
define_method m do |*args|
|
88
|
-
constants[i]
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
def exec_eval(code, binding)
|
95
|
-
ret = eval code, binding
|
96
|
-
"#{code} # => #{ret.inspect}"
|
97
|
-
end
|
98
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Kernel
|
4
|
+
module Kernel
|
5
|
+
# Print code + eval result
|
6
|
+
#
|
7
|
+
# print_eval 8/4, binding # => 8/4 # => 2
|
8
|
+
# message = 'msg';print_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"
|
9
|
+
def print_eval(code, binding)
|
10
|
+
print exec_eval code, binding
|
11
|
+
end
|
12
|
+
|
13
|
+
# Puts code + eval result
|
14
|
+
#
|
15
|
+
# puts_eval 8/4, binding # => 8/4 # => 2\n
|
16
|
+
# message = 'msg';puts_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"\n
|
17
|
+
def puts_eval(code, binding)
|
18
|
+
puts exec_eval code, binding
|
19
|
+
end
|
20
|
+
|
21
|
+
# define methods to classes. methods have simple return value.
|
22
|
+
#
|
23
|
+
# bulk_define_methods [NilClass, FalseClass], :blank?, true
|
24
|
+
# bulk_define_methods [TrueClass, Numeric], "blank?", false
|
25
|
+
#
|
26
|
+
# puts nil.blank? # => true
|
27
|
+
# puts false.blank? # => true
|
28
|
+
# puts true.blank? # => false
|
29
|
+
# puts 1.blank? # => false
|
30
|
+
#
|
31
|
+
# bulk_define_methods [NilClass, FalseClass], [:blank?, :present?], [true, false]
|
32
|
+
# bulk_define_methods [TrueClass, Numeric], [:blank?, :present?], [false, true]
|
33
|
+
#
|
34
|
+
# puts nil.blank? # => true
|
35
|
+
# puts nil.present? # => false
|
36
|
+
# puts false.blank? # => true
|
37
|
+
# puts false.present? # => false
|
38
|
+
# puts true.blank? # => false
|
39
|
+
# puts true.present? # => true
|
40
|
+
# puts 1.blank? # => false
|
41
|
+
# puts 1.present? # => true
|
42
|
+
#
|
43
|
+
# bulk_define_methods NilClass, :blank?, true is same as following code
|
44
|
+
#
|
45
|
+
# class NilClass
|
46
|
+
# def blank?
|
47
|
+
# true
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
def bulk_define_methods(classes, methods, constants)
|
51
|
+
validate_bulk_define_classes!(classes)
|
52
|
+
validate_bulk_define_methods!(methods)
|
53
|
+
size = get_classes_size(classes)
|
54
|
+
tmp_classes = fill_same_values(classes, size)
|
55
|
+
tmp_methods = fill_same_values(methods, size)
|
56
|
+
tmp_constants = fill_same_values(constants, size)
|
57
|
+
tmp_classes.each do |klass|
|
58
|
+
define_method_constants(klass, tmp_methods, tmp_constants)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
def get_classes_size(classes)
|
64
|
+
size = classes.is_a?(Array) ? classes.size : 1
|
65
|
+
end
|
66
|
+
|
67
|
+
def fill_same_values(ary, size)
|
68
|
+
return ary if ary.is_a?(Array)
|
69
|
+
tmp = []
|
70
|
+
size.times { tmp << ary }
|
71
|
+
tmp
|
72
|
+
end
|
73
|
+
|
74
|
+
def validate_bulk_define_classes!(classes)
|
75
|
+
return if classes.class.any_of?(Array, Class)
|
76
|
+
fail TypeError, "invalid type #{a.class}. you have to use Array or Class or String or Symbol"
|
77
|
+
end
|
78
|
+
|
79
|
+
def validate_bulk_define_methods!(methods)
|
80
|
+
return if methods.class.any_of?(Array, String, Symbol)
|
81
|
+
fail TypeError, "invalid type #{a.class}. you have to use Array or Class or String or Symbol"
|
82
|
+
end
|
83
|
+
|
84
|
+
def define_method_constants(klass, methods, constants)
|
85
|
+
methods.each_with_index do |m, i|
|
86
|
+
klass.class_eval do
|
87
|
+
define_method m do |*args|
|
88
|
+
constants[i]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def exec_eval(code, binding)
|
95
|
+
ret = eval code, binding
|
96
|
+
"#{code} # => #{ret.inspect}"
|
97
|
+
end
|
98
|
+
end
|
data/lib/open_classes/object.rb
CHANGED
@@ -1,51 +1,51 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Object
|
4
|
-
class Object
|
5
|
-
# Check boolean type
|
6
|
-
#
|
7
|
-
# boolean? true # => true
|
8
|
-
# boolean? false # => true
|
9
|
-
# boolean? nil # => false
|
10
|
-
# boolean? 'true' # => false
|
11
|
-
# boolean? 'false' # => false
|
12
|
-
# boolean? '' # => false
|
13
|
-
def boolean?
|
14
|
-
self.is_a?(TrueClass) || self.is_a?(FalseClass)
|
15
|
-
end
|
16
|
-
|
17
|
-
# Get self define methods.
|
18
|
-
#
|
19
|
-
# class SampleClass < String
|
20
|
-
# def public_hello
|
21
|
-
# "public hello"
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# protected
|
25
|
-
#
|
26
|
-
# def protected_hello
|
27
|
-
# "protected hello"
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
# private
|
31
|
-
#
|
32
|
-
# def private_hello
|
33
|
-
# "private hello"
|
34
|
-
# end
|
35
|
-
# end
|
36
|
-
#
|
37
|
-
# SampleClass.new.my_methods # => [:public_hello, :protected_hello, :private_hello]
|
38
|
-
def my_methods
|
39
|
-
public_methods(false) + protected_methods(false) + private_methods(false)
|
40
|
-
end
|
41
|
-
|
42
|
-
# If self match any one of args, return true.
|
43
|
-
#
|
44
|
-
# "hoge".any_of? %w{hoge hige} # => true
|
45
|
-
# "hige".any_of? %w{hoge hige} # => true
|
46
|
-
# "hege".any_of? %w{hoge hige} # => false
|
47
|
-
def any_of?(*args)
|
48
|
-
args.each {|value|return true if self == value}
|
49
|
-
false
|
50
|
-
end
|
51
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Object
|
4
|
+
class Object
|
5
|
+
# Check boolean type
|
6
|
+
#
|
7
|
+
# boolean? true # => true
|
8
|
+
# boolean? false # => true
|
9
|
+
# boolean? nil # => false
|
10
|
+
# boolean? 'true' # => false
|
11
|
+
# boolean? 'false' # => false
|
12
|
+
# boolean? '' # => false
|
13
|
+
def boolean?
|
14
|
+
self.is_a?(TrueClass) || self.is_a?(FalseClass)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get self define methods.
|
18
|
+
#
|
19
|
+
# class SampleClass < String
|
20
|
+
# def public_hello
|
21
|
+
# "public hello"
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# protected
|
25
|
+
#
|
26
|
+
# def protected_hello
|
27
|
+
# "protected hello"
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# private
|
31
|
+
#
|
32
|
+
# def private_hello
|
33
|
+
# "private hello"
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# SampleClass.new.my_methods # => [:public_hello, :protected_hello, :private_hello]
|
38
|
+
def my_methods
|
39
|
+
public_methods(false) + protected_methods(false) + private_methods(false)
|
40
|
+
end
|
41
|
+
|
42
|
+
# If self match any one of args, return true.
|
43
|
+
#
|
44
|
+
# "hoge".any_of? %w{hoge hige} # => true
|
45
|
+
# "hige".any_of? %w{hoge hige} # => true
|
46
|
+
# "hege".any_of? %w{hoge hige} # => false
|
47
|
+
def any_of?(*args)
|
48
|
+
args.each { |value|return true if self == value }
|
49
|
+
false
|
50
|
+
end
|
51
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
data/lib/templatable.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require
|
3
|
-
require
|
2
|
+
require 'erb'
|
3
|
+
require 'active_support/concern'
|
4
4
|
|
5
5
|
module Templatable
|
6
6
|
extend ActiveSupport::Concern
|
@@ -14,7 +14,7 @@ module Templatable
|
|
14
14
|
define_method :get_placeholders do
|
15
15
|
ret = template.scan /<%=placeholders\[:(.*)\]%>/
|
16
16
|
ret_hash = {}
|
17
|
-
ret.each {|v| ret_hash[v.first.to_sym] = v.first}
|
17
|
+
ret.each { |v| ret_hash[v.first.to_sym] = v.first }
|
18
18
|
ret_hash
|
19
19
|
end
|
20
20
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support/concern'
|
3
|
+
require 'tbpgr_utils'
|
4
|
+
|
5
|
+
module TemplateMethodable
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# template method force class macro
|
10
|
+
#
|
11
|
+
# concrete class must define *methods.
|
12
|
+
# if not define '*method', raise NotImplementedError.
|
13
|
+
#
|
14
|
+
# sample BaseClass
|
15
|
+
# require "template_methodable"
|
16
|
+
# class BaseDeveloper
|
17
|
+
# include TemplateMethodable
|
18
|
+
# must_impl :easy_coding, :difficult_coding, :normal_coding
|
19
|
+
# module DIFFICILTY
|
20
|
+
# EASY = 1
|
21
|
+
# NORMAL = 2
|
22
|
+
# DIFFICILT = 3
|
23
|
+
# end
|
24
|
+
# def coding(difficulty)
|
25
|
+
# ret = []
|
26
|
+
# ret << "start coding"
|
27
|
+
# case difficulty
|
28
|
+
# when DIFFICILTY::EASY
|
29
|
+
# ret << easy_coding("hoge", "hige")
|
30
|
+
# when DIFFICILTY::NORMAL
|
31
|
+
# ret << normal_coding("hoge", "hige")
|
32
|
+
# when DIFFICILTY::DIFFICILT
|
33
|
+
# ret << difficult_coding("hoge", "hige")
|
34
|
+
# else
|
35
|
+
# fail 'error'
|
36
|
+
# end
|
37
|
+
# ret << "finish coding"
|
38
|
+
# ret.join("\n")
|
39
|
+
# end
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# sample Concrete Class. if you don't define xxxx_coding method, it raises NotImplementedError.
|
43
|
+
# class StarDeveloper < BaseDeveloper
|
44
|
+
# def easy_coding(hoge, hige)
|
45
|
+
# "complete 1 minutes"
|
46
|
+
# end
|
47
|
+
# def normal_coding(hoge, hige)
|
48
|
+
# "complete 10 minutes"
|
49
|
+
# end
|
50
|
+
# def difficult_coding(hoge, hige)
|
51
|
+
# "complete 59 minutes"
|
52
|
+
# end
|
53
|
+
# end
|
54
|
+
def must_impl(*methods)
|
55
|
+
return if methods.nil?
|
56
|
+
fail TypeError, "invalid args type #{methods.class}. you must use Array or Symbol" unless methods.class.any_of? Array, Symbol
|
57
|
+
methods = (methods.class.is_a? Symbol) ? [methods] : methods
|
58
|
+
methods.each do |method_name|
|
59
|
+
fail TypeError, "invalid args type #{method_name.class}. you must use Symbol" unless method_name.is_a? Symbol
|
60
|
+
define_method method_name do |*args|
|
61
|
+
fail NotImplementedError.new
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/test_toolbox/kernel.rb
CHANGED
@@ -1,46 +1,46 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
# Kernel
|
4
|
-
module Kernel
|
5
|
-
# capture STDOUT
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# capture print
|
9
|
-
# print capture_stdout { print "hoge" } # => hoge
|
10
|
-
#
|
11
|
-
# if block have no STDOUT, capture_stdout returns empty.
|
12
|
-
# print capture_stdout { }.empty? # => true
|
13
|
-
def capture_stdout
|
14
|
-
begin
|
15
|
-
$stdout = StringIO.new
|
16
|
-
yield
|
17
|
-
result = $stdout.string
|
18
|
-
ensure
|
19
|
-
$stdout = STDOUT
|
20
|
-
end
|
21
|
-
result
|
22
|
-
end
|
23
|
-
|
24
|
-
# debug print line for print-debugging
|
25
|
-
#
|
26
|
-
#
|
27
|
-
# debug print default
|
28
|
-
# dp_line __LINE__
|
29
|
-
# # yy = call line no
|
30
|
-
# # => --------------------|filename=|line=yy|--------------------
|
31
|
-
#
|
32
|
-
# debug print with filename
|
33
|
-
# dp_line __LINE__, filename: __FILE__
|
34
|
-
# # xx = filename, yy = call line no
|
35
|
-
# # => --------------------|filename=xx|line=yy|--------------------
|
36
|
-
#
|
37
|
-
# debug print with no filename, specific char
|
38
|
-
# dp_line __LINE__, char: '@'
|
39
|
-
# # xx = filename, yy = call line no
|
40
|
-
# # => @@@@@@@@@@@@@@@@@@@@|filename=|line=yy|@@@@@@@@@@@@@@@@@@@@
|
41
|
-
def dp_line(line, options = {filename:
|
42
|
-
filename = options[:filename].nil? ?
|
43
|
-
char = options[:char].nil? ?
|
44
|
-
puts "#{char*20}|filename=#{filename}|line=#{line}|#{char*20}"
|
45
|
-
end
|
46
|
-
end
|
1
|
+
require 'stringio'
|
2
|
+
|
3
|
+
# Kernel
|
4
|
+
module Kernel
|
5
|
+
# capture STDOUT
|
6
|
+
#
|
7
|
+
#
|
8
|
+
# capture print
|
9
|
+
# print capture_stdout { print "hoge" } # => hoge
|
10
|
+
#
|
11
|
+
# if block have no STDOUT, capture_stdout returns empty.
|
12
|
+
# print capture_stdout { }.empty? # => true
|
13
|
+
def capture_stdout
|
14
|
+
begin
|
15
|
+
$stdout = StringIO.new
|
16
|
+
yield
|
17
|
+
result = $stdout.string
|
18
|
+
ensure
|
19
|
+
$stdout = STDOUT
|
20
|
+
end
|
21
|
+
result
|
22
|
+
end
|
23
|
+
|
24
|
+
# debug print line for print-debugging
|
25
|
+
#
|
26
|
+
#
|
27
|
+
# debug print default
|
28
|
+
# dp_line __LINE__
|
29
|
+
# # yy = call line no
|
30
|
+
# # => --------------------|filename=|line=yy|--------------------
|
31
|
+
#
|
32
|
+
# debug print with filename
|
33
|
+
# dp_line __LINE__, filename: __FILE__
|
34
|
+
# # xx = filename, yy = call line no
|
35
|
+
# # => --------------------|filename=xx|line=yy|--------------------
|
36
|
+
#
|
37
|
+
# debug print with no filename, specific char
|
38
|
+
# dp_line __LINE__, char: '@'
|
39
|
+
# # xx = filename, yy = call line no
|
40
|
+
# # => @@@@@@@@@@@@@@@@@@@@|filename=|line=yy|@@@@@@@@@@@@@@@@@@@@
|
41
|
+
def dp_line(line, options = { filename: '', char: '-' })
|
42
|
+
filename = options[:filename].nil? ? '' : options[:filename]
|
43
|
+
char = options[:char].nil? ? '-' : options[:char]
|
44
|
+
puts "#{char * 20}|filename=#{filename}|line=#{line}|#{char * 20}"
|
45
|
+
end
|
46
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require
|
3
|
-
require
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'attributes_initializable'
|
4
4
|
|
5
5
|
describe AttributesInitializable do
|
6
6
|
context :attr_accessor_init do
|
@@ -12,7 +12,7 @@ describe AttributesInitializable do
|
|
12
12
|
cases = [
|
13
13
|
{
|
14
14
|
case_no: 1,
|
15
|
-
case_title:
|
15
|
+
case_title: 'not block case',
|
16
16
|
klass: AccessorSample,
|
17
17
|
inputs: {
|
18
18
|
atr1: 'atr1',
|
@@ -26,7 +26,7 @@ describe AttributesInitializable do
|
|
26
26
|
},
|
27
27
|
{
|
28
28
|
case_no: 2,
|
29
|
-
case_title:
|
29
|
+
case_title: 'block case',
|
30
30
|
klass: AccessorSample,
|
31
31
|
inputs: {
|
32
32
|
atr1: 'atr1',
|
@@ -56,7 +56,7 @@ describe AttributesInitializable do
|
|
56
56
|
a.atr2 = c[:inputs][:atr2]
|
57
57
|
end
|
58
58
|
else
|
59
|
-
accessor_sample = c[:klass].new :
|
59
|
+
accessor_sample = c[:klass].new atr1: 'atr1', atr2: 'atr2'
|
60
60
|
end
|
61
61
|
|
62
62
|
# -- then --
|
data/spec/ghostable_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require
|
3
|
-
require
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'ghostable'
|
4
4
|
|
5
5
|
describe Ghostable do
|
6
6
|
context :ghost_method do
|
@@ -8,15 +8,15 @@ describe Ghostable do
|
|
8
8
|
include Ghostable
|
9
9
|
ghost_method /check_range_.*\?$/, :check_range do |method_name, *args, &block|
|
10
10
|
method_name.to_s =~ /(check_range_)(\d+)(_to_)(\d*)/
|
11
|
-
from =
|
12
|
-
to =
|
11
|
+
from = Regexp.last_match[2].to_i
|
12
|
+
to = Regexp.last_match[4].to_i
|
13
13
|
value = args.first
|
14
14
|
(from..to).include? value
|
15
15
|
end
|
16
16
|
|
17
17
|
ghost_method /^contain_.*\?$/, :check_contain do |method_name, *args, &block|
|
18
18
|
method_name.to_s =~ /^(contain_)(.*)(\?)/
|
19
|
-
word =
|
19
|
+
word = Regexp.last_match[2]
|
20
20
|
value = args.first
|
21
21
|
value.include? word
|
22
22
|
end
|
@@ -29,15 +29,15 @@ describe Ghostable do
|
|
29
29
|
cases = [
|
30
30
|
{
|
31
31
|
case_no: 1,
|
32
|
-
case_title:
|
32
|
+
case_title: 'valid case',
|
33
33
|
klass: SampleChecker,
|
34
|
-
methods: [:check_range_3_to_5?, :check_range_3_to_5?
|
35
|
-
values: [3, 6,
|
34
|
+
methods: [:check_range_3_to_5?, :check_range_3_to_5? , :contain_hoge?, :contain_hoge?],
|
35
|
+
values: [3, 6, 'testhogetest', 'testhigetest'],
|
36
36
|
expecteds: [true, false, true, false],
|
37
37
|
},
|
38
38
|
{
|
39
39
|
case_no: 2,
|
40
|
-
case_title:
|
40
|
+
case_title: 'method_missing case',
|
41
41
|
klass: SampleChecker,
|
42
42
|
methods: [:not_match_method],
|
43
43
|
values: [nil],
|
@@ -1,173 +1,173 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
|
6
|
-
describe Kernel do
|
7
|
-
context :print_eval do
|
8
|
-
cases = [
|
9
|
-
{
|
10
|
-
case_no: 1,
|
11
|
-
case_title:
|
12
|
-
code:
|
13
|
-
expected:
|
14
|
-
},
|
15
|
-
{
|
16
|
-
case_no: 2,
|
17
|
-
case_title:
|
18
|
-
code: "\"hoge-\#{message}\"",
|
19
|
-
bind:
|
20
|
-
expected: "\"hoge-\#{message}\" # => \"hoge-msg\"",
|
21
|
-
},
|
22
|
-
]
|
23
|
-
|
24
|
-
cases.each do |c|
|
25
|
-
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
26
|
-
begin
|
27
|
-
case_before c
|
28
|
-
|
29
|
-
# -- given --
|
30
|
-
message = c[:bind] if c[:bind]
|
31
|
-
|
32
|
-
# -- when --
|
33
|
-
actual = capture_stdout { print_eval c[:code], binding }
|
34
|
-
|
35
|
-
# -- then --
|
36
|
-
expect(actual).to eq(c[:expected])
|
37
|
-
ensure
|
38
|
-
case_after c
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def case_before(c)
|
43
|
-
# implement each case before
|
44
|
-
end
|
45
|
-
|
46
|
-
def case_after(c)
|
47
|
-
# implement each case after
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
context :puts_eval do
|
53
|
-
cases = [
|
54
|
-
{
|
55
|
-
case_no: 1,
|
56
|
-
case_title:
|
57
|
-
code:
|
58
|
-
expected: "8/4 # => 2\n",
|
59
|
-
},
|
60
|
-
{
|
61
|
-
case_no: 2,
|
62
|
-
case_title:
|
63
|
-
code: '"hoge-#{message}"',
|
64
|
-
bind:
|
65
|
-
expected: '"hoge-#{message}" # => "hoge-msg"' + "\n",
|
66
|
-
},
|
67
|
-
]
|
68
|
-
|
69
|
-
cases.each do |c|
|
70
|
-
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
71
|
-
begin
|
72
|
-
case_before c
|
73
|
-
|
74
|
-
# -- given --
|
75
|
-
message = c[:bind] if c[:bind]
|
76
|
-
|
77
|
-
# -- when --
|
78
|
-
actual = capture_stdout { puts_eval c[:code], binding }
|
79
|
-
|
80
|
-
# -- then --
|
81
|
-
expect(actual).to eq(c[:expected])
|
82
|
-
ensure
|
83
|
-
case_after c
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def case_before(c)
|
88
|
-
# implement each case before
|
89
|
-
end
|
90
|
-
|
91
|
-
def case_after(c)
|
92
|
-
# implement each case after
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
context :bulk_define_methods do
|
98
|
-
cases = [
|
99
|
-
{
|
100
|
-
case_no: 1,
|
101
|
-
case_title:
|
102
|
-
classes: [NilClass, FalseClass],
|
103
|
-
methods: [:blank?, :present?],
|
104
|
-
contents: [true, false],
|
105
|
-
instances: [nil, false],
|
106
|
-
expecteds: [true, false, true, false],
|
107
|
-
},
|
108
|
-
{
|
109
|
-
case_no: 2,
|
110
|
-
case_title:
|
111
|
-
classes: NilClass,
|
112
|
-
methods:
|
113
|
-
contents:
|
114
|
-
instances: [nil],
|
115
|
-
expecteds: [
|
116
|
-
},
|
117
|
-
{
|
118
|
-
case_no: 3,
|
119
|
-
case_title:
|
120
|
-
classes: :NilClass,
|
121
|
-
methods:
|
122
|
-
contents:
|
123
|
-
expect_error: true,
|
124
|
-
},
|
125
|
-
{
|
126
|
-
case_no: 4,
|
127
|
-
case_title:
|
128
|
-
classes: NilClass,
|
129
|
-
methods: 1,
|
130
|
-
contents:
|
131
|
-
expect_error: true,
|
132
|
-
},
|
133
|
-
]
|
134
|
-
|
135
|
-
cases.each do |c|
|
136
|
-
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
137
|
-
begin
|
138
|
-
case_before c
|
139
|
-
|
140
|
-
# -- given --
|
141
|
-
# nothing
|
142
|
-
|
143
|
-
# -- when --
|
144
|
-
if c[:expect_error]
|
145
|
-
lambda { bulk_define_methods c[:classes], c[:methods], c[:contents] }.should raise_error(StandardError)
|
146
|
-
next
|
147
|
-
end
|
148
|
-
bulk_define_methods c[:classes], c[:methods], c[:contents]
|
149
|
-
|
150
|
-
# -- then --
|
151
|
-
method_list = c[:methods].is_a?(Array) ? c[:methods] : [c[:methods]]
|
152
|
-
cnt = 0
|
153
|
-
c[:instances].each do |instance|
|
154
|
-
method_list.each do |method_name|
|
155
|
-
expect(instance.method(method_name).call).to eq(c[:expecteds][cnt])
|
156
|
-
cnt += 1
|
157
|
-
end
|
158
|
-
end
|
159
|
-
ensure
|
160
|
-
case_after c
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|
164
|
-
def case_before(c)
|
165
|
-
# implement each case before
|
166
|
-
end
|
167
|
-
|
168
|
-
def case_after(c)
|
169
|
-
# implement each case after
|
170
|
-
end
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'open_classes/kernel'
|
4
|
+
require 'test_toolbox'
|
5
|
+
|
6
|
+
describe Kernel do
|
7
|
+
context :print_eval do
|
8
|
+
cases = [
|
9
|
+
{
|
10
|
+
case_no: 1,
|
11
|
+
case_title: 'no bind case',
|
12
|
+
code: '8/4',
|
13
|
+
expected: '8/4 # => 2',
|
14
|
+
},
|
15
|
+
{
|
16
|
+
case_no: 2,
|
17
|
+
case_title: 'with bind case',
|
18
|
+
code: "\"hoge-\#{message}\"",
|
19
|
+
bind: 'msg',
|
20
|
+
expected: "\"hoge-\#{message}\" # => \"hoge-msg\"",
|
21
|
+
},
|
22
|
+
]
|
23
|
+
|
24
|
+
cases.each do |c|
|
25
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
26
|
+
begin
|
27
|
+
case_before c
|
28
|
+
|
29
|
+
# -- given --
|
30
|
+
message = c[:bind] if c[:bind]
|
31
|
+
|
32
|
+
# -- when --
|
33
|
+
actual = capture_stdout { print_eval c[:code], binding }
|
34
|
+
|
35
|
+
# -- then --
|
36
|
+
expect(actual).to eq(c[:expected])
|
37
|
+
ensure
|
38
|
+
case_after c
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def case_before(c)
|
43
|
+
# implement each case before
|
44
|
+
end
|
45
|
+
|
46
|
+
def case_after(c)
|
47
|
+
# implement each case after
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context :puts_eval do
|
53
|
+
cases = [
|
54
|
+
{
|
55
|
+
case_no: 1,
|
56
|
+
case_title: 'no bind case',
|
57
|
+
code: '8/4',
|
58
|
+
expected: "8/4 # => 2\n",
|
59
|
+
},
|
60
|
+
{
|
61
|
+
case_no: 2,
|
62
|
+
case_title: 'with bind case',
|
63
|
+
code: '"hoge-#{message}"',
|
64
|
+
bind: 'msg',
|
65
|
+
expected: '"hoge-#{message}" # => "hoge-msg"' + "\n",
|
66
|
+
},
|
67
|
+
]
|
68
|
+
|
69
|
+
cases.each do |c|
|
70
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
71
|
+
begin
|
72
|
+
case_before c
|
73
|
+
|
74
|
+
# -- given --
|
75
|
+
message = c[:bind] if c[:bind]
|
76
|
+
|
77
|
+
# -- when --
|
78
|
+
actual = capture_stdout { puts_eval c[:code], binding }
|
79
|
+
|
80
|
+
# -- then --
|
81
|
+
expect(actual).to eq(c[:expected])
|
82
|
+
ensure
|
83
|
+
case_after c
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def case_before(c)
|
88
|
+
# implement each case before
|
89
|
+
end
|
90
|
+
|
91
|
+
def case_after(c)
|
92
|
+
# implement each case after
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context :bulk_define_methods do
|
98
|
+
cases = [
|
99
|
+
{
|
100
|
+
case_no: 1,
|
101
|
+
case_title: 'all Array case',
|
102
|
+
classes: [NilClass, FalseClass],
|
103
|
+
methods: [:blank?, :present?],
|
104
|
+
contents: [true, false],
|
105
|
+
instances: [nil, false],
|
106
|
+
expecteds: [true, false, true, false],
|
107
|
+
},
|
108
|
+
{
|
109
|
+
case_no: 2,
|
110
|
+
case_title: 'Class, String, String case',
|
111
|
+
classes: NilClass,
|
112
|
+
methods: 'hoge',
|
113
|
+
contents: 'ret hoge',
|
114
|
+
instances: [nil],
|
115
|
+
expecteds: ['ret hoge'],
|
116
|
+
},
|
117
|
+
{
|
118
|
+
case_no: 3,
|
119
|
+
case_title: 'invalid classes type case',
|
120
|
+
classes: :NilClass,
|
121
|
+
methods: 'hoge',
|
122
|
+
contents: 'ret hoge',
|
123
|
+
expect_error: true,
|
124
|
+
},
|
125
|
+
{
|
126
|
+
case_no: 4,
|
127
|
+
case_title: 'invalid methods type case',
|
128
|
+
classes: NilClass,
|
129
|
+
methods: 1,
|
130
|
+
contents: 'ret hoge',
|
131
|
+
expect_error: true,
|
132
|
+
},
|
133
|
+
]
|
134
|
+
|
135
|
+
cases.each do |c|
|
136
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
137
|
+
begin
|
138
|
+
case_before c
|
139
|
+
|
140
|
+
# -- given --
|
141
|
+
# nothing
|
142
|
+
|
143
|
+
# -- when --
|
144
|
+
if c[:expect_error]
|
145
|
+
lambda { bulk_define_methods c[:classes], c[:methods], c[:contents] }.should raise_error(StandardError)
|
146
|
+
next
|
147
|
+
end
|
148
|
+
bulk_define_methods c[:classes], c[:methods], c[:contents]
|
149
|
+
|
150
|
+
# -- then --
|
151
|
+
method_list = c[:methods].is_a?(Array) ? c[:methods] : [c[:methods]]
|
152
|
+
cnt = 0
|
153
|
+
c[:instances].each do |instance|
|
154
|
+
method_list.each do |method_name|
|
155
|
+
expect(instance.method(method_name).call).to eq(c[:expecteds][cnt])
|
156
|
+
cnt += 1
|
157
|
+
end
|
158
|
+
end
|
159
|
+
ensure
|
160
|
+
case_after c
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def case_before(c)
|
165
|
+
# implement each case before
|
166
|
+
end
|
167
|
+
|
168
|
+
def case_after(c)
|
169
|
+
# implement each case after
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
data/spec/templatable_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require
|
3
|
-
require
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'templatable'
|
4
4
|
|
5
5
|
describe Templatable do
|
6
6
|
context :result do
|
@@ -28,7 +28,7 @@ line2:hige-sample
|
|
28
28
|
cases = [
|
29
29
|
{
|
30
30
|
case_no: 1,
|
31
|
-
case_title:
|
31
|
+
case_title: 'valid case',
|
32
32
|
klass: TemplateUser,
|
33
33
|
input: 'sample',
|
34
34
|
expected: RESULT_CASE1,
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'template_methodable'
|
4
|
+
|
5
|
+
describe TemplateMethodable do
|
6
|
+
|
7
|
+
context :must_impl do
|
8
|
+
class BaseDeveloper
|
9
|
+
include TemplateMethodable
|
10
|
+
must_impl :easy_coding, :difficult_coding, :normal_coding
|
11
|
+
|
12
|
+
module DIFFICILTY
|
13
|
+
EASY = 1
|
14
|
+
NORMAL = 2
|
15
|
+
DIFFICILT = 3
|
16
|
+
end
|
17
|
+
|
18
|
+
def coding(difficulty)
|
19
|
+
ret = []
|
20
|
+
ret << 'start coding'
|
21
|
+
case difficulty
|
22
|
+
when DIFFICILTY::EASY
|
23
|
+
ret << easy_coding('hoge', 'hige')
|
24
|
+
when DIFFICILTY::NORMAL
|
25
|
+
ret << normal_coding('hoge', 'hige')
|
26
|
+
when DIFFICILTY::DIFFICILT
|
27
|
+
ret << difficult_coding('hoge', 'hige')
|
28
|
+
else
|
29
|
+
fail 'error'
|
30
|
+
end
|
31
|
+
ret << 'finish coding'
|
32
|
+
ret.join("\n")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class StarDeveloper < BaseDeveloper
|
37
|
+
def easy_coding(hoge, hige)
|
38
|
+
'complete 1 minutes'
|
39
|
+
end
|
40
|
+
def normal_coding(hoge, hige)
|
41
|
+
'complete 10 minutes'
|
42
|
+
end
|
43
|
+
def difficult_coding(hoge, hige)
|
44
|
+
'complete 59 minutes'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class NotImplDeveloper < BaseDeveloper
|
49
|
+
end
|
50
|
+
|
51
|
+
cases = [
|
52
|
+
{
|
53
|
+
case_no: 1,
|
54
|
+
case_title: 'all imple easy case',
|
55
|
+
klass: StarDeveloper,
|
56
|
+
method_name: :coding,
|
57
|
+
difficulty: BaseDeveloper::DIFFICILTY::EASY,
|
58
|
+
expected: <<-EOS
|
59
|
+
start coding
|
60
|
+
complete 1 minutes
|
61
|
+
finish coding
|
62
|
+
EOS
|
63
|
+
},
|
64
|
+
{
|
65
|
+
case_no: 2,
|
66
|
+
case_title: 'not imple all case',
|
67
|
+
klass: NotImplDeveloper,
|
68
|
+
method_name: :coding,
|
69
|
+
difficulty: BaseDeveloper::DIFFICILTY::NORMAL,
|
70
|
+
expect_error: true,
|
71
|
+
error_class: NotImplementedError
|
72
|
+
},
|
73
|
+
]
|
74
|
+
|
75
|
+
cases.each do |c|
|
76
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
77
|
+
begin
|
78
|
+
case_before c
|
79
|
+
|
80
|
+
# -- given --
|
81
|
+
# nothing
|
82
|
+
|
83
|
+
# -- when --
|
84
|
+
exec = lambda { c[:klass].new.method(c[:method_name]).call c[:difficulty] }
|
85
|
+
if c[:expect_error]
|
86
|
+
exec.should raise_error(NotImplementedError)
|
87
|
+
next
|
88
|
+
end
|
89
|
+
actual = exec.call
|
90
|
+
|
91
|
+
# -- then --
|
92
|
+
expect(actual).to eq(c[:expected].chop)
|
93
|
+
ensure
|
94
|
+
case_after c
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def case_before(c)
|
99
|
+
# implement each case before
|
100
|
+
end
|
101
|
+
|
102
|
+
def case_after(c)
|
103
|
+
# implement each case after
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -1,21 +1,21 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
require
|
3
|
-
require
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'test_toolbox/kernel'
|
4
4
|
|
5
5
|
describe Kernel do
|
6
6
|
context :capture_stdout do
|
7
7
|
cases = [
|
8
8
|
{
|
9
9
|
case_no: 1,
|
10
|
-
case_title:
|
10
|
+
case_title: 'have STDOUT case',
|
11
11
|
code: "print 'hoge'",
|
12
|
-
expected:
|
12
|
+
expected: 'hoge',
|
13
13
|
},
|
14
14
|
{
|
15
15
|
case_no: 2,
|
16
16
|
case_title: "don't have STDOUT case",
|
17
|
-
code:
|
18
|
-
expected:
|
17
|
+
code: '',
|
18
|
+
expected: '',
|
19
19
|
},
|
20
20
|
]
|
21
21
|
|
@@ -28,7 +28,7 @@ describe Kernel do
|
|
28
28
|
# nothing
|
29
29
|
|
30
30
|
# -- when --
|
31
|
-
actual = capture_stdout {eval c[:code]}
|
31
|
+
actual = capture_stdout { eval c[:code] }
|
32
32
|
|
33
33
|
# -- then --
|
34
34
|
expect(actual).to eq(c[:expected])
|
@@ -51,14 +51,14 @@ describe Kernel do
|
|
51
51
|
cases = [
|
52
52
|
{
|
53
53
|
case_no: 1,
|
54
|
-
case_title:
|
54
|
+
case_title: 'default case',
|
55
55
|
have_filename: true,
|
56
56
|
have_char: false,
|
57
57
|
expected: "--------------------|filename=$filename$|line=$line$|--------------------\n",
|
58
58
|
},
|
59
59
|
{
|
60
60
|
case_no: 2,
|
61
|
-
case_title:
|
61
|
+
case_title: 'specific har case',
|
62
62
|
have_filename: false,
|
63
63
|
have_char: true,
|
64
64
|
char: '@',
|
@@ -66,7 +66,7 @@ describe Kernel do
|
|
66
66
|
},
|
67
67
|
{
|
68
68
|
case_no: 3,
|
69
|
-
case_title:
|
69
|
+
case_title: 'no options case',
|
70
70
|
have_filename: false,
|
71
71
|
have_char: false,
|
72
72
|
expected: "--------------------|filename=$filename$|line=$line$|--------------------\n",
|
@@ -86,16 +86,16 @@ describe Kernel do
|
|
86
86
|
# -- when --
|
87
87
|
line = nil
|
88
88
|
if !c[:have_filename] && !c[:have_char]
|
89
|
-
actual = capture_stdout{ line = __LINE__ if c[:have_line];dp_line line }
|
89
|
+
actual = capture_stdout { line = __LINE__ if c[:have_line]; dp_line line }
|
90
90
|
else
|
91
|
-
actual = capture_stdout{ line = __LINE__ if c[:have_line];dp_line line, options }
|
91
|
+
actual = capture_stdout { line = __LINE__ if c[:have_line]; dp_line line, options }
|
92
92
|
end
|
93
93
|
|
94
94
|
# -- then --
|
95
95
|
expected = c[:expected]
|
96
|
-
file_gsub = c[:have_filename] ? __FILE__.to_s :
|
97
|
-
expected = expected.gsub(
|
98
|
-
expected = expected.gsub(
|
96
|
+
file_gsub = c[:have_filename] ? __FILE__.to_s : ''
|
97
|
+
expected = expected.gsub('$filename$', file_gsub)
|
98
|
+
expected = expected.gsub('$line$', line.to_s)
|
99
99
|
expect(actual).to eq(expected)
|
100
100
|
ensure
|
101
101
|
case_after c
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tbpgr_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-01-
|
12
|
+
date: 2014-01-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &112152 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 4.0.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *112152
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &109104 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '1.3'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *109104
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &6863256 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *6863256
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &6865392 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: 2.14.1
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *6865392
|
58
58
|
description: Utilities
|
59
59
|
email:
|
60
60
|
- tbpgr@tbpgr.jp
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- lib/tbpgr_utils.rb
|
78
78
|
- lib/tbpgr_utils/version.rb
|
79
79
|
- lib/templatable.rb
|
80
|
+
- lib/template_methodable.rb
|
80
81
|
- lib/test_toolbox.rb
|
81
82
|
- lib/test_toolbox/kernel.rb
|
82
83
|
- spec/attributes_initializable_spec.rb
|
@@ -87,6 +88,7 @@ files:
|
|
87
88
|
- spec/open_classes/string_spec.rb
|
88
89
|
- spec/spec_helper.rb
|
89
90
|
- spec/templatable_spec.rb
|
91
|
+
- spec/template_methodable_spec.rb
|
90
92
|
- spec/test_toolbox/kernel_spec.rb
|
91
93
|
- tbpgr_utils.gemspec
|
92
94
|
homepage: https://github.com/tbpgr/tbpgr_utils
|
@@ -123,5 +125,6 @@ test_files:
|
|
123
125
|
- spec/open_classes/string_spec.rb
|
124
126
|
- spec/spec_helper.rb
|
125
127
|
- spec/templatable_spec.rb
|
128
|
+
- spec/template_methodable_spec.rb
|
126
129
|
- spec/test_toolbox/kernel_spec.rb
|
127
130
|
has_rdoc:
|