rasti-types 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::Float do
4
+
5
+ [100, '200', Time.now,2.0,"12.5"].each do |value|
6
+ it "#{value.inspect} -> #{value.to_i}" do
7
+ Rasti::Types::Float.cast(value).must_equal value.to_f
8
+ end
9
+ end
10
+
11
+ [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, "1.", ".2","."].each do |value|
12
+ it "#{value.inspect} -> CastError" do
13
+ error = proc { Rasti::Types::Float.cast(value) }.must_raise Rasti::Types::CastError
14
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Float"
15
+ end
16
+ end
17
+
18
+ end
data/spec/hash_spec.rb ADDED
@@ -0,0 +1,52 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::Hash do
4
+
5
+ it "{'a' => '123'} -> {a: 123}" do
6
+ Rasti::Types::Hash[Rasti::Types::Symbol, Rasti::Types::Integer].cast('a' => '123').must_equal a: 123
7
+ end
8
+
9
+ it "{'1' => :abc} -> {1 => 'abc'}" do
10
+ Rasti::Types::Hash[Rasti::Types::Integer, Rasti::Types::String].cast('1' => :abc).must_equal 1 => 'abc'
11
+ end
12
+
13
+ [nil, 1, 'text', :symbol, Object.new].each do |value|
14
+ it "#{value.inspect} -> CastError" do
15
+ error = proc { Rasti::Types::Hash[Rasti::Types::Symbol, Rasti::Types::Integer].cast(value) }.must_raise Rasti::Types::CastError
16
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Hash[Rasti::Types::Symbol, Rasti::Types::Integer]"
17
+ end
18
+ end
19
+
20
+ describe 'Multi cast errors' do
21
+
22
+ it 'Simple types' do
23
+ value = {
24
+ true => 1,
25
+ 2 => false
26
+ }
27
+
28
+ error = proc { Rasti::Types::Hash[Rasti::Types::Integer, Rasti::Types::Integer].cast(value) }.must_raise Rasti::Types::MultiCastError
29
+
30
+ error.errors.must_equal true => ['Invalid cast: true -> Rasti::Types::Integer'],
31
+ 2 => ['Invalid cast: false -> Rasti::Types::Integer']
32
+
33
+ error.message.must_equal "Cast errors:\n- true: [\"Invalid cast: true -> Rasti::Types::Integer\"]\n- 2: [\"Invalid cast: false -> Rasti::Types::Integer\"]"
34
+ end
35
+
36
+ it 'Models' do
37
+ value = {
38
+ 'a' => {x: 1, y: 2},
39
+ 'b' => {x: 1},
40
+ 'c' => true
41
+ }
42
+
43
+ error = proc { Rasti::Types::Hash[Rasti::Types::String, Rasti::Types::Model[Point]].cast(value) }.must_raise Rasti::Types::MultiCastError
44
+
45
+ error.errors.must_equal 'b.y' => ['not present'],
46
+ 'c' => ['Invalid cast: true -> Rasti::Types::Model[Point]']
47
+
48
+ error.message.must_equal "Cast errors:\n- b.y: [\"not present\"]\n- c: [\"Invalid cast: true -> Rasti::Types::Model[Point]\"]"
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,18 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::Integer do
4
+
5
+ [100, '200', Time.now, 2.1, "12.5"].each do |value|
6
+ it "#{value.inspect} -> #{value.to_i}" do
7
+ Rasti::Types::Integer.cast(value).must_equal value.to_i
8
+ end
9
+ end
10
+
11
+ [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new].each do |value|
12
+ it "#{value.inspect} -> CastError" do
13
+ error = proc { Rasti::Types::Integer.cast(value) }.must_raise Rasti::Types::CastError
14
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Integer"
15
+ end
16
+ end
17
+
18
+ end
data/spec/io_spec.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::IO do
4
+
5
+ [StringIO.new, File.new(__FILE__)].each do |value|
6
+ it "#{value.inspect} -> #{value}" do
7
+ Rasti::Types::IO.cast(value).must_equal value
8
+ end
9
+ end
10
+
11
+ [nil, 'text', 123, :symbol, [1,2], {a: 1, b: 2}, Object.new].each do |value|
12
+ it "#{value.inspect} -> CastError" do
13
+ error = proc { Rasti::Types::IO.cast(value) }.must_raise Rasti::Types::CastError
14
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::IO"
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,37 @@
1
+ require 'coverage_helper'
2
+ require 'minitest/autorun'
3
+ require 'minitest/colorin'
4
+ require 'pry-nav'
5
+ require 'rasti-types'
6
+
7
+ module Minitest
8
+ class Test
9
+ def as_string(value)
10
+ value.is_a?(::String) ? "'#{value}'" : value.inspect
11
+ end
12
+ end
13
+ end
14
+
15
+ class Point
16
+
17
+ attr_reader :x, :y
18
+
19
+ def initialize(attributes={})
20
+ errors = {}
21
+
22
+ if attributes.key? :x
23
+ @x = attributes[:x]
24
+ else
25
+ errors[:x] = ['not present']
26
+ end
27
+
28
+ if attributes.key? :y
29
+ @y = attributes[:y]
30
+ else
31
+ errors[:y] = ['not present']
32
+ end
33
+
34
+ raise Rasti::Types::CompoundError, errors if errors.any?
35
+ end
36
+
37
+ end
@@ -0,0 +1,24 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::Model do
4
+
5
+ it 'Class' do
6
+ result = Rasti::Types::Model[Point].cast x: 1, y: 2
7
+ result.must_be_instance_of Point
8
+ result.x.must_equal 1
9
+ result.y.must_equal 2
10
+ end
11
+
12
+ it 'Hash -> CastError' do
13
+ error = proc { Rasti::Types::Model[Point].cast(z: 'text') }.must_raise Rasti::Types::CompoundError
14
+ error.message.must_equal "Errors:\n- x: [\"not present\"]\n- y: [\"not present\"]"
15
+ end
16
+
17
+ [nil, 'text', :symbol, 1, [1,2], Object.new].each do |value|
18
+ it "#{value.inspect} -> CastError" do
19
+ error = proc { Rasti::Types::Model[Point].cast(value) }.must_raise Rasti::Types::CastError
20
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> #{Rasti::Types::Model[Point]}"
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,18 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::Regexp do
4
+
5
+ ['[a-z]', /[a-z]/].each do |value|
6
+ it "#{value.inspect} -> #{Regexp.new(value).inspect}" do
7
+ Rasti::Types::Regexp.cast(value).must_equal Regexp.new(value).source
8
+ end
9
+ end
10
+
11
+ [nil, '[a-z', :symbol, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
12
+ it "#{value.inspect} -> CastError" do
13
+ error = proc { Rasti::Types::Regexp.cast(value) }.must_raise Rasti::Types::CastError
14
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Regexp"
15
+ end
16
+ end
17
+
18
+ end
@@ -0,0 +1,20 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::String, 'Formatted' do
4
+
5
+ email_regexp = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
6
+
7
+ ['user@mail.com'.to_sym, 'user.name-123@mail-test.com.ar'].each do |value|
8
+ it "#{value.inspect} -> #{value.to_s}" do
9
+ Rasti::Types::String[email_regexp].cast(value).must_equal value.to_s
10
+ end
11
+ end
12
+
13
+ [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5].each do |value|
14
+ it "#{value.inspect} -> CastError" do
15
+ error = proc { Rasti::Types::String[email_regexp].cast(value) }.must_raise Rasti::Types::CastError
16
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::String[#{as_string(email_regexp)}]"
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,16 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::String do
4
+
5
+ ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
6
+ it "#{value.inspect} -> \"#{value.to_s}\"" do
7
+ Rasti::Types::String.cast(value).must_equal value.to_s
8
+ end
9
+ end
10
+
11
+ it 'nil -> CastError' do
12
+ error = proc { Rasti::Types::String.cast(nil) }.must_raise Rasti::Types::CastError
13
+ error.message.must_equal 'Invalid cast: nil -> Rasti::Types::String'
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::Symbol do
4
+
5
+ ['text', :symbol, true, false, 100, Time.now, [1,2], {a: 1, b: 2}, Object.new].each do |value|
6
+ it "#{value.inspect} -> \"#{value.to_s.to_sym}\"" do
7
+ Rasti::Types::Symbol.cast(value).must_equal value.to_s.to_sym
8
+ end
9
+ end
10
+
11
+ it 'nil -> CastError' do
12
+ error = proc { Rasti::Types::Symbol.cast(nil) }.must_raise Rasti::Types::CastError
13
+ error.message.must_equal 'Invalid cast: nil -> Rasti::Types::Symbol'
14
+ end
15
+
16
+ end
data/spec/time_spec.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::Time do
4
+
5
+ time = Time.new 2016, 8, 18
6
+
7
+ it "#{time.inspect} -> #{time.inspect}" do
8
+ Rasti::Types::Time['%F %T %z'].cast(time).must_equal time
9
+ end
10
+
11
+ ['%d/%m/%y', '%Y-%m-%d'].each do |format|
12
+ time_string = time.strftime(format)
13
+ it "#{time_string.inspect} -> #{time.inspect}" do
14
+ Rasti::Types::Time[format].cast(time_string).must_equal time
15
+ end
16
+ end
17
+
18
+ [time.strftime('%d/%m/%y'), 'text', nil, 1, :symbol, [1,2], {a: 1, b: 2}, Object.new].each do |value|
19
+ it "#{value.inspect} -> CastError" do
20
+ error = proc { Rasti::Types::Time['%F'].cast(value) }.must_raise Rasti::Types::CastError
21
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::Time['%F']"
22
+ end
23
+ end
24
+
25
+ end
data/spec/uuid_spec.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Rasti::Types::UUID do
4
+
5
+ ['f09b7716-81a9-11e6-a549-bb8f165bcf02', '12345678-1234-1234-1234-123456789123'].each do |value|
6
+ it "#{value.inspect} -> #{value.to_s}" do
7
+ Rasti::Types::UUID.cast(value).must_equal value.to_s
8
+ end
9
+ end
10
+
11
+ [nil, 'text', :symbol, '999'.to_sym, [1,2], {a: 1, b: 2}, Object.new, 5, 'f09b7716-81a9-11e6-a549-bb16502', 'f09b7716-11e6-a549-bb8f16502', '-84a9-11e6-a549-bb8f16502', 'f09b7716-81a9-11e6-a549-bh16502'].each do |value|
12
+ it "#{value.inspect} -> CastError" do
13
+ error = proc { Rasti::Types::UUID.cast(value) }.must_raise Rasti::Types::CastError
14
+ error.message.must_equal "Invalid cast: #{as_string(value)} -> Rasti::Types::UUID"
15
+ end
16
+ end
17
+
18
+ end
metadata ADDED
@@ -0,0 +1,221 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rasti-types
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Naiman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_require
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '5.11'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '5.0'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '5.11'
61
+ - !ruby/object:Gem::Dependency
62
+ name: minitest-colorin
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.1'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.1'
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest-line
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0.6'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0.6'
89
+ - !ruby/object:Gem::Dependency
90
+ name: simplecov
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.12'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.12'
103
+ - !ruby/object:Gem::Dependency
104
+ name: coveralls
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.8'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.8'
117
+ - !ruby/object:Gem::Dependency
118
+ name: pry-nav
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '0.2'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '0.2'
131
+ description: Type casting
132
+ email:
133
+ - gabynaiman@gmail.com
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".coveralls.yml"
139
+ - ".gitignore"
140
+ - ".ruby-gemset"
141
+ - ".ruby-version"
142
+ - ".travis.yml"
143
+ - Gemfile
144
+ - LICENSE.txt
145
+ - README.md
146
+ - Rakefile
147
+ - lib/rasti-types.rb
148
+ - lib/rasti/types.rb
149
+ - lib/rasti/types/array.rb
150
+ - lib/rasti/types/boolean.rb
151
+ - lib/rasti/types/castable.rb
152
+ - lib/rasti/types/enum.rb
153
+ - lib/rasti/types/errors.rb
154
+ - lib/rasti/types/float.rb
155
+ - lib/rasti/types/hash.rb
156
+ - lib/rasti/types/integer.rb
157
+ - lib/rasti/types/io.rb
158
+ - lib/rasti/types/model.rb
159
+ - lib/rasti/types/regexp.rb
160
+ - lib/rasti/types/string.rb
161
+ - lib/rasti/types/symbol.rb
162
+ - lib/rasti/types/time.rb
163
+ - lib/rasti/types/uuid.rb
164
+ - lib/rasti/types/version.rb
165
+ - rasti-types.gemspec
166
+ - spec/array_spec.rb
167
+ - spec/boolean_spec.rb
168
+ - spec/coverage_helper.rb
169
+ - spec/enum_spec.rb
170
+ - spec/float_spec.rb
171
+ - spec/hash_spec.rb
172
+ - spec/integer_spec.rb
173
+ - spec/io_spec.rb
174
+ - spec/minitest_helper.rb
175
+ - spec/model_spec.rb
176
+ - spec/regexp_spec.rb
177
+ - spec/string_formatted_spec.rb
178
+ - spec/string_spec.rb
179
+ - spec/symbol_spec.rb
180
+ - spec/time_spec.rb
181
+ - spec/uuid_spec.rb
182
+ homepage: https://github.com/gabynaiman/rasti-types
183
+ licenses:
184
+ - MIT
185
+ metadata: {}
186
+ post_install_message:
187
+ rdoc_options: []
188
+ require_paths:
189
+ - lib
190
+ required_ruby_version: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ required_rubygems_version: !ruby/object:Gem::Requirement
196
+ requirements:
197
+ - - ">="
198
+ - !ruby/object:Gem::Version
199
+ version: '0'
200
+ requirements: []
201
+ rubygems_version: 3.0.6
202
+ signing_key:
203
+ specification_version: 4
204
+ summary: Type casting
205
+ test_files:
206
+ - spec/array_spec.rb
207
+ - spec/boolean_spec.rb
208
+ - spec/coverage_helper.rb
209
+ - spec/enum_spec.rb
210
+ - spec/float_spec.rb
211
+ - spec/hash_spec.rb
212
+ - spec/integer_spec.rb
213
+ - spec/io_spec.rb
214
+ - spec/minitest_helper.rb
215
+ - spec/model_spec.rb
216
+ - spec/regexp_spec.rb
217
+ - spec/string_formatted_spec.rb
218
+ - spec/string_spec.rb
219
+ - spec/symbol_spec.rb
220
+ - spec/time_spec.rb
221
+ - spec/uuid_spec.rb