Skip to content

Commit

Permalink
Merge pull request #2 from Sija/crystal-0.33.0
Browse files Browse the repository at this point in the history
Refactors and support for Crystal 0.33.0
  • Loading branch information
robacarp authored Feb 11, 2020
2 parents 5450316 + 4a22758 commit 0c77795
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 236 deletions.
16 changes: 16 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
language: crystal

crystal:
- latest
- nightly

matrix:
allow_failures:
- crystal: nightly

install:
- shards install

script:
- crystal spec
- crystal tool format --check
- bin/ameba
71 changes: 37 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# timecop.cr
A [timecop](https://github.com/travisjeffery/timecop) inspired library to allow easy manipulation of time in tests. Originally authored by [TobiasGSmollett](https://github.com/TobiasGSmollett).

## Status

As of 2019-06-14
[![Build Status](https://travis-ci.com/crystal-community/timecop.cr.svg?branch=master)](https://travis-ci.com/crystal-community/timecop.cr) [![Releases](https://img.shields.io/github/release/crystal-community/timecop.cr.svg)](https://github.com/crystal-community/timecop.cr/releases) [![License](https://img.shields.io/github/license/crystal-community/timecop.cr.svg)](https://github.com/crystal-community/timecop.cr/blob/master/LICENSE)

[![Crystal Version](https://img.shields.io/badge/crystal-0.29-blueviolet.svg?longCache=true&style=for-the-badge)](https://crystal-lang.org/)
[![Travis.com](https://img.shields.io/travis/com/crystal-community/timecop.cr.svg?style=for-the-badge)](https://api.travis-ci.com/crystal-community/timecop.cr.svg)
A [timecop](https://github.com/travisjeffery/timecop) inspired library to allow easy manipulation of time in tests. Originally authored by [TobiasGSmollett](https://github.com/TobiasGSmollett).

## Installation

Expand All @@ -24,61 +20,68 @@ dependencies:
require "timecop"
```

### Timecop.freeze
### `Timecop.freeze`

```crystal
date = Time.new(2008, 10, 10, 10, 10, 10)
Timecop.freeze(date) do |frozen_time|
puts "#{frozen_time == Time.local}" # => true
time = Time.local(2008, 10, 10, 10, 10, 10)
Timecop.freeze(time) do |frozen_time|
frozen_time == Time.local # => true
end
```

### Timecop.travel
### `Timecop.travel`

```crystal
Timecop.travel(Time.new(2014, 1, 1, 0, 0, 0)) do
puts Time.local # => 2014-01-01 00:00:00 +0900
sleep(5)
puts Time.local # => 2014-01-01 00:00:05 +0900
Timecop.travel(Time.local(2014, 1, 1, 0, 0, 0)) do
Time.local # => "2014-01-01 00:00:00 +0900"
sleep(5.seconds)
Time.local # => "2014-01-01 00:00:05 +0900"
end
```

### Timecop.scale
### `Timecop.scale`

```crystal
# seconds will now seem like hours
Timecop.scale(Time.local, 3600)
puts Time.local # => 2017-08-28 23:50:06 +0900
sleep(2.0)
# seconds later, hours have passed and it's gone from 23pm at night to 1am in the morning
puts Time.local # => 2017-08-29 01:50:21 +0900
Timecop.scale(3600)
Time.local # => "2017-08-28 23:50:06 +0900"
sleep(2.seconds)
# 2 seconds later, hours have passed and it's gone from
# 23pm at night to 1am in the morning
Time.local # => "2017-08-29 01:50:21 +0900"
```

### Timecop.safe_mode
### `Timecop.safe_mode`

```crystal
# turn on safe mode
Timecop.safe_mode? # => false
Timecop.safe_mode = true
Timecop.safe_mode? # => true
# using method without block
Timecop.freeze Time.new(2008, 10, 10, 10, 10, 10)
# => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed.
Timecop.freeze Time.local(2008, 10, 10, 10, 10, 10)
# => raises Timecop::SafeModeException
```

## Development

Pull Request Welcome
Pull Requests Welcome!

## Contributing

1. Fork it ( https://github.com/TobiasGSmollett/timecop.cr/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
1. Fork it (<https://github.com/TobiasGSmollett/timecop.cr/fork>)
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request

## Contributors

- [TobiasGSmollett](https://github.com/TobiasGSmollett) TobiasGSmollett - creator
- [Robacarp](https://github.com/robacarp) maintainer
- [TobiasGSmollett](https://github.com/TobiasGSmollett) - creator
- [Robacarp](https://github.com/robacarp) - maintainer
- [Sija](https://github.com/Sija) - maintainer

## Thanks
Thanks to Travis Jeffery for his awesome work on [timecop](https://github.com/travisjeffery/timecop).
10 changes: 8 additions & 2 deletions shard.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
name: timecop
version: 0.3.0
version: 0.4.0

authors:
- TobiasGSmollett <[email protected]>
- robacarp
- Sijawusz Pur Rahnama <[email protected]>

crystal: 0.29.0
development_dependencies:
ameba:
github: crystal-ameba/ameba
version: ~> 0.11.0

crystal: 0.33.0

license: MIT
5 changes: 2 additions & 3 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "spec"
require "../src/timecop"

def times_effectively_equal(time1, time2, seconds_interval = 1.0)
(time1 - time2).abs.to_f <= seconds_interval
end
Spec.before_each { Timecop.return }
Spec.after_each { Timecop.return }
36 changes: 11 additions & 25 deletions spec/time_stack_item_spec.cr
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
require "./spec_helper"

describe Timecop do
describe Timecop::TimeStackItem do
it "properly delegates time-related methods" do
time = Time.local(2012, 7, 28, 20, 0)

it "new_with_time" do
t = Time.now
y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.minute, t.second
stack_item = Timecop::TimeStackItem.new(:freeze, t)

true.should eq(y == stack_item.year)
true.should eq(m == stack_item.month)
true.should eq(d == stack_item.day)
true.should eq(h == stack_item.hour)
true.should eq(min == stack_item.minute)
true.should eq(s == stack_item.second)
end

it "new_with_time_and_arguments" do
t = Time.new(2012, 7, 28, 20, 0)
y, m, d, h, min, s = t.year, t.month, t.day, t.hour, t.minute, t.second
stack_item = Timecop::TimeStackItem.new(:freeze, t)

true.should eq(y == stack_item.year)
true.should eq(m == stack_item.month)
true.should eq(d == stack_item.day)
true.should eq(h == stack_item.hour)
true.should eq(min == stack_item.minute)
true.should eq(s == stack_item.second)
Timecop::TimeStackItem.new(:freeze, time).tap do |stack_item|
stack_item.year.should eq(time.year)
stack_item.month.should eq(time.month)
stack_item.day.should eq(time.day)
stack_item.hour.should eq(time.hour)
stack_item.minute.should eq(time.minute)
stack_item.second.should eq(time.second)
end
end
end
Loading

0 comments on commit 0c77795

Please sign in to comment.