Skip to content

Commit

Permalink
ast: Adding example test for ast.AnnotationSet.Flatten() (#4404)
Browse files Browse the repository at this point in the history
Example code mirrors that documented in docs/content/annotations.md, to make sure it compiles and behaves as expected.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
  • Loading branch information
johanfylling committed Mar 3, 2022
1 parent a6c1584 commit 180ecb3
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions ast/annotations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,54 @@ package ast

import (
"encoding/json"
"fmt"
"testing"
)

func ExampleAnnotationSet_Flatten() {
modules := map[string]string{
"foo.rego": `# METADATA
# scope: subpackages
# organizations:
# - Acme Corp.
package foo`,
"mod": `# METADATA
# description: A couple of useful rules
package foo.bar
# METADATA
# title: My Rule P
p := 7`,
}

parsed := make([]*Module, 0, len(modules))
for f, module := range modules {
pm, err := ParseModuleWithOpts(f, module, ParserOptions{ProcessAnnotation: true})
if err != nil {
panic(err)
}
parsed = append(parsed, pm)
}

as, err := BuildAnnotationSet(parsed)
if err != nil {
panic(err)
}

flattened := as.Flatten()
for _, entry := range flattened {
fmt.Printf("%v at %v has annotations %v\n",
entry.Path,
entry.Location,
entry.Annotations)
}

// Output:
// data.foo at foo.rego:1 has annotations {"scope":"subpackages","organizations":["Acme Corp."]}
// data.foo.bar at mod:3 has annotations {"scope":"package","description":"A couple of useful rules"}
// data.foo.bar.p at mod:7 has annotations {"scope":"rule","title":"My Rule P"}
}

func TestAnnotationSetFlatten(t *testing.T) {
tests := []struct {
note string
Expand Down

0 comments on commit 180ecb3

Please sign in to comment.