|
| 1 | +package disco |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/project-chip/alchemy/asciidoc" |
| 11 | + "github.com/project-chip/alchemy/asciidoc/parse" |
| 12 | + "github.com/project-chip/alchemy/config" |
| 13 | + "github.com/project-chip/alchemy/matter" |
| 14 | + "github.com/project-chip/alchemy/matter/spec" |
| 15 | +) |
| 16 | + |
| 17 | +func TestXrefStyleOnlyInRootWithFile(t *testing.T) { |
| 18 | + b := NewBaller(nil, DiscoOptions{XrefStyleOnlyInRoot: true}) |
| 19 | + |
| 20 | + // Test Case 1: Root doc without xrefstyle (should add it) |
| 21 | + { |
| 22 | + path := asciidoc.Path{Relative: "testdata/root_without_xref.adoc"} |
| 23 | + in, err := os.ReadFile("testdata/root_without_xref.adoc") |
| 24 | + if err != nil { |
| 25 | + t.Fatalf("error reading file: %v", err) |
| 26 | + } |
| 27 | + |
| 28 | + doc, err := parse.Reader(path, bytes.NewReader(in)) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("error parsing file: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + lib := spec.NewLibrary(doc, config.Library{}, nil, nil) |
| 34 | + lib.Reader = asciidoc.RawReader |
| 35 | + dc := &discoContext{ |
| 36 | + Context: context.Background(), |
| 37 | + doc: doc, |
| 38 | + library: lib, |
| 39 | + potentialDataTypes: make(map[string][]*DataTypeEntry), |
| 40 | + } |
| 41 | + |
| 42 | + top := parse.FindFirst[*asciidoc.Section](doc, asciidoc.RawReader, doc) |
| 43 | + err = b.discoBallTopLevelSection(dc, top, matter.DocTypeCluster) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("unexpected error: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + // Verify added after Copyright Notice |
| 49 | + found := false |
| 50 | + for i, el := range doc.Elements { |
| 51 | + if s, ok := el.(*asciidoc.Section); ok { |
| 52 | + name := lib.SectionName(s) |
| 53 | + if strings.Contains(strings.ToLower(name), "copyright notice") { |
| 54 | + // Check next element |
| 55 | + if i+1 < len(doc.Elements) { |
| 56 | + if _, ok := doc.Elements[i+1].(*asciidoc.NewLine); ok { |
| 57 | + if i+2 < len(doc.Elements) { |
| 58 | + if ae, ok := doc.Elements[i+2].(*asciidoc.AttributeEntry); ok && ae.Name == "xrefstyle" { |
| 59 | + found = true |
| 60 | + break |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + if !found { |
| 69 | + t.Error("expected xrefstyle to be added after Copyright Notice section loaded from file") |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Test Case 2: Non-Root doc with xrefstyle (should remove it) |
| 74 | + { |
| 75 | + path := asciidoc.Path{Relative: "testdata/non_root_with_xref.adoc"} |
| 76 | + in, err := os.ReadFile("testdata/non_root_with_xref.adoc") |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("error reading file: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + doc, err := parse.Reader(path, bytes.NewReader(in)) |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("error parsing file: %v", err) |
| 84 | + } |
| 85 | + |
| 86 | + lib := spec.NewLibrary(nil, config.Library{}, nil, nil) // Root is nil, so doc is not root |
| 87 | + lib.Reader = asciidoc.RawReader |
| 88 | + dc := &discoContext{ |
| 89 | + Context: context.Background(), |
| 90 | + doc: doc, |
| 91 | + library: lib, |
| 92 | + potentialDataTypes: make(map[string][]*DataTypeEntry), |
| 93 | + } |
| 94 | + |
| 95 | + top := parse.FindFirst[*asciidoc.Section](doc, asciidoc.RawReader, doc) |
| 96 | + err = b.discoBallTopLevelSection(dc, top, matter.DocTypeCluster) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("unexpected error: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + // Verify no xrefstyle anywhere |
| 102 | + found := false |
| 103 | + for _, el := range doc.Elements { |
| 104 | + if ae, ok := el.(*asciidoc.AttributeEntry); ok && ae.Name == "xrefstyle" { |
| 105 | + found = true |
| 106 | + break |
| 107 | + } |
| 108 | + } |
| 109 | + if found { |
| 110 | + t.Error("expected all xrefstyle to be removed from non-root document loaded from file") |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Test Case 3: Root doc with xrefstyle (should do nothing) |
| 115 | + { |
| 116 | + path := asciidoc.Path{Relative: "testdata/root_with_xref.adoc"} |
| 117 | + in, err := os.ReadFile("testdata/root_with_xref.adoc") |
| 118 | + if err != nil { |
| 119 | + t.Fatalf("error reading file: %v", err) |
| 120 | + } |
| 121 | + |
| 122 | + doc, err := parse.Reader(path, bytes.NewReader(in)) |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("error parsing file: %v", err) |
| 125 | + } |
| 126 | + |
| 127 | + lib := spec.NewLibrary(doc, config.Library{}, nil, nil) |
| 128 | + lib.Reader = asciidoc.RawReader |
| 129 | + dc := &discoContext{ |
| 130 | + Context: context.Background(), |
| 131 | + doc: doc, |
| 132 | + library: lib, |
| 133 | + potentialDataTypes: make(map[string][]*DataTypeEntry), |
| 134 | + } |
| 135 | + |
| 136 | + // Count existing xrefstyle |
| 137 | + countBefore := 0 |
| 138 | + for _, el := range doc.Elements { |
| 139 | + if ae, ok := el.(*asciidoc.AttributeEntry); ok && ae.Name == "xrefstyle" { |
| 140 | + countBefore++ |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + top := parse.FindFirst[*asciidoc.Section](doc, asciidoc.RawReader, doc) |
| 145 | + err = b.discoBallTopLevelSection(dc, top, matter.DocTypeCluster) |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("unexpected error: %v", err) |
| 148 | + } |
| 149 | + |
| 150 | + // Count after |
| 151 | + countAfter := 0 |
| 152 | + for _, el := range doc.Elements { |
| 153 | + if ae, ok := el.(*asciidoc.AttributeEntry); ok && ae.Name == "xrefstyle" { |
| 154 | + countAfter++ |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if countAfter != countBefore { |
| 159 | + t.Errorf("expected number of xrefstyle to remain %d, got %d", countBefore, countAfter) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments