github.com/blend/go-sdk@v1.20220411.3/profanity/go_calls_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package profanity
     9  
    10  import (
    11  	"testing"
    12  
    13  	"github.com/blend/go-sdk/assert"
    14  )
    15  
    16  func TestCallsPassing(t *testing.T) {
    17  	it := assert.New(t)
    18  
    19  	file := `package main
    20  
    21  import "foo/bar"
    22  
    23  func doFoo() {
    24  	return
    25  }
    26  
    27  func main() {
    28  	thing := make(map[string]string)
    29  	fmt.Println(foo.Bar)
    30  	println(bar.Foo)
    31  	doFoo()
    32  }
    33  `
    34  	rule := GoCalls([]GoCall{
    35  		{
    36  			Package: "fmt",
    37  			Func:    "Printf",
    38  		},
    39  	})
    40  
    41  	res := rule.Check("main.go", []byte(file))
    42  	it.Nil(res.Err)
    43  	it.True(res.OK)
    44  }
    45  
    46  func TestCallsPrintln(t *testing.T) {
    47  	it := assert.New(t)
    48  
    49  	file := `package main
    50  
    51  import "foo/bar"
    52  
    53  func doFoo() {
    54  	return
    55  }
    56  
    57  func main() {
    58  	thing := make(map[string]string)
    59  	fmt.Println(foo.Bar)
    60  	println(bar.Foo)
    61  	doFoo()
    62  }
    63  `
    64  	rule := GoCalls([]GoCall{
    65  		{
    66  			Package: "fmt",
    67  			Func:    "Println",
    68  		},
    69  	})
    70  
    71  	res := rule.Check("main.go", []byte(file))
    72  	it.Nil(res.Err)
    73  	it.False(res.OK)
    74  	it.Equal("main.go", res.File)
    75  	it.Equal(11, res.Line)
    76  }
    77  
    78  func TestCallsEmptyPackage(t *testing.T) {
    79  	it := assert.New(t)
    80  
    81  	file := `package main
    82  
    83  import "foo/bar"
    84  
    85  func doFoo() {
    86  	return
    87  }
    88  
    89  func main() {
    90  	thing := make(map[string]string)
    91  	fmt.Println(foo.Bar)
    92  	println(bar.Foo)
    93  	doFoo()
    94  }
    95  `
    96  
    97  	rule := GoCalls([]GoCall{
    98  		{
    99  			Func: "println",
   100  		},
   101  	})
   102  
   103  	res := rule.Check("main.go", []byte(file))
   104  	it.Nil(res.Err)
   105  	it.False(res.OK)
   106  	it.Equal("main.go", res.File)
   107  	it.Equal(12, res.Line)
   108  }