github.com/Seikaijyu/gio@v0.0.1/text/family_parser_test.go (about) 1 package text 2 3 import ( 4 "testing" 5 6 "golang.org/x/exp/slices" 7 ) 8 9 func TestParser(t *testing.T) { 10 type scenario struct { 11 variantName string 12 input string 13 } 14 type testcase struct { 15 name string 16 inputs []scenario 17 expected []string 18 shouldErr bool 19 } 20 21 for _, tc := range []testcase{ 22 { 23 name: "empty", 24 inputs: []scenario{ 25 { 26 variantName: "", 27 }, 28 }, 29 shouldErr: true, 30 }, 31 { 32 name: "comma failure", 33 inputs: []scenario{ 34 { 35 variantName: "bare single", 36 input: ",", 37 }, 38 { 39 variantName: "bare multiple", 40 input: ",, ,,", 41 }, 42 }, 43 shouldErr: true, 44 }, 45 { 46 name: "comma success", 47 inputs: []scenario{ 48 { 49 variantName: "squote", 50 input: "','", 51 }, 52 { 53 variantName: "dquote", 54 input: `","`, 55 }, 56 }, 57 expected: []string{","}, 58 }, 59 { 60 name: "comma success multiple", 61 inputs: []scenario{ 62 { 63 variantName: "squote", 64 input: "',,', ',,'", 65 }, 66 { 67 variantName: "dquote", 68 input: `",,", ",,"`, 69 }, 70 }, 71 expected: []string{",,", ",,"}, 72 }, 73 { 74 name: "backslashes", 75 inputs: []scenario{ 76 { 77 variantName: "bare", 78 input: `\font\\`, 79 }, 80 { 81 variantName: "dquote", 82 input: `"\\font\\\\"`, 83 }, 84 { 85 variantName: "squote", 86 input: `'\\font\\\\'`, 87 }, 88 }, 89 expected: []string{`\font\\`}, 90 }, 91 { 92 name: "invalid backslashes", 93 inputs: []scenario{ 94 { 95 variantName: "dquote", 96 input: `"\\""`, 97 }, 98 { 99 variantName: "squote", 100 input: `'\\''`, 101 }, 102 }, 103 shouldErr: true, 104 }, 105 { 106 name: "too many quotes", 107 inputs: []scenario{ 108 { 109 variantName: "dquote", 110 input: `"""`, 111 }, 112 { 113 variantName: "squote", 114 input: `'''`, 115 }, 116 }, 117 shouldErr: true, 118 }, 119 { 120 name: "serif serif's serif\"s", 121 inputs: []scenario{ 122 { 123 variantName: "bare", 124 input: `serif, serif's, serif"s`, 125 }, 126 { 127 variantName: "squote", 128 input: `'serif', 'serif\'s', 'serif"s'`, 129 }, 130 { 131 variantName: "dquote", 132 input: `"serif", "serif's", "serif\"s"`, 133 }, 134 }, 135 expected: []string{"serif", `serif's`, `serif"s`}, 136 }, 137 { 138 name: "complex list", 139 inputs: []scenario{ 140 { 141 variantName: "bare", 142 input: `Times New Roman, Georgia Common, Helvetica Neue, serif`, 143 }, 144 { 145 variantName: "squote", 146 input: `'Times New Roman', 'Georgia Common', 'Helvetica Neue', 'serif'`, 147 }, 148 { 149 variantName: "dquote", 150 input: `"Times New Roman", "Georgia Common", "Helvetica Neue", "serif"`, 151 }, 152 { 153 variantName: "mixed", 154 input: `Times New Roman, "Georgia Common", 'Helvetica Neue', "serif"`, 155 }, 156 { 157 variantName: "mixed with weird spacing", 158 input: `Times New Roman ,"Georgia Common" , 'Helvetica Neue' ,"serif"`, 159 }, 160 }, 161 expected: []string{"Times New Roman", "Georgia Common", "Helvetica Neue", "serif"}, 162 }, 163 } { 164 t.Run(tc.name, func(t *testing.T) { 165 var p parser 166 for _, scen := range tc.inputs { 167 t.Run(scen.variantName, func(t *testing.T) { 168 actual, err := p.parse(scen.input) 169 if (err != nil) != tc.shouldErr { 170 t.Errorf("unexpected error state: %v", err) 171 } 172 if !slices.Equal(tc.expected, actual) { 173 t.Errorf("expected\n%q\ngot\n%q", tc.expected, actual) 174 } 175 }) 176 } 177 }) 178 } 179 }