github.com/neilotoole/jsoncolor@v0.7.2-0.20231115150201-1637fae69be1/ascii_test.go (about) 1 package jsoncolor 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 // Based on https://github.com/segmentio/encoding/blob/v0.1.14/ascii/valid_test.go 9 var testCases = [...]struct { 10 valid bool 11 validPrint bool 12 str string 13 }{ 14 {valid: true, validPrint: true, str: ""}, 15 {valid: true, validPrint: true, str: "hello"}, 16 {valid: true, validPrint: true, str: "Hello World!"}, 17 {valid: true, validPrint: true, str: "Hello\"World!"}, 18 {valid: true, validPrint: true, str: "Hello\\World!"}, 19 {valid: true, validPrint: false, str: "Hello\nWorld!"}, 20 {valid: true, validPrint: false, str: "Hello\rWorld!"}, 21 {valid: true, validPrint: false, str: "Hello\tWorld!"}, 22 {valid: true, validPrint: false, str: "Hello\bWorld!"}, 23 {valid: true, validPrint: false, str: "Hello\fWorld!"}, 24 {valid: true, validPrint: true, str: "H~llo World!"}, 25 {valid: true, validPrint: true, str: "H~llo"}, 26 {valid: false, validPrint: false, str: "你好"}, 27 {valid: true, validPrint: true, str: "~"}, 28 {valid: false, validPrint: false, str: "\x80"}, 29 {valid: true, validPrint: false, str: "\x7F"}, 30 {valid: false, validPrint: false, str: "\xFF"}, 31 {valid: true, validPrint: true, str: "some kind of long string with only ascii characters."}, 32 {valid: false, validPrint: false, str: "some kind of long string with a non-ascii character at the end.\xff"}, 33 {valid: true, validPrint: true, str: strings.Repeat("1234567890", 1000)}, 34 } 35 36 func TestAsciiValid(t *testing.T) { 37 for _, tc := range testCases { 38 t.Run(limit(tc.str), func(t *testing.T) { 39 expect := tc.validPrint 40 41 if valid := asciiValidPrint([]byte(tc.str)); expect != valid { 42 t.Errorf("expected %t but got %t", expect, valid) 43 } 44 }) 45 } 46 } 47 48 func TestAsciiValidPrint(t *testing.T) { 49 for _, tc := range testCases { 50 t.Run(limit(tc.str), func(t *testing.T) { 51 expect := tc.validPrint 52 53 if valid := asciiValidPrint([]byte(tc.str)); expect != valid { 54 t.Errorf("expected %t but got %t", expect, valid) 55 } 56 }) 57 } 58 } 59 60 func limit(s string) string { 61 if len(s) > 17 { 62 return s[:17] + "..." 63 } 64 return s 65 }