...

Source file src/github.com/huandu/xstrings/translate_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  	"fmt"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestTranslate(t *testing.T) {
    13  	runner := func(str string) string {
    14  		input := strings.Split(str, separator)
    15  		return Translate(input[0], input[1], input[2])
    16  	}
    17  
    18  	runTestCases(t, runner, _M{
    19  		sep("hello", "aeiou", "12345"):    "h2ll4",
    20  		sep("hello", "aeiou", ""):         "hll",
    21  		sep("hello", "a-z", "A-Z"):        "HELLO",
    22  		sep("hello", "z-a", "a-z"):        "svool",
    23  		sep("hello", "aeiou", "*"):        "h*ll*",
    24  		sep("hello", "^l", "*"):           "**ll*",
    25  		sep("hello", "p-z", "*"):          "hello",
    26  		sep("hello ^ world", `\^lo`, "*"): "he*** * w*r*d",
    27  
    28  		sep("中文字符测试", "文中谁敢试?", "123456"):  "21字符测5",
    29  		sep("中文字符测试", "^文中谁敢试?", "123456"): "中文666试",
    30  		sep("中文字符测试", "字-试", "0-9"):        "中90999",
    31  
    32  		sep("h1e2l3l4o, w5o6r7l8d", "a-z,0-9", `A-Z\-a-czk-p`):       "HbEcLzLkO- WlOmRnLoD",
    33  		sep("h1e2l3l4o, w5o6r7l8d", "a-zoh-n", "b-zakt-z"):           "t1f2x3x4k, x5k6s7x8e",
    34  		sep("h1e2l3l4o, w5o6r7l8d", "helloa-zoh-n", "99999b-zakt-z"): "t1f2x3x4k, x5k6s7x8e",
    35  
    36  		sep("hello", "e-", "p"):        "hpllo",
    37  		sep("hello", "-e-", "p"):       "hpllo",
    38  		sep("hello", "----e---", "p"):  "hpllo",
    39  		sep("hello", "^---e----", "p"): "peppp",
    40  
    41  		sep("hel\uFFFDlo", "\uFFFD", "H"):    "helHlo",
    42  		sep("hel\uFFFDlo", "^\uFFFD", "H"):   "HHHHH",
    43  		sep("hel\uFFFDlo", "o-\uFFFDh", "H"): "HelHlH",
    44  	})
    45  }
    46  
    47  func TestDelete(t *testing.T) {
    48  	runner := func(str string) string {
    49  		input := strings.Split(str, separator)
    50  		return Delete(input[0], input[1])
    51  	}
    52  
    53  	runTestCases(t, runner, _M{
    54  		sep("hello", "aeiou"): "hll",
    55  		sep("hello", "a-k"):   "llo",
    56  		sep("hello", "^a-k"):  "he",
    57  
    58  		sep("中文字符测试", "文中谁敢试?"): "字符测",
    59  	})
    60  }
    61  
    62  func TestCount(t *testing.T) {
    63  	runner := func(str string) string {
    64  		input := strings.Split(str, separator)
    65  		return fmt.Sprint(Count(input[0], input[1]))
    66  	}
    67  
    68  	runTestCases(t, runner, _M{
    69  		sep("hello", "aeiou"): "2",
    70  		sep("hello", "a-k"):   "2",
    71  		sep("hello", "^a-k"):  "3",
    72  
    73  		sep("中文字符测试", "文中谁敢试?"): "3",
    74  	})
    75  }
    76  
    77  func TestSqueeze(t *testing.T) {
    78  	runner := func(str string) string {
    79  		input := strings.Split(str, separator)
    80  		return Squeeze(input[0], input[1])
    81  	}
    82  
    83  	runTestCases(t, runner, _M{
    84  		sep("hello", ""):             "helo",
    85  		sep("hello     world", ""):   "helo world",
    86  		sep("hello     world", " "):  "hello world",
    87  		sep("hello     world", "  "): "hello world",
    88  		sep("hello", "a-k"):          "hello",
    89  		sep("hello", "^a-k"):         "helo",
    90  		sep("hello", "^a-l"):         "hello",
    91  		sep("foooo baaaaar", "a"):    "foooo bar",
    92  
    93  		sep("打打打打个劫!!", ""):  "打个劫!",
    94  		sep("打打打打个劫!!", "打"): "打个劫!!",
    95  	})
    96  }
    97  

View as plain text