package messaging import "testing" func TestCheckAnswers(t *testing.T) { req := QuestionnaireRequest{Questions: []Question{ {Name: "dir", Required: true, Choices: []QuestionChoice{{Value: "a"}, {Value: "b"}}}, {Name: "signals", Multiple: true, Choices: []QuestionChoice{{Value: "x"}, {Value: "y"}}}, {Name: "note", AllowOther: true, Choices: []QuestionChoice{{Value: "n"}}}, {Name: "free"}, }} tests := []struct { name string raw map[string][]string wantErr bool want map[string][]string }{ {"valid", map[string][]string{"dir": {"a"}, "signals": {"x", "y"}}, false, map[string][]string{"dir": {"a"}, "signals": {"x", "y"}}}, {"skipped optional is absent", map[string][]string{"dir": {"b"}, "signals": {" "}}, false, map[string][]string{"dir": {"b"}}}, {"free text and other", map[string][]string{"dir": {"a"}, "note": {"typed"}, "free": {"anything"}}, false, map[string][]string{"dir": {"a"}, "note": {"typed"}, "free": {"anything"}}}, {"required missing", map[string][]string{"signals": {"x"}}, true, nil}, {"unknown question", map[string][]string{"dir": {"a"}, "nope": {"x"}}, true, nil}, {"not a choice", map[string][]string{"dir": {"c"}}, true, nil}, {"two for a single", map[string][]string{"dir": {"a", "b"}}, true, nil}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := CheckAnswers(req, tt.raw) if (err != nil) != tt.wantErr { t.Fatalf("err = %v, wantErr %v", err, tt.wantErr) } if tt.wantErr { return } if len(got) != len(tt.want) { t.Fatalf("answers = %v, want %v", got, tt.want) } for name, values := range tt.want { if len(got[name]) != len(values) || (len(values) > 0 && got[name][0] != values[0]) { t.Fatalf("answers[%s] = %v, want %v", name, got[name], values) } } }) } }