Class: CabezaDeTermo::JsonSpec::DefaultLibraryInitializer

Inherits:
LibraryInitializer show all
Defined in:
lib/cabeza-de-termo/json-spec/expectations-library/initializers/default-library-initializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new_libraryObject



13
14
15
# File 'lib/cabeza-de-termo/json-spec/expectations-library/initializers/default-library-initializer.rb', line 13

def self.new_library()
	self.new.new_library
end

Instance Method Details

#define_library(library) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/cabeza-de-termo/json-spec/expectations-library/initializers/default-library-initializer.rb', line 22

def define_library(library)
	library.define do

		# Define expectations
		expectations do
			define :to_exist do
				with_block { |value_holder, expected_value| value_holder.not_missing_value? }
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' exists."
			end

			define :to_be_absent do
				negating :to_exist
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is absent."
			end

			define :to_be_kind_of do
				with_block { |value_holder, expected_class| value_holder.value.kind_of? expected_class }
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is a <%= expectation.args[0] %>."
			end

			define :to_be_true do
				expecting :to_be_kind_of, ::TrueClass
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is true."
			end

			define :to_be_false do
				expecting :to_be_kind_of, ::FalseClass
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is false."
			end

			define :to_be_boolean do
				expecting_any_of :to_be_true
				or_also :to_be_true

				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is a boolean."
			end

			define :to_be_float do
				expecting :to_be_kind_of, ::Float
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is a float."
			end

			define :to_be_integer do
				expecting :to_be_kind_of, ::Integer
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is an integer."
			end

			define :to_be_list do
				expecting :to_be_kind_of, ::Array
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is a list."
			end

			define :to_be_number do
				expecting :to_be_kind_of, ::Numeric
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is a number."
			end

			define :to_be_scalar do
				with_class IsScalarExpectation
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is a scalar."
			end

			define :to_be_object do
				expecting :to_be_kind_of, ::Hash
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is an object."
			end

			define :to_be_string do
				expecting :to_be_kind_of, ::String
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is a string."
			end

			define :to_be_null do
				with_block { |value_holder, expected_value| value_holder.value.nil? }
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is null."
			end

			define :not_null do
				negating :to_be_null
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is not null."
			end

			define :to_be_defined do
				expecting_all_of :to_exist
				and_also :not_null

				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is defined."
			end

			define :not_defined do
				negating :to_be_defined
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is not defined."
			end

			define :to_be_empty do
				with_block { |value_holder, expected_value| value_holder.is_empty? }
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is empty."
			end

			define :not_empty do
				negating :to_be_empty
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is not empty."
			end

			define :_to_be_blank do
				with_block { |value_holder, expected_value| value_holder.value.strip.empty? }
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is blank."
			end

			define :to_be_blank do
				expecting_all_of :to_be_string
				and_also :_to_be_blank

				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is blank."
			end

			define :not_blank do
				negating :to_be_blank
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is not blank."
			end

			define :to_be_equal_to do
				with_block { |value_holder, expected_value| expected_value == value_holder.value }
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is equal to '<%= expectation.args[0] %>'."
			end

			define :not_equal_to do
				negating :to_be_equal_to
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is not equal to '<%= expectation.args[0] %>'."
			end

			define :_to_match do
				with_block do |value_holder, expected_regex| 
					expected_regex.match(value_holder.value) != nil
				end

				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' matches '<%= expectation.args[0] %>'."
			end

			define :to_match do
				expecting_all_of :to_be_string
				and_also :_to_match

				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' matches '<%= expectation.child_expectations[1].args[0] %>'."
			end

			define :not_to_match do
				negating :to_match
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' does not match '<%= expectation.child_expectations[1].args[0] %>'."
			end

			define :to_be_string_as_integer do
				expecting :to_match, /^[-+]?\d+$/
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' has a valid integer format."
			end

			define :to_be_string_as_float do
				expecting :to_match, /^[-+]?[0-9]*\.?[0-9]+$/
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' has a valid float format."
			end

			define :to_be_in do
				with_block do |value_holder, possible_values|
					possible_values.include?( value_holder.value )
				end

				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is equal to any of <%= expectation.args[0] %>."
			end

			define :not_in do
				negating :to_be_in
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is not equal to any of <%= expectation.args[0] %>."
			end

			define :to_be_email do
				with_class IsEmailExpectation
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is an email."
			end

			define :to_be_url do
				with_class IsUrlExpectation
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is an url."
			end

			define :to_be_in_range do
				with_block do |value_holder, expectedRange|
					expectedRange.includes?( value_holder.value )
				end

				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is in range <%= expectation.args[0] %>."
			end

			define :not_in_range do
				negating :to_be_in_range
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is not in range <%= expectation.args[0] %>."
			end

			define :to_be_between do
				with_block do |value_holder, min, max| 
					Range::new_from('[', min, max, ']').includes?( value_holder.value )
				end

				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is between <%= Range.new(expectation.args[0], expectation.args[1]).to_s %>."
			end

			define :not_between do
				negating :to_be_between
				message "Failed asserting that the field '<%= field %>' with value = '<%= format value %>' is not between <%= Range.new(expectation.args[0], expectation.args[1]).to_s %>."
			end
		end

		# Define modifiers
		modifiers do
			define :can_be_absent do
				with_class CanBeAbsentModifier
			end

			define :can_be_null do
				with_class CanBeNullModifier
			end

			define :can_be_undefined do
				composing :can_be_null
				with :can_be_absent
			end
		end
	
		# Define default expectations
		default_expectations do
			for_every_object do
				to_be_object
			end

			for_every_list do
				to_be_list
			end

			for_every_scalar do
				to_be_scalar
			end
		end
	end

	library
end

#new_libraryObject



17
18
19
20
# File 'lib/cabeza-de-termo/json-spec/expectations-library/initializers/default-library-initializer.rb', line 17

def new_library()
	library = ExpectationsLibrary.new
	define_library(library)
end