-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
163 additions
and
51 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package cmd | ||
|
||
import ( | ||
"html/template" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckFileExistsReturnsTrueWhenFileExists(t *testing.T) { | ||
|
@@ -31,7 +34,7 @@ func TestCheckIsJsonReturnsFalseWhenFileIsNotJson(t *testing.T) { | |
} | ||
|
||
func TestGenerateSecretSantaMachesReturnsAppropriateLenghtOfMatchPairs(t *testing.T) { | ||
var payload []Data | ||
payload := make([]Data, 0, 3) | ||
payload = append(payload, Data{Name: "A", Email: "[email protected]"}) | ||
payload = append(payload, Data{Name: "B", Email: "[email protected]"}) | ||
payload = append(payload, Data{Name: "C", Email: "[email protected]"}) | ||
|
@@ -44,12 +47,12 @@ func TestGenerateSecretSantaMachesReturnsAppropriateLenghtOfMatchPairs(t *testin | |
} | ||
|
||
func TestCircularMatchingAlgorithmReturnsExpectedMatchPairs(t *testing.T) { | ||
var payload []Data | ||
payload := make([]Data, 0, 3) | ||
payload = append(payload, Data{Name: "A", Email: "[email protected]"}) | ||
payload = append(payload, Data{Name: "B", Email: "[email protected]"}) | ||
payload = append(payload, Data{Name: "C", Email: "[email protected]"}) | ||
|
||
var expected []MatchPair | ||
expected := make([]MatchPair, 0, 3) | ||
|
||
expected = append(expected, MatchPair{From: payload[0], To: payload[1]}) | ||
expected = append(expected, MatchPair{From: payload[1], To: payload[2]}) | ||
|
@@ -62,3 +65,69 @@ func TestCircularMatchingAlgorithmReturnsExpectedMatchPairs(t *testing.T) { | |
} | ||
|
||
} | ||
|
||
func TestPopulateEmailBodyReturnsExpectedString(t *testing.T) { | ||
p1 := Data{Name: "A", Email: "[email protected]"} | ||
p2 := Data{Name: "B", Email: "[email protected]"} | ||
|
||
matchPair := MatchPair{From: p1, To: p2} | ||
|
||
tmpl, err := template.New("test").Parse("Hi {{.From.Name}} your Secret Santa match is {{.To.Name}}!") | ||
if err != nil { | ||
t.Error("Expected template to be parsed without error") | ||
} | ||
|
||
emailBody, err := populateEmailBody(matchPair, tmpl) | ||
|
||
if err != nil { | ||
t.Error("Expected populateEmailBody to not return an error") | ||
} | ||
|
||
assert.Equal(t, "Hi A your Secret Santa match is B!", emailBody) | ||
|
||
} | ||
|
||
func TestPopulateEmailBodyReturnsErrorWhenInvalidTemplateProvided(t *testing.T) { | ||
p1 := Data{Name: "A", Email: "[email protected]"} | ||
p2 := Data{Name: "B", Email: "[email protected]"} | ||
|
||
matchPair := MatchPair{From: p1, To: p2} | ||
|
||
tmpl, err := template.New("test").Parse("Hi {{.Something.Unexpected}}!") | ||
if err != nil { | ||
t.Error("Expected template to be parsed without error") | ||
} | ||
|
||
emailBody, err := populateEmailBody(matchPair, tmpl) | ||
|
||
assert.Equal(t, "", emailBody) | ||
if err == nil { | ||
t.Error("Expected populateEmailBody to return an error when invalid template is provided") | ||
} | ||
|
||
} | ||
|
||
func TestGenerateEmailMessagesReturnsExpectedNumberOfMessages(t *testing.T) { | ||
payload := make([]Data, 0, 3) | ||
payload = append(payload, Data{Name: "A", Email: "[email protected]"}) | ||
payload = append(payload, Data{Name: "B", Email: "[email protected]"}) | ||
payload = append(payload, Data{Name: "C", Email: "[email protected]"}) | ||
|
||
matchPairs := make([]MatchPair, 0, 3) | ||
|
||
matchPairs = append(matchPairs, MatchPair{From: payload[0], To: payload[1]}) | ||
matchPairs = append(matchPairs, MatchPair{From: payload[1], To: payload[2]}) | ||
matchPairs = append(matchPairs, MatchPair{From: payload[2], To: payload[0]}) | ||
|
||
tmpl, err := template.New("test").Parse("Hi {{.From.Name}} your Secret Santa match is {{.To.Name}}!") | ||
if err != nil { | ||
t.Error("Expected template to be parsed without error") | ||
} | ||
messages, err := generateEmailMessages(matchPairs, tmpl) | ||
|
||
if err != nil { | ||
t.Error("Expected generateEmailMessages to not return an error") | ||
} | ||
|
||
assert.Equal(t, len(matchPairs), len(messages)) | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= | ||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= | ||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= | ||
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= | ||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= | ||
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |