github.com/richardwilkes/toolbox@v1.121.0/txt/substr_test.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package txt_test 11 12 import ( 13 "testing" 14 15 "github.com/richardwilkes/toolbox/check" 16 "github.com/richardwilkes/toolbox/txt" 17 ) 18 19 func TestFirstN(t *testing.T) { 20 table := []struct { 21 In string 22 Out string 23 N int 24 }{ 25 {In: "abcd", N: 3, Out: "abc"}, 26 {In: "abcd", N: 5, Out: "abcd"}, 27 {In: "abcd", N: 0, Out: ""}, 28 {In: "abcd", N: -1, Out: ""}, 29 {In: "aécd", N: 3, Out: "aéc"}, 30 {In: "aécd", N: 5, Out: "aécd"}, 31 {In: "aécd", N: 0, Out: ""}, 32 {In: "aécd", N: -1, Out: ""}, 33 } 34 for i, one := range table { 35 check.Equal(t, one.Out, txt.FirstN(one.In, one.N), "#%d", i) 36 } 37 } 38 39 func TestLastN(t *testing.T) { 40 table := []struct { 41 In string 42 Out string 43 N int 44 }{ 45 {In: "abcd", N: 3, Out: "bcd"}, 46 {In: "abcd", N: 5, Out: "abcd"}, 47 {In: "abcd", N: 0, Out: ""}, 48 {In: "abcd", N: -1, Out: ""}, 49 {In: "aécd", N: 3, Out: "écd"}, 50 {In: "aécd", N: 5, Out: "aécd"}, 51 {In: "aécd", N: 0, Out: ""}, 52 {In: "aécd", N: -1, Out: ""}, 53 } 54 for i, one := range table { 55 check.Equal(t, one.Out, txt.LastN(one.In, one.N), "#%d", i) 56 } 57 }