...

Source file src/github.com/bazelbuild/buildtools/warn/warn_operation_test.go

Documentation: github.com/bazelbuild/buildtools/warn

     1  /*
     2  Copyright 2020 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package warn
    18  
    19  import "testing"
    20  
    21  func TestIntegerDivision(t *testing.T) {
    22  	checkFindingsAndFix(t, "integer-division", `
    23  a = 1
    24  b = int(2.3)
    25  c = 1.0
    26  d = float(2)
    27  
    28  e = a / b
    29  f = a / c
    30  g = c / a
    31  h = c / d
    32  
    33  a /= b
    34  a /= c
    35  c /= a
    36  c /= d
    37  `, `
    38  a = 1
    39  b = int(2.3)
    40  c = 1.0
    41  d = float(2)
    42  
    43  e = a // b
    44  f = a / c
    45  g = c / a
    46  h = c / d
    47  
    48  a //= b
    49  a /= c
    50  c /= a
    51  c /= d
    52  `,
    53  		[]string{
    54  			":6: The \"/\" operator for integer division is deprecated in favor of \"//\".",
    55  			":11: The \"/=\" operator for integer division is deprecated in favor of \"//=\".",
    56  		},
    57  		scopeEverywhere)
    58  }
    59  
    60  func TestDictionaryConcatenation(t *testing.T) {
    61  	checkFindings(t, "dict-concatenation", `
    62  d = {}
    63  
    64  d + foo
    65  foo + d
    66  d + foo + bar  # Should trigger 2 warnings: (d + foo) is recognized as a dict
    67  foo + bar + d  # Should trigger 1 warning: (foo + bar) is unknown
    68  d += foo + bar
    69  `,
    70  		[]string{
    71  			":3: Dictionary concatenation is deprecated.",
    72  			":4: Dictionary concatenation is deprecated.",
    73  			":5: Dictionary concatenation is deprecated.",
    74  			":5: Dictionary concatenation is deprecated.",
    75  			":6: Dictionary concatenation is deprecated.",
    76  			":7: Dictionary concatenation is deprecated.",
    77  		},
    78  		scopeEverywhere)
    79  }
    80  
    81  func TestStringIteration(t *testing.T) {
    82  	checkFindings(t, "string-iteration", `
    83  s = "foo" + bar
    84  
    85  max(s)
    86  min(s)
    87  all(s)
    88  any(s)
    89  reversed(s)
    90  zip(s, a, b)
    91  zip(a, s)
    92  
    93  [foo(x) for x in s]
    94  
    95  for x in s:
    96      pass
    97  
    98  # The following iterations over a list don't trigger warnings
    99  
   100  l = list()
   101  
   102  max(l)
   103  zip(l, foo)
   104  [foo(x) for x in l]
   105  
   106  for x in l:
   107      pass
   108  `,
   109  		[]string{
   110  			":3: String iteration is deprecated.",
   111  			":4: String iteration is deprecated.",
   112  			":5: String iteration is deprecated.",
   113  			":6: String iteration is deprecated.",
   114  			":7: String iteration is deprecated.",
   115  			":8: String iteration is deprecated.",
   116  			":9: String iteration is deprecated.",
   117  			":11: String iteration is deprecated.",
   118  			":13: String iteration is deprecated.",
   119  		},
   120  		scopeEverywhere)
   121  }
   122  
   123  func TestListAppend(t *testing.T) {
   124  	checkFindingsAndFix(t, "list-append", `
   125  x = []
   126  x += y
   127  x += [1]
   128  x += [2, 3]
   129  x += [4 for y in z]
   130  x += 5
   131  x += [foo(
   132      bar,
   133      baz,
   134  )]
   135  `, `
   136  x = []
   137  x += y
   138  x.append(1)
   139  x += [2, 3]
   140  x += [4 for y in z]
   141  x += 5
   142  x.append(foo(
   143      bar,
   144      baz,
   145  ))
   146  `,
   147  		[]string{
   148  			`:3: Prefer using ".append()" to adding a single element list`,
   149  			`:7: Prefer using ".append()" to adding a single element list`,
   150  		},
   151  		scopeEverywhere)
   152  }
   153  
   154  func TestDictMethodNamedArg(t *testing.T) {
   155  	checkFindings(t, "dict-method-named-arg", `
   156  d = dict()
   157  d.get("a", "b")
   158  [].get("a", default = "b")
   159  
   160  d.get("a", default = "b") # warning
   161  d.pop("a", default = "b") # warning
   162  {}.setdefault("a", default = "b") # warning
   163  `,
   164  		[]string{
   165  			`:5: Named argument "default" not allowed`,
   166  			`:6: Named argument "default" not allowed`,
   167  			`:7: Named argument "default" not allowed`,
   168  		},
   169  		scopeEverywhere)
   170  }
   171  

View as plain text