go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/client/isolate/utils_test.go (about)

     1  // Copyright 2015 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 isolate
    16  
    17  import (
    18  	"io"
    19  	"log"
    20  	"os"
    21  	"testing"
    22  
    23  	. "github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  func TestUniqueMergeSortedStrings(t *testing.T) {
    27  	t.Parallel()
    28  	Convey(`Tests the unique merge of sorted of strings.`, t, func() {
    29  		SS := func(s string) []string {
    30  			out := make([]string, 0, len(s))
    31  			for _, c := range s {
    32  				out = append(out, string(c))
    33  			}
    34  			return out
    35  		}
    36  		So(uniqueMergeSortedStrings(SS("acde"), SS("abe")), ShouldResemble, SS("abcde"))
    37  		So(uniqueMergeSortedStrings(SS("abc"), SS("")), ShouldResemble, SS("abc"))
    38  		So(uniqueMergeSortedStrings(
    39  			[]string{"bar", "foo", "test"},
    40  			[]string{"foo", "toss", "xyz"}),
    41  			ShouldResemble, []string{"bar", "foo", "test", "toss", "xyz"})
    42  
    43  		// Test degenerate cases (empty and single-element lists)
    44  		So(uniqueMergeSortedStrings(SS(""), SS("")), ShouldResemble, SS(""))
    45  		So(uniqueMergeSortedStrings(SS("x"), SS("")), ShouldResemble, SS("x"))
    46  		So(uniqueMergeSortedStrings(SS(""), SS("x")), ShouldResemble, SS("x"))
    47  	})
    48  }
    49  
    50  func TestAssert(t *testing.T) {
    51  	Convey(`Helper function for test assertion.`, t, func() {
    52  		log.SetOutput(io.Discard)
    53  		defer log.SetOutput(os.Stderr)
    54  
    55  		wasPanic := func(f func()) (yes bool) {
    56  			defer func() {
    57  				yes = nil != recover()
    58  			}()
    59  			f()
    60  			return
    61  		}
    62  		So(wasPanic(func() { assert(false) }), ShouldBeTrue)
    63  		So(wasPanic(func() { assert(false, "format") }), ShouldBeTrue)
    64  		So(wasPanic(func() { assert(false, "format") }), ShouldBeTrue)
    65  	})
    66  }