Skip to content

Commit

Permalink
Fix Testable Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
taman9333 committed Jun 21, 2024
1 parent 66675f4 commit 59138dd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 12 additions & 1 deletion io_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ import (
"time"
)

var mockTimeNowYear = func() int {
return time.Now().Year()
}

func ExampleIO() {
originalTimeNowYear := mockTimeNowYear
defer func() { mockTimeNowYear = originalTimeNowYear }() // Restore the original function after the test

mockTimeNowYear = func() int {
return 2023
}

io := NewIO(func() int {
return time.Now().Year()
return mockTimeNowYear()
})

result1 := io.Run()
Expand Down
10 changes: 8 additions & 2 deletions task_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ package mo

import (
"fmt"
"time"
)

func ExampleTask() {
originalTimeNowYear := mockTimeNowYear
defer func() { mockTimeNowYear = originalTimeNowYear }() // Restore the original function after the test

mockTimeNowYear = func() int {
return 2023
}

task := NewTask(func() *Future[int] {
return NewFuture(func(resolve func(int), reject func(error)) {
resolve(time.Now().Year())
resolve(mockTimeNowYear())
})
})

Expand Down

0 comments on commit 59138dd

Please sign in to comment.