...

Source file src/github.com/huandu/xstrings/manipulate_test.go

Documentation: github.com/huandu/xstrings

     1  // Copyright 2015 Huan Du. All rights reserved.
     2  // Licensed under the MIT license that can be found in the LICENSE file.
     3  
     4  package xstrings
     5  
     6  import (
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestReverse(t *testing.T) {
    13  	runTestCases(t, Reverse, _M{
    14  		"reverse string": "gnirts esrever",
    15  		"中文如何?":          "?何如文中",
    16  		"中en文混~排怎样?a":    "a?样怎排~混文ne中",
    17  	})
    18  }
    19  
    20  func TestSlice(t *testing.T) {
    21  	runner := func(str string) (result string) {
    22  		defer func() {
    23  			if e := recover(); e != nil {
    24  				result = e.(string)
    25  			}
    26  		}()
    27  
    28  		strs := split(str)
    29  		start, _ := strconv.ParseInt(strs[1], 10, 0)
    30  		end, _ := strconv.ParseInt(strs[2], 10, 0)
    31  
    32  		result = Slice(strs[0], int(start), int(end))
    33  		return
    34  	}
    35  
    36  	runTestCases(t, runner, _M{
    37  		sep("abcdefghijk", "3", "8"):      "defgh",
    38  		sep("来点中文如何?", "2", "7"):          "中文如何?",
    39  		sep("中en文混~排总是少不了的a", "2", "8"):   "n文混~排总",
    40  		sep("中en文混~排总是少不了的a", "0", "0"):   "",
    41  		sep("中en文混~排总是少不了的a", "14", "14"): "",
    42  		sep("中en文混~排总是少不了的a", "5", "-1"):  "~排总是少不了的a",
    43  		sep("中en文混~排总是少不了的a", "14", "-1"): "",
    44  
    45  		sep("let us slice out of range", "-3", "3"): "out of range",
    46  		sep("超出范围哦", "2", "6"):                      "out of range",
    47  		sep("don't do this", "3", "2"):              "out of range",
    48  		sep("千gan万de不piao要liang", "19", "19"):       "out of range",
    49  	})
    50  }
    51  
    52  func TestPartition(t *testing.T) {
    53  	runner := func(str string) string {
    54  		input := strings.Split(str, separator)
    55  		head, match, tail := Partition(input[0], input[1])
    56  		return sep(head, match, tail)
    57  	}
    58  
    59  	runTestCases(t, runner, _M{
    60  		sep("hello", "l"):           sep("he", "l", "lo"),
    61  		sep("中文总少不了", "少"):          sep("中文总", "少", "不了"),
    62  		sep("z这个zh英文混排hao不", "h英文"): sep("z这个z", "h英文", "混排hao不"),
    63  		sep("边界tiao件zen能忘", "边界"):   sep("", "边界", "tiao件zen能忘"),
    64  		sep("尾巴ye别忘le", "忘le"):      sep("尾巴ye别", "忘le", ""),
    65  
    66  		sep("hello", "x"):     sep("hello", "", ""),
    67  		sep("不是晩香玉", "晚"):     sep("不是晩香玉", "", ""), // Hint: 晩 is not 晚 :)
    68  		sep("来ge混排ba", "e 混"): sep("来ge混排ba", "", ""),
    69  	})
    70  }
    71  
    72  func TestLastPartition(t *testing.T) {
    73  	runner := func(str string) string {
    74  		input := strings.Split(str, separator)
    75  		head, match, tail := LastPartition(input[0], input[1])
    76  		return sep(head, match, tail)
    77  	}
    78  
    79  	runTestCases(t, runner, _M{
    80  		sep("hello", "l"):               sep("hel", "l", "o"),
    81  		sep("少量中文总少不了", "少"):            sep("少量中文总", "少", "不了"),
    82  		sep("z这个zh英文ch英文混排hao不", "h英文"): sep("z这个zh英文c", "h英文", "混排hao不"),
    83  		sep("边界tiao件zen能忘边界", "边界"):     sep("边界tiao件zen能忘", "边界", ""),
    84  		sep("尾巴ye别忘le", "尾巴"):           sep("", "尾巴", "ye别忘le"),
    85  
    86  		sep("hello", "x"):     sep("", "", "hello"),
    87  		sep("不是晩香玉", "晚"):     sep("", "", "不是晩香玉"), // Hint: 晩 is not 晚 :)
    88  		sep("来ge混排ba", "e 混"): sep("", "", "来ge混排ba"),
    89  	})
    90  }
    91  
    92  func TestInsert(t *testing.T) {
    93  	runner := func(str string) (result string) {
    94  		defer func() {
    95  			if e := recover(); e != nil {
    96  				result = e.(string)
    97  			}
    98  		}()
    99  
   100  		strs := split(str)
   101  		index, _ := strconv.ParseInt(strs[2], 10, 0)
   102  		result = Insert(strs[0], strs[1], int(index))
   103  		return
   104  	}
   105  
   106  	runTestCases(t, runner, _M{
   107  		sep("abcdefg", "hi", "3"):    "abchidefg",
   108  		sep("少量中文是必须的", "混pai", "4"): "少量中文混pai是必须的",
   109  		sep("zh英文hun排", "~!", "5"):   "zh英文h~!un排",
   110  		sep("插在beginning", "我", "0"): "我插在beginning",
   111  		sep("插在ending", "我", "8"):    "插在ending我",
   112  
   113  		sep("超tian出yuan边tu界po", "foo", "-1"): "out of range",
   114  		sep("超tian出yuan边tu界po", "foo", "17"): "out of range",
   115  	})
   116  }
   117  
   118  func TestScrub(t *testing.T) {
   119  	runner := func(str string) string {
   120  		strs := split(str)
   121  		return Scrub(strs[0], strs[1])
   122  	}
   123  
   124  	runTestCases(t, runner, _M{
   125  		sep("ab\uFFFDcd\xFF\xCEefg\xFF\xFC\xFD\xFAhijk", "*"): "ab*cd*efg*hijk",
   126  		sep("no错误です", "*"):                                    "no错误です",
   127  		sep("", "*"):                                          "",
   128  	})
   129  }
   130  
   131  func TestWordSplit(t *testing.T) {
   132  	runner := func(str string) string {
   133  		return sep(WordSplit(str)...)
   134  	}
   135  
   136  	runTestCases(t, runner, _M{
   137  		"one word":                   sep("one", "word"),
   138  		"一个字:把他给我拿下!":                "",
   139  		"it's a super-fancy one!!!a": sep("it's", "a", "super-fancy", "one", "a"),
   140  		"a -b-c' 'd'e":               sep("a", "b-c'", "d'e"),
   141  	})
   142  }
   143  

View as plain text