Class: Clash::Tests

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/clash/tests.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#boldit, #colorize, #default_array, #greenit, #pout, #print_fail, #print_pass, #redit, #yellowit

Constructor Details

#initialize(options = {}) ⇒ Tests

Returns a new instance of Tests.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/clash/tests.rb', line 7

def initialize(options={})
  ENV['JEKYLL_ENV'] = 'test'
  
  @options = options
  @results = []
  @passed = []
  @failed = []

  @options[:file]    ||= '.clash.yml'
  @options[:only]    ||= []
  @options[:exit]    ||= true

  @tests = read_tests
end

Instance Attribute Details

#testsObject

Returns the value of attribute tests.



5
6
7
# File 'lib/clash/tests.rb', line 5

def tests
  @tests
end

Instance Method Details

#list_tests(tests) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/clash/tests.rb', line 84

def list_tests(tests)
  if tests.empty?
    ''
  else
    "- Tests: #{tests.join(',')}"
  end
end


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/clash/tests.rb', line 68

def print_results


  puts boldit("\n\nFailures:") unless @results.empty?
  @results.each do |results|
    puts "\n#{results.join('')}"
  end

  puts boldit("\n\nTest summary:")
  puts yellowit(" Tests run: #{@passed.dup.concat(@failed).size}")
  puts "#{greenit(" Passed #{@passed.size}")} #{list_tests(@passed)}"
  puts "#{redit(" Failed #{@failed.size}")} #{list_tests(@failed)}"

  exit 1 if @options[:exit] && !@results.empty?
end

#read_testsObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/clash/tests.rb', line 48

def read_tests
  tests = SafeYAML.load_file(@options[:file])
  index = 0

  default_array(tests).map do |test|
    index += 1

    # Admit all tests if no tests are excluded
    if @options[:only].empty?
      test
    # Only admit selected tests
    elsif @options[:only].include?(index)
      test
    # Remove tests not selected
    else
      nil
    end
  end
end

#runObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/clash/tests.rb', line 22

def run
  @tests.each_with_index do |options, index|
    # If tests are limited, only run specified tests
    #
    next if options.nil?
    run_test(options, index)
  end

  print_results
end

#run_test(options, index) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/clash/tests.rb', line 33

def run_test(options, index)

  options['index'] = index + 1
  options['context'] = @options[:context]

  results = Test.new(options).run

  if results.nil?
    @passed << index + 1
  else
    @failed << index + 1
    @results << results
  end
end