github.com/googleapis/api-linter@v1.65.2/lint/problem_test.go (about) 1 // Copyright 2019 Google LLC 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 // https://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 lint 16 17 import ( 18 "encoding/json" 19 "strings" 20 "testing" 21 22 "github.com/jhump/protoreflect/desc/builder" 23 dpb "google.golang.org/protobuf/types/descriptorpb" 24 "gopkg.in/yaml.v3" 25 ) 26 27 func TestProblemJSON(t *testing.T) { 28 problem := &Problem{ 29 Message: "foo bar", 30 Location: &dpb.SourceCodeInfo_Location{Span: []int32{2, 0, 42}}, 31 RuleID: "core::0131", 32 } 33 serialized, err := json.Marshal(problem) 34 if err != nil { 35 t.Fatalf("Could not marshal Problem to JSON.") 36 } 37 tests := []struct { 38 testName string 39 token string 40 }{ 41 {"Message", `"message":"foo bar"`}, 42 {"LineNumber", `"line_number":3`}, 43 {"ColumnNumberStart", `"column_number":1`}, 44 {"ColumnNumberEnd", `"column_number":42`}, 45 {"RuleID", `"rule_id":"core::0131"`}, 46 } 47 for _, test := range tests { 48 t.Run(test.testName, func(t *testing.T) { 49 if !strings.Contains(string(serialized), test.token) { 50 t.Errorf("Got\n%v\nExpected `%s` to be present.", string(serialized), test.token) 51 } 52 }) 53 } 54 } 55 56 func TestProblemYAML(t *testing.T) { 57 problem := &Problem{ 58 Message: "foo bar", 59 Location: &dpb.SourceCodeInfo_Location{Span: []int32{2, 0, 5, 70}}, 60 RuleID: "core::0131", 61 } 62 serialized, err := yaml.Marshal(problem) 63 if err != nil { 64 t.Fatalf("Could not marshal Problem to YAML.") 65 } 66 tests := []struct { 67 testName string 68 token string 69 }{ 70 {"Message", `message: foo bar`}, 71 {"LineNumberStart", `line_number: 3`}, 72 {"LintNumberEnd", `line_number: 6`}, 73 {"ColumnNumberStart", `column_number: 1`}, 74 {"ColumnNumberEnd", `column_number: 70`}, 75 {"RuleID", `rule_id: core::0131`}, 76 } 77 for _, test := range tests { 78 t.Run(test.testName, func(t *testing.T) { 79 if !strings.Contains(string(serialized), test.token) { 80 t.Errorf("Got\n%v\nExpected `%s` to be present.", string(serialized), test.token) 81 } 82 }) 83 } 84 } 85 86 func TestProblemDescriptor(t *testing.T) { 87 mb := builder.NewMessage("Foo") 88 builder.NewFile("foo.proto").AddMessage(mb) 89 90 m, err := mb.Build() 91 if err != nil { 92 t.Fatalf("%v", err) 93 } 94 m.GetSourceInfo().Span = []int32{42, 0, 79} 95 problem := &Problem{ 96 Message: "foo bar", 97 Descriptor: m, 98 RuleID: "core::0131", 99 } 100 serialized, err := yaml.Marshal(problem) 101 if err != nil { 102 t.Fatalf("Could not marshal Problem to YAML.") 103 } 104 tests := []struct { 105 testName string 106 token string 107 }{ 108 {"Message", `message: foo bar`}, 109 {"LineNumber", `line_number: 43`}, 110 {"ColumnNumberStart", `column_number: 1`}, 111 {"ColumnNumberEnd", `column_number: 79`}, 112 {"RuleID", `rule_id: core::0131`}, 113 {"Path", `path: foo.proto`}, 114 } 115 for _, test := range tests { 116 t.Run(test.testName, func(t *testing.T) { 117 if !strings.Contains(string(serialized), test.token) { 118 t.Errorf("Got\n%v\nExpected `%s` to be present.", string(serialized), test.token) 119 } 120 }) 121 } 122 }