Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkova committed Dec 7, 2022
1 parent 36e1dc5 commit 143e0bb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Config struct {
// excludes dirs and files in SearchDir,comma separated
Excludes string

// outputs only those operations with "x-public: true" extension
// outputs only specific extension
ParseExtension string

// OutputDir represents the output directory for all the generated files
Expand Down
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type Parser struct {
// excludes excludes dirs and files in SearchDir
excludes map[string]struct{}

// tells parser to include operations with "x-public: true" extension
// tells parser to include only specific extension
parseExtension string

// debugging output goes here
Expand Down
48 changes: 48 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3878,3 +3878,51 @@ func TestParser_matchTags(t *testing.T) {
})
}
}

func TestParser_parseExtension(t *testing.T) {

src, err := os.ReadFile("testdata/parseExtension/parseExtension.go")
assert.NoError(t, err)

f, err := goparser.ParseFile(token.NewFileSet(), "", src, goparser.ParseComments)
assert.NoError(t, err)

tests := []struct {
name string
parser *Parser
expectedPaths map[string]bool
}{
{
name: "when no flag is set, everything is exported",
parser: New(),
expectedPaths: map[string]bool{"/without-extension": true, "/with-another-extension": true, "/with-correct-extension": true},
},
{
name: "when nonexistent flag is set, nothing is exported",
parser: New(SetParseExtension("nonexistent-extension-filter")),
expectedPaths: map[string]bool{"/without-extension": false, "/with-another-extension": false, "/with-correct-extension": false},
},
{
name: "when correct flag is set, only that Path is exported",
parser: New(SetParseExtension("google-backend")),
expectedPaths: map[string]bool{"/without-extension": false, "/with-another-extension": false, "/with-correct-extension": true},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err = tt.parser.ParseRouterAPIInfo("", f)
assert.NoError(t, err)
for p, isExpected := range tt.expectedPaths {
_, ok := tt.parser.swagger.Paths.Paths[p]
assert.Equal(t, isExpected, ok)
}

for p := range tt.parser.swagger.Paths.Paths {
_, isExpected := tt.expectedPaths[p]
assert.Equal(t, isExpected, true)
}
})

}
}
12 changes: 12 additions & 0 deletions testdata/parseExtension/parseExtension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

// @Router /without-extension [get]
func Fun() {}

// @Router /with-another-extension [get]
// @x-another-extension {"address": "http://backend"}
func Fun2() {}

// @Router /with-correct-extension [get]
// @x-google-backend {"address": "http://backend"}
func Fun3() {}

0 comments on commit 143e0bb

Please sign in to comment.