Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/crystal-community/timecop.cr
A testing library that allows "time travel," "freezing time," and "time acceleration". Inspired by the ruby-timecop library.
https://github.com/crystal-community/timecop.cr
crystal testing timecop
Last synced: 9 days ago
JSON representation
A testing library that allows "time travel," "freezing time," and "time acceleration". Inspired by the ruby-timecop library.
- Host: GitHub
- URL: https://github.com/crystal-community/timecop.cr
- Owner: crystal-community
- License: mit
- Created: 2018-10-28T20:55:34.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-11-26T18:25:24.000Z (almost 2 years ago)
- Last Synced: 2024-08-01T17:36:36.184Z (3 months ago)
- Topics: crystal, testing, timecop
- Language: Crystal
- Homepage:
- Size: 40 KB
- Stars: 19
- Watchers: 6
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-crystal - timecop.cr - Library for mocking with `Time.now`. Inspired by the [timecop ruby gem](https://github.com/travisjeffery/timecop) (Testing)
- awesome-crystal - timecop.cr - Library for mocking with `Time.now`. Inspired by the [timecop ruby gem](https://github.com/travisjeffery/timecop) (Testing)
README
# timecop.cr
[![CI](https://github.com/crystal-community/timecop.cr/actions/workflows/ci.yml/badge.svg)](https://github.com/crystal-community/timecop.cr/actions/workflows/ci.yml) [![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)
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
Add this to your application's `shard.yml`:
```diff
dependencies:
+ timecop:
+ github: crystal-community/timecop.cr
```## Usage
```crystal
require "timecop"
```### `Timecop.freeze`
```crystal
time = Time.local(2008, 10, 10, 10, 10, 10)
Timecop.freeze(time) do |frozen_time|
frozen_time == Time.local # => true
end
```### `Timecop.travel`
```crystal
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`
```crystal
# seconds will now seem like hours
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 morningTime.local # => "2017-08-29 01:50:21 +0900"
```### `Timecop.safe_mode`
```crystal
Timecop.safe_mode? # => false
Timecop.safe_mode = true# using method without block
Timecop.freeze Time.local(2008, 10, 10, 10, 10, 10)
# => raises Timecop::SafeModeException
```## Development
Pull Requests Welcome!
## Contributing
1. Fork it ()
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) - 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).