...

Source file src/github.com/bazelbuild/buildtools/warn/warn_naming_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 TestAmbiguousNames(t *testing.T) {
    22  	checkFindings(t, "confusing-name", `
    23  i = 0
    24  I, x = 1, 2  # here
    25  l = 3  # here
    26  L = []
    27  L[l] = 4
    28  O = 5  # here
    29  
    30  x, l = a, b  # here
    31  a, b = x, l
    32  `,
    33  		[]string{
    34  			":2: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    35  			":3: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    36  			":6: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    37  			":8: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    38  		}, scopeEverywhere)
    39  
    40  	checkFindings(t, "confusing-name", `
    41  def l():  # here
    42    if True:
    43      i = 1
    44      I = 2  # here
    45    else:
    46      l = 3  # here
    47      L = 4
    48    
    49    for O in Os:  # here
    50      pass
    51  
    52    x, l = a, b  # here
    53    a, b = x, l
    54  `,
    55  		[]string{
    56  			":1: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    57  			":4: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    58  			":6: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    59  			":9: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    60  			":12: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    61  		}, scopeEverywhere)
    62  
    63  	checkFindings(t, "confusing-name", `
    64  [l for l in s]
    65  
    66  cc_library(
    67    name = "name-conventions",
    68    tags = [I for I in tags if I],
    69  )
    70  
    71  def f(x):
    72    return [O for O in x]
    73  `,
    74  		[]string{
    75  			":1: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    76  			":5: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    77  			":9: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    78  		}, scopeEverywhere)
    79  
    80  	checkFindings(t, "confusing-name", `
    81  [
    82    foo(l, I, O)
    83    for l in ls  # here
    84    if l < m
    85    for I, (x, O) in bar # here 2 times
    86    for L in Ls
    87    if I > l and O == 0
    88    if x == L
    89  ]
    90  `,
    91  		[]string{
    92  			":3: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    93  			":5: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    94  			":5: Never use 'l', 'I', or 'O' as names (they're too easily confused with 'I', 'l', or '0').",
    95  		}, scopeEverywhere)
    96  }
    97  
    98  func TestVariableNames(t *testing.T) {
    99  	checkFindings(t, "name-conventions", `
   100  _ = 0
   101  foo = 1
   102  FOO = 2
   103  Foo = 3
   104  foo, Bar, BazInfo = 4, 5, 6
   105  foo, (BAR, _) = 7, (8, 9)
   106  fOO = 10
   107  FooInfo = 11
   108  _FooInfo = 12
   109  _BAR = 13
   110  _baz = 14
   111  _Foo_bar, foo_Baz = 15, 16
   112  `,
   113  		[]string{
   114  			`:4: Variable name "Foo" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   115  			`:5: Variable name "Bar" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   116  			`:7: Variable name "fOO" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   117  			`:12: Variable name "_Foo_bar" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   118  			`:12: Variable name "foo_Baz" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   119  		}, scopeEverywhere)
   120  
   121  	checkFindings(t, "name-conventions", `
   122  def f(x, _, Arg = None):
   123    _ = 0
   124    foo = 1
   125    FOO = 2
   126    Foo = 3
   127    foo, Bar, BazInfo = 4, 5, 6
   128    foo, (BAR, _) = 7, (8, 9)
   129    fOO = 10
   130    FooInfo = 11
   131    _FooInfo = 12
   132    _BAR = 13
   133    _baz = 14
   134    _Foo_bar, foo_Baz = 15, 16
   135  `,
   136  		[]string{
   137  			`:5: Variable name "Foo" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   138  			`:6: Variable name "Bar" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   139  			`:8: Variable name "fOO" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   140  			`:13: Variable name "_Foo_bar" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   141  			`:13: Variable name "foo_Baz" should be lower_snake_case (for variables), UPPER_SNAKE_CASE (for constants), or UpperCamelCase ending with 'Info' (for providers).`,
   142  		}, scopeEverywhere)
   143  
   144  	checkFindings(t, "name-conventions", `
   145  foo(
   146    Bar = 1,
   147    ___ = 2,
   148    BazInfo = 3,
   149  	baz = 4,
   150    FOO = 5,
   151  )
   152  `,
   153  		[]string{}, scopeEverywhere)
   154  }
   155  

View as plain text