From 143e0bb88292489836de406ea79bf5062e4c58c6 Mon Sep 17 00:00:00 2001 From: Igor Kova Date: Wed, 7 Dec 2022 14:02:20 +0100 Subject: [PATCH] chore: add tests --- gen/gen.go | 2 +- parser.go | 2 +- parser_test.go | 48 +++++++++++++++++++++++ testdata/parseExtension/parseExtension.go | 12 ++++++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 testdata/parseExtension/parseExtension.go diff --git a/gen/gen.go b/gen/gen.go index d74202623..ddd02f751 100644 --- a/gen/gen.go +++ b/gen/gen.go @@ -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 diff --git a/parser.go b/parser.go index ed2294d08..dfb0ca82b 100644 --- a/parser.go +++ b/parser.go @@ -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 diff --git a/parser_test.go b/parser_test.go index c7e5bacb1..0876b94e9 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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) + } + }) + + } +} diff --git a/testdata/parseExtension/parseExtension.go b/testdata/parseExtension/parseExtension.go new file mode 100644 index 000000000..9948c7a25 --- /dev/null +++ b/testdata/parseExtension/parseExtension.go @@ -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() {}