github.com/onsi/ginkgo@v1.16.6-0.20211118180735-4e1925ba4c95/ginkgo/outline/outline_test.go (about) 1 package outline 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "go/parser" 7 "go/token" 8 "log" 9 "os" 10 "path/filepath" 11 "strconv" 12 "strings" 13 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = DescribeTable("Validate outline from file with", 19 func(srcFilename, jsonOutlineFilename, csvOutlineFilename string) { 20 fset := token.NewFileSet() 21 astFile, err := parser.ParseFile(fset, filepath.Join("_testdata", srcFilename), nil, 0) 22 Expect(err).To(BeNil(), "error parsing source: %s", err) 23 24 if err != nil { 25 log.Fatalf("error parsing source: %s", err) 26 } 27 28 o, err := FromASTFile(fset, astFile) 29 Expect(err).To(BeNil(), "error creating outline: %s", err) 30 31 gotJSON, err := json.MarshalIndent(o, "", " ") 32 Expect(err).To(BeNil(), "error marshalling outline to json: %s", err) 33 34 wantJSON, err := os.ReadFile(filepath.Join("_testdata", jsonOutlineFilename)) 35 Expect(err).To(BeNil(), "error reading JSON outline fixture: %s", err) 36 Expect(gotJSON).To(MatchJSON(wantJSON)) 37 38 gotCSV := o.String() 39 40 wantCSV, err := os.ReadFile(filepath.Join("_testdata", csvOutlineFilename)) 41 Expect(err).To(BeNil(), "error reading CSV outline fixture: %s", err) 42 43 Expect(gotCSV).To(Equal(string(wantCSV))) 44 }, 45 // To add a test: 46 // 1. Create the input, e.g., `myspecialcase_test.go` 47 // 2. Create the sample CSV and JSON results: Run `bash ./_testdata/create_result.sh ./_testdata/myspecialcase_test.go` 48 // 3. Add an Entry below, by copying an existing one, and substituting `myspecialcase` where needed. 49 // To re-create the sample results for a test: 50 // 1. Run `bash ./_testdata/create_result.sh ./testdata/myspecialcase_test.go` 51 // To re-create the sample results for all tests: 52 // 1. Run `for name in ./_testdata/*_test.go; do bash ./_testdata/create_result.sh $name; done` 53 Entry("normal import of ginkgo package (no dot, no alias), normal container and specs", "nodot_test.go", "nodot_test.go.json", "nodot_test.go.csv"), 54 Entry("aliased import of ginkgo package, normal container and specs", "alias_test.go", "alias_test.go.json", "alias_test.go.csv"), 55 Entry("normal containers and specs", "normal_test.go", "normal_test.go.json", "normal_test.go.csv"), 56 Entry("focused containers and specs", "focused_test.go", "focused_test.go.json", "focused_test.go.csv"), 57 Entry("pending containers and specs", "pending_test.go", "pending_test.go.json", "pending_test.go.csv"), 58 Entry("nested focused containers and specs", "nestedfocused_test.go", "nestedfocused_test.go.json", "nestedfocused_test.go.csv"), 59 Entry("mixed focused containers and specs", "mixed_test.go", "mixed_test.go.json", "mixed_test.go.csv"), 60 Entry("specs used to verify position", "position_test.go", "position_test.go.json", "position_test.go.csv"), 61 Entry("suite setup", "suite_test.go", "suite_test.go.json", "suite_test.go.csv"), 62 ) 63 64 var _ = Describe("Validate position", func() { 65 It("should report the correct start and end byte offsets of the ginkgo container or spec", func() { 66 fset := token.NewFileSet() 67 astFile, err := parser.ParseFile(fset, filepath.Join("_testdata", "position_test.go"), nil, 0) 68 Expect(err).To(BeNil(), "error parsing source: %s", err) 69 70 if err != nil { 71 log.Fatalf("error parsing source: %s", err) 72 } 73 74 o, err := FromASTFile(fset, astFile) 75 Expect(err).To(BeNil(), "error creating outline: %s", err) 76 77 for _, n := range o.Nodes { 78 n.PreOrder(func(n *ginkgoNode) { 79 wantPositions := strings.Split(n.Text, ",") 80 Expect(len(wantPositions)).To(Equal(2), "test fixture node text should be \"start position,end position") 81 wantStart, err := strconv.Atoi(wantPositions[0]) 82 Expect(err).To(BeNil(), "could not parse start offset") 83 wantEnd, err := strconv.Atoi(wantPositions[1]) 84 Expect(err).To(BeNil(), "could not parse end offset") 85 86 Expect(int(n.Start)).To(Equal(wantStart), fmt.Sprintf("test fixture node text says the node should start at %d, but it starts at %d", wantStart, n.Start)) 87 Expect(int(n.End)).To(Equal(wantEnd), fmt.Sprintf("test fixture node text says the node should end at %d, but it ends at %d", wantEnd, n.End)) 88 }) 89 } 90 91 }) 92 })