...

Source file src/github.com/onsi/ginkgo/v2/dsl/dsl_suite_test.go

Documentation: github.com/onsi/ginkgo/v2/dsl

     1  package dsl_test
     2  
     3  import (
     4  	"go/ast"
     5  	"go/parser"
     6  	"go/token"
     7  	"testing"
     8  
     9  	. "github.com/onsi/ginkgo/v2"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  func TestDSL(t *testing.T) {
    14  	RegisterFailHandler(Fail)
    15  	RunSpecs(t, "DSL Suite")
    16  }
    17  
    18  func ExtractSymbols(f *ast.File) []string {
    19  	symbols := []string{}
    20  	for _, decl := range f.Decls {
    21  		names := []string{}
    22  		switch v := decl.(type) {
    23  		case *ast.FuncDecl:
    24  			if v.Recv == nil {
    25  				names = append(names, v.Name.Name)
    26  			}
    27  		case *ast.GenDecl:
    28  			switch v.Tok {
    29  			case token.TYPE:
    30  				s := v.Specs[0].(*ast.TypeSpec)
    31  				names = append(names, s.Name.Name)
    32  			case token.CONST, token.VAR:
    33  				s := v.Specs[0].(*ast.ValueSpec)
    34  				for _, n := range s.Names {
    35  					names = append(names, n.Name)
    36  				}
    37  			}
    38  		}
    39  		for _, name := range names {
    40  			if ast.IsExported(name) {
    41  				symbols = append(symbols, name)
    42  			}
    43  		}
    44  	}
    45  	return symbols
    46  }
    47  
    48  var _ = It("ensures complete coverage of the core dsl", func() {
    49  	fset := token.NewFileSet()
    50  	pkgs, err := parser.ParseDir(fset, "../", nil, 0)
    51  	Ω(err).ShouldNot(HaveOccurred())
    52  	expectedSymbols := []string{}
    53  	for fn, file := range pkgs["ginkgo"].Files {
    54  		if fn == "../deprecated_dsl.go" {
    55  			continue
    56  		}
    57  		expectedSymbols = append(expectedSymbols, ExtractSymbols(file)...)
    58  	}
    59  
    60  	actualSymbols := []string{}
    61  	for _, pkg := range []string{"core", "reporting", "decorators", "table"} {
    62  		pkgs, err := parser.ParseDir(fset, "./"+pkg, nil, 0)
    63  		Ω(err).ShouldNot(HaveOccurred())
    64  		for _, file := range pkgs[pkg].Files {
    65  			actualSymbols = append(actualSymbols, ExtractSymbols(file)...)
    66  		}
    67  	}
    68  
    69  	Ω(actualSymbols).Should(ConsistOf(expectedSymbols))
    70  })
    71  

View as plain text