TestExpressionBreakingWithQuotes tests that quotes are handled correctly during line breaking
(t *testing.T)
| 1191 | |
| 1192 | // TestExpressionBreakingWithQuotes tests that quotes are handled correctly during line breaking |
| 1193 | func TestExpressionBreakingWithQuotes(t *testing.T) { |
| 1194 | tests := []struct { |
| 1195 | name string |
| 1196 | expression string |
| 1197 | }{ |
| 1198 | { |
| 1199 | name: "single quoted strings", |
| 1200 | expression: "contains(github.event.issue.body, 'this is a very long string that should not be broken even though it contains || and && operators') && github.event.action == 'opened'", |
| 1201 | }, |
| 1202 | { |
| 1203 | name: "double quoted strings", |
| 1204 | expression: `contains(github.event.issue.body, "this is a very long string that should not be broken even though it contains || and && operators") && github.event.action == "opened"`, |
| 1205 | }, |
| 1206 | { |
| 1207 | name: "mixed quotes", |
| 1208 | expression: `contains(github.event.issue.body, 'single quoted || string') && contains(github.event.comment.body, "double quoted && string")`, |
| 1209 | }, |
| 1210 | { |
| 1211 | name: "escaped quotes", |
| 1212 | expression: `contains(github.event.issue.body, 'string with \\'escaped\\' quotes || and operators') && github.event.action == 'opened'`, |
| 1213 | }, |
| 1214 | } |
| 1215 | |
| 1216 | for _, tt := range tests { |
| 1217 | t.Run(tt.name, func(t *testing.T) { |
| 1218 | lines := BreakLongExpression(tt.expression) |
| 1219 | |
| 1220 | // Verify that quotes are preserved and no breaking happens inside quoted strings |
| 1221 | joined := strings.Join(lines, " ") |
| 1222 | originalNorm := strings.Join(strings.Fields(tt.expression), " ") |
| 1223 | joinedNorm := strings.Join(strings.Fields(joined), " ") |
| 1224 | |
| 1225 | if joinedNorm != originalNorm { |
| 1226 | t.Errorf("Expression with quotes not preserved correctly\nOriginal: %s\nJoined: %s", originalNorm, joinedNorm) |
| 1227 | } |
| 1228 | |
| 1229 | // Check that no line contains half of a quoted string |
| 1230 | for _, line := range lines { |
| 1231 | singleQuotes := strings.Count(line, "'") |
| 1232 | doubleQuotes := strings.Count(line, `"`) |
| 1233 | |
| 1234 | // Count non-escaped quotes |
| 1235 | nonEscapedSingle := singleQuotes - strings.Count(line, `\'`) |
| 1236 | nonEscapedDouble := doubleQuotes - strings.Count(line, `\"`) |
| 1237 | |
| 1238 | if nonEscapedSingle%2 != 0 { |
| 1239 | t.Errorf("Line has unmatched single quotes: %s", line) |
| 1240 | } |
| 1241 | if nonEscapedDouble%2 != 0 { |
| 1242 | t.Errorf("Line has unmatched double quotes: %s", line) |
| 1243 | } |
| 1244 | } |
| 1245 | }) |
| 1246 | } |
| 1247 | } |
nothing calls this directly
no test coverage detected