|
| 1 | +// Copyright 2026 The kpt Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package docs |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "pgregory.net/rapid" |
| 23 | +) |
| 24 | + |
| 25 | +// For any three strings (short, long, examples), formatting them into a README |
| 26 | +// with mdtogo markers and then parsing that README with ParseMarkers SHALL |
| 27 | +// produce a Sections struct with fields equal to the original strings (after trimming). |
| 28 | + |
| 29 | +// genSectionContent generates arbitrary non-empty strings that do not contain |
| 30 | +// mdtogo markers (which would confuse the parser). |
| 31 | +func genSectionContent() *rapid.Generator[string] { |
| 32 | + return rapid.Custom(func(t *rapid.T) string { |
| 33 | + s := rapid.StringMatching(`[a-zA-Z0-9 \t\n.,;:!?(){}\[\]'"/_-]{1,200}`).Draw(t, "content") |
| 34 | + // Ensure the generated content does not accidentally contain marker strings. |
| 35 | + s = strings.ReplaceAll(s, "<!--mdtogo:", "") |
| 36 | + s = strings.ReplaceAll(s, "<!--", "") |
| 37 | + return s |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +// formatMarkedREADME formats three section strings into a README with mdtogo markers. |
| 42 | +func formatMarkedREADME(short, long, examples string) string { |
| 43 | + return fmt.Sprintf(`<!--mdtogo:Short--> |
| 44 | +%s |
| 45 | +<!--mdtogo--> |
| 46 | +
|
| 47 | +<!--mdtogo:Long--> |
| 48 | +%s |
| 49 | +<!--mdtogo--> |
| 50 | +
|
| 51 | +<!--mdtogo:Examples--> |
| 52 | +%s |
| 53 | +<!--mdtogo--> |
| 54 | +`, short, long, examples) |
| 55 | +} |
| 56 | + |
| 57 | +// genNoMarkerContent generates arbitrary strings guaranteed not to contain |
| 58 | +// any mdtogo marker substrings. |
| 59 | +func genNoMarkerContent() *rapid.Generator[string] { |
| 60 | + return rapid.Custom(func(t *rapid.T) string { |
| 61 | + s := rapid.StringMatching(`[a-zA-Z0-9 \t\n.,;:!?(){}\[\]'"/_-]{0,300}`).Draw(t, "content") |
| 62 | + // Strip anything that could form a marker. |
| 63 | + s = strings.ReplaceAll(s, "<!--mdtogo:", "") |
| 64 | + s = strings.ReplaceAll(s, "<!--mdtogo", "") |
| 65 | + s = strings.ReplaceAll(s, "<!--", "") |
| 66 | + return s |
| 67 | + }) |
| 68 | +} |
| 69 | + |
| 70 | +func TestProperty7_MissingMarkersFallback(t *testing.T) { |
| 71 | + rapid.Check(t, func(t *rapid.T) { |
| 72 | + content := genNoMarkerContent().Draw(t, "readme") |
| 73 | + |
| 74 | + sections := ParseMarkers([]byte(content)) |
| 75 | + |
| 76 | + if sections.Short != "" { |
| 77 | + t.Fatalf("Short should be empty for content without markers, got: %q", sections.Short) |
| 78 | + } |
| 79 | + if sections.Examples != "" { |
| 80 | + t.Fatalf("Examples should be empty for content without markers, got: %q", sections.Examples) |
| 81 | + } |
| 82 | + if got, want := sections.Long, strings.TrimSpace(content); got != want { |
| 83 | + t.Fatalf("Long mismatch:\n got: %q\n want: %q", got, want) |
| 84 | + } |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +func TestProperty1_MarkerParserRoundTrip(t *testing.T) { |
| 89 | + rapid.Check(t, func(t *rapid.T) { |
| 90 | + short := genSectionContent().Draw(t, "short") |
| 91 | + long := genSectionContent().Draw(t, "long") |
| 92 | + examples := genSectionContent().Draw(t, "examples") |
| 93 | + |
| 94 | + readme := formatMarkedREADME(short, long, examples) |
| 95 | + sections := ParseMarkers([]byte(readme)) |
| 96 | + |
| 97 | + if got, want := sections.Short, strings.TrimSpace(short); got != want { |
| 98 | + t.Fatalf("Short mismatch:\n got: %q\n want: %q", got, want) |
| 99 | + } |
| 100 | + if got, want := sections.Long, strings.TrimSpace(long); got != want { |
| 101 | + t.Fatalf("Long mismatch:\n got: %q\n want: %q", got, want) |
| 102 | + } |
| 103 | + if got, want := sections.Examples, strings.TrimSpace(examples); got != want { |
| 104 | + t.Fatalf("Examples mismatch:\n got: %q\n want: %q", got, want) |
| 105 | + } |
| 106 | + }) |
| 107 | +} |
0 commit comments