mutant 0.8.24 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +3 -3
  3. data/Changelog.md +14 -654
  4. data/Gemfile +13 -0
  5. data/Gemfile.lock +59 -64
  6. data/LICENSE +271 -20
  7. data/README.md +73 -140
  8. data/Rakefile +0 -21
  9. data/bin/mutant +7 -2
  10. data/config/reek.yml +2 -1
  11. data/config/rubocop.yml +5 -9
  12. data/docs/incremental.md +76 -0
  13. data/docs/known-problems.md +0 -14
  14. data/docs/mutant-minitest.md +1 -1
  15. data/docs/mutant-rspec.md +2 -24
  16. data/lib/mutant.rb +45 -53
  17. data/lib/mutant/ast/nodes.rb +0 -2
  18. data/lib/mutant/ast/types.rb +1 -117
  19. data/lib/mutant/base.rb +192 -0
  20. data/lib/mutant/bootstrap.rb +145 -0
  21. data/lib/mutant/cli.rb +68 -54
  22. data/lib/mutant/config.rb +119 -6
  23. data/lib/mutant/env.rb +94 -8
  24. data/lib/mutant/expression.rb +6 -1
  25. data/lib/mutant/expression/parser.rb +9 -31
  26. data/lib/mutant/integration.rb +64 -36
  27. data/lib/mutant/isolation.rb +16 -1
  28. data/lib/mutant/isolation/fork.rb +105 -40
  29. data/lib/mutant/license.rb +34 -0
  30. data/lib/mutant/license/subscription.rb +47 -0
  31. data/lib/mutant/license/subscription/commercial.rb +57 -0
  32. data/lib/mutant/license/subscription/opensource.rb +77 -0
  33. data/lib/mutant/loader.rb +27 -4
  34. data/lib/mutant/matcher.rb +48 -1
  35. data/lib/mutant/matcher/chain.rb +1 -1
  36. data/lib/mutant/matcher/config.rb +0 -2
  37. data/lib/mutant/matcher/filter.rb +1 -1
  38. data/lib/mutant/matcher/method.rb +11 -7
  39. data/lib/mutant/matcher/methods.rb +1 -1
  40. data/lib/mutant/matcher/namespace.rb +1 -1
  41. data/lib/mutant/matcher/null.rb +1 -1
  42. data/lib/mutant/matcher/scope.rb +1 -1
  43. data/lib/mutant/meta/example/dsl.rb +0 -8
  44. data/lib/mutant/mutation.rb +1 -2
  45. data/lib/mutant/mutator/node.rb +2 -9
  46. data/lib/mutant/mutator/node/arguments.rb +1 -1
  47. data/lib/mutant/mutator/node/class.rb +0 -8
  48. data/lib/mutant/mutator/node/define.rb +0 -12
  49. data/lib/mutant/mutator/node/generic.rb +30 -44
  50. data/lib/mutant/mutator/node/index.rb +4 -4
  51. data/lib/mutant/mutator/node/literal/regex.rb +0 -39
  52. data/lib/mutant/mutator/node/send.rb +13 -12
  53. data/lib/mutant/parallel.rb +61 -40
  54. data/lib/mutant/parallel/driver.rb +59 -0
  55. data/lib/mutant/parallel/source.rb +6 -2
  56. data/lib/mutant/parallel/worker.rb +63 -45
  57. data/lib/mutant/range.rb +15 -0
  58. data/lib/mutant/reporter/cli.rb +5 -11
  59. data/lib/mutant/reporter/cli/format.rb +3 -46
  60. data/lib/mutant/reporter/cli/printer/config.rb +5 -6
  61. data/lib/mutant/reporter/cli/printer/env.rb +40 -0
  62. data/lib/mutant/reporter/cli/printer/env_progress.rb +13 -17
  63. data/lib/mutant/reporter/cli/printer/isolation_result.rb +17 -3
  64. data/lib/mutant/reporter/cli/printer/mutation_result.rb +2 -3
  65. data/lib/mutant/reporter/cli/printer/status_progressive.rb +19 -10
  66. data/lib/mutant/repository.rb +0 -65
  67. data/lib/mutant/repository/diff.rb +104 -0
  68. data/lib/mutant/repository/diff/ranges.rb +52 -0
  69. data/lib/mutant/result.rb +16 -7
  70. data/lib/mutant/runner.rb +38 -47
  71. data/lib/mutant/runner/sink.rb +1 -1
  72. data/lib/mutant/selector/null.rb +19 -0
  73. data/lib/mutant/subject.rb +3 -1
  74. data/lib/mutant/subject/method/instance.rb +3 -1
  75. data/lib/mutant/transform.rb +511 -0
  76. data/lib/mutant/variable.rb +282 -0
  77. data/lib/mutant/version.rb +1 -1
  78. data/lib/mutant/warnings.rb +113 -0
  79. data/meta/case.rb +1 -0
  80. data/meta/class.rb +0 -9
  81. data/meta/def.rb +1 -26
  82. data/meta/regexp.rb +10 -20
  83. data/meta/send.rb +14 -46
  84. data/mutant-minitest.gemspec +1 -1
  85. data/mutant-rspec.gemspec +2 -2
  86. data/mutant.gemspec +15 -16
  87. data/mutant.yml +6 -0
  88. data/spec/integration/mutant/isolation/fork_spec.rb +22 -5
  89. data/spec/integration/mutant/minitest_spec.rb +3 -2
  90. data/spec/integration/mutant/rspec_spec.rb +4 -3
  91. data/spec/integrations.yml +16 -13
  92. data/spec/shared/base_behavior.rb +45 -0
  93. data/spec/shared/framework_integration_behavior.rb +43 -14
  94. data/spec/spec_helper.rb +21 -17
  95. data/spec/support/corpus.rb +56 -95
  96. data/spec/support/shared_context.rb +37 -14
  97. data/spec/support/xspec.rb +7 -3
  98. data/spec/unit/mutant/bootstrap_spec.rb +216 -0
  99. data/spec/unit/mutant/cli_spec.rb +173 -117
  100. data/spec/unit/mutant/config_spec.rb +126 -0
  101. data/spec/unit/mutant/either_spec.rb +247 -0
  102. data/spec/unit/mutant/env_spec.rb +162 -40
  103. data/spec/unit/mutant/expression/method_spec.rb +16 -0
  104. data/spec/unit/mutant/expression/parser_spec.rb +29 -33
  105. data/spec/unit/mutant/expression_spec.rb +5 -7
  106. data/spec/unit/mutant/integration_spec.rb +100 -9
  107. data/spec/unit/mutant/isolation/fork_spec.rb +125 -67
  108. data/spec/unit/mutant/isolation/result_spec.rb +33 -1
  109. data/spec/unit/mutant/license_spec.rb +257 -0
  110. data/spec/unit/mutant/loader_spec.rb +50 -11
  111. data/spec/unit/mutant/matcher/compiler_spec.rb +0 -78
  112. data/spec/unit/mutant/matcher/method/instance_spec.rb +55 -11
  113. data/spec/unit/mutant/matcher/method/singleton_spec.rb +12 -2
  114. data/spec/unit/mutant/matcher_spec.rb +102 -0
  115. data/spec/unit/mutant/maybe_spec.rb +60 -0
  116. data/spec/unit/mutant/meta/example/dsl_spec.rb +1 -17
  117. data/spec/unit/mutant/mutation_spec.rb +13 -6
  118. data/spec/unit/mutant/parallel/driver_spec.rb +112 -14
  119. data/spec/unit/mutant/parallel/source/array_spec.rb +25 -17
  120. data/spec/unit/mutant/parallel/worker_spec.rb +182 -44
  121. data/spec/unit/mutant/parallel_spec.rb +105 -8
  122. data/spec/unit/mutant/range_spec.rb +141 -0
  123. data/spec/unit/mutant/reporter/cli/printer/config_spec.rb +7 -21
  124. data/spec/unit/mutant/reporter/cli/printer/env_progress_spec.rb +15 -6
  125. data/spec/unit/mutant/reporter/cli/printer/env_result_spec.rb +10 -2
  126. data/spec/unit/mutant/reporter/cli/printer/isolation_result_spec.rb +12 -4
  127. data/spec/unit/mutant/reporter/cli/printer/mutation_result_spec.rb +31 -2
  128. data/spec/unit/mutant/reporter/cli/printer/status_progressive_spec.rb +4 -4
  129. data/spec/unit/mutant/reporter/cli/printer/subject_result_spec.rb +5 -0
  130. data/spec/unit/mutant/reporter/cli_spec.rb +46 -123
  131. data/spec/unit/mutant/repository/diff/ranges_spec.rb +180 -0
  132. data/spec/unit/mutant/repository/diff_spec.rb +84 -71
  133. data/spec/unit/mutant/require_highjack_spec.rb +1 -1
  134. data/spec/unit/mutant/result/env_spec.rb +39 -9
  135. data/spec/unit/mutant/result/test_spec.rb +14 -0
  136. data/spec/unit/mutant/runner_spec.rb +88 -41
  137. data/spec/unit/mutant/selector/expression_spec.rb +11 -10
  138. data/spec/unit/mutant/selector/null_spec.rb +17 -0
  139. data/spec/unit/mutant/subject/method/instance_spec.rb +44 -5
  140. data/spec/unit/mutant/subject/method/singleton_spec.rb +9 -2
  141. data/spec/unit/mutant/subject_spec.rb +9 -1
  142. data/spec/unit/mutant/transform/array_spec.rb +92 -0
  143. data/spec/unit/mutant/transform/bool_spec.rb +63 -0
  144. data/spec/unit/mutant/transform/error_spec.rb +132 -0
  145. data/spec/unit/mutant/transform/exception_spec.rb +44 -0
  146. data/spec/unit/mutant/transform/hash_spec.rb +236 -0
  147. data/spec/unit/mutant/transform/index_spec.rb +92 -0
  148. data/spec/unit/mutant/transform/named_spec.rb +49 -0
  149. data/spec/unit/mutant/transform/primitive_spec.rb +56 -0
  150. data/spec/unit/mutant/transform/sequence_spec.rb +98 -0
  151. data/spec/unit/mutant/variable_spec.rb +618 -0
  152. data/spec/unit/mutant/warnings_spec.rb +89 -0
  153. data/spec/unit/mutant/world_spec.rb +63 -0
  154. data/test_app/Gemfile.minitest +0 -2
  155. metadata +79 -113
  156. data/.gitattributes +0 -1
  157. data/.ruby-gemset +0 -1
  158. data/config/triage.yml +0 -2
  159. data/lib/mutant/actor.rb +0 -57
  160. data/lib/mutant/actor/env.rb +0 -31
  161. data/lib/mutant/actor/mailbox.rb +0 -34
  162. data/lib/mutant/actor/receiver.rb +0 -42
  163. data/lib/mutant/actor/sender.rb +0 -26
  164. data/lib/mutant/ast/meta/restarg.rb +0 -19
  165. data/lib/mutant/ast/regexp.rb +0 -42
  166. data/lib/mutant/ast/regexp/transformer.rb +0 -187
  167. data/lib/mutant/ast/regexp/transformer/direct.rb +0 -123
  168. data/lib/mutant/ast/regexp/transformer/named_group.rb +0 -59
  169. data/lib/mutant/ast/regexp/transformer/options_group.rb +0 -83
  170. data/lib/mutant/ast/regexp/transformer/quantifier.rb +0 -114
  171. data/lib/mutant/ast/regexp/transformer/recursive.rb +0 -58
  172. data/lib/mutant/ast/regexp/transformer/root.rb +0 -31
  173. data/lib/mutant/ast/regexp/transformer/text.rb +0 -60
  174. data/lib/mutant/env/bootstrap.rb +0 -160
  175. data/lib/mutant/matcher/compiler.rb +0 -60
  176. data/lib/mutant/mutator/node/regexp.rb +0 -35
  177. data/lib/mutant/mutator/node/regexp/alternation_meta.rb +0 -23
  178. data/lib/mutant/mutator/node/regexp/capture_group.rb +0 -28
  179. data/lib/mutant/mutator/node/regexp/character_type.rb +0 -32
  180. data/lib/mutant/mutator/node/regexp/end_of_line_anchor.rb +0 -23
  181. data/lib/mutant/mutator/node/regexp/end_of_string_or_before_end_of_line_anchor.rb +0 -23
  182. data/lib/mutant/mutator/node/regexp/greedy_zero_or_more.rb +0 -27
  183. data/lib/mutant/parallel/master.rb +0 -181
  184. data/lib/mutant/reporter/cli/printer/status.rb +0 -53
  185. data/lib/mutant/reporter/cli/tput.rb +0 -46
  186. data/lib/mutant/warning_filter.rb +0 -61
  187. data/meta/regexp/character_types.rb +0 -23
  188. data/meta/regexp/regexp_alternation_meta.rb +0 -13
  189. data/meta/regexp/regexp_bol_anchor.rb +0 -10
  190. data/meta/regexp/regexp_bos_anchor.rb +0 -18
  191. data/meta/regexp/regexp_capture_group.rb +0 -19
  192. data/meta/regexp/regexp_eol_anchor.rb +0 -10
  193. data/meta/regexp/regexp_eos_anchor.rb +0 -8
  194. data/meta/regexp/regexp_eos_ob_eol_anchor.rb +0 -10
  195. data/meta/regexp/regexp_greedy_zero_or_more.rb +0 -12
  196. data/meta/regexp/regexp_root_expression.rb +0 -10
  197. data/meta/restarg.rb +0 -10
  198. data/spec/support/fake_actor.rb +0 -111
  199. data/spec/support/warning.rb +0 -66
  200. data/spec/unit/mutant/actor/binding_spec.rb +0 -34
  201. data/spec/unit/mutant/actor/env_spec.rb +0 -31
  202. data/spec/unit/mutant/actor/mailbox_spec.rb +0 -28
  203. data/spec/unit/mutant/actor/message_spec.rb +0 -25
  204. data/spec/unit/mutant/actor/receiver_spec.rb +0 -58
  205. data/spec/unit/mutant/actor/sender_spec.rb +0 -24
  206. data/spec/unit/mutant/ast/regexp/parse_spec.rb +0 -19
  207. data/spec/unit/mutant/ast/regexp/transformer/lookup_table/table_spec.rb +0 -21
  208. data/spec/unit/mutant/ast/regexp/transformer/lookup_table_spec.rb +0 -35
  209. data/spec/unit/mutant/ast/regexp/transformer_spec.rb +0 -21
  210. data/spec/unit/mutant/ast/regexp_spec.rb +0 -704
  211. data/spec/unit/mutant/env/bootstrap_spec.rb +0 -188
  212. data/spec/unit/mutant/matcher/compiler/subject_prefix_spec.rb +0 -26
  213. data/spec/unit/mutant/parallel/master_spec.rb +0 -338
  214. data/spec/unit/mutant/reporter/cli/printer/status_spec.rb +0 -121
  215. data/spec/unit/mutant/reporter/cli/tput_spec.rb +0 -50
  216. data/spec/unit/mutant/warning_filter_spec.rb +0 -106
  217. data/spec/unit/mutant_spec.rb +0 -17
  218. data/test_app/Gemfile.rspec3.7 +0 -7
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Mutant::Transform::Named do
4
+ subject { described_class.new(name, transform) }
5
+
6
+ let(:name) { 'transform-name' }
7
+ let(:transform) { Mutant::Transform::Boolean.new }
8
+
9
+ describe '#slug' do
10
+ def apply
11
+ subject.slug
12
+ end
13
+
14
+ it 'returns name' do
15
+ expect(apply).to be(name)
16
+ end
17
+ end
18
+
19
+ describe '#apply' do
20
+ def apply
21
+ subject.apply(input)
22
+ end
23
+
24
+ context 'on valid input' do
25
+ let(:input) { true }
26
+
27
+ it 'returns sucess' do
28
+ expect(apply).to eql(Mutant::Either::Right.new(input))
29
+ end
30
+ end
31
+
32
+ context 'on invalid input' do
33
+ let(:input) { 1 }
34
+
35
+ let(:error) do
36
+ Mutant::Transform::Error.new(
37
+ cause: transform.apply(input).from_left,
38
+ input: input,
39
+ message: nil,
40
+ transform: subject
41
+ )
42
+ end
43
+
44
+ it 'returns failure' do
45
+ expect(apply).to eql(Mutant::Either::Left.new(error))
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Mutant::Transform::Primitive do
4
+ subject { described_class.new(primitive) }
5
+
6
+ let(:primitive) { String }
7
+
8
+ describe '#slug' do
9
+ def apply
10
+ subject.slug
11
+ end
12
+
13
+ it 'returns strigified primitive' do
14
+ expect(apply).to eql('String')
15
+ end
16
+
17
+ it 'is idempotent' do
18
+ expect(apply).to be(apply)
19
+ end
20
+
21
+ it 'is frozen' do
22
+ expect(apply.frozen?).to be(true)
23
+ end
24
+ end
25
+
26
+ describe '#apply' do
27
+ def apply
28
+ subject.apply(input)
29
+ end
30
+
31
+ context 'on string input' do
32
+ let(:input) { 'some-string' }
33
+
34
+ it 'returns sucess' do
35
+ expect(apply).to eql(Mutant::Either::Right.new(input))
36
+ end
37
+ end
38
+
39
+ context 'on other input' do
40
+ let(:input) { 1 }
41
+
42
+ let(:error) do
43
+ Mutant::Transform::Error.new(
44
+ cause: nil,
45
+ input: input,
46
+ message: 'Expected: String but got: Integer',
47
+ transform: subject
48
+ )
49
+ end
50
+
51
+ it 'returns failure' do
52
+ expect(apply).to eql(Mutant::Either::Left.new(error))
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Mutant::Transform::Sequence do
4
+ subject { described_class.new(steps) }
5
+
6
+ let(:steps) do
7
+ [
8
+ hash_transform,
9
+ hash_symbolize
10
+ ]
11
+ end
12
+
13
+ let(:hash_transform) do
14
+ Mutant::Transform::Hash.new(optional: [], required: [key_transform])
15
+ end
16
+
17
+ let(:key_transform) do
18
+ Mutant::Transform::Hash::Key.new('foo', Mutant::Transform::Boolean.new)
19
+ end
20
+
21
+ let(:boolean_transform) do
22
+ Mutant::Transform::Boolean.new
23
+ end
24
+
25
+ let(:hash_symbolize) do
26
+ Mutant::Transform::Hash::Symbolize.new
27
+ end
28
+
29
+ describe '#apply' do
30
+ def apply
31
+ subject.apply(input)
32
+ end
33
+
34
+ context 'on valid input' do
35
+ let(:input) { { 'foo' => true } }
36
+
37
+ it 'returns success' do
38
+ expect(apply).to eql(Mutant::Either::Right.new(foo: true))
39
+ end
40
+ end
41
+
42
+ context 'on invalid input' do
43
+ let(:input) { { 'foo' => 1 } }
44
+
45
+ let(:boolean_error) do
46
+ Mutant::Transform::Error.new(
47
+ cause: nil,
48
+ input: 1,
49
+ message: 'Expected: boolean but got: 1',
50
+ transform: boolean_transform
51
+ )
52
+ end
53
+
54
+ let(:key_error) do
55
+ Mutant::Transform::Error.new(
56
+ cause: boolean_error,
57
+ input: 1,
58
+ message: nil,
59
+ transform: key_transform
60
+ )
61
+ end
62
+
63
+ let(:hash_error) do
64
+ Mutant::Transform::Error.new(
65
+ cause: key_error,
66
+ input: input,
67
+ message: nil,
68
+ transform: hash_transform
69
+ )
70
+ end
71
+
72
+ let(:index_error) do
73
+ Mutant::Transform::Error.new(
74
+ cause: hash_error,
75
+ input: input,
76
+ message: nil,
77
+ transform: Mutant::Transform::Index.new(
78
+ index: 0,
79
+ transform: hash_transform
80
+ )
81
+ )
82
+ end
83
+
84
+ let(:error) do
85
+ Mutant::Transform::Error.new(
86
+ cause: index_error,
87
+ input: input,
88
+ message: nil,
89
+ transform: subject
90
+ )
91
+ end
92
+
93
+ it 'returns failure' do
94
+ expect(apply).to eql(Mutant::Either::Left.new(error))
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,618 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MutantSpec
4
+ module VariableHelper
5
+ def empty
6
+ described_class.new(
7
+ condition_variable: condition_variable_class,
8
+ mutex: mutex_class
9
+ )
10
+ end
11
+
12
+ def full(value)
13
+ described_class.new(
14
+ condition_variable: condition_variable_class,
15
+ mutex: mutex_class,
16
+ value: value
17
+ )
18
+ end
19
+
20
+ # rubocop:disable Metrics/AbcSize
21
+ # rubocop:disable Metrics/MethodLength
22
+ def self.shared_setup
23
+ lambda do |_host|
24
+ let(:condition_variable_class) { class_double(ConditionVariable) }
25
+ let(:expected_result) { value }
26
+ let(:full_condition) { instance_double(ConditionVariable, 'full') }
27
+ let(:mutex) { instance_double(Mutex) }
28
+ let(:mutex_class) { class_double(Mutex) }
29
+ let(:value) { instance_double(Object, 'value') }
30
+
31
+ let(:synchronize) do
32
+ {
33
+ receiver: mutex,
34
+ selector: :synchronize,
35
+ reaction: { yields: [] }
36
+ }
37
+ end
38
+
39
+ let(:signal_full) do
40
+ {
41
+ receiver: full_condition,
42
+ selector: :signal
43
+ }
44
+ end
45
+
46
+ let(:put) do
47
+ {
48
+ receiver: full_condition,
49
+ selector: :wait,
50
+ arguments: [mutex],
51
+ reaction: { execute: -> { subject.put(value) } }
52
+ }
53
+ end
54
+
55
+ let(:wait_empty) do
56
+ {
57
+ receiver: empty_condition,
58
+ selector: :wait,
59
+ arguments: [mutex]
60
+ }
61
+ end
62
+
63
+ let(:wait_full) do
64
+ {
65
+ receiver: full_condition,
66
+ selector: :wait,
67
+ arguments: [mutex]
68
+ }
69
+ end
70
+
71
+ let(:signal_empty) do
72
+ {
73
+ receiver: empty_condition,
74
+ selector: :signal
75
+ }
76
+ end
77
+
78
+ shared_examples 'consumes events' do
79
+ specify do
80
+ verify_events do
81
+ expect(apply).to eql(expected_result)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ RSpec.describe Mutant::Variable::IVar do
91
+ include MutantSpec::VariableHelper
92
+
93
+ class_eval(&MutantSpec::VariableHelper.shared_setup)
94
+
95
+ subject { empty }
96
+
97
+ let(:setup) do
98
+ [
99
+ {
100
+ receiver: condition_variable_class,
101
+ selector: :new,
102
+ reaction: { return: full_condition }
103
+ },
104
+ {
105
+ receiver: mutex_class,
106
+ selector: :new,
107
+ reaction: { return: mutex }
108
+ },
109
+ synchronize
110
+ ]
111
+ end
112
+
113
+ describe '#take' do
114
+ def apply
115
+ subject.take
116
+ end
117
+
118
+ context 'when ivar is initially full' do
119
+ subject { full(value) }
120
+
121
+ let(:raw_expectations) { setup }
122
+
123
+ include_examples 'consumes events'
124
+ end
125
+
126
+ context 'when ivar is initially empty' do
127
+ let(:raw_expectations) do
128
+ [
129
+ *setup,
130
+ put,
131
+ synchronize,
132
+ signal_full
133
+ ]
134
+ end
135
+
136
+ include_examples 'consumes events'
137
+ end
138
+ end
139
+
140
+ describe '#take_timeout' do
141
+ def apply
142
+ subject.take_timeout(1.0)
143
+ end
144
+
145
+ context 'when ivar is initially full' do
146
+ subject { full(value) }
147
+
148
+ let(:raw_expectations) { setup }
149
+
150
+ let(:expected_result) do
151
+ Mutant::Variable.const_get(:Result)::Value.new(value)
152
+ end
153
+
154
+ include_examples 'consumes events'
155
+ end
156
+
157
+ context 'when ivar is initially empty' do
158
+ def wait(time)
159
+ {
160
+ receiver: full_condition,
161
+ selector: :wait,
162
+ arguments: [mutex, time]
163
+ }
164
+ end
165
+
166
+ def elapsed(time)
167
+ {
168
+ receiver: Mutant::Timer,
169
+ selector: :elapsed,
170
+ reaction: { yields: [], return: time }
171
+ }
172
+ end
173
+
174
+ context 'and timeout occurs before value is put' do
175
+ let(:expected_result) do
176
+ Mutant::Variable.const_get(:Result)::Timeout.new
177
+ end
178
+
179
+ context 'wait exactly runs to zero left time on the clock' do
180
+ let(:raw_expectations) do
181
+ [
182
+ *setup,
183
+ elapsed(0.5),
184
+ wait(1.0),
185
+ elapsed(0.5),
186
+ wait(0.5)
187
+ ]
188
+ end
189
+
190
+ include_examples 'consumes events'
191
+ end
192
+
193
+ context 'wait overruns timeout' do
194
+ let(:raw_expectations) do
195
+ [
196
+ *setup,
197
+ elapsed(1.5),
198
+ wait(1.0)
199
+ ]
200
+ end
201
+
202
+ include_examples 'consumes events'
203
+ end
204
+ end
205
+
206
+ context 'and put occurs before timeout' do
207
+ let(:expected_result) do
208
+ Mutant::Variable.const_get(:Result)::Value.new(value)
209
+ end
210
+
211
+ let(:raw_expectations) do
212
+ [
213
+ *setup,
214
+ elapsed(0.5),
215
+ wait(1.0).merge(reaction: { execute: -> { subject.put(value) } }),
216
+ synchronize,
217
+ signal_full
218
+ ]
219
+ end
220
+
221
+ include_examples 'consumes events'
222
+ end
223
+ end
224
+ end
225
+
226
+ describe '#put' do
227
+ def apply
228
+ subject.put(value)
229
+ end
230
+
231
+ context 'when ivar is initially empty' do
232
+ context 'when not reading result' do
233
+ let(:expected_result) { subject }
234
+
235
+ let(:raw_expectations) do
236
+ [
237
+ *setup,
238
+ signal_full
239
+ ]
240
+ end
241
+
242
+ include_examples 'consumes events'
243
+ end
244
+
245
+ context 'when reading result back' do
246
+ let(:expected_result) { value }
247
+
248
+ def apply
249
+ super
250
+ subject.read
251
+ end
252
+
253
+ let(:raw_expectations) do
254
+ [
255
+ *setup,
256
+ signal_full,
257
+ synchronize
258
+ ]
259
+ end
260
+
261
+ include_examples 'consumes events'
262
+ end
263
+ end
264
+
265
+ context 'when ivar is initially full' do
266
+ subject { full(value) }
267
+
268
+ let(:raw_expectations) { setup }
269
+
270
+ it 'raises expected exception' do
271
+ verify_events do
272
+ expect { apply }.to raise_error(Mutant::Variable::IVar::Error, 'is immutable')
273
+ end
274
+ end
275
+ end
276
+ end
277
+
278
+ describe '#try_put' do
279
+ def apply
280
+ subject.try_put(value)
281
+ end
282
+
283
+ let(:expected_result) { subject }
284
+
285
+ context 'when ivar is initially empty' do
286
+ let(:raw_expectations) do
287
+ [
288
+ *setup,
289
+ signal_full
290
+ ]
291
+ end
292
+
293
+ include_examples 'consumes events'
294
+
295
+ context 'reading the put value' do
296
+ let(:expected_result) { value }
297
+
298
+ let(:raw_expectations) do
299
+ [
300
+ *super(),
301
+ synchronize
302
+ ]
303
+ end
304
+
305
+ def apply
306
+ super
307
+ subject.read
308
+ end
309
+
310
+ include_examples 'consumes events'
311
+ end
312
+ end
313
+
314
+ context 'when ivar is initially full' do
315
+ subject { full(value) }
316
+
317
+ let(:raw_expectations) { setup }
318
+
319
+ include_examples 'consumes events'
320
+ end
321
+ end
322
+
323
+ describe '#read' do
324
+ def apply
325
+ subject.read
326
+ end
327
+
328
+ context 'when ivar is initially empty' do
329
+ let(:raw_expectations) do
330
+ [
331
+ *setup,
332
+ wait_full.merge(reaction: { execute: -> { subject.put(value) } }),
333
+ synchronize,
334
+ signal_full
335
+ ]
336
+ end
337
+
338
+ include_examples 'consumes events'
339
+ end
340
+
341
+ context 'when ivar is initially full' do
342
+ subject { full(value) }
343
+
344
+ let(:raw_expectations) { setup }
345
+
346
+ include_examples 'consumes events'
347
+ end
348
+ end
349
+
350
+ describe '#with' do
351
+ def apply
352
+ subject.with do |value|
353
+ @value = value
354
+ end
355
+ end
356
+
357
+ before { @value = nil }
358
+
359
+ context 'when ivar is initially full' do
360
+ subject { full(value) }
361
+
362
+ let(:raw_expectations) { setup }
363
+
364
+ include_examples 'consumes events'
365
+
366
+ it 'should yield value' do
367
+ verify_events do
368
+ expect { apply }.to change { @value }.from(nil).to(value)
369
+ end
370
+ end
371
+ end
372
+
373
+ context 'when ivar is initially empty' do
374
+ subject { empty }
375
+
376
+ let(:raw_expectations) do
377
+ [
378
+ *setup,
379
+ put,
380
+ synchronize,
381
+ signal_full
382
+ ]
383
+ end
384
+
385
+ include_examples 'consumes events'
386
+
387
+ it 'should yield value' do
388
+ verify_events do
389
+ expect { apply }.to change { @value }.from(nil).to(value)
390
+ end
391
+ end
392
+ end
393
+ end
394
+ end
395
+
396
+ describe Mutant::Variable::MVar do
397
+ include MutantSpec::VariableHelper
398
+
399
+ class_eval(&MutantSpec::VariableHelper.shared_setup)
400
+
401
+ subject { empty }
402
+
403
+ let(:empty_condition) { instance_double(ConditionVariable, 'empty') }
404
+
405
+ let(:setup) do
406
+ [
407
+ {
408
+ receiver: condition_variable_class,
409
+ selector: :new,
410
+ reaction: { return: full_condition }
411
+ },
412
+ {
413
+ receiver: mutex_class,
414
+ selector: :new,
415
+ reaction: { return: mutex }
416
+ },
417
+ {
418
+ receiver: condition_variable_class,
419
+ selector: :new,
420
+ reaction: { return: empty_condition }
421
+ },
422
+ synchronize
423
+ ]
424
+ end
425
+
426
+ describe '#put' do
427
+ def apply
428
+ subject.put(value)
429
+ end
430
+
431
+ context 'when ivar is initially empty' do
432
+ context 'when not reading result' do
433
+ let(:expected_result) { subject }
434
+
435
+ let(:raw_expectations) do
436
+ [
437
+ *setup,
438
+ signal_full
439
+ ]
440
+ end
441
+
442
+ include_examples 'consumes events'
443
+ end
444
+
445
+ context 'when reading result back' do
446
+ let(:expected_result) { value }
447
+
448
+ def apply
449
+ super
450
+ subject.read
451
+ end
452
+
453
+ let(:raw_expectations) do
454
+ [
455
+ *setup,
456
+ signal_full,
457
+ synchronize
458
+ ]
459
+ end
460
+
461
+ include_examples 'consumes events'
462
+ end
463
+ end
464
+
465
+ context 'when ivar is initially full' do
466
+ context 'when not reading result' do
467
+ subject { full(value) }
468
+
469
+ let(:expected_result) { subject }
470
+
471
+ let(:raw_expectations) do
472
+ [
473
+ *setup,
474
+ wait_empty.merge(reaction: { execute: -> { subject.take } }),
475
+ synchronize,
476
+ signal_empty,
477
+ signal_full
478
+ ]
479
+ end
480
+
481
+ include_examples 'consumes events'
482
+ end
483
+
484
+ context 'when reading result back' do
485
+ subject { full(value) }
486
+
487
+ def apply
488
+ super
489
+ subject.read
490
+ end
491
+
492
+ let(:expected_result) { value }
493
+
494
+ let(:raw_expectations) do
495
+ [
496
+ *setup,
497
+ wait_empty.merge(reaction: { execute: -> { subject.take } }),
498
+ synchronize,
499
+ signal_empty,
500
+ signal_full,
501
+ synchronize
502
+ ]
503
+ end
504
+
505
+ include_examples 'consumes events'
506
+ end
507
+ end
508
+ end
509
+
510
+ describe '#modify' do
511
+ let(:expected_result) { 1 }
512
+ let(:value) { 0 }
513
+
514
+ def apply
515
+ subject.modify(&:succ)
516
+ end
517
+
518
+ context 'when ivar is initially empty' do
519
+ let(:raw_expectations) do
520
+ [
521
+ *setup,
522
+ wait_full.merge(reaction: { execute: -> { subject.put(value) } }),
523
+ synchronize,
524
+ signal_full,
525
+ signal_full
526
+ ]
527
+ end
528
+
529
+ include_examples 'consumes events'
530
+ end
531
+
532
+ context 'when ivar is initially full' do
533
+ subject { full(value) }
534
+
535
+ let(:raw_expectations) do
536
+ [
537
+ *setup,
538
+ signal_full
539
+ ]
540
+ end
541
+
542
+ include_examples 'consumes events'
543
+ end
544
+ end
545
+
546
+ describe '#take' do
547
+ def apply
548
+ subject.take
549
+ end
550
+
551
+ context 'when ivar is initially empty' do
552
+ let(:expected_result) { value }
553
+
554
+ let(:raw_expectations) do
555
+ [
556
+ *setup,
557
+ wait_full.merge(reaction: { execute: -> { subject.put(value) } }),
558
+ synchronize,
559
+ signal_full,
560
+ signal_empty
561
+ ]
562
+ end
563
+
564
+ include_examples 'consumes events'
565
+ end
566
+
567
+ context 'when ivar is initially full' do
568
+ subject { full(value) }
569
+
570
+ let(:expected_result) { value }
571
+
572
+ let(:raw_expectations) do
573
+ [
574
+ *setup,
575
+ signal_empty
576
+ ]
577
+ end
578
+
579
+ include_examples 'consumes events'
580
+ end
581
+ end
582
+ end
583
+
584
+ describe Mutant::Variable.const_get(:Result)::Value do
585
+ subject { described_class.new(nil) }
586
+
587
+ describe '#timeout?' do
588
+ def apply
589
+ subject.timeout?
590
+ end
591
+
592
+ it 'returns false' do
593
+ expect(apply).to be(false)
594
+ end
595
+ end
596
+ end
597
+
598
+ describe Mutant::Variable.const_get(:Result)::Timeout do
599
+ describe '.new' do
600
+ it 'is instance of timeout' do
601
+ expect(described_class.new.instance_of?(described_class)).to be(true)
602
+ end
603
+
604
+ it 'is idempotent' do
605
+ expect(described_class.new).to be(described_class.new)
606
+ end
607
+ end
608
+
609
+ describe '#timeout?' do
610
+ def apply
611
+ subject.timeout?
612
+ end
613
+
614
+ it 'returns true' do
615
+ expect(apply).to be(true)
616
+ end
617
+ end
618
+ end