respect 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +289 -0
  3. data/RELATED_WORK.md +40 -0
  4. data/RELEASE_NOTES.md +23 -0
  5. data/Rakefile +31 -0
  6. data/STATUS_MATRIX.html +137 -0
  7. data/lib/respect.rb +231 -0
  8. data/lib/respect/any_schema.rb +22 -0
  9. data/lib/respect/array_def.rb +28 -0
  10. data/lib/respect/array_schema.rb +203 -0
  11. data/lib/respect/boolean_schema.rb +32 -0
  12. data/lib/respect/composite_schema.rb +86 -0
  13. data/lib/respect/core_statements.rb +206 -0
  14. data/lib/respect/datetime_schema.rb +27 -0
  15. data/lib/respect/def_without_name.rb +6 -0
  16. data/lib/respect/divisible_by_validator.rb +20 -0
  17. data/lib/respect/doc_helper.rb +24 -0
  18. data/lib/respect/doc_parser.rb +37 -0
  19. data/lib/respect/dsl_dumper.rb +181 -0
  20. data/lib/respect/equal_to_validator.rb +20 -0
  21. data/lib/respect/fake_name_proxy.rb +116 -0
  22. data/lib/respect/float_schema.rb +27 -0
  23. data/lib/respect/format_validator.rb +136 -0
  24. data/lib/respect/global_def.rb +79 -0
  25. data/lib/respect/greater_than_or_equal_to_validator.rb +19 -0
  26. data/lib/respect/greater_than_validator.rb +19 -0
  27. data/lib/respect/has_constraints.rb +34 -0
  28. data/lib/respect/hash_def.rb +40 -0
  29. data/lib/respect/hash_schema.rb +218 -0
  30. data/lib/respect/in_validator.rb +19 -0
  31. data/lib/respect/integer_schema.rb +27 -0
  32. data/lib/respect/ip_addr_schema.rb +23 -0
  33. data/lib/respect/ipv4_addr_schema.rb +27 -0
  34. data/lib/respect/ipv6_addr_schema.rb +27 -0
  35. data/lib/respect/items_def.rb +21 -0
  36. data/lib/respect/json_schema_html_formatter.rb +143 -0
  37. data/lib/respect/less_than_or_equal_to_validator.rb +19 -0
  38. data/lib/respect/less_than_validator.rb +19 -0
  39. data/lib/respect/match_validator.rb +19 -0
  40. data/lib/respect/max_length_validator.rb +20 -0
  41. data/lib/respect/min_length_validator.rb +20 -0
  42. data/lib/respect/multiple_of_validator.rb +10 -0
  43. data/lib/respect/null_schema.rb +26 -0
  44. data/lib/respect/numeric_schema.rb +33 -0
  45. data/lib/respect/org3_dumper.rb +213 -0
  46. data/lib/respect/regexp_schema.rb +19 -0
  47. data/lib/respect/schema.rb +285 -0
  48. data/lib/respect/schema_def.rb +16 -0
  49. data/lib/respect/string_schema.rb +21 -0
  50. data/lib/respect/unit_test_helper.rb +37 -0
  51. data/lib/respect/uri_schema.rb +23 -0
  52. data/lib/respect/utc_time_schema.rb +17 -0
  53. data/lib/respect/validator.rb +51 -0
  54. data/lib/respect/version.rb +3 -0
  55. data/test/any_schema_test.rb +79 -0
  56. data/test/array_def_test.rb +113 -0
  57. data/test/array_schema_test.rb +487 -0
  58. data/test/boolean_schema_test.rb +89 -0
  59. data/test/composite_schema_test.rb +30 -0
  60. data/test/datetime_schema_test.rb +83 -0
  61. data/test/doc_helper_test.rb +34 -0
  62. data/test/doc_parser_test.rb +109 -0
  63. data/test/dsl_dumper_test.rb +395 -0
  64. data/test/fake_name_proxy_test.rb +138 -0
  65. data/test/float_schema_test.rb +146 -0
  66. data/test/format_validator_test.rb +224 -0
  67. data/test/hash_def_test.rb +126 -0
  68. data/test/hash_schema_test.rb +613 -0
  69. data/test/integer_schema_test.rb +142 -0
  70. data/test/ip_addr_schema_test.rb +78 -0
  71. data/test/ipv4_addr_schema_test.rb +71 -0
  72. data/test/ipv6_addr_schema_test.rb +71 -0
  73. data/test/json_schema_html_formatter_test.rb +214 -0
  74. data/test/null_schema_test.rb +46 -0
  75. data/test/numeric_schema_test.rb +294 -0
  76. data/test/org3_dumper_test.rb +784 -0
  77. data/test/regexp_schema_test.rb +54 -0
  78. data/test/respect_test.rb +108 -0
  79. data/test/schema_def_test.rb +405 -0
  80. data/test/schema_test.rb +290 -0
  81. data/test/string_schema_test.rb +209 -0
  82. data/test/support/circle.rb +11 -0
  83. data/test/support/color.rb +24 -0
  84. data/test/support/point.rb +11 -0
  85. data/test/support/respect/circle_schema.rb +16 -0
  86. data/test/support/respect/color_def.rb +19 -0
  87. data/test/support/respect/color_schema.rb +33 -0
  88. data/test/support/respect/point_schema.rb +19 -0
  89. data/test/support/respect/rgba_schema.rb +20 -0
  90. data/test/support/respect/universal_validator.rb +25 -0
  91. data/test/support/respect/user_macros.rb +12 -0
  92. data/test/support/rgba.rb +11 -0
  93. data/test/test_helper.rb +90 -0
  94. data/test/uri_schema_test.rb +54 -0
  95. data/test/utc_time_schema_test.rb +63 -0
  96. data/test/validator_test.rb +22 -0
  97. metadata +288 -0
@@ -0,0 +1,89 @@
1
+ require "test_helper"
2
+
3
+ class BooleanSchemaTest < Test::Unit::TestCase
4
+ def test_boolean_schema_validate_type
5
+ [
6
+ [ "42", nil, "integer in string" ],
7
+ [ { "test" => 42 }, nil, "hash" ],
8
+ [ "true", true, "valid true value" ],
9
+ [ "false", false, "valid false value" ],
10
+ [ true, true, "true" ],
11
+ [ false, false, "false" ],
12
+ [ "nil", nil, "nil in string" ],
13
+ ].each do |data|
14
+ s = Respect::BooleanSchema.new
15
+ # Check validate_type
16
+ if data[1].nil?
17
+ assert_raise(Respect::ValidationError) do
18
+ s.validate_type(data[0])
19
+ end
20
+ else
21
+ assert_equal data[1], s.validate_type(data[0]), data[2]
22
+ end
23
+ # Check sanitized_object
24
+ assert_nil s.sanitized_object
25
+ assert_schema_validation_is (data[1].nil? ? false : true), s, data[0], data[2]
26
+ unless data[1].nil?
27
+ assert_equal data[1], s.sanitized_object, data[2]
28
+ end
29
+ end
30
+ end
31
+
32
+ def test_boolean_schema_accept_constraint_equal_to
33
+ s_true = Respect::BooleanSchema.new equal_to: true
34
+ assert_schema_validation_is true, s_true, "true"
35
+ assert_schema_validation_is false, s_true, "false"
36
+
37
+ s_false = Respect::BooleanSchema.new equal_to: false
38
+ assert_schema_validation_is false, s_false, "true"
39
+ assert_schema_validation_is true, s_false, "false"
40
+ end
41
+
42
+ def test_failed_validation_reset_sanitized_object
43
+ s = Respect::BooleanSchema.define equal_to: true
44
+ assert_schema_validate(s, true)
45
+ assert_equal(true, s.sanitized_object)
46
+ assert_schema_invalidate(s, false)
47
+ assert_equal(nil, s.sanitized_object)
48
+ end
49
+
50
+ def test_allow_nil
51
+ s = Respect::BooleanSchema.new(allow_nil: true)
52
+ assert_schema_validate s, nil
53
+ assert_equal(nil, s.sanitized_object)
54
+ assert_schema_validate s, true
55
+ assert_equal(true, s.sanitized_object)
56
+ assert_schema_validate s, false
57
+ assert_equal(false, s.sanitized_object)
58
+ assert_schema_validate s, "true"
59
+ assert_equal(true, s.sanitized_object)
60
+ assert_schema_validate s, "false"
61
+ assert_equal(false, s.sanitized_object)
62
+ end
63
+
64
+ def test_disallow_nil
65
+ s = Respect::BooleanSchema.new
66
+ assert !s.allow_nil?
67
+ exception = assert_exception(Respect::ValidationError) { s.validate(nil) }
68
+ assert_match exception.message, /\bBooleanSchema\b/
69
+ assert_equal(nil, s.sanitized_object)
70
+ assert_schema_validate s, true
71
+ assert_equal(true, s.sanitized_object)
72
+ assert_schema_validate s, false
73
+ assert_equal(false, s.sanitized_object)
74
+ assert_schema_validate s, "true"
75
+ assert_equal(true, s.sanitized_object)
76
+ assert_schema_validate s, "false"
77
+ assert_equal(false, s.sanitized_object)
78
+ end
79
+
80
+ def test_allow_nil_with_constraint
81
+ s = Respect::BooleanSchema.new(allow_nil: true, equal_to: false)
82
+ assert_schema_validate s, nil
83
+ assert_equal(nil, s.sanitized_object)
84
+ assert_schema_validate s, false
85
+ assert_equal(false, s.sanitized_object)
86
+ assert_schema_invalidate s, true
87
+ assert_equal(nil, s.sanitized_object)
88
+ end
89
+ end
@@ -0,0 +1,30 @@
1
+ require "test_helper"
2
+
3
+ class CompositeSchemaTest < Test::Unit::TestCase
4
+ def test_failed_validation_reset_sanitized_object
5
+ s = Respect::PointSchema.new
6
+ assert_schema_validate(s, { x: 42.5, y: 51.3 })
7
+ assert_equal(Point.new(42.5, 51.3), s.sanitized_object)
8
+ assert_schema_invalidate(s, { x: 42.5 })
9
+ assert_equal(nil, s.sanitized_object)
10
+ end
11
+
12
+ def test_allow_nil
13
+ s = Respect::PointSchema.new(allow_nil: true)
14
+ assert s.allow_nil?
15
+ assert_schema_validate s, nil
16
+ assert_equal(nil, s.sanitized_object)
17
+ assert_schema_validate(s, { x: 42.5, y: 51.3 })
18
+ assert_equal(Point.new(42.5, 51.3), s.sanitized_object)
19
+ end
20
+
21
+ def test_disallow_nil
22
+ s = Respect::PointSchema.new
23
+ assert !s.allow_nil?
24
+ exception = assert_exception(Respect::ValidationError) { s.validate(nil) }
25
+ assert_match exception.message, /\bPointSchema\b/
26
+ assert_equal(nil, s.sanitized_object)
27
+ assert_schema_validate(s, { x: 42.5, y: 51.3 })
28
+ assert_equal(Point.new(42.5, 51.3), s.sanitized_object)
29
+ end
30
+ end
@@ -0,0 +1,83 @@
1
+ require "test_helper"
2
+
3
+ class DatetimeSchemaTest < Test::Unit::TestCase
4
+
5
+ def test_datetime_schema_creates_datetime_object
6
+ s = Respect::DatetimeSchema.new
7
+ assert_nil s.sanitized_object
8
+ assert_schema_validate s, "2013-12-01T00:00:00+00:00"
9
+ assert s.sanitized_object.is_a?(DateTime)
10
+ assert_equal DateTime.rfc3339("2013-12-01T00:00:00+00:00"), s.sanitized_object
11
+ end
12
+
13
+ def test_datetime_schema_relies_on_format_validator
14
+ doc = "2013-12-01T00:00:00+00:00"
15
+ Respect::FormatValidator.any_instance.stubs(:validate_datetime).with(doc).at_least_once
16
+ Respect::DatetimeSchema.new.validate(doc)
17
+ end
18
+
19
+ def test_datetime_schema_validate_iso8601
20
+ t = Time.now.to_datetime
21
+ s = Respect::DatetimeSchema.new
22
+ assert_schema_validate(s, t.iso8601)
23
+ assert_equal t.to_time.to_i, s.sanitized_object.to_time.to_i
24
+ end
25
+
26
+ def test_failed_validation_reset_sanitized_object
27
+ s = Respect::DatetimeSchema.new
28
+ assert_schema_validate(s, "2013-12-01T00:00:00+00:00")
29
+ assert_not_nil(s.sanitized_object)
30
+ assert_schema_invalidate(s, "wrong")
31
+ assert_nil(s.sanitized_object)
32
+ end
33
+
34
+ def test_allow_nil
35
+ s = Respect::DatetimeSchema.new(allow_nil: true)
36
+ assert s.allow_nil?
37
+ assert_schema_validate s, nil
38
+ assert_equal(nil, s.sanitized_object)
39
+ assert_schema_validate s, "2013-12-01T00:00:00+00:00"
40
+ assert_not_nil(s.sanitized_object)
41
+ assert_schema_invalidate(s, "wrong")
42
+ assert_nil(s.sanitized_object)
43
+ end
44
+
45
+ def test_disallow_nil
46
+ s = Respect::DatetimeSchema.new
47
+ assert !s.allow_nil?
48
+ exception = assert_exception(Respect::ValidationError) { s.validate(nil) }
49
+ assert_match exception.message, /\bDatetimeSchema\b/
50
+ assert_equal(nil, s.sanitized_object)
51
+ assert_schema_validate s, "2013-12-01T00:00:00+00:00"
52
+ assert_not_nil(s.sanitized_object)
53
+ assert_schema_invalidate(s, "wrong")
54
+ assert_nil(s.sanitized_object)
55
+ end
56
+
57
+ def test_validate_datetime_object
58
+ s = Respect::DatetimeSchema.new
59
+ t = Time.now.to_datetime
60
+ assert_equal(DateTime, t.class)
61
+ assert_schema_validate s, t
62
+ assert_equal DateTime, s.sanitized_object.class
63
+ assert_equal t, s.sanitized_object
64
+ end
65
+
66
+ def test_validate_time_object
67
+ s = Respect::DatetimeSchema.new
68
+ t = Time.now
69
+ assert_equal(Time, t.class)
70
+ assert_schema_validate s, t
71
+ assert_equal DateTime, s.sanitized_object.class
72
+ assert_equal t.to_datetime, s.sanitized_object
73
+ end
74
+
75
+ def test_validate_date_object
76
+ s = Respect::DatetimeSchema.new
77
+ t = Time.now.to_date
78
+ assert_equal(Date, t.class)
79
+ assert_schema_validate s, t
80
+ assert_equal Date, s.sanitized_object.class
81
+ assert_equal t.to_date, s.sanitized_object
82
+ end
83
+ end
@@ -0,0 +1,34 @@
1
+ require "test_helper"
2
+
3
+ class DocHelperTest < Test::Unit::TestCase
4
+ def test_schema_title_use_doc_parser
5
+ object = mock()
6
+ object.extend(Respect::DocHelper)
7
+ doc = "Hey!"
8
+ doc.expects(:to_s).returns(doc).once
9
+ object.stubs(:documentation).returns(doc)
10
+ Respect::DocParser.any_instance.stubs(:parse).with(doc).returns(Respect::DocParser.new).at_least_once
11
+ Respect::DocParser.any_instance.stubs(:title).returns("title").at_least_once
12
+ assert_equal "title", object.title
13
+ end
14
+
15
+ def test_schema_description_use_doc_parser
16
+ object = mock()
17
+ object.extend(Respect::DocHelper)
18
+ doc = "Hey!"
19
+ doc.expects(:to_s).returns(doc).once
20
+ object.stubs(:documentation).returns(doc)
21
+ Respect::DocParser.any_instance.stubs(:parse).with(doc).returns(Respect::DocParser.new).at_least_once
22
+ Respect::DocParser.any_instance.stubs(:description).returns("desc").at_least_once
23
+ assert_equal "desc", object.description
24
+ end
25
+
26
+ def test_nil_title_and_description_if_nil_doc
27
+ object = mock()
28
+ object.extend(Respect::DocHelper)
29
+ object.stubs(:documentation).returns(nil)
30
+ assert_nil object.documentation
31
+ assert_nil object.title
32
+ assert_nil object.description
33
+ end
34
+ end
@@ -0,0 +1,109 @@
1
+ require "test_helper"
2
+
3
+ class DocParserTest < Test::Unit::TestCase
4
+ def setup
5
+ @parser = Respect::DocParser.new
6
+ end
7
+
8
+ def test_full_doc
9
+ doc = <<-EOS.strip_heredoc
10
+ A title.
11
+
12
+ A description.
13
+ A long description.
14
+ EOS
15
+ @parser.parse(doc)
16
+ assert_equal "A title.", @parser.title
17
+ assert_equal "A description.\nA long description.\n", @parser.description
18
+ end
19
+
20
+ def test_no_title
21
+ doc = <<-EOS.strip_heredoc
22
+ A description.
23
+ A long description.
24
+ EOS
25
+ @parser.parse(doc)
26
+ assert_equal nil, @parser.title
27
+ assert_equal "A description.\nA long description.\n", @parser.description
28
+ end
29
+
30
+ def test_no_desc
31
+ doc = <<-EOS.strip_heredoc
32
+ A title.
33
+ EOS
34
+ @parser.parse(doc)
35
+ assert_equal "A title.", @parser.title
36
+ assert_equal nil, @parser.description
37
+ end
38
+
39
+ def test_no_desc_and_no_newline
40
+ doc = <<-EOS.strip_heredoc
41
+ A title.
42
+
43
+ A sparse...
44
+
45
+ ... description.
46
+ EOS
47
+ @parser.parse(doc)
48
+ assert_equal "A title.", @parser.title
49
+ desc = <<-EOS.strip_heredoc
50
+ A sparse...
51
+
52
+ ... description.
53
+ EOS
54
+ assert_equal desc, @parser.description
55
+ end
56
+
57
+ def test_no_title_but_long_desc
58
+ doc = <<-EOS.strip_heredoc
59
+ A first paragraph...
60
+ ...on several lines.
61
+
62
+ A second one...
63
+ ... on several lines.
64
+
65
+
66
+ And a far away third one...
67
+ ... on several lines.
68
+ EOS
69
+ @parser.parse(doc)
70
+ assert_equal nil, @parser.title
71
+ assert_equal doc, @parser.description
72
+ end
73
+
74
+ def test_title_and_empty_desc
75
+ doc = <<-EOS.strip_heredoc
76
+ A title with many empty line for desc
77
+
78
+
79
+
80
+
81
+ EOS
82
+ @parser.parse(doc)
83
+ assert_equal "A title with many empty line for desc", @parser.title
84
+ assert_equal nil, @parser.description
85
+ end
86
+
87
+ def test_title_separated_by_desc_with_many_lines
88
+ doc = <<-EOS.strip_heredoc
89
+ A title with many empty line for desc
90
+
91
+
92
+
93
+
94
+ The description finally...
95
+ come.
96
+ EOS
97
+ @parser.parse(doc)
98
+ assert_equal "A title with many empty line for desc", @parser.title
99
+ assert_equal "The description finally...\ncome.\n", @parser.description
100
+ end
101
+
102
+ def test_title_with_no_newline
103
+ doc = "a title"
104
+ @parser.parse(doc)
105
+ assert_equal "a title", @parser.title
106
+ assert_equal nil, @parser.description
107
+ end
108
+
109
+ end
@@ -0,0 +1,395 @@
1
+ require "test_helper"
2
+
3
+ class DslDumperTest < Test::Unit::TestCase
4
+ def test_dump_nested_hashs
5
+ assert_bijective_dump do
6
+ <<-EOF.strip_heredoc
7
+ Respect::Schema.define do |s|
8
+ s.hash do |s|
9
+ s.hash "o" do |s|
10
+ s.integer "i"
11
+ end
12
+ end
13
+ end
14
+ EOF
15
+ end
16
+ end
17
+
18
+ def test_dump_hash_with_multiple_integer
19
+ assert_bijective_dump do
20
+ <<-EOF.strip_heredoc
21
+ Respect::Schema.define do |s|
22
+ s.hash do |s|
23
+ s.integer "i1"
24
+ s.integer "i2"
25
+ s.integer "i3"
26
+ end
27
+ end
28
+ EOF
29
+ end
30
+ end
31
+
32
+ def test_dump_nested_hash_with_multiple_integer
33
+ assert_bijective_dump do
34
+ <<-EOF.strip_heredoc
35
+ Respect::Schema.define do |s|
36
+ s.hash do |s|
37
+ s.hash "o1" do |s|
38
+ s.integer "i1"
39
+ s.integer "i2"
40
+ s.integer "i3"
41
+ end
42
+ s.hash "o2" do |s|
43
+ s.integer "i1"
44
+ s.integer "i2"
45
+ s.integer "i3"
46
+ end
47
+ s.hash "o3" do |s|
48
+ s.integer "i1"
49
+ s.integer "i2"
50
+ s.integer "i3"
51
+ end
52
+ end
53
+ end
54
+ EOF
55
+ end
56
+ end
57
+
58
+ def test_property_name_are_escaped
59
+ assert_bijective_dump do
60
+ <<-EOF.strip_heredoc
61
+ Respect::Schema.define do |s|
62
+ s.hash do |s|
63
+ s.string "s\\""
64
+ end
65
+ end
66
+ EOF
67
+ end
68
+ end
69
+
70
+ def test_dump_terminal_statement
71
+ PRIMITIVE_STATEMENTS_LIST.each do |statement|
72
+ assert_bijective_dump("dump statement #{statement}") do
73
+ <<-EOF.strip_heredoc
74
+ Respect::Schema.define do |s|
75
+ s.hash do |s|
76
+ s.#{statement} "property_name"
77
+ end
78
+ end
79
+ EOF
80
+ end
81
+ end
82
+ end
83
+
84
+ def test_dump_array_item
85
+ PRIMITIVE_STATEMENTS_LIST.each do |statement|
86
+ assert_bijective_dump("dump statement #{statement}") do
87
+ <<-EOF.strip_heredoc
88
+ Respect::Schema.define do |s|
89
+ s.array do |s|
90
+ s.#{statement}
91
+ end
92
+ end
93
+ EOF
94
+ end
95
+ end
96
+ end
97
+
98
+ def test_dump_array_items
99
+ PRIMITIVE_STATEMENTS_LIST.each do |statement|
100
+ assert_bijective_dump("dump statement #{statement}") do
101
+ <<-EOF.strip_heredoc
102
+ Respect::Schema.define do |s|
103
+ s.array do |s|
104
+ s.items do |s|
105
+ s.#{statement}
106
+ s.#{statement}
107
+ end
108
+ end
109
+ end
110
+ EOF
111
+ end
112
+ end
113
+ end
114
+
115
+ def test_dump_array_extra_items
116
+ PRIMITIVE_STATEMENTS_LIST.each do |statement|
117
+ assert_bijective_dump("dump statement #{statement}") do
118
+ <<-EOF.strip_heredoc
119
+ Respect::Schema.define do |s|
120
+ s.array do |s|
121
+ s.extra_items do |s|
122
+ s.#{statement}
123
+ s.#{statement}
124
+ end
125
+ end
126
+ end
127
+ EOF
128
+ end
129
+ end
130
+ end
131
+
132
+ def test_dump_array_items_and_extra_items
133
+ PRIMITIVE_STATEMENTS_LIST.each do |statement|
134
+ assert_bijective_dump("dump statement #{statement}") do
135
+ <<-EOF.strip_heredoc
136
+ Respect::Schema.define do |s|
137
+ s.array do |s|
138
+ s.items do |s|
139
+ s.#{statement}
140
+ s.#{statement}
141
+ s.#{statement}
142
+ end
143
+ s.extra_items do |s|
144
+ s.#{statement}
145
+ s.#{statement}
146
+ s.#{statement}
147
+ end
148
+ end
149
+ end
150
+ EOF
151
+ end
152
+ end
153
+ end
154
+
155
+ def test_dump_nested_array_with_item
156
+ assert_bijective_dump do
157
+ <<-EOF.strip_heredoc
158
+ Respect::Schema.define do |s|
159
+ s.array do |s|
160
+ s.array do |s|
161
+ s.array do |s|
162
+ s.array do |s|
163
+ s.array do |s|
164
+ s.integer
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
171
+ EOF
172
+ end
173
+ end
174
+
175
+ def test_dump_nested_array_with_items
176
+ assert_bijective_dump do
177
+ <<-EOF.strip_heredoc
178
+ Respect::Schema.define do |s|
179
+ s.array do |s|
180
+ s.items do |s|
181
+ s.integer
182
+ s.array do |s|
183
+ s.items do |s|
184
+ s.integer
185
+ s.array do |s|
186
+ s.integer
187
+ end
188
+ end
189
+ end
190
+ end
191
+ end
192
+ end
193
+ EOF
194
+ end
195
+ end
196
+
197
+ def test_dump_nested_array_with_extra_items
198
+ assert_bijective_dump do
199
+ <<-EOF.strip_heredoc
200
+ Respect::Schema.define do |s|
201
+ s.array do |s|
202
+ s.extra_items do |s|
203
+ s.integer
204
+ s.array do |s|
205
+ s.extra_items do |s|
206
+ s.integer
207
+ s.array do |s|
208
+ s.integer
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
215
+ EOF
216
+ end
217
+ end
218
+
219
+ def test_dump_nested_array_and_hash
220
+ assert_bijective_dump do
221
+ <<-EOF.strip_heredoc
222
+ Respect::Schema.define do |s|
223
+ s.array do |s|
224
+ s.hash do |s|
225
+ s.integer "i"
226
+ s.array "a" do |s|
227
+ s.hash do |s|
228
+ s.integer "i"
229
+ s.array "a" do |s|
230
+ s.integer
231
+ end
232
+ s.string "s"
233
+ end
234
+ end
235
+ s.string "s"
236
+ end
237
+ end
238
+ end
239
+ EOF
240
+ end
241
+ end
242
+
243
+ def test_dump_terminal_schema
244
+ PRIMITIVE_STATEMENTS_LIST.each do |statement|
245
+ assert_bijective_dump("dump statement #{statement}") do
246
+ <<-EOF.strip_heredoc
247
+ Respect::Schema.define do |s|
248
+ s.#{statement}
249
+ end
250
+ EOF
251
+ end
252
+ end
253
+ end
254
+
255
+ def test_dump_format_helper_statement
256
+ FORMAT_HELPER_STATEMENTS_LIST.each do |statement|
257
+ schema = Respect::Schema.define do |s|
258
+ s.__send__(statement)
259
+ end
260
+ expected_result = <<-EOF.strip_heredoc
261
+ Respect::Schema.define do |s|
262
+ s.string :format => #{statement.inspect}
263
+ end
264
+ EOF
265
+ assert_equal(expected_result,
266
+ Respect::DslDumper.new(schema).dump,
267
+ "dump for statement #{statement}")
268
+ end
269
+ end
270
+
271
+ def test_dump_statement_with_no_names_and_several_options
272
+ assert_bijective_dump do
273
+ <<-EOF.strip_heredoc
274
+ Respect::Schema.define do |s|
275
+ s.integer :greater_than => 42, :equal_to => 51
276
+ end
277
+ EOF
278
+ end
279
+ end
280
+
281
+ def test_dump_statement_with_name_and_several_options
282
+ assert_bijective_dump do
283
+ <<-EOF.strip_heredoc
284
+ Respect::Schema.define do |s|
285
+ s.hash do |s|
286
+ s.integer "an_int", :greater_than => 42, :equal_to => 51
287
+ end
288
+ end
289
+ EOF
290
+ end
291
+ end
292
+
293
+ def test_dump_statement_with_name_and_no_options
294
+ assert_bijective_dump do
295
+ <<-EOF.strip_heredoc
296
+ Respect::Schema.define do |s|
297
+ s.hash do |s|
298
+ s.integer "an_int"
299
+ end
300
+ end
301
+ EOF
302
+ end
303
+ end
304
+
305
+ def test_dump_primitive_statement_documentation
306
+ PRIMITIVE_STATEMENTS_LIST.each do |statement|
307
+ assert_bijective_dump("dump statement #{statement}") do
308
+ <<-EOF.strip_heredoc
309
+ Respect::Schema.define do |s|
310
+ s.doc <<-EOS.strip_heredoc
311
+ a title
312
+
313
+ a description
314
+ EOS
315
+ s.#{statement}
316
+ end
317
+ EOF
318
+ end
319
+ end
320
+ end
321
+
322
+ def test_dump_primitive_statement_with_no_description
323
+ PRIMITIVE_STATEMENTS_LIST.each do |statement|
324
+ assert_bijective_dump("dump statement #{statement}") do
325
+ <<-EOF.strip_heredoc
326
+ Respect::Schema.define do |s|
327
+ s.doc "a title"
328
+ s.#{statement}
329
+ end
330
+ EOF
331
+ end
332
+ end
333
+ end
334
+
335
+ def test_dump_primitive_statement_with_no_title
336
+ PRIMITIVE_STATEMENTS_LIST.each do |statement|
337
+ assert_bijective_dump("dump statement #{statement}") do
338
+ <<-EOF.strip_heredoc
339
+ Respect::Schema.define do |s|
340
+ s.doc <<-EOS.strip_heredoc
341
+ a long...
342
+ ... description
343
+ EOS
344
+ s.#{statement}
345
+ end
346
+ EOF
347
+ end
348
+ end
349
+ end
350
+
351
+ def test_dump_hash_documentation
352
+ assert_bijective_dump do
353
+ <<-EOF.strip_heredoc
354
+ Respect::Schema.define do |s|
355
+ s.doc <<-EOS.strip_heredoc
356
+ a title
357
+
358
+ a description
359
+ EOS
360
+ s.hash do |s|
361
+ s.integer "i"
362
+ s.string "s"
363
+ end
364
+ end
365
+ EOF
366
+ end
367
+ end
368
+
369
+ def test_dump_array_documentation
370
+ assert_bijective_dump do
371
+ <<-EOF.strip_heredoc
372
+ Respect::Schema.define do |s|
373
+ s.doc <<-EOS.strip_heredoc
374
+ a title
375
+
376
+ a description
377
+ EOS
378
+ s.array do |s|
379
+ s.integer
380
+ end
381
+ end
382
+ EOF
383
+ end
384
+ end
385
+
386
+ private
387
+
388
+ def assert_bijective_dump(message = nil, &block)
389
+ source = block.call
390
+ schema = eval(source, block.binding,
391
+ block.source_location.first, block.source_location[1] + 2)
392
+ assert schema.is_a?(Respect::Schema), "#{schema.class} should Respect::Schema"
393
+ assert_equal(source, Respect::DslDumper.new(schema).dump, message)
394
+ end
395
+ end