adalog 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/lib/adalog/mock_logging_adapter.rb +142 -0
- data/lib/adalog/stub_logging_adapter.rb +65 -0
- data/lib/adalog/version.rb +1 -1
- data/test/unit/mock_logging_adapter_test.rb +287 -0
- data/test/unit/simple_logging_adapter_test.rb +66 -0
- data/test/unit/stub_logging_adapter_test.rb +90 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4234965ef03a7ffddfd7e02bc88a31fb61916e56
|
4
|
+
data.tar.gz: 5ae14eb814ce16a28e5780421183583f3d6565bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e49c93e19ea49fc9a31ed021c1ce2a147ab81d4490a0b5ac02747539b931f55d654026c743fdccde3d90ba814e37a1753e195c49d16202d1cf369851c7522176
|
7
|
+
data.tar.gz: 83557e9aad1ea5a311076435393a0c7c6b92d97f208392c04fdac01ba2983ac1d5659f73b1d5582cbd953e7befc3123df70719f128e04f9bcb0301b4800f4e62
|
data/.gitignore
CHANGED
@@ -0,0 +1,142 @@
|
|
1
|
+
module Adalog
|
2
|
+
module MockLoggingAdapter
|
3
|
+
|
4
|
+
##
|
5
|
+
# A factory to make new classes which implement the MockLogging
|
6
|
+
def self.new(service_name, repo, **stub_methods)
|
7
|
+
new_logger_class = Class.new(self::Base)
|
8
|
+
new_logger_class.instance_variable_set(:@service_name, service_name)
|
9
|
+
new_logger_class.instance_variable_set(:@repo, repo)
|
10
|
+
|
11
|
+
stub_methods.each do |message, value|
|
12
|
+
new_logger_class.instance_exec do
|
13
|
+
define_method(message, &MockLoggingAdapter.stub_method(message, value))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
new_logger_class
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# An isolated lambda to serve as the method body for stubs, both in
|
22
|
+
# the ::new method and in the Base#initialize method.
|
23
|
+
# Avoids repeating already clunky-looking logic.
|
24
|
+
def self.stub_method(message, value)
|
25
|
+
->(*args, **kwargs, &block) {
|
26
|
+
_received_messages << ReceivedMessage.new(message, args, kwargs, block)
|
27
|
+
repo.insert(
|
28
|
+
title: service_name,
|
29
|
+
message: "'#{message}', which has been stubbed with '#{value}'.",
|
30
|
+
details: args)
|
31
|
+
block.call unless nil == block
|
32
|
+
value
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# A class to encapsulate the data logged for received messages and all
|
38
|
+
# of their arguments.
|
39
|
+
ReceivedMessage = Struct.new(:message, :args, :kwargs, :block) do
|
40
|
+
|
41
|
+
##
|
42
|
+
# Just a little alias in case you're not as used to, or comfortable with,
|
43
|
+
# referring to Ruby method calls as 'messages' that are sent and received.
|
44
|
+
# But should be.
|
45
|
+
# Because that's what they are.
|
46
|
+
def name
|
47
|
+
message
|
48
|
+
end
|
49
|
+
|
50
|
+
##
|
51
|
+
# A handy list of the received arguments, without the message name.
|
52
|
+
def arguments
|
53
|
+
result = []
|
54
|
+
result += args
|
55
|
+
result << kwargs unless kwargs.empty?
|
56
|
+
result
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
##
|
62
|
+
# Used as the superclass of all logging classes returned from ::new
|
63
|
+
class Base
|
64
|
+
|
65
|
+
##
|
66
|
+
# Store the service_name and repo at the class level rather than coming up with some
|
67
|
+
# way of shoehorning it into each instance directly.
|
68
|
+
class << self
|
69
|
+
def service_name; @service_name ; end
|
70
|
+
def repo ; @repo ; end
|
71
|
+
end
|
72
|
+
|
73
|
+
##
|
74
|
+
# Allows for overriding stubbed methods which were initially built into the mock adapter.
|
75
|
+
# Does not explicitly restrict "overriding" to existing stubs, and so can be used to add
|
76
|
+
# additional stubs to a specific instance.
|
77
|
+
def initialize(**stub_method_overrides)
|
78
|
+
stub_method_overrides.each do |message, value|
|
79
|
+
define_singleton_method(message, &MockLoggingAdapter.stub_method(message, value))
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Convenience instance method versions of class-level storage of service_name and repo.
|
85
|
+
def service_name; self.class.service_name ; end
|
86
|
+
def repo ; self.class.repo ; end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Reader for the messages received by this mock, perhaps for use in testing
|
90
|
+
# details of particular calls.
|
91
|
+
def messages
|
92
|
+
_received_messages
|
93
|
+
end
|
94
|
+
|
95
|
+
##
|
96
|
+
# For use in simple boolean asserts, if all you need to test is the number of
|
97
|
+
# times a message was received. Flexible keyword arguments, which the the following
|
98
|
+
# precedence and details:
|
99
|
+
# - If :exactly is specified, will return true only if the message count is exactly the
|
100
|
+
# value of :exactly.
|
101
|
+
# - When :exactly is specified, :at_least and :at_most are ignored.
|
102
|
+
# - If :at_least is specified, will return true only if the message count is equal to or
|
103
|
+
# greater than the value of :at_least.
|
104
|
+
# - If :at_most is specified, will return true only if the message count is equal to or
|
105
|
+
# less than the value of :at_most.
|
106
|
+
# - :at_least and :at_most can be used simultaneously, and will return true if the
|
107
|
+
# message count falls in the range (at_least..at_most), that is, inclusive.
|
108
|
+
# - If no keyword options are specified, the default behavior is equivalent to
|
109
|
+
# calling with the option `at_least: 1`.
|
110
|
+
#
|
111
|
+
# Consequently, this method is as flexible as possible, to allow for expressive
|
112
|
+
# assertions in tests, such as:
|
113
|
+
# received?('unsubscribe', at_least: 1)
|
114
|
+
# received?('generate', exactly: 3)
|
115
|
+
# received?('toggle', at_least: 2, at_most: 4)
|
116
|
+
#
|
117
|
+
def received?(message, exactly: :none, at_least: :none, at_most: :none)
|
118
|
+
recv_count = _received_messages.select { |recv| recv.message == message }.count
|
119
|
+
|
120
|
+
return recv_count == exactly unless :none == exactly
|
121
|
+
# Substitute a default "at_least: 1" behavior for no options given.
|
122
|
+
if :none == at_least and :none == at_most
|
123
|
+
at_least = 1
|
124
|
+
end
|
125
|
+
|
126
|
+
result = true
|
127
|
+
result = result && recv_count >= at_least unless :none == at_least
|
128
|
+
result = result && recv_count <= at_most unless :none == at_most
|
129
|
+
result
|
130
|
+
end
|
131
|
+
|
132
|
+
##
|
133
|
+
# In case the service needs to stub-out a method named 'messages', we have
|
134
|
+
# this more direct, internal method name.
|
135
|
+
def _received_messages
|
136
|
+
@_received_messages ||= []
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Adalog
|
2
|
+
module StubLoggingAdapter
|
3
|
+
|
4
|
+
##
|
5
|
+
# A factory to make new classes which implement the MockLogging
|
6
|
+
def self.new(service_name, repo, **stub_methods)
|
7
|
+
new_logger_class = Class.new(self::Base)
|
8
|
+
new_logger_class.instance_variable_set(:@service_name, service_name)
|
9
|
+
new_logger_class.instance_variable_set(:@repo, repo)
|
10
|
+
|
11
|
+
stub_methods.each do |message, value|
|
12
|
+
new_logger_class.instance_exec do
|
13
|
+
define_method(message, &StubLoggingAdapter.stub_method(message, value))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
new_logger_class
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# An isolated lambda to serve as the method body for stubs, both in
|
22
|
+
# the ::new method and in the Base#initialize method.
|
23
|
+
# Avoids repeating already clunky-looking logic.
|
24
|
+
def self.stub_method(message, value)
|
25
|
+
->(*args, &block) {
|
26
|
+
repo.insert(
|
27
|
+
title: service_name,
|
28
|
+
message: "'#{message}', which has been stubbed with '#{value}'.",
|
29
|
+
details: args)
|
30
|
+
block.call unless nil == block
|
31
|
+
value
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Used as the superclass of all logging classes returned from ::new
|
37
|
+
class Base
|
38
|
+
|
39
|
+
##
|
40
|
+
# Store the service_name and repo at the class level rather than coming up with some
|
41
|
+
# way of shoehorning it into each instance directly.
|
42
|
+
class << self
|
43
|
+
def service_name; @service_name ; end
|
44
|
+
def repo ; @repo ; end
|
45
|
+
end
|
46
|
+
|
47
|
+
##
|
48
|
+
# Allows for overriding stubbed methods which were initially built into the mock adapter.
|
49
|
+
# Does not explicitly restrict "overriding" to existing stubs, and so can be used to add
|
50
|
+
# additional stubs to a specific instance.
|
51
|
+
def initialize(**stub_method_overrides)
|
52
|
+
stub_method_overrides.each do |message, value|
|
53
|
+
define_singleton_method(message, &StubLoggingAdapter.stub_method(message, value))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Convenience instance method versions of class-level storage of service_name and repo.
|
59
|
+
def service_name; self.class.service_name ; end
|
60
|
+
def repo ; self.class.repo ; end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
data/lib/adalog/version.rb
CHANGED
@@ -0,0 +1,287 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class MockLoggingAdapterTest < Minitest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@default_repo = Adalog::InMemoryRepo.new
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def default_repo
|
11
|
+
@default_repo ||= Adalog::InMemoryRepo.new
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def tg_service_adapter
|
16
|
+
stub_methods = { generate: Object.new, toggle: true }
|
17
|
+
adapter_class = Adalog::MockLoggingAdapter.new('TGService', default_repo, **stub_methods)
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def build_tg_service_adapter
|
22
|
+
tg_service_adapter.new
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
################################################################################
|
27
|
+
## The first tests are identical to those for StubLoggingAdapter. ##
|
28
|
+
## This is because MockLoggingAdapter is a superset of that functionality. ##
|
29
|
+
## A future refactoring may involve unifying these tests more elegantly, ##
|
30
|
+
## either in terms of the classes themselves, or by just unifying the tests ##
|
31
|
+
## as they are presented in test files themselves. ##
|
32
|
+
################################################################################
|
33
|
+
|
34
|
+
|
35
|
+
test "new creates a logging adapter class" do
|
36
|
+
adapter_class = Adalog::MockLoggingAdapter.new('SomeService', default_repo)
|
37
|
+
assert_kind_of(Class, adapter_class)
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
test "class accessors for service_name and repo" do
|
42
|
+
adapter_class = Adalog::MockLoggingAdapter.new('SomeService', default_repo)
|
43
|
+
assert_respond_to(adapter_class, :service_name)
|
44
|
+
assert_respond_to(adapter_class, :repo)
|
45
|
+
adapter = adapter_class.new
|
46
|
+
assert_equal('SomeService', adapter.service_name)
|
47
|
+
assert_equal(default_repo, adapter.repo)
|
48
|
+
end
|
49
|
+
|
50
|
+
|
51
|
+
test "instance methods are created for stubs specified in new" do
|
52
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
53
|
+
adapter_class = Adalog::MockLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
54
|
+
adapter = adapter_class.new
|
55
|
+
assert_respond_to(adapter, :foo)
|
56
|
+
assert_respond_to(adapter, :one)
|
57
|
+
assert_respond_to(adapter, :two)
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
test "stubbed instance methods return provided stubbed values" do
|
62
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
63
|
+
adapter_class = Adalog::MockLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
64
|
+
adapter = adapter_class.new
|
65
|
+
assert_equal('bar', adapter.foo)
|
66
|
+
assert_equal(1, adapter.one)
|
67
|
+
assert_equal(2, adapter.two)
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
test "new stubs can be added at instantiation" do
|
72
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
73
|
+
adapter_class = Adalog::MockLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
74
|
+
adapter = adapter_class.new(baz: 'bim')
|
75
|
+
assert_respond_to(adapter, :baz)
|
76
|
+
assert_equal('bim', adapter.baz)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
test "stubs can be overridden at instantiation" do
|
81
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
82
|
+
adapter_class = Adalog::MockLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
83
|
+
adapter = adapter_class.new(foo: 'rofl')
|
84
|
+
assert_respond_to(adapter, :foo)
|
85
|
+
assert_equal('rofl', adapter.foo)
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
test "calls to stubs are stored in the repo" do
|
90
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
91
|
+
adapter_class = Adalog::MockLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
92
|
+
adapter = adapter_class.new
|
93
|
+
|
94
|
+
adapter.foo('rofl', 'copter')
|
95
|
+
entry = default_repo.all.first
|
96
|
+
assert_equal("'foo', which has been stubbed with 'bar'.", entry.message)
|
97
|
+
assert_equal(['rofl', 'copter'], entry.details)
|
98
|
+
assert_equal('SomeService', entry.title)
|
99
|
+
|
100
|
+
adapter.one
|
101
|
+
entry = default_repo.all.first
|
102
|
+
assert_equal("'one', which has been stubbed with '1'.", entry.message)
|
103
|
+
assert_equal([], entry.details)
|
104
|
+
assert_equal('SomeService', entry.title)
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
################################################################################
|
109
|
+
|
110
|
+
|
111
|
+
test "messages records stubbed method calls" do
|
112
|
+
adapter = build_tg_service_adapter
|
113
|
+
adapter.generate
|
114
|
+
adapter.toggle
|
115
|
+
message_names = adapter.messages.map(&:name)
|
116
|
+
assert_includes(message_names, :generate)
|
117
|
+
assert_includes(message_names, :toggle)
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
test "messages is an alias for _received_messages" do
|
122
|
+
adapter = build_tg_service_adapter
|
123
|
+
adapter.generate
|
124
|
+
adapter.toggle
|
125
|
+
assert_equal(adapter.messages, adapter._received_messages)
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
test "elements in messages record args" do
|
130
|
+
adapter = build_tg_service_adapter
|
131
|
+
adapter.generate(1, 'two', :three, 4, foo: :bar) { |can_do| "a thing" }
|
132
|
+
assert_equal([1, 'two', :three, 4], adapter.messages.first.args)
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
test "elements in messages record kwargs" do
|
137
|
+
adapter = build_tg_service_adapter
|
138
|
+
adapter.generate(1, 2, 3, foo: :bar, baz: ['bim']) { |can_do| "a thing" }
|
139
|
+
assert_equal({ foo: :bar, baz: ['bim'] }, adapter.messages.first.kwargs)
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
test "elements in messages record blocks" do
|
144
|
+
adapter = build_tg_service_adapter
|
145
|
+
sent_block = Proc.new { |can_do| "a thing" }
|
146
|
+
adapter.generate(1, 2, 3, foo: :bar, &sent_block)
|
147
|
+
assert_equal(sent_block, adapter.messages.first.block)
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
test "elements in messages have a summary arguments method" do
|
152
|
+
adapter = build_tg_service_adapter
|
153
|
+
sent_block = Proc.new { |can_do| "a thing" }
|
154
|
+
adapter.generate(1, 2, 3, foo: :bar, &sent_block)
|
155
|
+
assert_equal([1, 2, 3, { foo: :bar }], adapter.messages.first.arguments)
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
test "stubs call the blocks passed" do
|
160
|
+
canary = "alive"
|
161
|
+
adapter = build_tg_service_adapter
|
162
|
+
adapter.generate(1, 2, 3, foo: :bar) { canary = "dead" }
|
163
|
+
assert_equal("dead", canary)
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
test "can be asked if messages are received at least n times" do
|
168
|
+
adapter = build_tg_service_adapter
|
169
|
+
assert(adapter.received?(:toggle, at_least: 0))
|
170
|
+
refute(adapter.received?(:toggle, at_least: 1))
|
171
|
+
assert(adapter.received?(:generate, at_least: 0))
|
172
|
+
refute(adapter.received?(:generate, at_least: 1))
|
173
|
+
|
174
|
+
adapter.generate
|
175
|
+
assert(adapter.received?(:toggle, at_least: 0))
|
176
|
+
refute(adapter.received?(:toggle, at_least: 1))
|
177
|
+
assert(adapter.received?(:generate, at_least: 0))
|
178
|
+
assert(adapter.received?(:generate, at_least: 1))
|
179
|
+
refute(adapter.received?(:generate, at_least: 2))
|
180
|
+
|
181
|
+
adapter.generate
|
182
|
+
assert(adapter.received?(:toggle, at_least: 0))
|
183
|
+
refute(adapter.received?(:toggle, at_least: 1))
|
184
|
+
assert(adapter.received?(:generate, at_least: 0))
|
185
|
+
assert(adapter.received?(:generate, at_least: 1))
|
186
|
+
assert(adapter.received?(:generate, at_least: 2))
|
187
|
+
refute(adapter.received?(:generate, at_least: 3))
|
188
|
+
|
189
|
+
adapter.toggle
|
190
|
+
assert(adapter.received?(:toggle, at_least: 0))
|
191
|
+
assert(adapter.received?(:toggle, at_least: 1))
|
192
|
+
refute(adapter.received?(:toggle, at_least: 2))
|
193
|
+
assert(adapter.received?(:generate, at_least: 0))
|
194
|
+
assert(adapter.received?(:generate, at_least: 1))
|
195
|
+
assert(adapter.received?(:generate, at_least: 2))
|
196
|
+
refute(adapter.received?(:generate, at_least: 3))
|
197
|
+
end
|
198
|
+
|
199
|
+
|
200
|
+
test "can be asked if messages are received at most n times" do
|
201
|
+
adapter = build_tg_service_adapter
|
202
|
+
assert(adapter.received?(:toggle, at_most: 0))
|
203
|
+
assert(adapter.received?(:generate, at_most: 0))
|
204
|
+
|
205
|
+
adapter.generate
|
206
|
+
assert(adapter.received?(:toggle, at_most: 0))
|
207
|
+
assert(adapter.received?(:generate, at_most: 1))
|
208
|
+
refute(adapter.received?(:generate, at_most: 0))
|
209
|
+
|
210
|
+
adapter.generate
|
211
|
+
assert(adapter.received?(:toggle, at_most: 0))
|
212
|
+
assert(adapter.received?(:generate, at_most: 2))
|
213
|
+
refute(adapter.received?(:generate, at_most: 1))
|
214
|
+
refute(adapter.received?(:generate, at_most: 0))
|
215
|
+
|
216
|
+
adapter.toggle
|
217
|
+
assert(adapter.received?(:toggle, at_most: 1))
|
218
|
+
refute(adapter.received?(:toggle, at_most: 0))
|
219
|
+
assert(adapter.received?(:generate, at_most: 2))
|
220
|
+
refute(adapter.received?(:generate, at_most: 1))
|
221
|
+
refute(adapter.received?(:generate, at_most: 0))
|
222
|
+
end
|
223
|
+
|
224
|
+
|
225
|
+
test "can be asked if messages are received exactly n times" do
|
226
|
+
adapter = build_tg_service_adapter
|
227
|
+
assert(adapter.received?(:toggle, exactly: 0))
|
228
|
+
assert(adapter.received?(:generate, exactly: 0))
|
229
|
+
|
230
|
+
adapter.generate
|
231
|
+
assert(adapter.received?(:toggle, exactly: 0))
|
232
|
+
assert(adapter.received?(:generate, exactly: 1))
|
233
|
+
refute(adapter.received?(:generate, exactly: 2))
|
234
|
+
|
235
|
+
adapter.generate
|
236
|
+
assert(adapter.received?(:toggle, exactly: 0))
|
237
|
+
assert(adapter.received?(:generate, exactly: 2))
|
238
|
+
refute(adapter.received?(:generate, exactly: 1))
|
239
|
+
|
240
|
+
adapter.toggle
|
241
|
+
assert(adapter.received?(:toggle, exactly: 1))
|
242
|
+
assert(adapter.received?(:generate, exactly: 2))
|
243
|
+
end
|
244
|
+
|
245
|
+
|
246
|
+
test "can be asked if messages are received amount within bounds" do
|
247
|
+
adapter = build_tg_service_adapter
|
248
|
+
assert(adapter.received?(:generate, at_least: 0, at_most: 0))
|
249
|
+
|
250
|
+
adapter.generate
|
251
|
+
refute(adapter.received?(:generate, at_least: 0, at_most: 0))
|
252
|
+
assert(adapter.received?(:generate, at_least: 0, at_most: 1))
|
253
|
+
assert(adapter.received?(:generate, at_least: 1, at_most: 1))
|
254
|
+
# Test even an invalid range:
|
255
|
+
refute(adapter.received?(:generate, at_least: 1, at_most: 0))
|
256
|
+
|
257
|
+
adapter.generate
|
258
|
+
assert(adapter.received?(:generate, at_least: 0, at_most: 2))
|
259
|
+
assert(adapter.received?(:generate, at_least: 1, at_most: 2))
|
260
|
+
assert(adapter.received?(:generate, at_least: 2, at_most: 2))
|
261
|
+
# Test even an invalid range:
|
262
|
+
refute(adapter.received?(:generate, at_least: 3, at_most: 2))
|
263
|
+
end
|
264
|
+
|
265
|
+
|
266
|
+
test "when asked if messages are received, defaults to at least once" do
|
267
|
+
adapter = build_tg_service_adapter
|
268
|
+
assert_equal_to_at_least_once = ->() {
|
269
|
+
assert_equal(
|
270
|
+
adapter.received?(:generate),
|
271
|
+
adapter.received?(:generate, at_least: 1)
|
272
|
+
)
|
273
|
+
}
|
274
|
+
# For zero calls
|
275
|
+
assert_equal_to_at_least_once.call
|
276
|
+
# One call case
|
277
|
+
adapter.generate
|
278
|
+
assert_equal_to_at_least_once
|
279
|
+
# More-than-one call case
|
280
|
+
adapter.generate
|
281
|
+
assert_equal_to_at_least_once
|
282
|
+
end
|
283
|
+
|
284
|
+
|
285
|
+
end
|
286
|
+
|
287
|
+
# flips = ->(a) { [!a, flips] }
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class SimpleLoggingAdapterTest < Minitest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@default_repo = Adalog::InMemoryRepo.new
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def default_repo
|
11
|
+
@default_repo ||= Adalog::InMemoryRepo.new
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
################################################################################
|
16
|
+
|
17
|
+
|
18
|
+
test "new creates a logging adapter class" do
|
19
|
+
adapter_class = Adalog::SimpleLoggingAdapter.new('SomeService', default_repo)
|
20
|
+
assert_kind_of(Class, adapter_class)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
test "class accessors for service_name and repo" do
|
25
|
+
adapter_class = Adalog::SimpleLoggingAdapter.new('SomeService', default_repo)
|
26
|
+
assert_respond_to(adapter_class, :service_name)
|
27
|
+
assert_respond_to(adapter_class, :repo)
|
28
|
+
adapter = adapter_class.new
|
29
|
+
assert_equal('SomeService', adapter.service_name)
|
30
|
+
assert_equal(default_repo, adapter.repo)
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
test "all messages sent are stored as entries with message as message attribute" do
|
35
|
+
adapter_class = Adalog::SimpleLoggingAdapter.new('SomeService', default_repo)
|
36
|
+
adapter = adapter_class.new
|
37
|
+
adapter.some
|
38
|
+
adapter.call
|
39
|
+
adapter.method_names("with", :arguments)
|
40
|
+
repo_contents = default_repo.all
|
41
|
+
assert(!!repo_contents.find { |entry| :some == entry.message })
|
42
|
+
assert(!!repo_contents.find { |entry| :call == entry.message })
|
43
|
+
assert(!!repo_contents.find { |entry| :method_names == entry.message })
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
test "message arguments are stored in the details section of an entry" do
|
48
|
+
adapter_class = Adalog::SimpleLoggingAdapter.new('OtherServiceName', default_repo)
|
49
|
+
adapter = adapter_class.new
|
50
|
+
adapter.capture_a_method_name("with", :arguments)
|
51
|
+
entry = default_repo.all.first
|
52
|
+
assert(!!entry)
|
53
|
+
assert_equal(["with", :arguments], entry.details)
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
test "the service name is used as the entry title" do
|
58
|
+
adapter_class = Adalog::SimpleLoggingAdapter.new('OtherServiceName', default_repo)
|
59
|
+
adapter = adapter_class.new
|
60
|
+
adapter.capture_a_method_name("with", :arguments)
|
61
|
+
entry = default_repo.all.first
|
62
|
+
assert(!!entry)
|
63
|
+
assert_equal('OtherServiceName', entry.title)
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class StubLoggingAdapterTest < Minitest::Test
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@default_repo = Adalog::InMemoryRepo.new
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def default_repo
|
11
|
+
@default_repo ||= Adalog::InMemoryRepo.new
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
################################################################################
|
16
|
+
|
17
|
+
|
18
|
+
test "new creates a logging adapter class" do
|
19
|
+
adapter_class = Adalog::StubLoggingAdapter.new('SomeService', default_repo)
|
20
|
+
assert_kind_of(Class, adapter_class)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
test "class accessors for service_name and repo" do
|
25
|
+
adapter_class = Adalog::StubLoggingAdapter.new('SomeService', default_repo)
|
26
|
+
assert_respond_to(adapter_class, :service_name)
|
27
|
+
assert_respond_to(adapter_class, :repo)
|
28
|
+
adapter = adapter_class.new
|
29
|
+
assert_equal('SomeService', adapter.service_name)
|
30
|
+
assert_equal(default_repo, adapter.repo)
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
test "instance methods are created for stubs specified in new" do
|
35
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
36
|
+
adapter_class = Adalog::StubLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
37
|
+
adapter = adapter_class.new
|
38
|
+
assert_respond_to(adapter, :foo)
|
39
|
+
assert_respond_to(adapter, :one)
|
40
|
+
assert_respond_to(adapter, :two)
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
test "stubbed instance methods return provided stubbed values" do
|
45
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
46
|
+
adapter_class = Adalog::StubLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
47
|
+
adapter = adapter_class.new
|
48
|
+
assert_equal('bar', adapter.foo)
|
49
|
+
assert_equal(1, adapter.one)
|
50
|
+
assert_equal(2, adapter.two)
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
test "new stubs can be added at instantiation" do
|
55
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
56
|
+
adapter_class = Adalog::StubLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
57
|
+
adapter = adapter_class.new(baz: 'bim')
|
58
|
+
assert_respond_to(adapter, :baz)
|
59
|
+
assert_equal('bim', adapter.baz)
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
test "stubs can be overridden at instantiation" do
|
64
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
65
|
+
adapter_class = Adalog::StubLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
66
|
+
adapter = adapter_class.new(foo: 'rofl')
|
67
|
+
assert_respond_to(adapter, :foo)
|
68
|
+
assert_equal('rofl', adapter.foo)
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
test "calls to stubs are stored in the repo" do
|
73
|
+
stub_methods = { foo: 'bar', one: 1, two: 2 }
|
74
|
+
adapter_class = Adalog::StubLoggingAdapter.new('SomeService', default_repo, **stub_methods)
|
75
|
+
adapter = adapter_class.new
|
76
|
+
|
77
|
+
adapter.foo('rofl', 'copter')
|
78
|
+
entry = default_repo.all.first
|
79
|
+
assert_equal("'foo', which has been stubbed with 'bar'.", entry.message)
|
80
|
+
assert_equal(['rofl', 'copter'], entry.details)
|
81
|
+
assert_equal('SomeService', entry.title)
|
82
|
+
|
83
|
+
adapter.one
|
84
|
+
entry = default_repo.all.first
|
85
|
+
assert_equal("'one', which has been stubbed with '1'.", entry.message)
|
86
|
+
assert_equal([], entry.details)
|
87
|
+
assert_equal('SomeService', entry.title)
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adalog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Kwiatkowski
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -116,8 +116,10 @@ files:
|
|
116
116
|
- lib/adalog/configuration.rb
|
117
117
|
- lib/adalog/entry.rb
|
118
118
|
- lib/adalog/in_memory_repo.rb
|
119
|
+
- lib/adalog/mock_logging_adapter.rb
|
119
120
|
- lib/adalog/pstore_repo.rb
|
120
121
|
- lib/adalog/simple_logging_adapter.rb
|
122
|
+
- lib/adalog/stub_logging_adapter.rb
|
121
123
|
- lib/adalog/version.rb
|
122
124
|
- lib/adalog/web.rb
|
123
125
|
- lib/adalog/web/public/javascripts/adalog.js
|
@@ -139,7 +141,10 @@ files:
|
|
139
141
|
- test/unit/configuration_test.rb
|
140
142
|
- test/unit/entry_test.rb
|
141
143
|
- test/unit/in_memory_repo_test.rb
|
144
|
+
- test/unit/mock_logging_adapter_test.rb
|
142
145
|
- test/unit/pstore_repo_test.rb
|
146
|
+
- test/unit/simple_logging_adapter_test.rb
|
147
|
+
- test/unit/stub_logging_adapter_test.rb
|
143
148
|
homepage: https://github.com/swifthand/adalog
|
144
149
|
licenses:
|
145
150
|
- Revised BSD, see LICENSE.md
|
@@ -172,4 +177,7 @@ test_files:
|
|
172
177
|
- test/unit/configuration_test.rb
|
173
178
|
- test/unit/entry_test.rb
|
174
179
|
- test/unit/in_memory_repo_test.rb
|
180
|
+
- test/unit/mock_logging_adapter_test.rb
|
175
181
|
- test/unit/pstore_repo_test.rb
|
182
|
+
- test/unit/simple_logging_adapter_test.rb
|
183
|
+
- test/unit/stub_logging_adapter_test.rb
|