зеркало из https://github.com/microsoft/clock.git
Readme, example, and travis
This commit is contained in:
Родитель
90057ad437
Коммит
396a60d6af
|
@ -0,0 +1,5 @@
|
|||
language: go
|
||||
go:
|
||||
- 1.4
|
||||
- 1.5
|
||||
- tip
|
|
@ -0,0 +1,21 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 WatchBeam LLC.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
|
@ -0,0 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/WatchBeam/clock"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("the time is %s", displayer{clock.C}.formatted())
|
||||
}
|
||||
|
||||
type displayer struct {
|
||||
c clock.Clock
|
||||
}
|
||||
|
||||
func (d displayer) formatted() string {
|
||||
now := d.c.Now()
|
||||
return fmt.Sprintf("%d:%d:%d", now.Hour(), now.Minute(), now.Second())
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/WatchBeam/clock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDisplaysCorrectly(t *testing.T) {
|
||||
date, _ := time.Parse(time.UnixDate, "Sat Mar 7 11:12:39 PST 2015")
|
||||
c := clock.NewMockClock(date)
|
||||
d := displayer{c}
|
||||
|
||||
assert.Equal(t, "11:12:39", d.formatted())
|
||||
c.AddTime(42 * time.Second)
|
||||
assert.Equal(t, "11:13:21", d.formatted())
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
# clock [![GoDoc](https://godoc.org/github.com/WatchBeam/clock?status.svg)](https://godoc.org/github.com/WatchBeam/clock) [![Build Status](https://travis-ci.org/WatchBeam/clock.svg)](https://travis-ci.org/WatchBeam/clock)
|
||||
|
||||
Time utility with lovely mocking support.
|
||||
|
||||
This is essentially a replacement for the `time` package which allows you to seamlessly swap in mock times, timers, and tickers. See the godocs (link above) for more detailed usage.
|
||||
|
||||
### Example
|
||||
|
||||
**hello.go**
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/WatchBeam/clock"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("the time is %s", displayer{clock.C}.formatted())
|
||||
}
|
||||
|
||||
type displayer struct {
|
||||
c clock.Clock
|
||||
}
|
||||
|
||||
func (d displayer) formatted() string {
|
||||
now := d.c.Now()
|
||||
return fmt.Sprintf("%d:%d:%d", now.Hour(), now.Minute(), now.Second())
|
||||
}
|
||||
```
|
||||
|
||||
**hello_test.go**
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/WatchBeam/clock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestDisplaysCorrectly(t *testing.T) {
|
||||
date, _ := time.Parse(time.UnixDate, "Sat Mar 7 11:12:39 PST 2015")
|
||||
c := clock.NewMockClock(date)
|
||||
d := displayer{c}
|
||||
|
||||
assert.Equal(t, "11:12:39", d.formatted())
|
||||
c.AddTime(42 * time.Second)
|
||||
assert.Equal(t, "11:13:21", d.formatted())
|
||||
}
|
||||
```
|
Загрузка…
Ссылка в новой задаче