Class: LieDetector::Suite

Inherits:
Object
  • Object
show all
Defined in:
lib/liedetector/suite.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ Suite

Returns a new instance of Suite.



5
6
7
8
9
# File 'lib/liedetector/suite.rb', line 5

def initialize(filepath)
  @filepath = filepath
  self.headers_defaults = {'Accept' => 'application/json'}
  self.http_defaults = {host: '127.0.0.1', port: 3000}
end

Instance Attribute Details

#headers_defaultsObject

Returns the value of attribute headers_defaults.



3
4
5
# File 'lib/liedetector/suite.rb', line 3

def headers_defaults
  @headers_defaults
end

#http_defaultsObject

Returns the value of attribute http_defaults.



3
4
5
# File 'lib/liedetector/suite.rb', line 3

def http_defaults
  @http_defaults
end

#storeObject (readonly)

Returns the value of attribute store.



55
56
57
# File 'lib/liedetector/suite.rb', line 55

def store
  @store
end

Instance Method Details

#build_uri(path, query: nil, fragment: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/liedetector/suite.rb', line 11

def build_uri(path, query: nil, fragment: nil)
  query = URI.encode_www_form(query) if query.is_a? Hash
  scheme = http_defaults[:scheme] || 'http'
  if scheme == 'http'
    URI::HTTP
  elsif scheme == 'https'
    URI::HTTPS
  else
    raise 'Bad configuration'
  end.build(http_defaults.merge(path: path, query: query, fragment: fragment)).to_s
end

#code_block(code) ⇒ Object



69
70
71
# File 'lib/liedetector/suite.rb', line 69

def code_block(code)
  self.instance_eval(code, @filepath)
end

#push_descriptions(text) ⇒ Object



32
33
34
35
# File 'lib/liedetector/suite.rb', line 32

def push_descriptions(text)
  @text ||= ''
  @text += text
end

#register(cls, name) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/liedetector/suite.rb', line 37

def register(cls, name)
  @ordered ||= []
  @registry ||= {}
  @ordered << cls
  @registry[name] = cls if name
  cls.description(@text || '')
  @text = ''
end

#request(name, method, path, &blk) ⇒ Object

Sugar to not define classes in the doc



74
75
76
77
78
79
80
# File 'lib/liedetector/suite.rb', line 74

def request(name, method, path, &blk)
  req = LieDetector::Request.new self
  req.method method
  req.path path
  req.instance_exec &blk
  register(req, name)
end

#runObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/liedetector/suite.rb', line 57

def run
  markdown = Redcarpet::Markdown.new(LieDetector::MarkdownRunner.new(self), fenced_code_blocks: true)
  markdown.render(File.new(@filepath).read)

  @ordered.each do |test|
    puts test.describe
    test.call
    puts 'passed'
    puts
  end
end

#sessionObject



23
24
25
26
27
28
29
30
# File 'lib/liedetector/suite.rb', line 23

def session
  @session ||= begin
    session = HTTPClient.new
    session.set_cookie_store("cookie.dat")
    # session.debug_dev = STDOUT
    session
  end
end

#store_dataObject



51
52
53
# File 'lib/liedetector/suite.rb', line 51

def store_data
  @store
end

#store_key(key, data) ⇒ Object



46
47
48
49
# File 'lib/liedetector/suite.rb', line 46

def store_key(key, data)
  @store ||= {}
  @store[key] = data
end