Class: Joust

Inherits:
Object
  • Object
show all
Defined in:
lib/joust.rb,
lib/joust/expected_response.rb,
lib/joust/expected_response/version_12.rb,
lib/joust/expected_response/version_20.rb

Defined Under Namespace

Classes: ExpectedResponse, Version12, Version20

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, test_cases, version) ⇒ Joust

initialize an instance to test a specific version



16
17
18
# File 'lib/joust.rb', line 16

def initialize(url, test_cases, version)
  @url, @test_cases, @version = url, test_cases, version
end

Class Method Details

.run(url, options, test_cases) ⇒ Object

run each version spec and return results



10
11
12
13
# File 'lib/joust.rb', line 10

def self.run(url, options, test_cases)
  specs = self.new(url, test_cases[options[:version]], options[:version])
  puts specs.check
end

Instance Method Details

#checkObject

run specification checks and return results



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/joust.rb', line 21

def check
  num_tests = passed_tests = 0
  results = @test_cases.map(&:flatten).inject([]) do |results, test|
    test_name, sent, expected = test.first, test.last.first, test.last.last
    resp = RestClient.post(@url, sent, :content_type => 'application/json')
    num_tests += 1
    json_hash = JSON.parse(resp.body) rescue {}
    if expected.is_a?(Array)
      pass = expected.inject(true) { |pass, exp| pass = false unless exp.match?(json_hash.shift); pass }
    else
      pass = expected.match?(json_hash)
    end
    if !!pass
      passed_tests += 1
      result_output = 'PASS'
    else
      result_output = 'FAIL: ' + resp.body
    end
    results << "#{@version} #{test_name} -- #{result_output}"
    results
  end
  results << "JSON-RPC #{@version}: #{passed_tests}/#{num_tests} tests passed"
end