striuct 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +35 -34
- data/.travis.yml +6 -6
- data/Gemfile +12 -12
- data/History.rdoc +279 -273
- data/LICENSE +21 -21
- data/Manifest.txt +91 -90
- data/README.ja.old.rdoc +297 -297
- data/README.md +128 -128
- data/Rakefile +11 -11
- data/benchmark/basics.rb +56 -56
- data/example/README.rb +44 -44
- data/example/example.old.rdoc +187 -187
- data/example/example1.rb +233 -233
- data/example/example2.rb +22 -22
- data/example/see_trace.rb +32 -32
- data/lib/striuct/classmethods/README.md +5 -5
- data/lib/striuct/classmethods/adjustment.rb +32 -32
- data/lib/striuct/classmethods/constructor.rb +63 -63
- data/lib/striuct/classmethods/default.rb +27 -27
- data/lib/striuct/classmethods/enum.rb +48 -48
- data/lib/striuct/classmethods/inner.rb +91 -91
- data/lib/striuct/classmethods/length.rb +14 -14
- data/lib/striuct/classmethods/macro.rb +144 -144
- data/lib/striuct/classmethods/named.rb +113 -113
- data/lib/striuct/classmethods/object.rb +54 -54
- data/lib/striuct/classmethods/prevent_conflicts.rb +89 -89
- data/lib/striuct/classmethods/requiremnets.rb +11 -11
- data/lib/striuct/classmethods/to_struct.rb +23 -25
- data/lib/striuct/classmethods/validation.rb +55 -55
- data/lib/striuct/classmethods.rb +1 -1
- data/lib/striuct/instancemethods/README.md +5 -5
- data/lib/striuct/instancemethods/assign.rb +30 -30
- data/lib/striuct/instancemethods/cast.rb +34 -34
- data/lib/striuct/instancemethods/compare.rb +30 -30
- data/lib/striuct/instancemethods/default.rb +13 -13
- data/lib/striuct/instancemethods/delegate_class_methods.rb +28 -28
- data/lib/striuct/instancemethods/enum.rb +103 -103
- data/lib/striuct/instancemethods/hashy.rb +121 -121
- data/lib/striuct/instancemethods/inner.rb +60 -60
- data/lib/striuct/instancemethods/keyvalidatable.rb +14 -14
- data/lib/striuct/instancemethods/lock.rb +61 -61
- data/lib/striuct/instancemethods/object.rb +52 -52
- data/lib/striuct/instancemethods/requirements.rb +14 -14
- data/lib/striuct/instancemethods/safety.rb +11 -11
- data/lib/striuct/instancemethods/subscript.rb +55 -55
- data/lib/striuct/instancemethods/validation.rb +29 -25
- data/lib/striuct/instancemethods/values.rb +57 -57
- data/lib/striuct/instancemethods.rb +1 -1
- data/lib/striuct/requirements.rb +5 -5
- data/lib/striuct/singleton_class.rb +66 -66
- data/lib/striuct/specificcontainer.rb +19 -19
- data/lib/striuct/structs.rb +7 -7
- data/lib/striuct/version.rb +5 -5
- data/lib/striuct.rb +13 -13
- data/striuct.gemspec +24 -24
- data/test/helper.rb +5 -5
- data/test/test_striuct-singleton_class-define.rb +19 -19
- data/test/test_striuct-subclass-class-cloning.rb +20 -20
- data/test/test_striuct-subclass-class-close.rb +36 -36
- data/test/test_striuct-subclass-class-constructor.rb +82 -82
- data/test/test_striuct-subclass-class-freeze.rb +36 -36
- data/test/test_striuct-subclass-class-inheritable.rb +57 -57
- data/test/test_striuct-subclass-class-macro.rb +13 -13
- data/test/test_striuct-subclass-class-safety_naming.rb +72 -72
- data/test/test_striuct-subclass-class-validation.rb +26 -26
- data/test/test_striuct-subclass-feature-alias_member.rb +53 -53
- data/test/test_striuct-subclass-feature-to_struct.rb +61 -25
- data/test/test_striuct-subclass-feature-validation_util.rb +57 -0
- data/test/test_striuct-subclass-instance-accessor.rb +137 -137
- data/test/test_striuct-subclass-instance-adjuster.rb +94 -94
- data/test/test_striuct-subclass-instance-assign.rb +30 -30
- data/test/test_striuct-subclass-instance-basic.rb +32 -32
- data/test/test_striuct-subclass-instance-cloning.rb +22 -22
- data/test/test_striuct-subclass-instance-compare.rb +51 -51
- data/test/test_striuct-subclass-instance-default_value.rb +128 -128
- data/test/test_striuct-subclass-instance-enum.rb +92 -92
- data/test/test_striuct-subclass-instance-freeze.rb +19 -19
- data/test/test_striuct-subclass-instance-hashlike.rb +153 -153
- data/test/test_striuct-subclass-instance-keyvalidatable.rb +24 -24
- data/test/test_striuct-subclass-instance-lock.rb +39 -39
- data/test/test_striuct-subclass-instance-to_s_family.rb +25 -25
- data/test/test_striuct-subclass-instance-validation_functional_condition.rb +50 -50
- data/test/test_striuct-subclass-instance-validation_inference.rb +50 -50
- data/test/test_striuct-subclass-instance-validation_specific_conditions.rb +247 -247
- data/test/test_striuct-subclass-instance-validation_with_getter.rb +33 -33
- data/test/test_striuct-subclass-instance_names.rb +18 -18
- data/test/test_striuct-version.rb +10 -10
- metadata +4 -2
@@ -1,66 +1,66 @@
|
|
1
|
-
require_relative 'instancemethods'
|
2
|
-
|
3
|
-
class Striuct
|
4
|
-
|
5
|
-
class << self
|
6
|
-
|
7
|
-
# @group Constructor
|
8
|
-
|
9
|
-
alias_method :new_instance, :new
|
10
|
-
private :new_instance
|
11
|
-
|
12
|
-
# @param [Symbol, String] autonyms
|
13
|
-
# @return [Class]
|
14
|
-
def new(*autonyms, &block)
|
15
|
-
# warning for Ruby's Struct.new user
|
16
|
-
first = autonyms.first
|
17
|
-
if first.instance_of?(String) and /\A[A-Z]/ =~ first
|
18
|
-
warn "no define constant #{first}"
|
19
|
-
end
|
20
|
-
|
21
|
-
Class.new self do
|
22
|
-
autonyms.each do |autonym|
|
23
|
-
add_member autonym
|
24
|
-
end
|
25
|
-
|
26
|
-
class_eval(&block) if block_given?
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# @yieldreturn [Class] (see Striuct.new) - reject floating class
|
31
|
-
# @return [void]
|
32
|
-
def define(&block)
|
33
|
-
raise ArgumentError, 'must with block' unless block_given?
|
34
|
-
|
35
|
-
new(&block).tap do |subclass|
|
36
|
-
subclass.instance_eval do
|
37
|
-
raise 'not yet finished' if autonyms.empty?
|
38
|
-
close
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
# @groupend
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
alias_method :original_inherited, :inherited
|
48
|
-
|
49
|
-
def inherited(subclass)
|
50
|
-
subclass.class_eval do
|
51
|
-
original_inherited subclass
|
52
|
-
|
53
|
-
extend ClassMethods
|
54
|
-
include Enumerable
|
55
|
-
include InstanceMethods
|
56
|
-
|
57
|
-
@autonyms = []
|
58
|
-
@conditions, @adjusters, @defaults, @inferences, @aliases, @setter_validations, @getter_validations = \
|
59
|
-
{}, {}, {}, {}, {}, {}, {}
|
60
|
-
@protect_level = :prevent
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
65
|
-
|
66
|
-
end
|
1
|
+
require_relative 'instancemethods'
|
2
|
+
|
3
|
+
class Striuct
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
# @group Constructor
|
8
|
+
|
9
|
+
alias_method :new_instance, :new
|
10
|
+
private :new_instance
|
11
|
+
|
12
|
+
# @param [Symbol, String] autonyms
|
13
|
+
# @return [Class]
|
14
|
+
def new(*autonyms, &block)
|
15
|
+
# warning for Ruby's Struct.new user
|
16
|
+
first = autonyms.first
|
17
|
+
if first.instance_of?(String) and /\A[A-Z]/ =~ first
|
18
|
+
warn "no define constant first-arg(#{first}), the Struct behavior is not supported in Striuct"
|
19
|
+
end
|
20
|
+
|
21
|
+
Class.new self do
|
22
|
+
autonyms.each do |autonym|
|
23
|
+
add_member autonym
|
24
|
+
end
|
25
|
+
|
26
|
+
class_eval(&block) if block_given?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# @yieldreturn [Class] (see Striuct.new) - reject floating class
|
31
|
+
# @return [void]
|
32
|
+
def define(&block)
|
33
|
+
raise ArgumentError, 'must with block' unless block_given?
|
34
|
+
|
35
|
+
new(&block).tap do |subclass|
|
36
|
+
subclass.instance_eval do
|
37
|
+
raise 'not yet finished' if autonyms.empty?
|
38
|
+
close
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# @groupend
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
alias_method :original_inherited, :inherited
|
48
|
+
|
49
|
+
def inherited(subclass)
|
50
|
+
subclass.class_eval do
|
51
|
+
original_inherited subclass
|
52
|
+
|
53
|
+
extend ClassMethods
|
54
|
+
include Enumerable
|
55
|
+
include InstanceMethods
|
56
|
+
|
57
|
+
@autonyms = []
|
58
|
+
@conditions, @adjusters, @defaults, @inferences, @aliases, @setter_validations, @getter_validations = \
|
59
|
+
{}, {}, {}, {}, {}, {}, {}
|
60
|
+
@protect_level = :prevent
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -1,19 +1,19 @@
|
|
1
|
-
class Striuct
|
2
|
-
|
3
|
-
class SpecificContainer
|
4
|
-
|
5
|
-
attr_reader :value
|
6
|
-
|
7
|
-
def initialize(value)
|
8
|
-
@value = value
|
9
|
-
end
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
class DefaultProcHolder < SpecificContainer; end
|
14
|
-
|
15
|
-
if respond_to? :private_constant
|
16
|
-
private_constant :SpecificContainer, :DefaultProcHolder
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
1
|
+
class Striuct
|
2
|
+
|
3
|
+
class SpecificContainer
|
4
|
+
|
5
|
+
attr_reader :value
|
6
|
+
|
7
|
+
def initialize(value)
|
8
|
+
@value = value
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
class DefaultProcHolder < SpecificContainer; end
|
14
|
+
|
15
|
+
if respond_to? :private_constant
|
16
|
+
private_constant :SpecificContainer, :DefaultProcHolder
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/lib/striuct/structs.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
class Striuct
|
2
|
-
|
3
|
-
# namespace for .to_struct_class, #to_struct
|
4
|
-
module Structs
|
5
|
-
end
|
6
|
-
|
7
|
-
end
|
1
|
+
class Striuct
|
2
|
+
|
3
|
+
# namespace for .to_struct_class, #to_struct
|
4
|
+
module Structs
|
5
|
+
end
|
6
|
+
|
7
|
+
end
|
data/lib/striuct/version.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
class Striuct
|
2
|
-
|
3
|
-
VERSION = '0.3.
|
4
|
-
|
5
|
-
end
|
1
|
+
class Striuct
|
2
|
+
|
3
|
+
VERSION = '0.3.4'.freeze
|
4
|
+
|
5
|
+
end
|
data/lib/striuct.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
# striuct - Provides a Struct++ class.
|
2
|
-
# Copyright (c) 2011 Kenichi Kamiya
|
3
|
-
|
4
|
-
require 'validation'
|
5
|
-
|
6
|
-
# @abstract
|
7
|
-
class Striuct
|
8
|
-
|
9
|
-
include Validation
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
require_relative 'striuct/requirements'
|
1
|
+
# striuct - Provides a Struct++ class.
|
2
|
+
# Copyright (c) 2011 Kenichi Kamiya
|
3
|
+
|
4
|
+
require 'validation'
|
5
|
+
|
6
|
+
# @abstract
|
7
|
+
class Striuct
|
8
|
+
|
9
|
+
include Validation
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
require_relative 'striuct/requirements'
|
data/striuct.gemspec
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
# I don't know why dose occur errors below.
|
2
|
-
# require_relative 'lib/striuct/version'
|
3
|
-
require File.expand_path('../lib/striuct/version', __FILE__)
|
4
|
-
|
5
|
-
Gem::Specification.new do |gem|
|
6
|
-
gem.authors = ['Kenichi Kamiya']
|
7
|
-
gem.email = ['kachick1+ruby@gmail.com']
|
8
|
-
gem.summary = %q{Struct++}
|
9
|
-
gem.description = %q{validatable, inheritable, And more Struct++ features :)}
|
10
|
-
gem.homepage = 'https://github.com/kachick/striuct'
|
11
|
-
|
12
|
-
gem.files = `git ls-files`.split($\)
|
13
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
-
gem.name = 'striuct'
|
16
|
-
gem.require_paths = ['lib']
|
17
|
-
gem.version = Striuct::VERSION.dup # dup for https://github.com/rubygems/rubygems/commit/48f1d869510dcd325d6566df7d0147a086905380#-P0
|
18
|
-
|
19
|
-
gem.required_ruby_version = '>= 1.9.2'
|
20
|
-
gem.add_runtime_dependency 'validation', '~> 0.0.3'
|
21
|
-
gem.add_runtime_dependency 'keyvalidatable', '~> 0.0.3'
|
22
|
-
gem.add_development_dependency 'yard', '>= 0.8.2.1'
|
23
|
-
end
|
24
|
-
|
1
|
+
# I don't know why dose occur errors below.
|
2
|
+
# require_relative 'lib/striuct/version'
|
3
|
+
require File.expand_path('../lib/striuct/version', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ['Kenichi Kamiya']
|
7
|
+
gem.email = ['kachick1+ruby@gmail.com']
|
8
|
+
gem.summary = %q{Struct++}
|
9
|
+
gem.description = %q{validatable, inheritable, And more Struct++ features :)}
|
10
|
+
gem.homepage = 'https://github.com/kachick/striuct'
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = 'striuct'
|
16
|
+
gem.require_paths = ['lib']
|
17
|
+
gem.version = Striuct::VERSION.dup # dup for https://github.com/rubygems/rubygems/commit/48f1d869510dcd325d6566df7d0147a086905380#-P0
|
18
|
+
|
19
|
+
gem.required_ruby_version = '>= 1.9.2'
|
20
|
+
gem.add_runtime_dependency 'validation', '~> 0.0.3'
|
21
|
+
gem.add_runtime_dependency 'keyvalidatable', '~> 0.0.3'
|
22
|
+
gem.add_development_dependency 'yard', '>= 0.8.2.1'
|
23
|
+
end
|
24
|
+
|
data/test/helper.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
|
3
|
-
$VERBOSE = true
|
4
|
-
|
5
|
-
require_relative '../lib/striuct'
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
$VERBOSE = true
|
4
|
+
|
5
|
+
require_relative '../lib/striuct'
|
@@ -1,19 +1,19 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Define < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def test_define
|
6
|
-
assert_raises RuntimeError do
|
7
|
-
Striuct.define do
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
klass = Striuct.define do
|
12
|
-
member :foo
|
13
|
-
end
|
14
|
-
|
15
|
-
assert_equal true, klass.closed?
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class Test_Striuct_Define < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_define
|
6
|
+
assert_raises RuntimeError do
|
7
|
+
Striuct.define do
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
klass = Striuct.define do
|
12
|
+
member :foo
|
13
|
+
end
|
14
|
+
|
15
|
+
assert_equal true, klass.closed?
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
@@ -1,21 +1,21 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Class_Cloning < Test::Unit::TestCase
|
4
|
-
|
5
|
-
class Sth < Striuct.new
|
6
|
-
member :sth
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_dup
|
10
|
-
klass = Sth.dup
|
11
|
-
Sth.__send__ :member, :dummy1
|
12
|
-
assert_equal false, klass.member?(:dummy1)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_clone
|
16
|
-
klass = Sth.clone
|
17
|
-
Sth.__send__ :member, :dummy2
|
18
|
-
assert_equal false, klass.member?(:dummy2)
|
19
|
-
end
|
20
|
-
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class Test_Striuct_Subclass_Class_Cloning < Test::Unit::TestCase
|
4
|
+
|
5
|
+
class Sth < Striuct.new
|
6
|
+
member :sth
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_dup
|
10
|
+
klass = Sth.dup
|
11
|
+
Sth.__send__ :member, :dummy1
|
12
|
+
assert_equal false, klass.member?(:dummy1)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_clone
|
16
|
+
klass = Sth.clone
|
17
|
+
Sth.__send__ :member, :dummy2
|
18
|
+
assert_equal false, klass.member?(:dummy2)
|
19
|
+
end
|
20
|
+
|
21
21
|
end
|
@@ -1,36 +1,36 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Class_Close < Test::Unit::TestCase
|
4
|
-
|
5
|
-
Sth = Striuct.new do
|
6
|
-
member :foo
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_class_close
|
10
|
-
sth = Sth.new
|
11
|
-
|
12
|
-
assert_equal true, sth.member?(:foo)
|
13
|
-
|
14
|
-
Sth.class_eval do
|
15
|
-
member :bar
|
16
|
-
end
|
17
|
-
|
18
|
-
assert_equal true, sth.member?(:bar)
|
19
|
-
assert_equal [:foo, :bar], sth.members
|
20
|
-
|
21
|
-
assert_equal false, Sth.closed?
|
22
|
-
|
23
|
-
Sth.__send__ :close
|
24
|
-
|
25
|
-
assert_equal true, Sth.closed?
|
26
|
-
|
27
|
-
assert_raises RuntimeError do
|
28
|
-
Sth.class_eval do
|
29
|
-
member :var2
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
assert_equal false, sth.member?(:var2)
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class Test_Striuct_Subclass_Class_Close < Test::Unit::TestCase
|
4
|
+
|
5
|
+
Sth = Striuct.new do
|
6
|
+
member :foo
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_class_close
|
10
|
+
sth = Sth.new
|
11
|
+
|
12
|
+
assert_equal true, sth.member?(:foo)
|
13
|
+
|
14
|
+
Sth.class_eval do
|
15
|
+
member :bar
|
16
|
+
end
|
17
|
+
|
18
|
+
assert_equal true, sth.member?(:bar)
|
19
|
+
assert_equal [:foo, :bar], sth.members
|
20
|
+
|
21
|
+
assert_equal false, Sth.closed?
|
22
|
+
|
23
|
+
Sth.__send__ :close
|
24
|
+
|
25
|
+
assert_equal true, Sth.closed?
|
26
|
+
|
27
|
+
assert_raises RuntimeError do
|
28
|
+
Sth.class_eval do
|
29
|
+
member :var2
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_equal false, sth.member?(:var2)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -1,82 +1,82 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Class_Constructor < Test::Unit::TestCase
|
4
|
-
|
5
|
-
class User < Striuct.new
|
6
|
-
member :id, Integer
|
7
|
-
member :last_name, /\A\w+\z/
|
8
|
-
member :family_name, /\A\w+\z/
|
9
|
-
member :address, /\A((\w+) ?)+\z/
|
10
|
-
member :age, ->age{(20..140).include? age}
|
11
|
-
end
|
12
|
-
|
13
|
-
User2 = Striuct.define do
|
14
|
-
member :name, AND(String, NOT(''))
|
15
|
-
member :age, Fixnum
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_new
|
19
|
-
klass = Striuct.new
|
20
|
-
assert_kind_of Striuct, klass.new
|
21
|
-
|
22
|
-
klass = Striuct.new :name, :age
|
23
|
-
|
24
|
-
assert_equal klass.members, [:name, :age]
|
25
|
-
|
26
|
-
klass = Striuct.new :foo do
|
27
|
-
member :var
|
28
|
-
end
|
29
|
-
|
30
|
-
assert_equal klass.members, [:foo, :var]
|
31
|
-
assert_equal User.members, [:id, :last_name, :family_name, :address, :age]
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_define
|
35
|
-
user = User2.define{|r|r.age = 1; r.name = 'a'}
|
36
|
-
assert_same 1, user.age
|
37
|
-
|
38
|
-
assert_raises RuntimeError do
|
39
|
-
user.age = 1
|
40
|
-
end
|
41
|
-
|
42
|
-
user = User2.define{|r|r.age = 1; r.name = 'a'}
|
43
|
-
assert_same 1, user.age
|
44
|
-
assert_same true, user.lock?
|
45
|
-
assert_same false, user.frozen?
|
46
|
-
|
47
|
-
assert_raises RuntimeError do
|
48
|
-
User2.define{|r|r.age = 1}
|
49
|
-
end
|
50
|
-
|
51
|
-
user = User2.define(lock: true){|r|r.age = 1; r.name = 'a'}
|
52
|
-
assert_same 1, user.age
|
53
|
-
assert_same true, user.lock?
|
54
|
-
user = User2.define(lock: false){|r|r.age = 1; r.name = 'a'}
|
55
|
-
assert_same false, user.lock?
|
56
|
-
assert_equal true, user.strict?
|
57
|
-
|
58
|
-
assert_raises Validation::InvalidWritingError do
|
59
|
-
User2.define{|r|r.age = 1; r.name = 'a'; r.name.clear}
|
60
|
-
end
|
61
|
-
|
62
|
-
user = User2.define(strict: false){|r|r.age = 1; r.name = 'a'; r.name.clear}
|
63
|
-
assert_equal '', user.name
|
64
|
-
assert_equal false, user.strict?
|
65
|
-
end
|
66
|
-
|
67
|
-
Sth = Striuct.new :foo, :bar, :hoge
|
68
|
-
SampleStruct = Struct.new :foo, :bar, :hoge
|
69
|
-
|
70
|
-
def test_for_pairs_from_hash
|
71
|
-
sth = Sth[hoge: 7, foo: 8]
|
72
|
-
|
73
|
-
assert_equal [8, nil, 7], sth.values
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_for_pairs_from_struct
|
77
|
-
sth = Sth[SampleStruct.new 8, nil, 7]
|
78
|
-
|
79
|
-
assert_equal [8, nil, 7], sth.values
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class Test_Striuct_Subclass_Class_Constructor < Test::Unit::TestCase
|
4
|
+
|
5
|
+
class User < Striuct.new
|
6
|
+
member :id, Integer
|
7
|
+
member :last_name, /\A\w+\z/
|
8
|
+
member :family_name, /\A\w+\z/
|
9
|
+
member :address, /\A((\w+) ?)+\z/
|
10
|
+
member :age, ->age{(20..140).include? age}
|
11
|
+
end
|
12
|
+
|
13
|
+
User2 = Striuct.define do
|
14
|
+
member :name, AND(String, NOT(''))
|
15
|
+
member :age, Fixnum
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_new
|
19
|
+
klass = Striuct.new
|
20
|
+
assert_kind_of Striuct, klass.new
|
21
|
+
|
22
|
+
klass = Striuct.new :name, :age
|
23
|
+
|
24
|
+
assert_equal klass.members, [:name, :age]
|
25
|
+
|
26
|
+
klass = Striuct.new :foo do
|
27
|
+
member :var
|
28
|
+
end
|
29
|
+
|
30
|
+
assert_equal klass.members, [:foo, :var]
|
31
|
+
assert_equal User.members, [:id, :last_name, :family_name, :address, :age]
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_define
|
35
|
+
user = User2.define{|r|r.age = 1; r.name = 'a'}
|
36
|
+
assert_same 1, user.age
|
37
|
+
|
38
|
+
assert_raises RuntimeError do
|
39
|
+
user.age = 1
|
40
|
+
end
|
41
|
+
|
42
|
+
user = User2.define{|r|r.age = 1; r.name = 'a'}
|
43
|
+
assert_same 1, user.age
|
44
|
+
assert_same true, user.lock?
|
45
|
+
assert_same false, user.frozen?
|
46
|
+
|
47
|
+
assert_raises RuntimeError do
|
48
|
+
User2.define{|r|r.age = 1}
|
49
|
+
end
|
50
|
+
|
51
|
+
user = User2.define(lock: true){|r|r.age = 1; r.name = 'a'}
|
52
|
+
assert_same 1, user.age
|
53
|
+
assert_same true, user.lock?
|
54
|
+
user = User2.define(lock: false){|r|r.age = 1; r.name = 'a'}
|
55
|
+
assert_same false, user.lock?
|
56
|
+
assert_equal true, user.strict?
|
57
|
+
|
58
|
+
assert_raises Validation::InvalidWritingError do
|
59
|
+
User2.define{|r|r.age = 1; r.name = 'a'; r.name.clear}
|
60
|
+
end
|
61
|
+
|
62
|
+
user = User2.define(strict: false){|r|r.age = 1; r.name = 'a'; r.name.clear}
|
63
|
+
assert_equal '', user.name
|
64
|
+
assert_equal false, user.strict?
|
65
|
+
end
|
66
|
+
|
67
|
+
Sth = Striuct.new :foo, :bar, :hoge
|
68
|
+
SampleStruct = Struct.new :foo, :bar, :hoge
|
69
|
+
|
70
|
+
def test_for_pairs_from_hash
|
71
|
+
sth = Sth[hoge: 7, foo: 8]
|
72
|
+
|
73
|
+
assert_equal [8, nil, 7], sth.values
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_for_pairs_from_struct
|
77
|
+
sth = Sth[SampleStruct.new 8, nil, 7]
|
78
|
+
|
79
|
+
assert_equal [8, nil, 7], sth.values
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
@@ -1,36 +1,36 @@
|
|
1
|
-
require_relative 'helper'
|
2
|
-
|
3
|
-
class Test_Striuct_Subclass_Class_Freeze < Test::Unit::TestCase
|
4
|
-
|
5
|
-
Sth = Striuct.new do
|
6
|
-
member :foo
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_class_freeze
|
10
|
-
sth = Sth.new
|
11
|
-
|
12
|
-
assert_same true, sth.member?(:foo)
|
13
|
-
|
14
|
-
Sth.class_eval do
|
15
|
-
member :bar
|
16
|
-
end
|
17
|
-
|
18
|
-
assert_same true, sth.member?(:bar)
|
19
|
-
assert_equal [:foo, :bar], sth.members
|
20
|
-
|
21
|
-
assert_same false, Sth.frozen?
|
22
|
-
|
23
|
-
Sth.freeze
|
24
|
-
|
25
|
-
assert_same true, Sth.frozen?
|
26
|
-
|
27
|
-
assert_raises RuntimeError do
|
28
|
-
Sth.class_eval do
|
29
|
-
member :var2
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
assert_same false, sth.member?(:var2)
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
class Test_Striuct_Subclass_Class_Freeze < Test::Unit::TestCase
|
4
|
+
|
5
|
+
Sth = Striuct.new do
|
6
|
+
member :foo
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_class_freeze
|
10
|
+
sth = Sth.new
|
11
|
+
|
12
|
+
assert_same true, sth.member?(:foo)
|
13
|
+
|
14
|
+
Sth.class_eval do
|
15
|
+
member :bar
|
16
|
+
end
|
17
|
+
|
18
|
+
assert_same true, sth.member?(:bar)
|
19
|
+
assert_equal [:foo, :bar], sth.members
|
20
|
+
|
21
|
+
assert_same false, Sth.frozen?
|
22
|
+
|
23
|
+
Sth.freeze
|
24
|
+
|
25
|
+
assert_same true, Sth.frozen?
|
26
|
+
|
27
|
+
assert_raises RuntimeError do
|
28
|
+
Sth.class_eval do
|
29
|
+
member :var2
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
assert_same false, sth.member?(:var2)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|