lab.nexedi.com/kirr/go123@v0.0.0-20240207185015-8299741fa871/xstrings/xstrings_test.go (about) 1 // Copyright (C) 2015-2017 Nexedi SA and Contributors. 2 // Kirill Smelkov <kirr@nexedi.com> 3 // 4 // This program is free software: you can Use, Study, Modify and Redistribute 5 // it under the terms of the GNU General Public License version 3, or (at your 6 // option) any later version, as published by the Free Software Foundation. 7 // 8 // You can also Link and Combine this program with other software covered by 9 // the terms of any of the Free Software licenses or any of the Open Source 10 // Initiative approved licenses and Convey the resulting work. Corresponding 11 // source of such a combination shall include the source code for all other 12 // software used. 13 // 14 // This program is distributed WITHOUT ANY WARRANTY; without even the implied 15 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16 // 17 // See COPYING file for full licensing terms. 18 // See https://www.nexedi.com/licensing for rationale and options. 19 20 package xstrings 21 22 import ( 23 "reflect" 24 "testing" 25 ) 26 27 func TestSplitLines(t *testing.T) { 28 var tests = []struct { input, sep string; output []string } { 29 {"", "\n", []string{}}, 30 {"hello", "\n", []string{"hello"}}, 31 {"hello\n", "\n", []string{"hello"}}, 32 {"hello\nworld", "\n", []string{"hello", "world"}}, 33 {"hello\nworld\n", "\n", []string{"hello", "world"}}, 34 {"hello\x00world\x00", "\n", []string{"hello\x00world\x00"}}, 35 {"hello\x00world\x00", "\x00", []string{"hello", "world"}}, 36 } 37 38 for _, tt := range tests { 39 sv := SplitLines(tt.input, tt.sep) 40 if !reflect.DeepEqual(sv, tt.output) { 41 t.Errorf("splitlines(%q, %q) -> %q ; want %q", tt.input, tt.sep, sv, tt.output) 42 } 43 } 44 } 45 46 func TestSplit2(t *testing.T) { 47 var tests = []struct { input, s1, s2 string; ok bool } { 48 {"", "", "", false}, 49 {" ", "", "", true}, 50 {"hello", "", "", false}, 51 {"hello world", "hello", "world", true}, 52 {"hello world 1", "", "", false}, 53 } 54 55 for _, tt := range tests { 56 s1, s2, err := Split2(tt.input, " ") 57 ok := err == nil 58 if s1 != tt.s1 || s2 != tt.s2 || ok != tt.ok { 59 t.Errorf("split2(%q) -> %q %q %v ; want %q %q %v", tt.input, s1, s2, ok, tt.s1, tt.s2, tt.ok) 60 } 61 } 62 } 63 64 func TestHeadtail(t *testing.T) { 65 var tests = []struct { input, head, tail string; ok bool } { 66 {"", "", "", false}, 67 {" ", "", "", true}, 68 {" ", "", " ", true}, 69 {"hello world", "hello", "world", true}, 70 {"hello world 1", "hello", "world 1", true}, 71 {"hello world 2", "hello", " world 2", true}, 72 } 73 74 for _, tt := range tests { 75 head, tail, err := HeadTail(tt.input, " ") 76 ok := err == nil 77 if head != tt.head || tail != tt.tail || ok != tt.ok { 78 t.Errorf("headtail(%q) -> %q %q %v ; want %q %q %v", tt.input, head, tail, ok, tt.head, tt.tail, tt.ok) 79 } 80 } 81 }