-
Notifications
You must be signed in to change notification settings - Fork 11
Refine Xrefstyle Rules in Disco #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AryaHassanli
merged 5 commits into
project-chip:main
from
AryaHassanli:disco-add-xref-only-in-root-doc
Apr 7, 2026
+245
−24
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
acd7fbf
Xref addition only for root
AryaHassanli de57785
xrefstyle should only be present in root
AryaHassanli 595d810
Remove redundant testdata
AryaHassanli 6c4b8a0
Update test
AryaHassanli 049efd4
Apply performance suggestions
AryaHassanli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package disco | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/project-chip/alchemy/asciidoc" | ||
| "github.com/project-chip/alchemy/asciidoc/parse" | ||
| "github.com/project-chip/alchemy/config" | ||
| "github.com/project-chip/alchemy/matter" | ||
| "github.com/project-chip/alchemy/matter/spec" | ||
| ) | ||
|
|
||
| func TestXrefStyleOnlyInRootWithFile(t *testing.T) { | ||
| b := NewBaller(nil, DiscoOptions{XrefStyleOnlyInRoot: true}) | ||
|
|
||
| // Test Case 1: Root doc without xrefstyle (should add it) | ||
| { | ||
| path := asciidoc.Path{Relative: "testdata/root_without_xref.adoc"} | ||
| in, err := os.ReadFile("testdata/root_without_xref.adoc") | ||
| if err != nil { | ||
| t.Fatalf("error reading file: %v", err) | ||
| } | ||
|
|
||
| doc, err := parse.Reader(path, bytes.NewReader(in)) | ||
| if err != nil { | ||
| t.Fatalf("error parsing file: %v", err) | ||
| } | ||
|
|
||
| lib := spec.NewLibrary(doc, config.Library{}, nil, nil) | ||
| lib.Reader = asciidoc.RawReader | ||
| dc := &discoContext{ | ||
| Context: context.Background(), | ||
| doc: doc, | ||
| library: lib, | ||
| potentialDataTypes: make(map[string][]*DataTypeEntry), | ||
| } | ||
|
|
||
| top := parse.FindFirst[*asciidoc.Section](doc, asciidoc.RawReader, doc) | ||
| err = b.discoBallTopLevelSection(dc, top, matter.DocTypeCluster) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| // Verify added after Copyright Notice | ||
| found := false | ||
| for i, el := range doc.Elements { | ||
| if s, ok := el.(*asciidoc.Section); ok { | ||
| name := lib.SectionName(s) | ||
| if strings.Contains(strings.ToLower(name), "copyright notice") { | ||
| // Check next element | ||
| if i+1 < len(doc.Elements) { | ||
| if _, ok := doc.Elements[i+1].(*asciidoc.NewLine); ok { | ||
| if i+2 < len(doc.Elements) { | ||
| if ae, ok := doc.Elements[i+2].(*asciidoc.AttributeEntry); ok && ae.Name == "xrefstyle" { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if !found { | ||
| t.Error("expected xrefstyle to be added after Copyright Notice section loaded from file") | ||
| } | ||
| } | ||
|
|
||
| // Test Case 2: Non-Root doc with xrefstyle (should remove it) | ||
| { | ||
| path := asciidoc.Path{Relative: "testdata/non_root_with_xref.adoc"} | ||
| in, err := os.ReadFile("testdata/non_root_with_xref.adoc") | ||
| if err != nil { | ||
| t.Fatalf("error reading file: %v", err) | ||
| } | ||
|
|
||
| doc, err := parse.Reader(path, bytes.NewReader(in)) | ||
| if err != nil { | ||
| t.Fatalf("error parsing file: %v", err) | ||
| } | ||
|
|
||
| lib := spec.NewLibrary(nil, config.Library{}, nil, nil) // Root is nil, so doc is not root | ||
| lib.Reader = asciidoc.RawReader | ||
| dc := &discoContext{ | ||
| Context: context.Background(), | ||
| doc: doc, | ||
| library: lib, | ||
| potentialDataTypes: make(map[string][]*DataTypeEntry), | ||
| } | ||
|
|
||
| top := parse.FindFirst[*asciidoc.Section](doc, asciidoc.RawReader, doc) | ||
| err = b.discoBallTopLevelSection(dc, top, matter.DocTypeCluster) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| // Verify no xrefstyle anywhere | ||
| found := false | ||
| for _, el := range doc.Elements { | ||
| if ae, ok := el.(*asciidoc.AttributeEntry); ok && ae.Name == "xrefstyle" { | ||
| found = true | ||
| break | ||
| } | ||
| } | ||
| if found { | ||
| t.Error("expected all xrefstyle to be removed from non-root document loaded from file") | ||
| } | ||
| } | ||
|
|
||
| // Test Case 3: Root doc with xrefstyle (should do nothing) | ||
| { | ||
| path := asciidoc.Path{Relative: "testdata/root_with_xref.adoc"} | ||
| in, err := os.ReadFile("testdata/root_with_xref.adoc") | ||
| if err != nil { | ||
| t.Fatalf("error reading file: %v", err) | ||
| } | ||
|
|
||
| doc, err := parse.Reader(path, bytes.NewReader(in)) | ||
| if err != nil { | ||
| t.Fatalf("error parsing file: %v", err) | ||
| } | ||
|
|
||
| lib := spec.NewLibrary(doc, config.Library{}, nil, nil) | ||
| lib.Reader = asciidoc.RawReader | ||
| dc := &discoContext{ | ||
| Context: context.Background(), | ||
| doc: doc, | ||
| library: lib, | ||
| potentialDataTypes: make(map[string][]*DataTypeEntry), | ||
| } | ||
|
|
||
| // Count existing xrefstyle | ||
| countBefore := 0 | ||
| for _, el := range doc.Elements { | ||
| if ae, ok := el.(*asciidoc.AttributeEntry); ok && ae.Name == "xrefstyle" { | ||
| countBefore++ | ||
| } | ||
| } | ||
|
|
||
| top := parse.FindFirst[*asciidoc.Section](doc, asciidoc.RawReader, doc) | ||
| err = b.discoBallTopLevelSection(dc, top, matter.DocTypeCluster) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
|
|
||
| // Count after | ||
| countAfter := 0 | ||
| for _, el := range doc.Elements { | ||
| if ae, ok := el.(*asciidoc.AttributeEntry); ok && ae.Name == "xrefstyle" { | ||
| countAfter++ | ||
| } | ||
| } | ||
|
|
||
| if countAfter != countBefore { | ||
| t.Errorf("expected number of xrefstyle to remain %d, got %d", countBefore, countAfter) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| :xrefstyle: full | ||
|
|
||
| = First Section | ||
| This is the first section. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| :xrefstyle: full | ||
|
|
||
| = Copyright Notice | ||
| This is the copyright notice. | ||
|
|
||
| = First Section | ||
| This is the first section. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| = Copyright Notice | ||
| This is the copyright notice. | ||
|
|
||
| = First Section | ||
| This is the first section. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.