From d1dcbbb71b8279f263374dd8317893b2c984e235 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 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) 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) + } + }) + + } +}