|
| 1 | +// Copyright 2024 The Cockroach 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 |
| 12 | +// implied. See the License for the specific language governing |
| 13 | +// permissions and limitations under the License. |
| 14 | + |
| 15 | +package crstrings |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +type num int |
| 24 | + |
| 25 | +func (n num) String() string { |
| 26 | + return fmt.Sprintf("%03d", int(n)) |
| 27 | +} |
| 28 | + |
| 29 | +func TestJoinStringers(t *testing.T) { |
| 30 | + nums := []num{0, 1, 2, 3} |
| 31 | + expect(t, "", JoinStringers(", ", nums[:0]...)) |
| 32 | + expect(t, "000", JoinStringers(", ", nums[0])) |
| 33 | + expect(t, "000, 001", JoinStringers(", ", nums[0], nums[1])) |
| 34 | + expect(t, "000, 001, 002, 003", JoinStringers(", ", nums...)) |
| 35 | +} |
| 36 | + |
| 37 | +func TestMapAndJoin(t *testing.T) { |
| 38 | + nums := []int{0, 1, 2, 3} |
| 39 | + fn := func(n int) string { |
| 40 | + return fmt.Sprintf("%d", n) |
| 41 | + } |
| 42 | + expect(t, "", MapAndJoin(fn, ", ", nums[:0]...)) |
| 43 | + expect(t, "0", MapAndJoin(fn, ", ", nums[0])) |
| 44 | + expect(t, "0, 1", MapAndJoin(fn, ", ", nums[0], nums[1])) |
| 45 | + expect(t, "0, 1, 2, 3", MapAndJoin(fn, ", ", nums...)) |
| 46 | +} |
| 47 | + |
| 48 | +func expect(t *testing.T, expected, actual string) { |
| 49 | + t.Helper() |
| 50 | + if actual != expected { |
| 51 | + t.Errorf("expected %q got %q", expected, actual) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestIf(t *testing.T) { |
| 56 | + expect(t, "", If(false, "true")) |
| 57 | + expect(t, "true", If(true, "true")) |
| 58 | +} |
| 59 | + |
| 60 | +func TestIfElse(t *testing.T) { |
| 61 | + expect(t, "false", IfElse(false, "true", "false")) |
| 62 | + expect(t, "true", IfElse(true, "true", "false")) |
| 63 | +} |
| 64 | + |
| 65 | +func TestWithSep(t *testing.T) { |
| 66 | + expect(t, "a,b", WithSep("a", ",", "b")) |
| 67 | + expect(t, "a", WithSep("a", ",", "")) |
| 68 | + expect(t, "b", WithSep("", ",", "b")) |
| 69 | +} |
| 70 | + |
| 71 | +func TestFilterEmpty(t *testing.T) { |
| 72 | + s := []string{"a", "", "b", "", "c", ""} |
| 73 | + expect(t, "a,b,c", strings.Join(FilterEmpty(s), ",")) |
| 74 | +} |
| 75 | + |
| 76 | +func TestLines(t *testing.T) { |
| 77 | + expect(t, `["a" "b" "c"]`, fmt.Sprintf("%q", Lines("a\nb\nc"))) |
| 78 | + expect(t, `["a" "b" "c"]`, fmt.Sprintf("%q", Lines("a\nb\nc\n"))) |
| 79 | + expect(t, `["a" "b" "c" ""]`, fmt.Sprintf("%q", Lines("a\nb\nc\n\n"))) |
| 80 | + expect(t, `["" "a" "b" "c"]`, fmt.Sprintf("%q", Lines("\na\nb\nc\n"))) |
| 81 | + expect(t, `[]`, fmt.Sprintf("%q", Lines(""))) |
| 82 | + expect(t, `[]`, fmt.Sprintf("%q", Lines("\n"))) |
| 83 | +} |
0 commit comments