Class: CabezaDeTermo::JsonSpec::JsonExpectationsRunner

Inherits:
ExpressionWalker show all
Includes:
ValueHoldersStackBehaviour
Defined in:
lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb

Direct Known Subclasses

JsonExpectationsReporter, JsonValidator

Instance Method Summary collapse

Methods included from ValueHoldersStackBehaviour

#initialize, #new_value_holder_on, #pop_value_holder, #push_value_holder, #walk_with_json, #walk_with_value_holder, #with_value_holder

Methods inherited from ExpressionWalker

#supressing

Instance Method Details

#has_errors?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 22

def has_errors?()
	!no_errors?
end

#in_isolation(&block) ⇒ Object

Walking expressions



52
53
54
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 52

def in_isolation(&block)
	supressing ValidationError, &block
end

#in_isolation_walk_each_field(json_each_field, field_name) ⇒ Object



124
125
126
127
128
129
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 124

def in_isolation_walk_each_field(json_each_field, field_name)
	in_isolation do
		walk_each_field_name(json_each_field, field_name)
		walk_each_field_value(json_each_field, field_name)
	end
end

#in_isolation_walk_on(json_walkable) ⇒ Object



56
57
58
59
60
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 56

def in_isolation_walk_on(json_walkable)
	in_isolation do
		walk_on(json_walkable)
	end
end

#new_json_expectations_runnerObject

Instance creation



46
47
48
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 46

def new_json_expectations_runner()
	CdT.subclass_responsibility
end

#no_errors?Boolean

Answer true if all the walked Expectations was satisfied.

Returns:

  • (Boolean)

    boolean



18
19
20
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 18

def no_errors?()
	CdT.subclass_responsibility
end

#on_json_any_of_failed_with_all_runners(expectations_runners) ⇒ Object



36
37
38
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 36

def on_json_any_of_failed_with_all_runners(expectations_runners)
	CdT.subclass_responsibility
end

#on_json_any_of_was_satisfied_with_runner(expectations_runner) ⇒ Object



32
33
34
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 32

def on_json_any_of_was_satisfied_with_runner(expectations_runner)
	CdT.subclass_responsibility
end

#on_unwalked_field(field_name, value_holder) ⇒ Object

Hooks



28
29
30
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 28

def on_unwalked_field(field_name, value_holder)
	CdT.subclass_responsibility
end

#on_walked_expectation(expectation, value_holder, was_satisfied) ⇒ Object



40
41
42
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 40

def on_walked_expectation(expectation, value_holder, was_satisfied)
	CdT.subclass_responsibility
end

#raise_validation_error(expectation) ⇒ Object

Raises:



205
206
207
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 205

def raise_validation_error(expectation)
	raise ValidationError.new( expectation.failed_message_on(@value_holder) )
end

#unexpected_fields_from_current_value_holder(walked_fields) ⇒ Object



209
210
211
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 209

def unexpected_fields_from_current_value_holder(walked_fields)
	@value_holder.field_names.reject { |field| walked_fields.include?(field) }
end

#walk_each_defined_field_of(json_object) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 87

def walk_each_defined_field_of(json_object)
	walked_fields = []

	json_object.fields.each do |field|
		with_value_holder( @value_holder.value_holder_on_field_named(field.name) ) do
			in_isolation_walk_on(field)
		end

		walked_fields << field.name
	end

	walked_fields
end

#walk_each_field_name(json_each_field, field_name) ⇒ Object



101
102
103
104
105
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 101

def walk_each_field_name(json_each_field, field_name)
	with_value_holder( @value_holder.new_value_holder(field_name, ':' + field_name) ) do
		walk_on(json_each_field.name_expression)
	end
end

#walk_each_field_value(json_each_field, field_name) ⇒ Object



107
108
109
110
111
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 107

def walk_each_field_value(json_each_field, field_name)
	with_value_holder( @value_holder.value_holder_on_field_named(field_name) ) do
		in_isolation_walk_on(json_each_field.value_expression)
	end
end

#walk_expectation(expectation) ⇒ Object



187
188
189
190
191
192
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 187

def walk_expectation(expectation)
	was_satisfied = expectation.is_satisfied_by?(@value_holder)
	on_walked_expectation(expectation, @value_holder, was_satisfied)

	raise_validation_error(expectation) unless was_satisfied
end

#walk_expectation_runner(expectation_runner) ⇒ Object



183
184
185
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 183

def walk_expectation_runner(expectation_runner)
	expectation_runner.accept_walker_with_value_holder(self, @value_holder)
end

#walk_expectations_of(expression) ⇒ Object



178
179
180
181
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 178

def walk_expectations_of(expression)
	# Intentionaly dont go through :walk_on to let the SkipBranchException errors bubble up
	walk_expectation_runner( expression.expectations_runner )
end

#walk_json_any_of(any_of) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 165

def walk_json_any_of(any_of)
	sub_walkers = []
	any_of.possible_expressions.each do |possible_expression|
		sub_walkers << walk_with_new_walker(possible_expression)
	end

	sub_walkers.each do |each_sub_walker|
		return on_json_any_of_was_satisfied_with_runner(each_sub_walker) if each_sub_walker.no_errors?
	end

	on_json_any_of_failed_with_all_runners(sub_walkers)
end

#walk_json_anything(json_anything) ⇒ Object



157
158
159
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 157

def walk_json_anything(json_anything)
	walk_expectations_of(json_anything)
end

#walk_json_each(json_each) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 141

def walk_json_each(json_each)
	walk_expectations_of(json_each)

	return if json_each.no_each_item_expression?

	(0...@value_holder.length).each do |i|
		with_value_holder( @value_holder.value_holder_at_index(i) ) do
			in_isolation_walk_on(json_each.each_item_expression)
		end
	end
end

#walk_json_each_field(json_each_field) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 113

def walk_json_each_field(json_each_field)
	walked_fields = []

	@value_holder.field_names.each do |field_name|
		walked_fields << field_name
		in_isolation_walk_each_field(json_each_field, field_name)
	end

	walked_fields
end

#walk_json_field(json_field) ⇒ Object



153
154
155
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 153

def walk_json_field(json_field)
	walk_on(json_field.value_expression)
end

#walk_json_field_name(json_field_name) ⇒ Object



131
132
133
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 131

def walk_json_field_name(json_field_name)
	walk_expectations_of(json_field_name)
end

#walk_json_list(json_list_expression) ⇒ Object



135
136
137
138
139
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 135

def walk_json_list(json_list_expression)
	walk_expectations_of(json_list_expression)

	walk_on(json_list_expression.each_expression)
end

#walk_json_object(json_object) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 72

def walk_json_object(json_object)
	walk_expectations_of(json_object)

	walked_fields = walk_each_defined_field_of(json_object)

	if( json_object.has_each_field? )
		more_walked_fields = walk_json_each_field(json_object.each_field_expression)
		walked_fields = walked_fields + more_walked_fields
	end

	unexpected_fields_from_current_value_holder(walked_fields).each do |field_name|
		on_unwalked_field(field_name, @value_holder)
	end
end

#walk_json_scalar(json_scalar) ⇒ Object



161
162
163
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 161

def walk_json_scalar(json_scalar)
	walk_expectations_of(json_scalar)
end

#walk_json_spec(json_spec) ⇒ Object



68
69
70
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 68

def walk_json_spec(json_spec)
	in_isolation_walk_on(json_spec.root_expression)
end

#walk_on(json_walkable) ⇒ Object



62
63
64
65
66
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 62

def walk_on(json_walkable)
	supressing SkipBranchSignal do
		super(json_walkable)
	end
end

#walk_with_new_walker(expression) ⇒ Object

Helpers



196
197
198
199
200
201
202
203
# File 'lib/cabeza-de-termo/json-spec/walkers/json-expectations-runner.rb', line 196

def walk_with_new_walker(expression)
	expectations_runner = new_json_expectations_runner
	in_isolation do
		expectations_runner.walk_with_value_holder(expression, @value_holder)
	end

	expectations_runner
end