go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/gerrit/metadata/metadata_test.go (about) 1 // Copyright 2021 The LUCI Authors. 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 // http://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 metadata 16 17 import ( 18 "strings" 19 "testing" 20 21 "go.chromium.org/luci/cv/internal/changelist" 22 23 . "github.com/smartystreets/goconvey/convey" 24 . "go.chromium.org/luci/common/testing/assertions" 25 ) 26 27 func TestExtract(t *testing.T) { 28 t.Parallel() 29 30 Convey("Extract works", t, func() { 31 32 Convey("Empty", func() { 33 So(Extract(`Title.`), ShouldResembleProto, []*changelist.StringPair{}) 34 }) 35 36 Convey("Git-style", func() { 37 So(Extract(strings.TrimSpace(` 38 Title. 39 40 Footer: value 41 No-rma-lIzes: but Only key. 42 `)), ShouldResembleProto, []*changelist.StringPair{ 43 {Key: "Footer", Value: "value"}, 44 {Key: "No-Rma-Lizes", Value: "but Only key."}, 45 }) 46 }) 47 48 Convey("TAGS=sTyLe", func() { 49 So(Extract(strings.TrimSpace(` 50 TAG=can BE anywhere 51 `)), ShouldResembleProto, []*changelist.StringPair{ 52 {Key: "TAG", Value: "can BE anywhere"}, 53 }) 54 }) 55 56 Convey("Ignores incorrect tags and footers", func() { 57 So(Extract(strings.TrimSpace(` 58 Tag=must have UPPEPCASE_KEY. 59 60 Footers: must reside in the last paragraph, not above it. 61 62 Footer-key must-have-not-spaces: but this one does. 63 `)), ShouldResembleProto, []*changelist.StringPair{}) 64 }) 65 66 Convey("Sorts by keys only, keeps values ordered from last to first", func() { 67 So(Extract(strings.TrimSpace(` 68 TAG=first 69 TAG=second 70 71 Footer: A 72 TAG=third 73 Footer: D 74 TAG=fourth 75 Footer: B 76 `)), ShouldResembleProto, []*changelist.StringPair{ 77 {Key: "Footer", Value: "B"}, 78 {Key: "Footer", Value: "D"}, 79 {Key: "Footer", Value: "A"}, 80 {Key: "TAG", Value: "fourth"}, 81 {Key: "TAG", Value: "third"}, 82 {Key: "TAG", Value: "second"}, 83 {Key: "TAG", Value: "first"}, 84 }) 85 }) 86 }) 87 }