Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an exception when timestamp is malformed #152

Merged
merged 1 commit into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Biz.configure do |config|
end
```

Configured timestamps must be in either `HH:MM` or `HH:MM:SS` format.

Shifts act as exceptions to the hours configured for a particular date; that is,
if a date is configured with both hours-based intervals and shifts, the shifts
are in force and the intervals are disregarded.
Expand Down
5 changes: 4 additions & 1 deletion lib/biz/day_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ def from_timestamp(timestamp)
match[:minute].to_i * Time.minute_seconds +
match[:second].to_i
)
}
} or fail(
Error::Configuration,
'invalid timestamp: must be in `HH:MM` or `HH:MM:SS` format'
)
end

def midnight
Expand Down
66 changes: 48 additions & 18 deletions spec/day_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,33 +78,63 @@
end

describe '.from_timestamp' do
context 'when the timestamp is malformed' do
context 'when the timestamp is not a timestamp' do
let(:timestamp) { 'timestamp' }

it 'returns nil' do
expect(described_class.from_timestamp(timestamp)).to eq nil
it 'raises a configuration error' do
expect {
described_class.from_timestamp(timestamp)
}.to raise_error Biz::Error::Configuration
end
end

context 'when the timestamp is in `H:MM` format' do
let(:timestamp) { '5:35' }

it 'raises a configuration error' do
expect {
described_class.from_timestamp(timestamp)
}.to raise_error Biz::Error::Configuration
end
end

context 'when the timestamp is well formed' do
context 'without seconds' do
let(:timestamp) { '21:43' }
context 'when the timestamp is in `HH:M` format' do
let(:timestamp) { '12:3' }

it 'returns the appropriate day time' do
expect(described_class.from_timestamp(timestamp)).to eq(
described_class.new(day_second(hour: 21, min: 43))
)
end
it 'raises a configuration error' do
expect {
described_class.from_timestamp(timestamp)
}.to raise_error Biz::Error::Configuration
end
end

context 'with seconds' do
let(:timestamp) { '10:55:23' }
context 'when the timestamp is in `HH:MM:S` format' do
let(:timestamp) { '11:35:3' }

it 'returns the appropriate day time' do
expect(described_class.from_timestamp(timestamp)).to eq(
described_class.new(day_second(hour: 10, min: 55, sec: 23))
)
end
it 'raises a configuration error' do
expect {
described_class.from_timestamp(timestamp)
}.to raise_error Biz::Error::Configuration
end
end

context 'when the timestamp is in `HH:MM` format' do
let(:timestamp) { '21:43' }

it 'returns the appropriate day time' do
expect(described_class.from_timestamp(timestamp)).to eq(
described_class.new(day_second(hour: 21, min: 43))
)
end
end

context 'when the timestamp is in `HH:MM:SS` format' do
let(:timestamp) { '10:55:23' }

it 'returns the appropriate day time' do
expect(described_class.from_timestamp(timestamp)).to eq(
described_class.new(day_second(hour: 10, min: 55, sec: 23))
)
end
end
end
Expand Down