105 lines
3.3 KiB
Go
105 lines
3.3 KiB
Go
package generatedtext
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidateTomorrowNormalizesFields(t *testing.T) {
|
|
value, err := ValidateTomorrow([]byte(`{
|
|
"summary": " Storms become more likely tomorrow. ",
|
|
"forecast_discussion": [
|
|
" A front will keep showers in the forecast. ",
|
|
"",
|
|
" Temperatures stay seasonable by afternoon. "
|
|
],
|
|
"precipitation_timing": " Rain is most likely before sunrise. "
|
|
}`))
|
|
if err != nil {
|
|
t.Fatalf("ValidateTomorrow() error = %v", err)
|
|
}
|
|
if value.Summary != "Storms become more likely tomorrow." {
|
|
t.Fatalf("Summary = %q, want trimmed summary", value.Summary)
|
|
}
|
|
if strings.Join(value.ForecastDiscussion, "|") != "A front will keep showers in the forecast.|Temperatures stay seasonable by afternoon." {
|
|
t.Fatalf("ForecastDiscussion = %#v, want trimmed non-empty paragraphs", value.ForecastDiscussion)
|
|
}
|
|
if value.PrecipitationTiming != "Rain is most likely before sunrise." {
|
|
t.Fatalf("PrecipitationTiming = %q, want trimmed precipitation timing", value.PrecipitationTiming)
|
|
}
|
|
}
|
|
|
|
func TestValidateTomorrowPreservesRequiredEmptyPrecipitationTiming(t *testing.T) {
|
|
value, err := ValidateTomorrow([]byte(`{
|
|
"summary": "Storms become more likely tomorrow.",
|
|
"forecast_discussion": ["A front will keep showers in the forecast."],
|
|
"precipitation_timing": " "
|
|
}`))
|
|
if err != nil {
|
|
t.Fatalf("ValidateTomorrow() error = %v", err)
|
|
}
|
|
if value.PrecipitationTiming != "" {
|
|
t.Fatalf("PrecipitationTiming = %q, want required empty value", value.PrecipitationTiming)
|
|
}
|
|
}
|
|
|
|
func TestValidateTomorrowRejectsInvalidInput(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{
|
|
name: "malformed",
|
|
in: `{`,
|
|
want: "decode tomorrow generated text",
|
|
},
|
|
{
|
|
name: "unknown field",
|
|
in: `{"summary":"Storms become more likely tomorrow.","forecast_discussion":["A front will keep showers in the forecast."],"extra":"value"}`,
|
|
want: "unsupported field",
|
|
},
|
|
{
|
|
name: "missing summary",
|
|
in: `{"forecast_discussion":["A front will keep showers in the forecast."]}`,
|
|
want: "summary is required",
|
|
},
|
|
{
|
|
name: "blank summary",
|
|
in: `{"summary":" ","forecast_discussion":["A front will keep showers in the forecast."]}`,
|
|
want: "summary is required",
|
|
},
|
|
{
|
|
name: "missing forecast discussion",
|
|
in: `{"summary":"Storms become more likely tomorrow."}`,
|
|
want: "forecast discussion is required",
|
|
},
|
|
{
|
|
name: "blank forecast discussion",
|
|
in: `{"summary":"Storms become more likely tomorrow.","forecast_discussion":[" ",""]}`,
|
|
want: "forecast discussion is required",
|
|
},
|
|
{
|
|
name: "forecast discussion wrong type",
|
|
in: `{"summary":"Storms become more likely tomorrow.","forecast_discussion":"A front will keep showers in the forecast."}`,
|
|
want: "forecast discussion must be an array",
|
|
},
|
|
{
|
|
name: "multiple values",
|
|
in: `{"summary":"Storms become more likely tomorrow.","forecast_discussion":["A front will keep showers in the forecast."]} {}`,
|
|
want: "multiple JSON values",
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
_, err := ValidateTomorrow([]byte(test.in))
|
|
if err == nil {
|
|
t.Fatal("ValidateTomorrow() error = nil, want error")
|
|
}
|
|
if !strings.Contains(err.Error(), test.want) {
|
|
t.Fatalf("ValidateTomorrow() error = %v, want %q", err, test.want)
|
|
}
|
|
})
|
|
}
|
|
}
|