Files
weatherfeeder/standards/docs_test.go

70 lines
1.6 KiB
Go

package standards
import (
"go/ast"
"go/parser"
"go/token"
"os"
"strconv"
"strings"
"testing"
)
func TestDocumentedEventSchemas(t *testing.T) {
raw, err := os.ReadFile("../docs/integrations/events.md")
if err != nil {
t.Fatalf("ReadFile(events.md) error = %v", err)
}
doc := string(raw)
schemas := schemaConstants(t)
for _, schema := range schemas {
if !strings.Contains(doc, schema) {
t.Fatalf("docs/integrations/events.md missing schema %q", schema)
}
}
}
func schemaConstants(t *testing.T) []string {
t.Helper()
file, err := parser.ParseFile(token.NewFileSet(), "schema.go", nil, 0)
if err != nil {
t.Fatalf("ParseFile(schema.go) error = %v", err)
}
var out []string
ast.Inspect(file, func(n ast.Node) bool {
valueSpec, ok := n.(*ast.ValueSpec)
if !ok {
return true
}
for i, name := range valueSpec.Names {
if !strings.HasPrefix(name.Name, "Schema") || schemaConstantNotInCurrentContract(name.Name) {
continue
}
if i >= len(valueSpec.Values) {
t.Fatalf("schema constant %s has no explicit value", name.Name)
}
lit, ok := valueSpec.Values[i].(*ast.BasicLit)
if !ok || lit.Kind != token.STRING {
t.Fatalf("schema constant %s is not a string literal", name.Name)
}
schema, err := strconv.Unquote(lit.Value)
if err != nil {
t.Fatalf("schema constant %s value is not a quoted string: %v", name.Name, err)
}
out = append(out, schema)
}
return true
})
if len(out) == 0 {
t.Fatalf("no schema constants found")
}
return out
}
func schemaConstantNotInCurrentContract(name string) bool {
return name == "SchemaRawOpenWeatherHourlyForecastV1"
}