Skip to content

Commit 1ffe23f

Browse files
authored
Refactor/cck testing (#1791)
* When there is no error, return nil instead of empty array Apportion an undiagnosable error when it occurs * Ensure both `nil` and `[]` are cleaned up in test response Avoid void context of items not being returned when erroneous * Remove redundant part of keyschecker * Refactoring of bloated class now complete * Add changelog * Fix situation for blank keys needing compacting Fix situation where extra keys are found but they are :ci inside a meta message * Fix up tests to deal with nil response vs empty response Amend one test to signify we only return 1 error now not 2 * Rubocop fix
1 parent 1ff4596 commit 1ffe23f

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Please visit [cucumber/CONTRIBUTING.md](https://github.com/cucumber/cucumber/blo
1111
## [Unreleased]
1212
### Changed
1313
- Updated `cucumber-compatibility-kit` to v19
14+
- Optimised `compatibility` tests (That use the CCK), so that tests run slightly more optimal (Creating less empty arrays)
1415

1516
### Fixed
1617
- Fixed an issue where the html-formatter wasn't respecting the new structure for `StackTrace` cucumber messages ([#1790](https://github.com/cucumber/cucumber-ruby/pull/1790) [luke-hill](https://github.com/luke-hill))

compatibility/spec/cck/keys_checker_spec.rb

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,40 @@
66

77
describe CCK::KeysChecker do
88
describe '#compare' do
9-
let(:expected_values) { Cucumber::Messages::Attachment.new(url: 'https://foo.com', file_name: 'file.extension') }
10-
let(:erroneous_values) { Cucumber::Messages::Attachment.new(source: '1', test_step_id: '123') }
11-
let(:wrong_values) { Cucumber::Messages::Attachment.new(url: 'https://otherfoo.com', file_name: 'file.other') }
9+
let(:expected_kvps) { Cucumber::Messages::Attachment.new(url: 'https://foo.com', file_name: 'file.extension', test_step_id: 123_456) }
10+
let(:missing_kvps) { Cucumber::Messages::Attachment.new(url: 'https://foo.com') }
11+
let(:extra_kvps) { Cucumber::Messages::Attachment.new(url: 'https://foo.com', file_name: 'file.extension', test_step_id: 123_456, source: '1') }
12+
let(:missing_and_extra_kvps) { Cucumber::Messages::Attachment.new(file_name: 'file.extension', test_step_id: 123_456, test_run_started_id: 123_456) }
13+
let(:wrong_values) { Cucumber::Messages::Attachment.new(url: 'https://otherfoo.com', file_name: 'file.other', test_step_id: 456_789) }
1214

1315
it 'finds missing keys' do
14-
expect(described_class.compare(erroneous_values, expected_values)).to include(
15-
'Missing keys in message Cucumber::Messages::Attachment: [:file_name, :url]'
16+
expect(described_class.compare(missing_kvps, expected_kvps)).to eq(
17+
'Missing keys in message Cucumber::Messages::Attachment: [:file_name, :test_step_id]'
1618
)
1719
end
1820

1921
it 'finds extra keys' do
20-
expect(described_class.compare(erroneous_values, expected_values)).to include(
21-
'Detected extra keys in message Cucumber::Messages::Attachment: [:source, :test_step_id]'
22+
expect(described_class.compare(extra_kvps, expected_kvps)).to eq(
23+
'Detected extra keys in message Cucumber::Messages::Attachment: [:source]'
2224
)
2325
end
2426

25-
it 'finds extra and missing keys' do
26-
expect(described_class.compare(erroneous_values, expected_values)).to contain_exactly(
27-
'Missing keys in message Cucumber::Messages::Attachment: [:file_name, :url]',
28-
'Detected extra keys in message Cucumber::Messages::Attachment: [:source, :test_step_id]'
27+
it 'finds the extra keys first' do
28+
expect(described_class.compare(missing_and_extra_kvps, expected_kvps)).to eq(
29+
'Detected extra keys in message Cucumber::Messages::Attachment: [:test_run_started_id]'
2930
)
3031
end
3132

3233
it 'does not care about the values' do
33-
expect(described_class.compare(expected_values, wrong_values)).to be_empty
34+
expect(described_class.compare(expected_kvps, wrong_values)).to be_nil
3435
end
3536

3637
context 'when default values are omitted' do
3738
let(:default_set) { Cucumber::Messages::Duration.new(seconds: 0, nanos: 12) }
3839
let(:default_not_set) { Cucumber::Messages::Duration.new(nanos: 12) }
3940

4041
it 'does not raise an exception' do
41-
expect(described_class.compare(default_set, default_not_set)).to be_empty
42+
expect(described_class.compare(default_set, default_not_set)).to be_nil
4243
end
4344
end
4445

@@ -49,7 +50,7 @@
4950
detected = Cucumber::Messages::Meta.new(ci: Cucumber::Messages::Ci.new(name: 'Some CI'))
5051
expected = Cucumber::Messages::Meta.new
5152

52-
expect(described_class.compare(detected, expected)).to be_empty
53+
expect(described_class.compare(detected, expected)).to be_nil
5354
end
5455
end
5556

compatibility/support/cck/keys_checker.rb

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ def initialize(detected, expected)
1414
end
1515

1616
def compare
17-
return [] if identical_keys?
17+
return if identical_keys?
18+
return "Detected extra keys in message #{message_name}: #{extra_keys}" if extra_keys.any?
1819

19-
errors << "Detected extra keys in message #{message_name}: #{extra_keys}" if extra_keys.any?
20-
errors << "Missing keys in message #{message_name}: #{missing_keys}" if missing_keys.any?
21-
errors
20+
"Missing keys in message #{message_name}: #{missing_keys}" if missing_keys.any?
2221
rescue StandardError => e
2322
["Unexpected error: #{e.message}"]
2423
end
2524

2625
private
2726

27+
def identical_keys?
28+
detected_keys == expected_keys
29+
end
30+
2831
def detected_keys
2932
@detected_keys ||= ordered_uniq_hash_keys(detected)
3033
end
@@ -33,32 +36,24 @@ def expected_keys
3336
@expected_keys ||= ordered_uniq_hash_keys(expected)
3437
end
3538

36-
def identical_keys?
37-
detected_keys == expected_keys
38-
end
39-
40-
def missing_keys
41-
(expected_keys - detected_keys).reject { |key| meta_message? && key == :ci }
39+
def ordered_uniq_hash_keys(object)
40+
object.to_h(reject_nil_values: true).keys.sort
4241
end
4342

4443
def extra_keys
4544
(detected_keys - expected_keys).reject { |key| meta_message? && key == :ci }
4645
end
4746

47+
def missing_keys
48+
(expected_keys - detected_keys).reject { |key| meta_message? && key == :ci }
49+
end
50+
4851
def meta_message?
4952
detected.instance_of?(Cucumber::Messages::Meta)
5053
end
5154

5255
def message_name
5356
detected.class.name
5457
end
55-
56-
def ordered_uniq_hash_keys(object)
57-
object.to_h(reject_nil_values: true).keys.sort
58-
end
59-
60-
def errors
61-
@errors ||= []
62-
end
6358
end
6459
end

compatibility/support/cck/messages_comparator.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def initialize(detected, expected)
1212
end
1313

1414
def errors
15-
all_errors.flatten
15+
all_errors.compact
1616
end
1717

1818
private
@@ -51,7 +51,6 @@ def compare_message(detected, expected)
5151
return if ignorable?(detected)
5252
return if incomparable?(detected)
5353

54-
# TODO: This needs refactoring as it's becoming rather large and bloated
5554
all_errors << CCK::KeysChecker.compare(detected, expected)
5655
compare_sub_messages(detected, expected)
5756
end

0 commit comments

Comments
 (0)