Class: Exemplor::Examples

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

Instance Method Summary collapse

Constructor Details

#initializeExamples

Returns a new instance of Examples.



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

def initialize
  # TODO: no OrderedHash here
  @examples = OrderedHash.new
end

Instance Method Details

#add(name, &body) ⇒ Object



37
38
39
# File 'lib/examples.rb', line 37

def add(name, &body)
  @examples[name] = body
end

#list(patterns) ⇒ Object



55
56
57
58
59
# File 'lib/examples.rb', line 55

def list(patterns)
  patterns = Regexp.new(patterns.join('|'))
  list = @examples.keys.select { |name| name =~ patterns }
  print YAML.without_header(list)
end

#run(patterns) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/examples.rb', line 41

def run(patterns)
  fails = 0
  # unoffically supports multiple patterns
  patterns = Regexp.new(patterns.join('|'))
  examples_to_run = @examples.select { |name,_| name =~ patterns }
  return 0 if examples_to_run.empty?
  examples_to_run.each do |name, code|
    result = ResultPrinter.new(name, *Environment.run(&code))
    fails +=1 if result.failure?
    puts($stdout.tty? ? result.fancy : result.yaml)
  end
  (fails.to_f/examples_to_run.size)*100
end