Class: ApiValidator::Spec

Inherits:
Object
  • Object
show all
Extended by:
SharedClassAndInstanceMethods
Includes:
SharedClassAndInstanceMethods
Defined in:
lib/api-validator/spec.rb,
lib/api-validator/spec/results.rb

Defined Under Namespace

Modules: SharedClassAndInstanceMethods Classes: Results

Constant Summary collapse

BehaviourNotFoundError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SharedClassAndInstanceMethods

describe, shared_example, shared_examples, validations

Constructor Details

#initialize(name, options = {}, &block) ⇒ Spec

Returns a new instance of Spec.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/api-validator/spec.rb', line 46

def initialize(name, options = {}, &block)
  @parent = options.delete(:parent)
  @parent = nil if @parent == self.class
  @name = name

  initialize_before_hooks(options.delete(:before))

  if block_given?
    instance_eval(&block)
  else
    @pending = true
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'lib/api-validator/spec.rb', line 45

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



45
46
47
# File 'lib/api-validator/spec.rb', line 45

def parent
  @parent
end

#pendingObject (readonly)

Returns the value of attribute pending.



45
46
47
# File 'lib/api-validator/spec.rb', line 45

def pending
  @pending
end

Class Method Details

.runObject



38
39
40
41
42
43
# File 'lib/api-validator/spec.rb', line 38

def self.run
  validations.inject(Results.new(self.new(''), [])) do |memo, validation|
    results = validation.run
    memo.merge!(results)
  end
end

Instance Method Details

#before_hooksObject



70
71
72
# File 'lib/api-validator/spec.rb', line 70

def before_hooks
  @before_hooks ||= []
end

#behaves_as(name) ⇒ Object



89
90
91
92
93
# File 'lib/api-validator/spec.rb', line 89

def behaves_as(name)
  block = find_shared_example(name)
  raise BehaviourNotFoundError.new("Behaviour #{name.inspect} could not be found") unless block
  instance_eval(&block)
end

#cacheObject



105
106
107
# File 'lib/api-validator/spec.rb', line 105

def cache
  @cache ||= Hash.new
end

#expect_response(options = {}, &block) ⇒ Object



99
100
101
102
103
# File 'lib/api-validator/spec.rb', line 99

def expect_response(options = {}, &block)
  expectation = ResponseExpectation.new(self, options, &block)
  self.expectations << expectation
  expectation
end

#expectationsObject



95
96
97
# File 'lib/api-validator/spec.rb', line 95

def expectations
  @expectations ||= []
end

#find_shared_example(name) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/api-validator/spec.rb', line 78

def find_shared_example(name)
  ref = self
  begin
    if block = ref.shared_examples[name]
      return block
    end
  end while ref = ref.parent
  self.class.shared_examples[name]
end

#get(path) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/api-validator/spec.rb', line 109

def get(path)
  if Symbol === path
    path = "/#{path}"
  end

  pointer = JsonPointer.new(cache, path, :symbolize_keys => true)
  unless pointer.exists?
    return parent ? parent.get(path) : nil
  end
  val = pointer.value
  Proc === val ? val.call : val
end

#initialize_before_hooks(hooks) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/api-validator/spec.rb', line 60

def initialize_before_hooks(hooks)
  Array(hooks).each do |method_name_or_block|
    if method_name_or_block.respond_to?(:call)
      self.before_hooks << method_name_or_block
    elsif respond_to?(method_name_or_block)
      self.before_hooks << method(method_name_or_block)
    end
  end
end

#new(*args, &block) ⇒ Object



74
75
76
# File 'lib/api-validator/spec.rb', line 74

def new(*args, &block)
  self.class.new(*args, &block)
end

#runObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/api-validator/spec.rb', line 132

def run
  before_hooks.each do |hook|
    if hook.respond_to?(:receiver) && hook.receiver == self
      # It's a method
      hook.call
    else
      # It's a block
      instance_eval(&hook)
    end
  end

  results = self.expectations.inject([]) do |memo, expectation|
    memo << expectation.run
  end

  self.validations.inject(Results.new(self, results)) do |memo, validation|
    memo.merge!(validation.run)
  end
end

#set(path, val = nil, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/api-validator/spec.rb', line 122

def set(path, val=nil, &block)
  if Symbol === path
    path = "/#{path}"
  end

  pointer = JsonPointer.new(cache, path, :symbolize_keys => true)
  pointer.value = block_given? ? block : val
  val
end