summaryrefslogtreecommitdiff
path: root/test/acceptance/acceptance_test.rb
blob: b77e56daf249a9f7dfb72c5f85eacc3db9964555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# coding: utf-8

require "highline/import"

class HighLine
  class AcceptanceTest
    @answers ||= {}

    class << self
      attr_reader :answers
    end

    def self.check
      caller_file = File.basename(caller(1..1).first.split(":")[-3])

      test = new
      yield test
      test.caller_file = caller_file
      test.check
    end

    def self.answers_for_report
      answers.map do |file, answer|
        "#{file}: #{answer}"
      end.join("\n")
    end

    # A test description to be shown to user.
    # It should express what the user is
    # expected to check.
    attr_accessor :desc

    # A test action to be checked by the user
    attr_accessor :action

    # A text asking the confirmation if
    # the action worked (y) or not (n).
    attr_accessor :question

    # Automatically filled attribute pointing
    # to the file where the current test
    # source is located. So we could check
    # at the report what tests passed or failed.
    attr_accessor :caller_file

    def check
      # Print a header with the test description
      puts "====="
      puts "   #{caller_file}"
      puts "====="
      puts
      puts desc

      # Execute the proc/lambda assigned to action
      puts "---"
      puts
      action.call
      puts
      puts "---"
      puts

      # Gather the user feedback about the test
      print question
      answer = STDIN.gets.chomp
      answer = "y" if answer.empty?
      HighLine::AcceptanceTest.answers[caller_file] = answer

      puts
    end
  end
end