Skip to content

Commit

Permalink
Merge pull request #1445 from mbj/fix/coverge
Browse files Browse the repository at this point in the history
Fix coverage
  • Loading branch information
mbj committed May 12, 2024
2 parents b928205 + bfe2897 commit a8b5509
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 27 deletions.
6 changes: 3 additions & 3 deletions lib/mutant/parallel/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class Reader
attr_reader :log

def error
@errors.first
Util.max_one(@errors)
end

def result
@results.first
Util.max_one(@results)
end

def initialize(*)
Expand Down Expand Up @@ -96,7 +96,7 @@ def advance_result
end

def length
@lengths.first
Util.max_one(@lengths)
end

def advance_log
Expand Down
25 changes: 23 additions & 2 deletions lib/mutant/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,30 @@ module Util
#
# @return [Object] first entry
def self.one(array)
return array.first if array.one?
case array
in [value]
value
else
fail SizeError, "expected size to be exactly 1 but size was #{array.size}"
end
end

fail SizeError, "expected size to be exactly 1 but size was #{array.size}"
# Return only element in array if it contains max one member
#
# @param array [Array]
#
# @return [Object] first entry
# @return [nil] if empty
#
# rubocop:disable Lint/EmptyInPattern
def self.max_one(array)
case array
in []
in [value]
value
else
fail SizeError, "expected size to be max 1 but size was #{array.size}"
end
end
end # Util
end # Mutant
22 changes: 0 additions & 22 deletions spec/unit/mutant/util/one_spec.rb

This file was deleted.

107 changes: 107 additions & 0 deletions spec/unit/mutant/util_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# frozen_string_literal: true

RSpec.describe Mutant::Util, '.one' do
let(:item) { instance_double(Object) }

def apply
described_class.one(array)
end

context 'when array has exactly one element' do
context 'and that element is nil' do
let(:array) { [nil] }

it 'returns nil' do
expect(apply).to be(nil)
end
end

context 'and that element is false' do
let(:array) { [false] }

it 'returns false' do
expect(apply).to be(false)
end
end

context 'and that element is a regular object' do
let(:array) { [item] }

it 'returns first element' do
expect(apply).to be(item)
end
end
end

context 'when array is empty' do
let(:array) { [] }

it 'raises expected error' do
expect { apply }
.to raise_error(described_class::SizeError)
.with_message('expected size to be exactly 1 but size was 0')
end
end

context 'when array has more than one element' do
let(:array) { [1, 2] }

it 'raises expected error' do
expect { apply }
.to raise_error(described_class::SizeError)
.with_message('expected size to be exactly 1 but size was 2')
end
end
end

RSpec.describe Mutant::Util, '.max_one' do
let(:item) { instance_double(Object) }

def apply
described_class.max_one(array)
end

context 'when array has exactly one element' do
context 'and that element is nil' do
let(:array) { [nil] }

it 'returns nil' do
expect(apply).to be(nil)
end
end

context 'and that element is false' do
let(:array) { [false] }

it 'returns false' do
expect(apply).to be(false)
end
end

context 'and that element is a regular object' do
let(:array) { [item] }

it 'returns first element' do
expect(apply).to be(item)
end
end
end

context 'when array is empty' do
let(:array) { [] }

it 'returns nil' do
expect(apply).to be(nil)
end
end

context 'when array has more than one element' do
let(:array) { [1, 2] }

it 'raises expected error' do
expect { apply }
.to raise_error(described_class::SizeError)
.with_message('expected size to be max 1 but size was 2')
end
end
end

0 comments on commit a8b5509

Please sign in to comment.