nidyx 0.1.0

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.
@@ -0,0 +1,237 @@
1
+ require "minitest/autorun"
2
+ require "nidyx"
3
+ require "nidyx/property"
4
+ require "nidyx/objc/property"
5
+
6
+ class TestObjCProperty < Minitest::Test
7
+
8
+ def test_is_obj
9
+ assert_equal(true, simple_property("array").is_obj?)
10
+ assert_equal(false, simple_property("boolean").is_obj?)
11
+ assert_equal(false, simple_property("integer").is_obj?)
12
+ assert_equal(false, simple_property("number").is_obj?)
13
+ assert_equal(true, simple_property(%w(boolean null)).is_obj?)
14
+ assert_equal(true, simple_property(%w(integer null)).is_obj?)
15
+ assert_equal(true, simple_property(%w(number null)).is_obj?)
16
+ assert_equal(true, simple_property("string").is_obj?)
17
+ assert_equal(true, simple_property("object").is_obj?)
18
+ assert_equal(true, simple_property("null").is_obj?)
19
+ end
20
+
21
+ def test_simple_array
22
+ obj = { "type" => "array" }
23
+ p = property(obj, false)
24
+ assert_equal(:array, p.type)
25
+ assert_equal(nil, p.getter_override)
26
+ assert_equal([], p.protocols)
27
+ assert_equal(false, p.has_protocols?)
28
+
29
+ # optional array
30
+ p = property(obj, true)
31
+ assert_equal(:array, p.type)
32
+ assert_equal(["Optional"], p.protocols)
33
+ assert_equal(true, p.has_protocols?)
34
+ end
35
+
36
+ def test_typed_optional_array
37
+ obj = { "type" => ["array", "null"] }
38
+ p = property(obj, false)
39
+ assert_equal(:array, p.type)
40
+ assert_equal(["Optional"], p.protocols)
41
+ end
42
+
43
+ def test_boolean
44
+ obj = { "type" => "boolean" }
45
+ p = property(obj, false)
46
+ assert_equal(:boolean, p.type)
47
+
48
+ # optional boolean
49
+ p = property(obj, true)
50
+ assert_equal(:number_obj, p.type)
51
+ end
52
+
53
+ def test_typed_optional_boolean
54
+ obj = { "type" => ["boolean", "null"] }
55
+ p = property(obj, false)
56
+ assert_equal(:number_obj, p.type)
57
+ end
58
+
59
+ def test_integer
60
+ obj = { "type" => "integer" }
61
+ p = property(obj, false)
62
+ assert_equal(:integer, p.type)
63
+
64
+ p = property(obj, true)
65
+ assert_equal(:number_obj, p.type)
66
+ assert_equal(["Optional"], p.protocols)
67
+ end
68
+
69
+ def test_unsigned_integer
70
+ obj = { "type" => "integer", "minimum" => 0 }
71
+ p = property(obj, false)
72
+ assert_equal(:unsigned, p.type)
73
+ end
74
+
75
+ def test_typed_optional_integer
76
+ obj = { "type" => ["integer", "null"] }
77
+ p = property(obj, false)
78
+ assert_equal(:number_obj, p.type)
79
+ assert_equal(["Optional"], p.protocols)
80
+ end
81
+
82
+ def test_number
83
+ obj = { "type" => "number" }
84
+ p = property(obj, false)
85
+ assert_equal(:number, p.type)
86
+
87
+ p = property(obj, true)
88
+ assert_equal(:number_obj, p.type)
89
+ end
90
+
91
+ def test_typed_optional_number
92
+ obj = { "type" => ["number", "null"] }
93
+ p = property(obj, false)
94
+ assert_equal(:number_obj, p.type)
95
+ end
96
+
97
+ def test_string
98
+ obj = { "type" => "string" }
99
+ p = property(obj, false)
100
+ assert_equal(:string, p.type)
101
+
102
+ p = property(obj, true)
103
+ assert_equal(:string, p.type)
104
+ end
105
+
106
+ def test_typed_optional_string
107
+ obj = { "type" => ["string", "null"] }
108
+ p = property(obj, false)
109
+ assert_equal(:string, p.type)
110
+ end
111
+
112
+ def test_object
113
+ obj = {
114
+ "type" => "object",
115
+ "properties" => {}
116
+ }
117
+
118
+ p = property(obj, false)
119
+ assert_equal(:object, p.type)
120
+ end
121
+
122
+ def test_multiple_numeric_types
123
+ obj = { "type" => ["number", "integer", "boolean"] }
124
+ p = property(obj, false)
125
+ assert_equal(:number_obj, p.type)
126
+ end
127
+
128
+ def test_typed_optional_multiple_numeric_types
129
+ obj = { "type" => ["number", "integer", "boolean", "null"] }
130
+ p = property(obj, false)
131
+ assert_equal(:number_obj, p.type)
132
+ assert_equal(["Optional"], p.protocols)
133
+ end
134
+
135
+ def test_multiple_disparate_types
136
+ obj = { "type" => ["object", "number"] }
137
+ p = property(obj, false)
138
+ assert_equal(:id, p.type)
139
+ end
140
+
141
+ def test_typed_optional_multiple_disparate_types
142
+ obj = { "type" => ["object", "number", "null"] }
143
+ p = property(obj, false)
144
+ assert_equal(:id, p.type)
145
+ end
146
+
147
+ def test_simple_numbers
148
+ obj = { "type" => [ "integer", "number" ] }
149
+ p = property(obj, false)
150
+ assert_equal(:number, p.type)
151
+
152
+ p = property(obj, true)
153
+ assert_equal(:number_obj, p.type)
154
+ end
155
+
156
+ def test_typed_optional_simple_numbers
157
+ obj = { "type" => [ "integer", "number", "null" ] }
158
+ p = property(obj, false)
159
+ assert_equal(:number_obj, p.type)
160
+ end
161
+
162
+ def test_integer_enum
163
+ obj = { "enum" => [1, 2] }
164
+ p = property(obj, false)
165
+ assert_equal(:integer, p.type)
166
+
167
+ p = property(obj, true)
168
+ assert_equal(:number_obj, p.type)
169
+ assert_equal(["Optional"], p.protocols)
170
+ end
171
+
172
+ def test_string_enum
173
+ obj = { "enum" => ["a", "b"] }
174
+ p = property(obj, false)
175
+ assert_equal(:string, p.type)
176
+ end
177
+
178
+ def test_typed_optional_enum
179
+ obj = { "enum" => [1, 2, nil] }
180
+ p = property(obj, false)
181
+ assert_equal(:number_obj, p.type)
182
+ assert_equal(["Optional"], p.protocols)
183
+ end
184
+
185
+ def test_single_element_array_type
186
+ obj = { "type" => ["integer"] }
187
+ p = property(obj, false)
188
+ assert_equal(:integer, p.type)
189
+ end
190
+
191
+ def test_anonymous_object
192
+ obj = { "type" => "object" }
193
+ p = property(obj, false)
194
+ assert_equal(:id, p.type)
195
+ end
196
+
197
+ def test_unsafe_getter
198
+ obj = { "type" => "integer" }
199
+ p = Nidyx::ObjCProperty.new(Nidyx::Property.new("newInt", nil, false, obj))
200
+ assert_equal(", getter=getNewInt", p.getter_override)
201
+ end
202
+
203
+ def test_protocols
204
+ obj = {
205
+ "type" => "array",
206
+ COLLECTION_TYPES_KEY => ["SomeModel", "OtherModel"]
207
+ }
208
+
209
+ p = property(obj, false)
210
+ assert_equal(true, p.has_protocols?)
211
+ assert_equal("SomeModel, OtherModel", p.protocols_string)
212
+ assert_equal(%w(SomeModel OtherModel), p.protocols)
213
+
214
+ p = property(obj, true)
215
+ assert_equal(true, p.has_protocols?)
216
+ assert_equal("SomeModel, OtherModel, Optional", p.protocols_string)
217
+ assert_equal(%w(SomeModel OtherModel Optional), p.protocols)
218
+ end
219
+
220
+ def test_unsupported_types_enum
221
+ assert_raises(Nidyx::ObjCProperty::UnsupportedEnumTypeError) do
222
+ obj = { "enum" => ["a", {}] }
223
+ Nidyx::ObjCProperty.new(Nidyx::Property.new("i", nil, false, obj))
224
+ end
225
+ end
226
+
227
+ private
228
+
229
+ def simple_property(type)
230
+ obj = { "type" => type }
231
+ Nidyx::ObjCProperty.new(Nidyx::Property.new("p", nil, false, obj))
232
+ end
233
+
234
+ def property(obj, optional, class_name = nil)
235
+ Nidyx::ObjCProperty.new(Nidyx::Property.new("name", class_name, optional, obj))
236
+ end
237
+ end
@@ -0,0 +1,102 @@
1
+ require "minitest/autorun"
2
+ require "nidyx"
3
+ require "nidyx/common"
4
+
5
+ include Nidyx::Common
6
+
7
+ class TestCommon < Minitest::Test
8
+ def test_class_name
9
+ assert_equal("DKResponseModel", class_name("DK", "response"))
10
+ assert_equal("DKModel", class_name("DK", nil))
11
+ assert_equal("DKLargeButtonModel", class_name("DK", "large_button"))
12
+ end
13
+
14
+ def test_class_name_from_path
15
+ schema = {
16
+ "type" => "object",
17
+ "properties" => {
18
+ "obj" => { "$ref" => "#/definitions/obj" }
19
+ },
20
+ "definitions" => {
21
+ "obj" => {
22
+ "type" => "object",
23
+ "properties" => {
24
+ "name" => { "type" => "string" }
25
+ }
26
+ }
27
+ }
28
+ }
29
+
30
+ path = ["definitions", "obj"]
31
+ assert_equal("DKObjModel", class_name_from_path("DK", path, schema))
32
+ # empty
33
+ assert_equal("DKModel", class_name_from_path("DK", [], schema))
34
+ end
35
+
36
+ def test_class_name_from_path_overrides
37
+ schema = {
38
+ "type" => "object",
39
+ "properties" => {
40
+ "obj" => {
41
+ "type" => "object",
42
+ "nameOverride" => "otherObject",
43
+ "properties" => {
44
+ "subObject" => {
45
+ "type" => "object",
46
+ "nameOverride" => "otherSubObject",
47
+ "properties" => {
48
+ "count" => { "type" => "integer" }
49
+ }
50
+ }
51
+ }
52
+ }
53
+ }
54
+ }
55
+
56
+ path = ["properties", "obj"]
57
+ assert_equal("DKOtherObjectModel", class_name_from_path("DK", path, schema))
58
+ path = ["properties", "obj", "properties", "subObject"]
59
+ assert_equal("DKOtherSubObjectModel", class_name_from_path("DK", path, schema))
60
+ end
61
+
62
+ def test_object_at_path
63
+ schema = {
64
+ "type" => "object",
65
+ "properties" => {
66
+ "value" => { "$ref" => "#/definitions/obj" }
67
+ },
68
+ "definitions" => {
69
+ "obj" => {
70
+ "type" => "object",
71
+ "properties" => {
72
+ "name" => { "type" => "string" },
73
+ "count" => { "type" => "integer" }
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ obj = object_at_path(["properties", "value"], schema)
80
+ assert_equal("#/definitions/obj", obj["$ref"])
81
+
82
+ obj = object_at_path(["definitions", "obj"], schema)
83
+ assert_equal("object", obj["type"])
84
+ assert_equal("string", obj["properties"]["name"]["type"])
85
+
86
+ obj = object_at_path(["definitions", "obj", "properties", "count"], schema)
87
+ assert_equal("integer", obj["type"])
88
+ end
89
+
90
+ def test_object_at_path_raises
91
+ schema = {
92
+ "type" => "object",
93
+ "properties" => {
94
+ "value" => { "type" => "string" }
95
+ }
96
+ }
97
+
98
+ assert_raises(Nidyx::Common::NoObjectAtPathError) do
99
+ object_at_path(["bad", "path"], schema)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,64 @@
1
+ require "minitest/autorun"
2
+ require "fileutils"
3
+
4
+ class TestComparison < Minitest::Test
5
+ def setup
6
+ FileUtils.mkdir_p(TMP_PATH)
7
+ end
8
+
9
+ def teardown
10
+ FileUtils.rm_rf(TMP_PATH)
11
+ end
12
+
13
+ def test_simple_properties
14
+ validate_files("simple_properties")
15
+ end
16
+
17
+ def test_complex_properties
18
+ validate_files("complex_properties")
19
+ end
20
+
21
+ def test_defs_and_refs
22
+ validate_files("defs_and_refs")
23
+ end
24
+
25
+ private
26
+
27
+ ROOT = File.absolute_path(File.join(File.dirname(__FILE__), "../.."))
28
+ TMP_PATH = File.join(ROOT, "tmp")
29
+ EXAMPLES_PATH = File.join(ROOT, "examples")
30
+ PREFIX = "Example"
31
+
32
+ def validate_files(example_name)
33
+ run_generate(example_name)
34
+
35
+ Dir.foreach(TMP_PATH) do |f|
36
+ validate_file(example_name, f) unless [".", ".."].include?(f)
37
+ end
38
+ end
39
+
40
+ def run_generate(example_name)
41
+ cmd = "bundle exec nidyx " <<
42
+ example_schema_path(example_name) <<
43
+ " #{PREFIX} #{TMP_PATH} -n --json-model"
44
+
45
+ assert(false) unless system(cmd)
46
+ end
47
+
48
+ def validate_file(ename, fname)
49
+ assert_equal(IO.read(example_file_path(ename, fname)),
50
+ IO.read(tmp_file_path(fname)))
51
+ end
52
+
53
+ def tmp_file_path(file_name)
54
+ File.join(TMP_PATH, file_name)
55
+ end
56
+
57
+ def example_file_path(example_name, file_name)
58
+ File.join(EXAMPLES_PATH, example_name, file_name)
59
+ end
60
+
61
+ def example_schema_path(example_name)
62
+ File.join(EXAMPLES_PATH, example_name, "#{example_name}.json.schema")
63
+ end
64
+ end