kinda-core 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/__all__.rb ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/ruby19
2
+ # encoding: utf-8
3
+
4
+ ruby_files = Dir.glob(File.join(File.dirname(__FILE__), '*_test.rb'))
5
+ ruby_files.each { |file| require file }
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/ruby19
2
+ # encoding: utf-8
3
+
4
+ require 'test/unit'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')).uniq!
7
+ require 'kinda-core'
8
+
9
+ class AccessorTest < Test::Unit::TestCase
10
+ def setup
11
+ @human_class = Class.new do
12
+ include Kinda::Core
13
+ end
14
+ @human = @human_class.new
15
+ end
16
+
17
+ def test_attribute_defined_at_class_level
18
+ @human_class.class_eval do
19
+ attr_accessor :name
20
+ end
21
+ assert_nil @human.name
22
+ @human.name = 'Dave'
23
+ assert_equal 'Dave', @human.name
24
+ @human.name 'Fred'
25
+ assert_equal 'Fred', @human.name
26
+ end
27
+
28
+ def test_attribute_defined_at_instance_level
29
+ @human.attr_accessor :name
30
+ assert_nil @human.name
31
+ @human.name = 'Dave'
32
+ assert_equal 'Dave', @human.name
33
+ @human.name 'Fred'
34
+ assert_equal 'Fred', @human.name
35
+ @other_human = @human_class.new
36
+ assert_raise NoMethodError do
37
+ @other_human.name
38
+ end
39
+ end
40
+
41
+ def test_attribute_with_block_defined_at_class_level
42
+ @human_class.class_eval do
43
+ attr_reader :name do |capitalize=false|
44
+ capitalize ? @the_name.capitalize : @the_name
45
+ end
46
+ attr_writer :name do |value|
47
+ @the_name = value
48
+ end
49
+ end
50
+ assert_equal nil, @human.name
51
+ @human.name = 'Dave'
52
+ assert_equal 'Dave', @human.name
53
+ @human.name = 'fred'
54
+ assert_equal 'Fred', @human.name(true)
55
+ end
56
+
57
+ def test_attribute_with_filter_defined_at_class_level
58
+ @human_class.class_eval do
59
+ attr_accessor :nickname
60
+
61
+ attr_reader_with_filter :name do |value|
62
+ value || nickname || 'Mike'
63
+ end
64
+ attr_writer_with_filter :name do |value|
65
+ value.capitalize
66
+ end
67
+ end
68
+ assert_equal 'Mike', @human.name
69
+ @human.nickname = 'Mikey'
70
+ assert_equal 'Mikey', @human.name
71
+ @human.name = 'dave'
72
+ assert_equal 'Dave', @human.name
73
+ @human.name 'FRED'
74
+ assert_equal 'Fred', @human.name
75
+ end
76
+
77
+ def test_attribute_redirection
78
+ @human.attr_accessor :name => :@the_name
79
+ assert_nil @human.name
80
+ @human.name = 'Dave'
81
+ assert_equal 'Dave', @human.name
82
+ assert_equal 'Dave', @human.instance_variable_get(:@the_name)
83
+ @human.name = 'Fred'
84
+ assert_equal 'Fred', @human.name
85
+ @human.attr_accessor :last_name => :name
86
+ assert_equal 'Fred', @human.last_name
87
+ @human.last_name = 'Mike'
88
+ assert_equal 'Mike', @human.last_name
89
+ assert_equal 'Mike', @human.name
90
+ end
91
+
92
+ def test_attribute_delegation
93
+ @child = @human_class.new
94
+ @father = @human_class.new
95
+ @child.attr_accessor :father
96
+ @child.father = @father
97
+ assert_equal @father, @child.father
98
+ @father.attr_accessor :name
99
+ @father.name = 'Dave'
100
+ @child.attr_accessor :name => [:father, :name]
101
+ assert_equal 'Dave', @child.name
102
+ @child.name = 'Fred'
103
+ assert_equal 'Fred', @child.name
104
+ assert_equal 'Fred', @father.name
105
+ end
106
+
107
+ def test_attribute_double_delegation
108
+ @child = @human_class.new
109
+ @mother = @human_class.new
110
+ @husband = @human_class.new
111
+ @child.attr_accessor :mother
112
+ @child.mother = @mother
113
+ @mother.attr_accessor :husband
114
+ @mother.husband = @husband
115
+ assert_equal @husband, @child.mother.husband
116
+ @husband.attr_accessor :name
117
+ @husband.name = 'Dave'
118
+ @child.attr_accessor :name => [:mother, :husband, :name]
119
+ assert_equal 'Dave', @child.name
120
+ @child.name = 'Fred'
121
+ assert_equal 'Fred', @child.name
122
+ assert_equal 'Fred', @husband.name
123
+ end
124
+
125
+ def test_attribute_with_default
126
+ @human.attr_accessor_with_default :name, 'Bart'
127
+ assert_equal 'Bart', @human.name
128
+ @human.name = 'Dave'
129
+ assert_equal 'Dave', @human.name
130
+ end
131
+ end
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/ruby19
2
+ # encoding: utf-8
3
+
4
+ require 'test/unit'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')).uniq!
7
+ require 'kinda-core'
8
+
9
+ class ForwardableTest < Test::Unit::TestCase
10
+ def setup
11
+ @human_class = Class.new do
12
+ include Kinda::Core
13
+ end
14
+ @human = @human_class.new
15
+ end
16
+
17
+ def test_delegate_attribute
18
+ @child = @human_class.new
19
+ @father = @human_class.new
20
+ @child.attr_accessor :father
21
+ @child.father = @father
22
+ assert_equal @father, @child.father
23
+ @father.attr_accessor :name
24
+ @father.name = 'Dave'
25
+ @child.delegate_attr :name => :father
26
+ assert_equal 'Dave', @child.name
27
+ @child.name = 'Fred'
28
+ assert_equal 'Fred', @child.name
29
+ assert_equal 'Fred', @father.name
30
+ end
31
+
32
+ def test_delegate_attribute_with_filter
33
+ @child = @human_class.new
34
+ @father = @human_class.new
35
+ @child.attr_accessor :father
36
+ @child.father = @father
37
+ assert_equal @father, @child.father
38
+ @father.attr_accessor :name
39
+ @father.name = 'Dave'
40
+ @child.delegate_attr_reader_with_filter :name => :father do |value|
41
+ value + ' junior'
42
+ end
43
+ assert_equal 'Dave junior', @child.name
44
+ @child.delegate_attr_writer_with_filter :name => :father do |value|
45
+ value.capitalize
46
+ end
47
+ @child.name = 'fred'
48
+ assert_equal 'Fred junior', @child.name
49
+ assert_equal 'Fred', @father.name
50
+ end
51
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kinda-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Manuel Vila
@@ -45,7 +45,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
45
45
  requirements:
46
46
  - - ">="
47
47
  - !ruby/object:Gem::Version
48
- version: "0"
48
+ version: 1.9.1
49
49
  version:
50
50
  required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  requirements:
@@ -60,5 +60,7 @@ rubygems_version: 1.2.0
60
60
  signing_key:
61
61
  specification_version: 2
62
62
  summary: Basic helpers used by other kinda projects
63
- test_files: []
64
-
63
+ test_files:
64
+ - test/__all__.rb
65
+ - test/accessor_test.rb
66
+ - test/forwardable_test.rb