rspec-api-matchers 0.1.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 480e18da2657efd7c3645a753ce1cc105c8bd4d4
4
- data.tar.gz: fb4600f19714aa5eb6f0a49b4e45bcc05be3d1ca
3
+ metadata.gz: 52d99afa65e3dc027e30e06fe06e5abdb6ca7b77
4
+ data.tar.gz: 670f2f96336bf3b7ffdd41434f7a756fc358da4c
5
5
  SHA512:
6
- metadata.gz: dfc14d736db8c6d4f9c059d32ab0012c579c585a2b903ae2cf8920211be1ed95f5b42c07e4d56a710c8357df97d50fb7539ee8ea97d05bc030f2c8846adca79b
7
- data.tar.gz: 12d82522d32fb0154b6e5e7c20464a2c01e47b4adb863a2475db3aa0ca8a584db430afb1ba84556240b8ab76398bf2b1a911f88d9b2196578f5a42137e7850a7
6
+ metadata.gz: 76fcda35ccae07f245892ec5773f20dc43c875714cf2bda6f7f44efdcafafc98e493739b2d4e0c7056d91f3aabe1055397d95c3e278ec3bc11983e345fa397db
7
+ data.tar.gz: 8e71adfec9954e778684d3ecce2ac4bce83c9319cb7a5f0acd7dfaf9efa6363d8ddc60101d646aa43a30c08ee5b8869cd9d4069ac4ed525e3eab927d68ff01dc
data/README.md CHANGED
@@ -18,13 +18,20 @@ Install
18
18
  Add `gem 'rspec-api-matchers'` to your `Gemfile` and run `bundle`.
19
19
  Or install yourself by running `gem install rspec-api-matchers`.
20
20
 
21
+ Version changes respect [Semantic Versioning](http://semver.org).
22
+ To ensure `bundle update` won't cause problems in your repository,
23
+ always indicate the patch version until version 1.0.0 is released, e.g.:
24
+
25
+ gem 'rspec-api-matchers', '~> 0.5.0'
26
+
27
+
21
28
  Available matchers
22
29
  ------------------
23
30
 
24
31
  expect(response).to have_status(:ok)
25
32
  expect(response).to include_content_type(:json)
26
33
  expect(response).to have_prev_page_link
27
- expect(response).to be_valid_json(Array)
34
+ expect(response).to be_a_collection
28
35
  expect(response).to be_a_jsonp('alert')
29
36
  expect(response).to be_sorted(by: :id, verse: :desc)
30
37
  expect(response).to be_filtered(10, by: :id)
@@ -0,0 +1,24 @@
1
+ require 'rspec-api/matchers/collection'
2
+
3
+ module RSpecApi
4
+ module Matchers
5
+ # Convert RSpecAPI::Matchers classes into RSpec-compatible matchers, e.g.:
6
+ # makes RSpecApi::Matchers::Status available as expect(...).to match_status
7
+ module DSL
8
+ # Passes if response body is a collection
9
+ #
10
+ # @example
11
+ #
12
+ # # Passes if the body is a collection
13
+ # body = '[{"id": 1}]'
14
+ # expect(OpenStruct.new body: body).to be_a_collection
15
+ #
16
+ # # Passes if the body is not a collection
17
+ # body = '{"id": 1}'
18
+ # expect(OpenStruct.new body: body).not_to be_a_collection
19
+ def be_a_collection
20
+ RSpecApi::Matchers::Collection.new
21
+ end
22
+ end
23
+ end
24
+ end
@@ -15,6 +15,7 @@ module RSpecApi
15
15
  def include_content_type(type = nil)
16
16
  content_type = case type
17
17
  when :json then 'application/json; charset=utf-8'
18
+ when :any then %r{}
18
19
  end
19
20
  RSpecApi::Matchers::ContentType.new(content_type)
20
21
  end
@@ -3,10 +3,9 @@ require 'rspec-api/dsl/include_content_type'
3
3
  require 'rspec-api/dsl/have_prev_page_link'
4
4
  require 'rspec-api/dsl/be_a_jsonp'
5
5
  require 'rspec-api/dsl/be_sorted'
6
- require 'rspec-api/dsl/be_valid_json'
6
+ require 'rspec-api/dsl/be_a_collection'
7
7
  require 'rspec-api/dsl/be_filtered'
8
8
  require 'rspec-api/dsl/have_attributes'
9
- require 'rspec-api/dsl/run_if' # should be the last, for metaprogramming
10
9
 
11
10
  require 'rspec/matchers'
12
11
 
@@ -20,7 +20,7 @@ module RSpecApi
20
20
  # can just check them when there are no query params)... of course
21
21
  # unless the query params is ?fields=id,name
22
22
  # if json.is_a?(Array) and json.empty?
23
- # raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array is sorted, but the array is empty. Try with more fixtures"
23
+ # raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array has attributes, but the array is empty. Try with more fixtures"
24
24
  # end
25
25
  has_attributes? json, @attributes
26
26
  rescue JSON::ParserError, JSON::GeneratorError
@@ -2,16 +2,12 @@ require 'json'
2
2
 
3
3
  module RSpecApi
4
4
  module Matchers
5
- class Json
6
- def initialize(type)
7
- @type = type
8
- @desc = " #{@type}" if @type
9
- end
5
+ class Collection
10
6
 
11
7
  def matches?(response)
12
8
  @body = response.body
13
9
  json = parse_json_of @body
14
- @type ? json.is_a?(@type) : true
10
+ json.is_a?(Array)
15
11
  end
16
12
  alias == matches?
17
13
 
@@ -20,11 +16,11 @@ module RSpecApi
20
16
  end
21
17
 
22
18
  def failure_message_for_should_not
23
- "expected body not to #{description}, but it was"
19
+ "expected body not to #{description}, but got #{@body}"
24
20
  end
25
21
 
26
22
  def description
27
- %Q(be a valid JSON#{@desc})
23
+ %Q(be a collection)
28
24
  end
29
25
 
30
26
  private
@@ -7,7 +7,7 @@ module RSpecApi
7
7
 
8
8
  def matches?(response)
9
9
  @headers = response.headers
10
- @headers['Content-Type'] == @content_type if @headers.key? 'Content-Type'
10
+ @content_type.match @headers['Content-Type'] if @headers.key? 'Content-Type'
11
11
  end
12
12
 
13
13
  def failure_message_for_should
@@ -21,10 +21,6 @@ module RSpecApi
21
21
  def description
22
22
  %Q{include 'Content-Type': '#{@content_type}'}
23
23
  end
24
-
25
- def description_for_run_if
26
- %Q{include any specific 'Content-Type'}
27
- end
28
24
  end
29
25
  end
30
26
  end
@@ -6,15 +6,19 @@ module RSpecApi
6
6
  def initialize(value, options = {})
7
7
  @value = value
8
8
  @field = options[:by]
9
- @comparing_with = options.fetch :comparing_with, Proc.new{|x,y| x == y}
9
+ @comparing_with = options[:comparing_with]
10
10
  end
11
11
 
12
12
  def matches?(response)
13
- @desc = " by #{@field}#{@comparing_with}#{@value}"
13
+ @desc = " by #{@field}#{@comparing_with || '='}#{@value}"
14
14
  @body = response.body
15
15
  array = extract_json_from @body
16
16
  array.all? do |item| # TODO: Don't always use string
17
- @comparing_with.call @value, item[@field.to_s].to_s
17
+ if @comparing_with
18
+ @comparing_with.call @value, item[@field.to_s].to_s
19
+ else
20
+ @value.to_s == item[@field.to_s].to_s
21
+ end
18
22
  end
19
23
  end
20
24
  alias == matches?
@@ -38,11 +42,11 @@ module RSpecApi
38
42
  def extract_json_from(something)
39
43
  array = JSON without_callbacks(something)
40
44
  if array.is_a?(Array) and array.empty?
41
- raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array is sorted, but the array is empty. Try with more fixtures"
45
+ raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array is filtered, but the array is empty. Try with more fixtures"
42
46
  end
43
47
  array
44
48
  rescue JSON::ParserError, JSON::GeneratorError
45
- nil
49
+ raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array is filtered, but the array is empty. Try with more fixtures"
46
50
  end
47
51
 
48
52
  def without_callbacks(something)
@@ -18,10 +18,6 @@ module RSpecApi
18
18
  def description
19
19
  %Q{include a 'Link' to the previous page}
20
20
  end
21
-
22
- def description_for_run_if
23
- %Q{include any specific pagination 'Link'}
24
- end
25
21
  end
26
22
  end
27
23
  end
@@ -15,7 +15,7 @@ module RSpecApi
15
15
  if @field.nil?
16
16
  true
17
17
  else
18
- @desc = " by #{'descending ' if @reverse}#{@field}"
18
+ @desc = " by #{'descending ' if @reverse}#{@field}" # TODO: use arrows
19
19
  @body = response.body
20
20
  array = extract_array of: @field, from: @body # what if this fails?
21
21
  is_sorted? array, @reverse
@@ -53,7 +53,7 @@ module RSpecApi
53
53
  end
54
54
  array.map{|item| item[options[:of].to_s]} # what if it's not an array of hashes?
55
55
  rescue JSON::ParserError, JSON::GeneratorError
56
- nil
56
+ raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array is sorted, but the array is empty. Try with more fixtures"
57
57
  end
58
58
 
59
59
  def without_callbacks(something)
@@ -1,5 +1,5 @@
1
1
  module RSpecApi
2
2
  module Matchers
3
- VERSION = '0.1.0'
3
+ VERSION = '0.5.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-api-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - claudiob
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-03 00:00:00.000000000 Z
11
+ date: 2013-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -101,22 +101,20 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
+ - lib/rspec-api/dsl/be_a_collection.rb
104
105
  - lib/rspec-api/dsl/be_a_jsonp.rb
105
106
  - lib/rspec-api/dsl/be_filtered.rb
106
107
  - lib/rspec-api/dsl/be_sorted.rb
107
- - lib/rspec-api/dsl/be_valid_json.rb
108
108
  - lib/rspec-api/dsl/have_attributes.rb
109
109
  - lib/rspec-api/dsl/have_prev_page_link.rb
110
110
  - lib/rspec-api/dsl/have_status.rb
111
111
  - lib/rspec-api/dsl/include_content_type.rb
112
- - lib/rspec-api/dsl/run_if.rb
113
112
  - lib/rspec-api/matchers/attributes.rb
113
+ - lib/rspec-api/matchers/collection.rb
114
114
  - lib/rspec-api/matchers/content_type.rb
115
115
  - lib/rspec-api/matchers/filter.rb
116
- - lib/rspec-api/matchers/json.rb
117
116
  - lib/rspec-api/matchers/jsonp.rb
118
117
  - lib/rspec-api/matchers/prev_page_link.rb
119
- - lib/rspec-api/matchers/run_if.rb
120
118
  - lib/rspec-api/matchers/sort.rb
121
119
  - lib/rspec-api/matchers/status.rb
122
120
  - lib/rspec-api/matchers/version.rb
@@ -1,29 +0,0 @@
1
- require 'rspec-api/matchers/json'
2
-
3
- module RSpecApi
4
- module Matchers
5
- # Convert RSpecAPI::Matchers classes into RSpec-compatible matchers, e.g.:
6
- # makes RSpecApi::Matchers::Status available as expect(...).to match_status
7
- module DSL
8
- # Passes if response body is JSON. Optionally check if Array or Hash.
9
- # Skips if if response body is JSON. Optionally check if Array or Hash.
10
- #
11
- # @example
12
- #
13
- # # Passes if the body is valid JSON
14
- # body = '{"id": 1}'
15
- # expect(OpenStruct.new body: body).to be_valid_json
16
- #
17
- # # Passes if the body is a valid JSON-marshalled Hash
18
- # body = '{"id": 1}'
19
- # expect(OpenStruct.new body: body).to be_valid_json(Hash)
20
- #
21
- # # Passes if the body is a valid JSON-marshalled Array
22
- # body = '[{"id": 1}, {"id": 2}]'
23
- # expect(OpenStruct.new body: body).to be_valid_json(Array)
24
- def be_valid_json(type = nil)
25
- RSpecApi::Matchers::Json.new(type)
26
- end
27
- end
28
- end
29
- end
@@ -1,24 +0,0 @@
1
- require 'rspec-api/matchers/run_if'
2
-
3
- module RSpecApi
4
- module Matchers
5
- module DSL
6
- # Creates a +method_name+_if method for each DSL method
7
- #
8
- # @example
9
- #
10
- # def have_prev_page_link_if(condition, *args)
11
- # RSpecApi::Matchers::RunIf.new condition, have_prev_page_link(*args)
12
- # end
13
- RSpecApi::Matchers::DSL.instance_methods.each do |method|
14
- define_method("#{method}_if") do |condition, *args|
15
- run_if condition, send(method, *args)
16
- end
17
- end
18
-
19
- def run_if(run, matcher)
20
- RSpecApi::Matchers::RunIf.new run, matcher
21
- end
22
- end
23
- end
24
- end
@@ -1,39 +0,0 @@
1
- module RSpecApi
2
- module Matchers
3
- class RunIf
4
- def initialize(run, matcher)
5
- @run = run
6
- @matcher = matcher
7
- end
8
-
9
- def matches?(response)
10
- !@run || @matcher.matches?(response)
11
- end
12
- alias == matches?
13
-
14
- def does_not_match?(response)
15
- !@run || !matches?(response)
16
- end
17
-
18
- def failure_message_for_should
19
- @matcher.failure_message_for_should
20
- end
21
-
22
- def failure_message_for_should_not
23
- @matcher.failure_message_for_should_not
24
- end
25
-
26
- def description
27
- if @run
28
- @matcher.description
29
- else
30
- if @matcher.respond_to? :description_for_run_if
31
- %Q{not be expected to #{@matcher.description_for_run_if}}
32
- else
33
- %Q{not be expected to #{@matcher.description}}
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end