emery 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/tod_test.rb ADDED
@@ -0,0 +1,41 @@
1
+ require "test/unit/runner/junitxml"
2
+
3
+ require 'emery/type'
4
+ require 'emery/tod'
5
+ require 'emery/jsoner'
6
+
7
+
8
+ module Emery
9
+ class TypeCheckTimeOfDay < Test::Unit::TestCase
10
+ def test_success
11
+ assert_equal TimeOfDay.parse("10:40:50"), T.check(TimeOfDay, TimeOfDay.parse("10:40:50"))
12
+ end
13
+
14
+ def test_fail
15
+ assert_raise TypeError do
16
+ T.check(DateTime, TimeOfDay.parse("10:40:50"))
17
+ end
18
+ end
19
+ end
20
+
21
+ class TimeOfDayDeserialization < Test::Unit::TestCase
22
+ def test_deserialize
23
+ data = Jsoner.from_json(TimeOfDay, '"10:40:50"')
24
+ T.check(TimeOfDay, data)
25
+ assert_equal TimeOfDay.parse("10:40:50"), data
26
+ end
27
+
28
+ def test_deserialize_fail
29
+ assert_raise JsonerError do
30
+ Jsoner.from_json(TimeOfDay, '"abc"')
31
+ end
32
+ end
33
+ end
34
+
35
+ class TimeOfDaySerialization < Test::Unit::TestCase
36
+ def test_serialize
37
+ json_str = Jsoner.to_json(TimeOfDay, TimeOfDay.parse("10:40:50"))
38
+ assert_equal '"10:40:50"', json_str
39
+ end
40
+ end
41
+ end
data/test/type_test.rb ADDED
@@ -0,0 +1,185 @@
1
+ require "test/unit/runner/junitxml"
2
+ require "date"
3
+
4
+ require 'emery/type'
5
+
6
+ module Emery
7
+ class TypeEquality < Test::Unit::TestCase
8
+ def test_plain_equals
9
+ assert_true Integer == Integer
10
+ end
11
+
12
+ def test_plain_not_equals
13
+ assert_false Integer == String
14
+ end
15
+
16
+ def test_uuid_equals
17
+ assert_true UUID == UUID
18
+ end
19
+
20
+ def test_boolean_equals
21
+ assert_true Boolean == Boolean
22
+ end
23
+
24
+ def test_untyped_equals
25
+ assert_true Untyped == Untyped
26
+ end
27
+
28
+ def test_nilable_equals
29
+ assert_true T.nilable(Integer) == T.nilable(Integer)
30
+ end
31
+
32
+ def test_nilable_not_equals
33
+ assert_false T.nilable(Integer) == T.nilable(String)
34
+ end
35
+
36
+ def test_array_equals
37
+ assert_true T.array(Integer) == T.array(Integer)
38
+ end
39
+
40
+ def test_array_not_equals
41
+ assert_false T.array(Integer) == Integer
42
+ end
43
+
44
+ def test_array_other_item_type
45
+ assert_false T.array(Integer) == T.array(String)
46
+ end
47
+
48
+ def test_hash_equals
49
+ assert_true T.hash(String, Integer) == T.hash(String, Integer)
50
+ end
51
+
52
+ def test_hash_not_equals
53
+ assert_false T.hash(String, Integer) == String
54
+ end
55
+
56
+ def test_hash_other_value_type
57
+ assert_false T.hash(String, Integer) == T.hash(String, String)
58
+ end
59
+
60
+ def test_any_equals
61
+ assert_true T.any(String, Integer) == T.any(String, Integer)
62
+ end
63
+
64
+ def test_any_equals_other_order
65
+ assert_true T.any(String, Integer) == T.any(Integer, String)
66
+ end
67
+
68
+ def test_any_of_other_type
69
+ assert_false T.any(String, Integer) == T.any(Integer, Float)
70
+ end
71
+ end
72
+
73
+ class TypeCheck < Test::Unit::TestCase
74
+ def test_nil
75
+ assert_equal nil, T.check(NilClass, nil)
76
+ end
77
+
78
+ def test_nil_string
79
+ assert_raise TypeError do
80
+ T.check(String, nil)
81
+ end
82
+ end
83
+
84
+ def test_types_mismatch
85
+ assert_raise TypeError do
86
+ T.check(String, 123)
87
+ end
88
+ end
89
+
90
+ def test_string
91
+ assert_equal "the string", T.check(String, "the string"), "Plain String type should allow String value"
92
+ end
93
+
94
+ def test_string_nil
95
+ err = assert_raise TypeError do
96
+ T.check(String, nil)
97
+ end
98
+ assert_match "Type String does not allow nil value", err.message
99
+ end
100
+
101
+ def test_boolean
102
+ assert_equal true, T.check(Boolean, true), "Artificial Boolean type should allow true value"
103
+ end
104
+
105
+ def test_date
106
+ assert_equal Date.new(2020, 5, 24), T.check(Date, Date.new(2020, 5, 24)), "Date type should pass validation"
107
+ end
108
+
109
+ def test_datetime
110
+ assert_equal DateTime.new(2020, 5, 24, 14, 30, 30), T.check(Date, DateTime.new(2020, 5, 24, 14, 30, 30)), "DateTime type should pass validation"
111
+ end
112
+
113
+ def test_time
114
+ assert_equal Time.new(2007,11,5,13,45,0, "-05:00"), T.check(Time, Time.new(2007, 11, 5, 13, 45, 0, "-05:00")), "Time type should pass validation"
115
+ end
116
+
117
+ def test_uuid
118
+ assert_equal "123e4567-e89b-12d3-a456-426655440000", T.check(UUID, "123e4567-e89b-12d3-a456-426655440000"), "UUID type should pass validation on correctly formatted string"
119
+ end
120
+
121
+ def test_uuid_fail
122
+ assert_raise TypeError do
123
+ T.check(UUID, "really not the uuid")
124
+ end
125
+ end
126
+
127
+ def test_untyped_success
128
+ assert_equal "bla", T.check(Untyped, "bla"), "Untyped should accept strings"
129
+ end
130
+
131
+ def test_untyped_nil
132
+ assert_raise TypeError do
133
+ T.check(Untyped, nil)
134
+ end
135
+ end
136
+
137
+ def test_nilable_nil
138
+ assert_equal nil, T.check(T.nilable(String), nil), "Nilable type should allow nil value"
139
+ end
140
+
141
+ def test_nilable
142
+ assert_equal "the string", T.check(T.nilable(String), "the string"), "Nilable String type should allow String value"
143
+ end
144
+ end
145
+
146
+ class TypeCheckArray < Test::Unit::TestCase
147
+ def test_array_string
148
+ assert_equal ["the string"], T.check(T.array(String), ["the string"]), "Array of String should allow String value"
149
+ end
150
+
151
+ def test_array_fail
152
+ assert_raise TypeError do
153
+ T.check(T.array(String), "the string")
154
+ end
155
+ end
156
+
157
+ def test_array_wrong_item_type
158
+ assert_raise TypeError do
159
+ T.check(T.array(String), ["the string", 123])
160
+ end
161
+ end
162
+ end
163
+
164
+ class TypeCheckHash < Test::Unit::TestCase
165
+ def test_hash_string_to_string
166
+ assert_equal({"key" => "the value"}, T.check(T.hash(String, String), {"key" => "the value"}), "Hash of String -> String should allow String -> String value")
167
+ end
168
+
169
+ def test_hash_string_to_untyped
170
+ assert_equal({"key" => "the value"}, T.check(T.hash(String, Untyped), {"key" => "the value"}), "Hash of String -> Untyped should allow String -> String value")
171
+ end
172
+ end
173
+
174
+ class TypeCheckAny < Test::Unit::TestCase
175
+ def test_success
176
+ assert_equal(123, T.check(T.any(String, Integer), 123), "Any of String, Integer should allow Integer value")
177
+ end
178
+
179
+ def test_fail
180
+ assert_raise TypeError do
181
+ T.check(T.any(String, Integer), true)
182
+ end
183
+ end
184
+ end
185
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: emery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Sapronov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description:
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/emery.rb
34
+ - lib/emery/dataclass.rb
35
+ - lib/emery/enum.rb
36
+ - lib/emery/jsoner.rb
37
+ - lib/emery/tod.rb
38
+ - lib/emery/type.rb
39
+ - test/dataclass_test.rb
40
+ - test/enum_test.rb
41
+ - test/jsoner_test.rb
42
+ - test/tod_test.rb
43
+ - test/type_test.rb
44
+ homepage: https://github.com/vsapronov/emery
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.0.3
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Type safety library
67
+ test_files: []