Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/support/matchers')
-rw-r--r--spec/support/matchers/be_sorted.rb71
-rw-r--r--spec/support/matchers/be_valid_json.rb16
-rw-r--r--spec/support/matchers/schema_matcher.rb10
3 files changed, 74 insertions, 23 deletions
diff --git a/spec/support/matchers/be_sorted.rb b/spec/support/matchers/be_sorted.rb
index 1455060fe71..b0ab93efbb2 100644
--- a/spec/support/matchers/be_sorted.rb
+++ b/spec/support/matchers/be_sorted.rb
@@ -4,18 +4,75 @@
#
# By default, this checks that the collection is sorted ascending
# but you can check order by specific field and order by passing
-# them, eg:
+# them, either as arguments, or using the fluent interface, eg:
#
# ```
+# # Usage examples:
+# expect(collection).to be_sorted
+# expect(collection).to be_sorted(:field)
# expect(collection).to be_sorted(:field, :desc)
+# expect(collection).to be_sorted.asc
+# expect(collection).to be_sorted.desc.by(&:field)
+# expect(collection).to be_sorted.by(&:field).desc
+# expect(collection).to be_sorted.by { |x| [x.foo, x.bar] }
# ```
-RSpec::Matchers.define :be_sorted do |by, order = :asc|
+RSpec::Matchers.define :be_sorted do |on = :itself, order = :asc|
+ def by(&block)
+ @comparator = block
+ self
+ end
+
+ def asc
+ @direction = :asc
+ self
+ end
+
+ def desc
+ @direction = :desc
+ self
+ end
+
+ def format_with(proc)
+ @format_with = proc
+ self
+ end
+
+ define_method :comparator do
+ @comparator || on
+ end
+
+ define_method :descending? do
+ (@direction || order.to_sym) == :desc
+ end
+
+ def order(items)
+ descending? ? items.reverse : items
+ end
+
+ def sort(items)
+ items.sort_by(&comparator)
+ end
+
match do |actual|
- next true unless actual.present? # emtpy collection is sorted
+ next true unless actual.present? # empty collection is sorted
+
+ actual = actual.to_a if actual.respond_to?(:to_a) && !actual.respond_to?(:sort_by)
+
+ @got = actual
+ @expected = order(sort(actual))
+
+ @expected == actual
+ end
+
+ def failure_message
+ "Expected #{show(@expected)}, got #{show(@got)}"
+ end
- actual
- .then { |collection| by ? collection.sort_by(&by) : collection.sort }
- .then { |sorted_collection| order.to_sym == :desc ? sorted_collection.reverse : sorted_collection }
- .then { |sorted_collection| sorted_collection == actual }
+ def show(things)
+ if @format_with
+ things.map(&@format_with)
+ else
+ things
+ end
end
end
diff --git a/spec/support/matchers/be_valid_json.rb b/spec/support/matchers/be_valid_json.rb
index f46c35c7198..228c1fc986e 100644
--- a/spec/support/matchers/be_valid_json.rb
+++ b/spec/support/matchers/be_valid_json.rb
@@ -1,20 +1,8 @@
# frozen_string_literal: true
RSpec::Matchers.define :be_valid_json do
- def according_to_schema(schema)
- @schema = schema
- self
- end
-
match do |actual|
- data = Gitlab::Json.parse(actual)
-
- if @schema.present?
- @validation_errors = JSON::Validator.fully_validate(@schema, data)
- @validation_errors.empty?
- else
- data.present?
- end
+ Gitlab::Json.parse(actual).present?
rescue JSON::ParserError => e
@error = e
false
@@ -23,8 +11,6 @@ RSpec::Matchers.define :be_valid_json do
def failure_message
if @error
"Parse failed with error: #{@error}"
- elsif @validation_errors.present?
- "Validation failed because #{@validation_errors.join(', and ')}"
else
"Parsing did not return any data"
end
diff --git a/spec/support/matchers/schema_matcher.rb b/spec/support/matchers/schema_matcher.rb
index ebbd57c8115..f0e7a52c51e 100644
--- a/spec/support/matchers/schema_matcher.rb
+++ b/spec/support/matchers/schema_matcher.rb
@@ -2,6 +2,8 @@
module SchemaPath
def self.expand(schema, dir = nil)
+ return schema unless schema.is_a?(String)
+
if Gitlab.ee? && dir.nil?
ee_path = expand(schema, 'ee')
@@ -35,7 +37,13 @@ RSpec::Matchers.define :match_schema do |schema, dir: nil, **options|
end
failure_message do |response|
- "didn't match the schema defined by #{SchemaPath.expand(schema, dir)}" \
+ "didn't match the schema defined by #{schema_name(schema, dir)}" \
" The validation errors were:\n#{@errors.join("\n")}"
end
+
+ def schema_name(schema, dir)
+ return 'provided schema' unless schema.is_a?(String)
+
+ SchemaPath.expand(schema, dir)
+ end
end