Last active
May 23, 2017 04:38
-
-
Save iafan/fca6a69b045aac7cdb00e16f55c06b78 to your computer and use it in GitHub Desktop.
Example of agenda testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import "errors" | |
type Movie struct { | |
TotalTime int `json:"total_time"` | |
CurrentTime int `json:"current_time"` | |
IsPlaying bool `json:"is_playing"` | |
} | |
func (m *Movie) Rewind() { | |
m.CurrentTime = 0 | |
} | |
func (m *Movie) Play() error { | |
if m.IsPlaying { | |
return errors.New("Movie is already playing") | |
} | |
m.IsPlaying = true | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example | |
import ( | |
"encoding/json" | |
"testing" | |
"github.com/iafan/agenda" | |
) | |
func TestMovie(t *testing.T) { | |
agenda.Run(t, ".", func(path string, data []byte) ([]byte, error) { | |
type MovieTestResult struct { | |
M *Movie `json:"movie"` | |
Err interface{} `json:"play_error"` | |
} | |
in := make([]*Movie, 0) | |
if err := json.Unmarshal(data, &in); err != nil { | |
return nil, err | |
} | |
out := make([]*MovieTestResult, len(in)) | |
for i, m := range in { | |
// Rewind() method modifies the struct itself | |
m.Rewind() | |
// m.Play returns the error which we want to compare across test runs | |
err := m.Play() | |
// prepare the output data structure | |
// 1) we want to test that the struct state matches our expectations | |
// 2) we want to test that the returned error (nil or string value) | |
// matches our expectations | |
out[i] = &MovieTestResult{m, agenda.SerializableError(err)} | |
} | |
return json.MarshalIndent(out, "", "\t") | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{"total_time":100,"current_time":0,"is_playing":false}, | |
{"total_time":150,"current_time":35,"is_playing":true}, | |
{"total_time":95,"current_time":4,"is_playing":true}, | |
{"total_time":125,"current_time":110,"is_playing":false} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"movie": { | |
"total_time": 100, | |
"current_time": 0, | |
"is_playing": true | |
}, | |
"play_error": null | |
}, | |
{ | |
"movie": { | |
"total_time": 150, | |
"current_time": 0, | |
"is_playing": true | |
}, | |
"play_error": "Movie is already playing" | |
}, | |
{ | |
"movie": { | |
"total_time": 95, | |
"current_time": 0, | |
"is_playing": true | |
}, | |
"play_error": "Movie is already playing" | |
}, | |
{ | |
"movie": { | |
"total_time": 125, | |
"current_time": 0, | |
"is_playing": true | |
}, | |
"play_error": null | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment