package tentaclewire import ( "encoding/json" "errors" "strings" "testing" ) func TestPresentationContract(t *testing.T) { for _, value := range []Presentation{ {Speech: "Hello, é猫😀é"}, {Braille: "é猫😀é"}, {Speech: "Speech text", Braille: "Display text"}, {Speech: "Hello world!", Braille: "Hello world!", Uninterruptible: true}, {Speech: strings.Repeat("é", MaxPresentationBytes/2)}, } { data, _ := json.Marshal(value) decoded, err := DecodePresentation(data) if err != nil || decoded != value { t.Fatalf("round trip: %+v, %v", decoded, err) } } for _, data := range []string{`null`, `[]`, `{}`, `{"speech":null}`, `{"speech":42}`, `{"speech":""}`, `{"speech":"hello","voice":"default"}`, `{"speech":"one","speech":"two"}`, `{"braille":"bad\u0000text"}`, `{"speech":"bad\u001btext"}`, `{"braille":"hello","uninterruptible":true}`, `{"uninterruptible":true}`, `{"speech":"hello","uninterruptible":null}`, `{"speech":"hello","uninterruptible":"true"}`, `{"speech":"hello","uninterruptible":true,"uninterruptible":false}`} { if _, err := DecodePresentation([]byte(data)); !errors.Is(err, ErrProtocol) { t.Fatalf("accepted %s: %v", data, err) } } data, _ := json.Marshal(Presentation{Braille: strings.Repeat("é", MaxPresentationBytes/2+1)}) if _, err := DecodePresentation(data); !errors.Is(err, ErrLimit) { t.Fatal("UTF-8 byte bound", err) } }