Skip to content
Permalink
Browse files

Added test to check if correct error is returned for incorrect yaml

Modified the function to return a status array and an error as well.
  • Loading branch information...
kiriappeee committed Jan 25, 2018
1 parent 115e95b commit 5b3cbfeff21e5c119c30bd9c676255685991287b
Showing with 40 additions and 5 deletions.
  1. +5 −4 setstatus.go
  2. +35 −1 setstatus_test.go
@@ -12,13 +12,14 @@ type Status struct {
StatusText string `yaml:"statusText"`
}

func ConvertTextToStructArray(textToConvert string) []Status {
func ConvertTextToStructArray(textToConvert string) ([]Status, error) {

var statusesToReturn []Status
err := yaml.Unmarshal([]byte(textToConvert), &statusesToReturn)
if err != nil {
log.Fatalf("couldn't convert it %s", err)
log.Printf("ssucore.ConvertTextToStructArray: couldn't convert it %s", err)
return nil, err
}

return statusesToReturn
}
return statusesToReturn, nil
}
@@ -2,6 +2,7 @@ package ssucore

import (
"testing"
"strings"
)

func TestSlackStatusCanBeInitializedCorrectly(t *testing.T) {
@@ -29,7 +30,10 @@ func TestSlackStatusCanBeReadCorrectlyFromString(t *testing.T) {
emoji: ''
statusText: In Focus mode
`
var statusesToTest []Status = ConvertTextToStructArray(textToReadIn)
statusesToTest, err := ConvertTextToStructArray(textToReadIn)
if err != nil {
t.Fatalf("An error occured when converting text. Did not expect error. Error was %v", err)
}
if len(statusesToTest) != 4 {
t.Fatalf("Length of returned array was %d. Expected 4", len(statusesToTest))
} else {
@@ -46,3 +50,33 @@ func TestSlackStatusCanBeReadCorrectlyFromString(t *testing.T) {
}

}

func TestAnErrorIsThrownWhenAPoorlyFormattedYamlIsProvided(t *testing.T) {
textToReadIn :=`
- statusName: lunch
emoji: chompy
statusText: Having lunch
- statusName: resting
emoji
statusText: Resting
- statusName: awesome
emoji: awesome
statusText: ''
-
`
statusesToTest, err := ConvertTextToStructArray(textToReadIn)
if err == nil {
t.Fatalf("Error was nil. Expected an error")
}

if ! strings.Contains(err.Error(), "could not find expected") {
t.Fatalf("Error message was not the expected one. Received: %s", err.Error())
}

if statusesToTest != nil{
t.Fatalf("Statuses to test was %v. Expected nil", statusesToTest)
}

}

0 comments on commit 5b3cbfe

Please sign in to comment.
You can’t perform that action at this time.