speq 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/.travis.yml +2 -2
- data/README.md +40 -290
- data/bin/console +3 -3
- data/exe/speq +5 -1
- data/lib/speq/cli.rb +34 -13
- data/lib/speq/question.rb +162 -0
- data/lib/speq/recruit.rb +86 -0
- data/lib/speq/string_fmt.rb +93 -0
- data/lib/speq/values.rb +130 -0
- data/lib/speq/version.rb +3 -1
- data/lib/speq.rb +150 -29
- data/speq.gemspec +6 -2
- metadata +14 -17
- data/Gemfile.lock +0 -28
- data/lib/speq/action.rb +0 -97
- data/lib/speq/fake.rb +0 -22
- data/lib/speq/matcher.rb +0 -93
- data/lib/speq/report.rb +0 -24
- data/lib/speq/test.rb +0 -21
- data/lib/speq/unit.rb +0 -24
data/lib/speq/action.rb
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
require 'speq'
|
|
2
|
-
|
|
3
|
-
module Speq
|
|
4
|
-
class Action
|
|
5
|
-
attr_accessor :test_group, :message_queue, :receiver, :arguments_queue
|
|
6
|
-
|
|
7
|
-
def initialize(
|
|
8
|
-
test_group,
|
|
9
|
-
receiver = Object,
|
|
10
|
-
messages = [:itself],
|
|
11
|
-
arguments = [{ args: [], block: nil }]
|
|
12
|
-
)
|
|
13
|
-
@test_group = test_group
|
|
14
|
-
|
|
15
|
-
@message_queue = messages
|
|
16
|
-
@arguments_queue = arguments
|
|
17
|
-
@receiver = receiver
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def method_missing(method, *args, &block)
|
|
21
|
-
if method.to_s.end_with?('?')
|
|
22
|
-
matcher = Matcher.send(method, *args, &block)
|
|
23
|
-
@test_group << Unit.new(clone, matcher)
|
|
24
|
-
else
|
|
25
|
-
super
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def clone
|
|
30
|
-
self.class.new(
|
|
31
|
-
test_group,
|
|
32
|
-
receiver,
|
|
33
|
-
message_queue.clone,
|
|
34
|
-
arguments_queue.clone
|
|
35
|
-
)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def result
|
|
39
|
-
until @message_queue.empty?
|
|
40
|
-
args = arguments_queue.shift
|
|
41
|
-
message = message_queue.shift
|
|
42
|
-
|
|
43
|
-
@receiver = receiver.send(message, *args[:args], &args[:block])
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
@receiver
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def on(receiver, description = nil)
|
|
50
|
-
@receiver = receiver
|
|
51
|
-
Speq.descriptions[receiver] = description || receiver
|
|
52
|
-
self
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def does(*messages)
|
|
56
|
-
messages.each do |message|
|
|
57
|
-
message_queue.push(message)
|
|
58
|
-
arguments_queue.push(args: [], block: nil)
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
self
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
def with(*args, &block)
|
|
65
|
-
arguments_queue.last[:args] = args
|
|
66
|
-
arguments_queue.last[:block] = block
|
|
67
|
-
|
|
68
|
-
self
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def format_arguments
|
|
72
|
-
arguments = arguments_queue.last
|
|
73
|
-
argument_description = ''
|
|
74
|
-
|
|
75
|
-
unless arguments[:args].empty?
|
|
76
|
-
argument_description << "with '#{arguments[:args].join(', ')}'"
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
argument_description << ' and a block' if arguments[:block]
|
|
80
|
-
|
|
81
|
-
argument_description
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def format_receiver
|
|
85
|
-
Speq.descriptions[receiver] ? "on '#{Speq.descriptions[receiver]}'" : ''
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
def to_s
|
|
89
|
-
[message_queue.last, format_arguments, format_receiver]
|
|
90
|
-
.reject(&:empty?)
|
|
91
|
-
.join(' ')
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
alias is does
|
|
95
|
-
alias of with
|
|
96
|
-
end
|
|
97
|
-
end
|
data/lib/speq/fake.rb
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# The Fake class is a lightweight, general-purpose test double
|
|
2
|
-
module Speq
|
|
3
|
-
class Fake
|
|
4
|
-
def initialize(mapping)
|
|
5
|
-
mapping.each do |method_name, return_value|
|
|
6
|
-
define_singleton_method(method_name, &infer_method_body(return_value))
|
|
7
|
-
end
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def infer_method_body(return_value)
|
|
11
|
-
if return_value.respond_to?(:call)
|
|
12
|
-
return_value
|
|
13
|
-
else
|
|
14
|
-
-> { return_value }
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def to_s
|
|
19
|
-
description = send(:class) == self.class ? 'a fake' : "a fake#{send(:class)}"
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
end
|
data/lib/speq/matcher.rb
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
# The Match class includes factory methods for generating objects that
|
|
2
|
-
# respond to match, returning true for an expected return value of a unit test
|
|
3
|
-
module Speq
|
|
4
|
-
class Matcher
|
|
5
|
-
def initialize(expectation, phrase)
|
|
6
|
-
@expectation = expectation
|
|
7
|
-
@phrase = phrase
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def match?(actual)
|
|
11
|
-
@actual = actual
|
|
12
|
-
@expectation[actual]
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def to_s
|
|
16
|
-
@phrase[@actual]
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
def self.method_missing(method_name, *args, &block)
|
|
20
|
-
if method_name.to_s.end_with?('?')
|
|
21
|
-
dynamic_matcher(method_name, *args, &block)
|
|
22
|
-
|
|
23
|
-
else
|
|
24
|
-
super
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def self.dynamic_matcher(method_name, *args, &block)
|
|
29
|
-
Matcher.new(
|
|
30
|
-
->(object) { object.send(method_name, *args, &block) },
|
|
31
|
-
proc { "passes: #{method_name}" }
|
|
32
|
-
)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def self.pass?(&prc)
|
|
36
|
-
Matcher.new(prc)
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def self.true?
|
|
40
|
-
Matcher.eq?(true)
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def self.truthy?
|
|
44
|
-
Matcher.new(->(actual_value) { actual_value ? true : false })
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def self.falsy?
|
|
48
|
-
Matcher.new(->(actual_value) { actual_value ? false : true })
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def self.eq?(expected_value)
|
|
52
|
-
Matcher.new(
|
|
53
|
-
->(actual_value) { expected_value.eql?(actual_value) },
|
|
54
|
-
->(actual_value) { "equals #{actual_value}" }
|
|
55
|
-
)
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def self.have?(*symbols, **key_value_pairs)
|
|
59
|
-
new(
|
|
60
|
-
lambda do |object|
|
|
61
|
-
symbols.each { |symbol| return false unless object.respond_to?(symbol) }
|
|
62
|
-
key_value_pairs.each { |key, value| return false unless object.send(key) == value }
|
|
63
|
-
end,
|
|
64
|
-
proc do
|
|
65
|
-
properties = symbols.empty? ? '' : symbols.join(' ') + ', '
|
|
66
|
-
values = key_value_pairs.map { |key, value| "#{key}: #{value}" }.join(' ')
|
|
67
|
-
"has #{properties}#{values}"
|
|
68
|
-
end
|
|
69
|
-
)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def self.raise?(expected_except)
|
|
73
|
-
case expected_except
|
|
74
|
-
when Class
|
|
75
|
-
raise_class(expected_except)
|
|
76
|
-
when String
|
|
77
|
-
raise_message(expected_except)
|
|
78
|
-
else
|
|
79
|
-
raise ArgumentError
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def self.raise_class(expected_error)
|
|
84
|
-
Matcher.new(->(actual_except) { actual_except.class <= expected_error })
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def self.raise_message(expected_message)
|
|
88
|
-
Matcher.new(
|
|
89
|
-
->(actual_except) { actual_except.message == expected_message }
|
|
90
|
-
)
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
end
|
data/lib/speq/report.rb
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# The Report class produces and prints a report per test group
|
|
2
|
-
module Speq
|
|
3
|
-
class Report
|
|
4
|
-
def initialize(tests)
|
|
5
|
-
@units = tests.flat_map(&:units)
|
|
6
|
-
@passed = []
|
|
7
|
-
@failed = []
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
def print_report
|
|
11
|
-
@units.each do |unit|
|
|
12
|
-
if unit.passed?
|
|
13
|
-
@passed << unit.to_s.colorize(:green)
|
|
14
|
-
else
|
|
15
|
-
@failed << unit.to_s.colorize(:red)
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
puts "Passed (#{@passed.length}/#{@units.length})" unless @passed.empty?
|
|
20
|
-
puts @passed
|
|
21
|
-
puts "Failed (#{@failed.length}/#{@units.length})" unless @failed.empty?
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|
data/lib/speq/test.rb
DELETED
data/lib/speq/unit.rb
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
require 'speq/action'
|
|
2
|
-
|
|
3
|
-
# The Unit class is responsible for running a test and storing the result
|
|
4
|
-
module Speq
|
|
5
|
-
class Unit
|
|
6
|
-
attr_reader :result, :action, :matcher
|
|
7
|
-
|
|
8
|
-
def initialize(action, matcher)
|
|
9
|
-
@action = action
|
|
10
|
-
@matcher = matcher
|
|
11
|
-
@result = matcher.match?(action.clone.result)
|
|
12
|
-
rescue StandardError => exception
|
|
13
|
-
@result = matcher.match?(exception)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def passed?
|
|
17
|
-
@result
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def to_s
|
|
21
|
-
"#{action} #{matcher}"
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
end
|