Improve tests embebbing#40
Conversation
dmytro-kashcheiev
left a comment
There was a problem hiding this comment.
@Vladyslav-Kuksiuk LGTM with minor comment.
|
|
||
| class MultiLinePatternSample { | ||
|
|
||
| private static final String LINE_SEPARATOR = "\n"; |
There was a problem hiding this comment.
I takes some time for me to understand that LINE_SEPARATOR in readme is a part of source code and not a part of the Glob syntax. Lets change this constant name to something that doesn't sound like a property for <embed-code tag
There was a problem hiding this comment.
Pull request overview
This PR extends embed-code’s glob-based matching to support multi-line start/end/line patterns separated by \n, and adds support for writing quotes in XML attributes as \" (converted to " internally). It updates documentation and adds new test fixtures + test cases to validate the behavior.
Changes:
- Add multi-line pattern parsing/matching for
start,end, andlinepatterns using\nseparators (with\\nfor literal\ntext). - Allow backslash-escaped quotes (
\") in<embed-code ...>XML attributes by converting them to"before XML unmarshalling. - Add new embedding/parsing tests and documentation examples; bump version to
1.2.1.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
embedding/parsing/pattern.go |
Adds multi-line pattern splitting and matching helpers. |
embedding/parsing/instruction.go |
Implements multi-line matching for line, and multi-line-aware start/end matching. |
embedding/parsing/xml_parse.go |
Preprocesses \" into " prior to xml.Unmarshal. |
embedding/parsing/instruction_test.go |
Adds tests for \" parsing and literal glob-character escaping. |
embedding/embedding_test.go |
Adds end-to-end embedding tests covering multi-line patterns and literal \\n matching. |
EMBEDDING.md |
Documents multi-line patterns and escaping rules (including \"). |
test/resources/docs/escaped-newline-pattern.md |
New doc fixture for multi-line start/end patterns. |
test/resources/docs/escaped-newline-exact-pattern.md |
New doc fixture for exact (^...$) multi-line patterns. |
test/resources/docs/escaped-newline-line-pattern.md |
New doc fixture for multi-line line patterns. |
test/resources/docs/escaped-newline-literal-pattern.md |
New doc fixture for matching literal \\n text. |
test/resources/code/java/org/example/MultiLinePatternSample.java |
New code fixture used by multi-line pattern embedding tests. |
test/resources/code/java/literal-patterns.txt |
New text fixture for literal wildcard/caret/dollar matching tests. |
main.go |
Bumps application version to 1.2.1. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func matchLineSequence(pattern *Pattern, lines []string, startFrom int) (int, int, bool) { | ||
| patternLines, _ := pattern.linePatterns() | ||
| lineCount := len(patternLines) | ||
| lastStart := len(lines) - lineCount | ||
| for start := startFrom; start <= lastStart; start++ { | ||
| end := start + lineCount | ||
| if pattern.MatchLineSequence(lines[start:end]) { | ||
| return start, end - 1, true |
| ``` | ||
| ```` | ||
|
|
||
| It s possible to write quote characters in patterns as `\"` instead of the XML entity `"`. |
| // linePatterns returns trimmed pattern lines separated by an escaped newline. | ||
| func (p Pattern) linePatterns() ([]string, bool) { | ||
| var patternLines []string | ||
| var line strings.Builder | ||
| hasSeparator := false | ||
| for i := 0; i < len(p.sourceGlob); { | ||
| remaining := p.sourceGlob[i:] | ||
| switch { | ||
| case strings.HasPrefix(remaining, escapedLineSeparator): | ||
| line.WriteString(escapedLineSeparator) | ||
| i += len(escapedLineSeparator) | ||
| case strings.HasPrefix(remaining, lineSeparator): | ||
| patternLines = append(patternLines, strings.TrimSpace(line.String())) | ||
| line.Reset() | ||
| hasSeparator = true | ||
| i += len(lineSeparator) | ||
| default: | ||
| line.WriteByte(p.sourceGlob[i]) | ||
| i++ | ||
| } | ||
| } | ||
| patternLines = append(patternLines, strings.TrimSpace(line.String())) | ||
|
|
This PR improves pattern-based embedding by adding explicit multi-line matching for
start,endandlinepatterns. Instead of requiring a single source line to identify a fragment boundary, patterns can now use\nto match consecutive source lines.For example:
Each line in a multi-line pattern follows the same glob rules as regular one-line patterns, including
^and$for exact matching.Spaces around
\nare ignored.Additional change: added possibility to write quote characters in patterns as
\"instead of the XML entity".Resolves #44.