go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/gerrit/util_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 gerrit
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestTruncateMessage(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	Convey("TruncateMessage", t, func() {
    27  
    28  		Convey("Noop when message doesn't exceed max length", func() {
    29  			const msg = "this is a message"
    30  			So(truncate(msg, len(msg)), ShouldEqual, msg)
    31  		})
    32  
    33  		Convey("Truncate works", func() {
    34  			const msg = "message is 45 characters long; max length 40."
    35  			So(truncate(msg, 40), ShouldEqual, "message \n...[truncated too long message]")
    36  		})
    37  
    38  		Convey("Last rune is valid utf-8", func() {
    39  			const msg = "😀😀😀😀😀😀😀😀😀😀😀😀"
    40  			So(msg, ShouldHaveLength, 12*4) // 😀 is 4 bytes
    41  			const expected = "😀\n...[truncated too long message]"
    42  			// result should only keep (39-32)/4 = 1 emoji, where 32 is the
    43  			// length of placeholder.
    44  			So(truncate(msg, 39), ShouldEqual, expected)
    45  		})
    46  	})
    47  }
    48  
    49  func TestTag(t *testing.T) {
    50  	t.Parallel()
    51  
    52  	Convey("Tag", t, func() {
    53  
    54  		Convey("panic if name is not provided", func() {
    55  			So(func() { Tag("", "") }, ShouldPanic)
    56  		})
    57  		Convey("panic if name contains ~", func() {
    58  			So(func() { Tag("a~b", "") }, ShouldPanic)
    59  		})
    60  		Convey("with suffix", func() {
    61  			So(Tag("dry-run", "infra/abc"), ShouldEqual, "autogenerated:luci-cv:dry-run~infra/abc")
    62  		})
    63  		Convey("without", func() {
    64  			So(Tag("dry-run", ""), ShouldEqual, "autogenerated:luci-cv:dry-run")
    65  		})
    66  	})
    67  }