Skip to content

Simple clock library that can be mocked for testing if needed

License

Notifications You must be signed in to change notification settings

anothermemory/clock

Another Memory Clock

Go Report Card Go Doc Coveralls github Release license Codacy grade

This library contains interface and set of implementations for getting current time. For production usage time must be mostly real time. For testing purposes it's often much easier to use mocked time so it will return required time each time.

Table of Contents

Getting Started

Prerequisites

The project is tested with go 1.9 but probably will work with earlier versions.

Installing

Simple go get it

go get github.com/anothermemory/clock

See it in action

Mocked clock for testing

package clock_test

import (
    "time"
    
    "github.com/anothermemory/clock"
)

func ExampleMock() {
	var dummyTime = time.Date(2017, 11, 24, 17, 0, 0, 0, time.Local)
	c := clock.NewMock(dummyTime)
	c.Now() // Will return dummyTime each time
}

func ExampleMockPartial() {
	firstTime := time.Date(2017, 11, 24, 17, 0, 0, 0, time.Local)
    secondTime := time.Date(2018, 11, 24, 17, 0, 0, 0, time.Local)

    c := clock.NewMockPartial(firstTime, secondTime)
    c.Now() // This will return firstTime
    c.Now() // This will return secondTime
    c.Now() // This will return real time
}

Real clock for production

package clock_test

import (
    "time"
    
    "github.com/anothermemory/clock"
)

func ExampleReal() {
    c := clock.NewReal()
    c.Now() // Will return real current time 
}

Built With

  • dep - The dependency management tool for Go

Contributing

Please read CODE_OF_CONDUCT.md for details on our code of conduct, and CONTRIBUTING.md for details on the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Vyacheslav Enis

See also the list of contributors who participated in this project.

License

This project is licensed under the Apache License 2.0 - see the LICENSE.md file for details

About

Simple clock library that can be mocked for testing if needed

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages