github.com/Johnny2210/revive@v1.0.8-0.20210625134200-febf37ccd0f5/test/string-format_test.go (about) 1 package test 2 3 import ( 4 "testing" 5 6 "github.com/mgechev/revive/lint" 7 "github.com/mgechev/revive/rule" 8 ) 9 10 func TestStringFormat(t *testing.T) { 11 testRule(t, "string-format", &rule.StringFormatRule{}, &lint.RuleConfig{ 12 Arguments: lint.Arguments{ 13 []interface{}{ 14 "stringFormatMethod1", // The first argument is checked by default 15 "/^[A-Z]/", 16 "must start with a capital letter"}, 17 18 []interface{}{ 19 "stringFormatMethod2[2].d", 20 "/[^\\.]$/"}, // Must not end with a period 21 []interface{}{ 22 "s.Method3[2]", 23 "/^[^Tt][^Hh]/", 24 "must not start with 'th'"}}}) 25 } 26 27 func TestStringFormatArgumentParsing(t *testing.T) { 28 r := &rule.StringFormatRule{} 29 type argumentsTest struct { 30 name string 31 config lint.Arguments 32 expectedError *string 33 } 34 stringPtr := func(s string) *string { 35 return &s 36 } 37 tests := []argumentsTest{ 38 { 39 name: "Not a Slice", 40 config: lint.Arguments{ 41 "this is not a slice"}, 42 expectedError: stringPtr("invalid configuration for string-format: argument is not a slice [argument 0, option 0]")}, 43 { 44 name: "Missing Regex", 45 config: lint.Arguments{ 46 []interface{}{ 47 "method[0]"}}, 48 expectedError: stringPtr("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]")}, 49 { 50 name: "Bad Argument Type", 51 config: lint.Arguments{ 52 []interface{}{ 53 1}}, 54 expectedError: stringPtr("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]")}, 55 { 56 name: "Empty Scope", 57 config: lint.Arguments{ 58 []interface{}{ 59 "", 60 "//"}}, 61 expectedError: stringPtr("invalid configuration for string-format: empty scope provided [argument 0, option 0]")}, 62 { 63 name: "Small or Empty Regex", 64 config: lint.Arguments{ 65 []interface{}{ 66 "method[1].a", 67 "-"}}, 68 expectedError: stringPtr("invalid configuration for string-format: regex is too small (regexes should begin and end with '/') [argument 0, option 1]")}, 69 { 70 name: "Bad Scope", 71 config: lint.Arguments{ 72 []interface{}{ 73 "1.a", 74 "//"}}, 75 expectedError: stringPtr("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0]")}, 76 { 77 name: "Bad Regex", 78 config: lint.Arguments{ 79 []interface{}{ 80 "method[1].a", 81 "/(/"}}, 82 expectedError: stringPtr("failed to parse configuration for string-format: unable to compile /(/ as regexp [argument 0, option 1]")}, 83 { 84 name: "Sample Config", 85 config: lint.Arguments{ 86 []interface{}{ 87 "core.WriteError[1].Message", "/^([^A-Z]$)/", "must not start with a capital letter"}, 88 []interface{}{ 89 "fmt.Errorf[0]", "/^|[^\\.!?]$/", "must not end in punctuation"}, 90 []interface{}{ 91 "panic", "/^[^\\n]*$/", "must not contain line breaks"}}}, 92 { 93 name: "Underscores in Scope", 94 config: lint.Arguments{ 95 []interface{}{ 96 "some_pkg._some_function_name[5].some_member", 97 "//"}}}} 98 99 for _, a := range tests { 100 t.Run(a.name, func(t *testing.T) { 101 err := r.ParseArgumentsTest(a.config) 102 if err != nil { 103 if a.expectedError == nil || *err != *a.expectedError { 104 t.Errorf("unexpected panic message: %s", *err) 105 } 106 } else if a.expectedError != nil { 107 t.Error("error expected but not received") 108 } 109 }) 110 } 111 }