github.com/kaisawind/go-swagger@v0.19.0/scan/classifier_test.go (about)

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package scan
    16  
    17  import (
    18  	gobuild "go/build"
    19  	goparser "go/parser"
    20  	"log"
    21  	"path/filepath"
    22  	"sort"
    23  	"testing"
    24  
    25  	"golang.org/x/tools/go/loader"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func TestAnnotationMatcher(t *testing.T) {
    31  	variations := []string{
    32  		"// swagger",
    33  		" swagger",
    34  		"swagger",
    35  		" * swagger",
    36  	}
    37  	known := []string{
    38  		"meta",
    39  		"route",
    40  		"model",
    41  		"parameters",
    42  		"strfmt",
    43  		"response",
    44  		"enum",
    45  		"default",
    46  	}
    47  
    48  	for _, variation := range variations {
    49  		for _, tpe := range known {
    50  			assert.True(t, rxSwaggerAnnotation.MatchString(variation+":"+tpe))
    51  		}
    52  	}
    53  }
    54  
    55  func classifierProgram() *loader.Program {
    56  	var ldr loader.Config
    57  	ldr.ParserMode = goparser.ParseComments
    58  	ldr.Build = &gobuild.Default
    59  	ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/classification")
    60  	ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/classification/models")
    61  	ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/classification/operations")
    62  	prog, err := ldr.Load()
    63  	if err != nil {
    64  		log.Fatal(err)
    65  	}
    66  	return prog
    67  }
    68  
    69  func petstoreProgram() *loader.Program {
    70  	var ldr loader.Config
    71  	ldr.ParserMode = goparser.ParseComments
    72  	ldr.Build = &gobuild.Default
    73  	ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/petstore")
    74  	ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/petstore/models")
    75  	ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/petstore/rest/handlers")
    76  	prog, err := ldr.Load()
    77  	if err != nil {
    78  		log.Fatal(err)
    79  	}
    80  	return prog
    81  }
    82  
    83  func invalidProgram(name string) *loader.Program {
    84  	var ldr loader.Config
    85  	ldr.ParserMode = goparser.ParseComments
    86  	ldr.ImportWithTests("github.com/go-swagger/go-swagger/fixtures/goparsing/" + name)
    87  	prog, err := ldr.Load()
    88  	if err != nil {
    89  		log.Fatal(err)
    90  	}
    91  	return prog
    92  }
    93  
    94  func testInvalidProgram(t testing.TB, name string) bool {
    95  	prog := invalidProgram(name)
    96  	classifier := &programClassifier{}
    97  	_, err := classifier.Classify(prog)
    98  	return assert.Error(t, err)
    99  }
   100  
   101  func TestDuplicateAnnotations(t *testing.T) {
   102  	for _, b := range []string{"model_param", "param_model", "model_response", "response_model"} {
   103  		testInvalidProgram(t, "invalid_"+b)
   104  	}
   105  }
   106  
   107  func TestClassifier(t *testing.T) {
   108  
   109  	prog := petstoreProgram()
   110  	classifier := &programClassifier{}
   111  	classified, err := classifier.Classify(prog)
   112  	assert.NoError(t, err)
   113  
   114  	// ensure all the dependencies are there
   115  	assert.Len(t, classified.Meta, 1)
   116  	assert.Len(t, classified.Routes, 2)
   117  
   118  	var fNames []string
   119  	for _, file := range classified.Models {
   120  		fNames = append(
   121  			fNames,
   122  			filepath.Base(prog.Fset.File(file.Pos()).Name()))
   123  	}
   124  
   125  	sort.Sort(sort.StringSlice(fNames))
   126  	assert.EqualValues(t, []string{"order.go", "pet.go", "tag.go", "user.go"}, fNames)
   127  }
   128  
   129  func TestClassifierInclude(t *testing.T) {
   130  
   131  	prog := classificationProg
   132  	classifier := &programClassifier{
   133  		Includes: packageFilters([]packageFilter{
   134  			{Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification"},
   135  			{Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification/transitive/mods"},
   136  			{Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification/operations"},
   137  			{Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification/operations_annotation"},
   138  		}),
   139  	}
   140  	classified, err := classifier.Classify(prog)
   141  	assert.NoError(t, err)
   142  
   143  	// ensure all the dependencies are there
   144  	assert.Len(t, classified.Meta, 1)
   145  	assert.Len(t, classified.Routes, 1)
   146  
   147  	//var fNames []string
   148  	//for _, file := range classified.Models {
   149  	//fNames = append(
   150  	//fNames,
   151  	//filepath.Base(prog.Fset.File(file.Pos()).Name()))
   152  	//}
   153  
   154  	//sort.Sort(sort.StringSlice(fNames))
   155  	//assert.EqualValues(t, []string{"pet.go"}, fNames)
   156  }
   157  
   158  func TestClassifierExclude(t *testing.T) {
   159  
   160  	prog := classificationProg
   161  	classifier := &programClassifier{
   162  		Excludes: packageFilters([]packageFilter{
   163  			{Name: "github.com/go-swagger/go-swagger/fixtures/goparsing/classification/transitive/mods"},
   164  		}),
   165  	}
   166  	classified, err := classifier.Classify(prog)
   167  	assert.NoError(t, err)
   168  
   169  	// ensure all the dependencies are there
   170  	assert.Len(t, classified.Meta, 1)
   171  	assert.Len(t, classified.Routes, 1)
   172  
   173  	//var fNames []string
   174  	//for _, file := range classified.Models {
   175  	//fNames = append(
   176  	//fNames,
   177  	//filepath.Base(prog.Fset.File(file.Pos()).Name()))
   178  	//}
   179  
   180  	//sort.Sort(sort.StringSlice(fNames))
   181  	//assert.EqualValues(t, []string{"order.go", "user.go"}, fNames)
   182  }