github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zstring/string_test.go (about)

     1  package zstring
     2  
     3  import (
     4  	"math/rand"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/sohaha/zlsgo"
    10  )
    11  
    12  func TestBuffer(T *testing.T) {
    13  	t := zlsgo.NewTest(T)
    14  	l := "0"
    15  	l += "1"
    16  	b := Buffer(2)
    17  	b.WriteString("0")
    18  	b.WriteString("1")
    19  	t.Equal(l, b.String())
    20  }
    21  
    22  func TestLen(T *testing.T) {
    23  	t := zlsgo.NewTest(T)
    24  	s := "我是中国人"
    25  	t.Equal(5, Len(s))
    26  	t.Log(Len(s), len(s))
    27  }
    28  
    29  func TestSubstr(T *testing.T) {
    30  	t := zlsgo.NewTest(T)
    31  	s := "0123"
    32  	t.Equal(Substr(s, 1), "123")
    33  	t.Equal(Substr(s, 2, 1), "2")
    34  	t.Equal("A,是我呀", Substr("你好A,是我呀", 2))
    35  	t.Equal("是我呀", Substr("你好A,是我呀", -3))
    36  }
    37  
    38  func TestPad(T *testing.T) {
    39  	t := zlsgo.NewTest(T)
    40  	l := "我的这里一共8字"
    41  	t.Equal(8, Len(l))
    42  
    43  	s := "我的长度是二十,不够右边补零"
    44  	t.Equal("我的长度是二十,不够右边补零000000", Pad(s, 20, "0", PadRight))
    45  
    46  	s2 := "我的长度是二十,不够左边补零"
    47  	t.Equal("000000我的长度是二十,不够左边补零", Pad(s2, 20, "0", PadLeft))
    48  
    49  	s3 := "我的长度很长不需要填充"
    50  	t.Equal("我的长度很长不需要填充", Pad(s3, 5, "我的长度很长不需要填充", PadRight))
    51  
    52  	t.Equal("长度", Substr(s3, 2, 2))
    53  
    54  	s4 := "我的长度是二十,不够两边补零"
    55  	t.Equal("000我的长度是二十,不够两边补零000", Pad(s4, 20, "0", PadSides))
    56  }
    57  
    58  func TestFirst(T *testing.T) {
    59  	t := zlsgo.NewTest(T)
    60  	str := "myName"
    61  	str = Ucfirst(str)
    62  	t.Equal(true, IsUcfirst(str))
    63  	t.Equal(false, IsLcfirst(str))
    64  	t.Equal("MyName", str)
    65  	str = Lcfirst(str)
    66  	t.Equal(true, IsLcfirst(str))
    67  	t.Equal(false, IsUcfirst(str))
    68  	t.Equal("myName", str)
    69  }
    70  
    71  func TestSnakeCaseCamelCase(T *testing.T) {
    72  	t := zlsgo.NewTest(T)
    73  	t.Equal("", SnakeCaseToCamelCase("", true))
    74  	t.Equal("HelloWorld", SnakeCaseToCamelCase("hello_world", true))
    75  	t.Equal("helloWorld", SnakeCaseToCamelCase("hello_world", false))
    76  	t.Equal("helloWorld", SnakeCaseToCamelCase("hello-world", false, "-"))
    77  
    78  	t.Equal("", CamelCaseToSnakeCase(""))
    79  	t.Equal("hello_world", CamelCaseToSnakeCase("HelloWorld"))
    80  	t.Equal("hello_world", CamelCaseToSnakeCase("helloWorld"))
    81  	t.Equal("hello-world", CamelCaseToSnakeCase("helloWorld", "-"))
    82  }
    83  
    84  func TestXss(T *testing.T) {
    85  	t := zlsgo.NewTest(T)
    86  	htmls := [][]string{
    87  		{"", ""},
    88  		{"Hello, World!", "Hello, World!"},
    89  		{"foo&bar", "foo&bar"},
    90  		{`Hello <a href="www.example.com/">World</a>!`, "Hello World!"},
    91  		{"Foo <textarea>Bar</textarea> Baz", "Foo Bar Baz"},
    92  		{"Foo <!-- Bar --> Baz", "Foo Baz"},
    93  		{"<", "<"},
    94  		{"foo < bar", "foo < bar"},
    95  		{`Foo<script type="text/javascript">alert(1337)</script>Bar`, "FooBar"},
    96  		{`Foo<div title="1>2">Bar`, "Foo2\">Bar"},
    97  		{`I <3 Ponies!`, `I <3 Ponies!`},
    98  		{`<script>foo()</script>`, ``},
    99  		{`<script>foo()</script>`, ``},
   100  	}
   101  	for _, v := range htmls {
   102  		t.Equal(v[1], XSSClean(v[0]))
   103  	}
   104  }
   105  
   106  func TestTrimSpace(t *testing.T) {
   107  	const space = "\t\v\r\f\n\u0085\u00a0\u2000\u3000"
   108  	tt := zlsgo.NewTest(t)
   109  	for _, v := range []string{
   110  		`    22 33`, `456     `, " \t\n a lone gopher \n\t\r\n", " ", " ", " \t\r\n \t\t\r\r\n\n ", " \t\r\n x\t\t\r\r\n\n ", "1 \t\r\n2", "x ☺\xc0\xc0 ", "x \xc0 ", "x ☺", space + "abc" + space,
   111  	} {
   112  		t.Log(strings.TrimSpace(v), "==", TrimSpace(v))
   113  		tt.Equal(strings.TrimSpace(v), TrimSpace(v))
   114  	}
   115  }
   116  
   117  func TestTrimLine(t *testing.T) {
   118  	const html = `
   119  		<html>
   120  		<head>
   121  		<title>Test</title>
   122  		</head>
   123  		<body>
   124  		<h1>Hello, 世界</h1>
   125  		<p>This is a test.</p>
   126  		</body>
   127  		</html>`
   128  	tt := zlsgo.NewTest(t)
   129  	tt.Equal(`<html><head><title>Test</title></head><body><h1>Hello, 世界</h1><p>This is a test.</p></body></html>`, TrimLine(html))
   130  }
   131  
   132  func BenchmarkStr(b *testing.B) {
   133  	s := ""
   134  	for i := 0; i < b.N; i++ {
   135  		s += "1"
   136  	}
   137  }
   138  
   139  func BenchmarkStrBuffer(b *testing.B) {
   140  	s := Buffer()
   141  	for i := 0; i < b.N; i++ {
   142  		s.WriteString("1")
   143  	}
   144  	_ = s.String()
   145  }
   146  
   147  func BenchmarkTo1(b *testing.B) {
   148  	for i := 0; i < b.N; i++ {
   149  		s := "我是中国人"
   150  		b := String2Bytes(s)
   151  		_ = Bytes2String(b)
   152  	}
   153  }
   154  
   155  func BenchmarkTo2(b *testing.B) {
   156  	for i := 0; i < b.N; i++ {
   157  		s := "我是中国人"
   158  		b := []byte(s)
   159  		_ = string(b)
   160  	}
   161  }
   162  
   163  func getRandomString(l int) string {
   164  	str := "0123456789abcdefghijklmnopqrstuvwxyz"
   165  	bytes := []byte(str)
   166  	var result []byte
   167  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
   168  	for i := 0; i < l; i++ {
   169  		result = append(result, bytes[r.Intn(len(bytes))])
   170  	}
   171  	return string(result)
   172  }
   173  
   174  func BenchmarkBuffer1(b *testing.B) {
   175  	bb := Buffer()
   176  	str := getRandomString(99999)
   177  	for i := 0; i < b.N; i++ {
   178  		bb.WriteString(str)
   179  	}
   180  }
   181  
   182  func BenchmarkBuffer2(b *testing.B) {
   183  	bb := Buffer(99999 * b.N)
   184  	str := getRandomString(99999)
   185  	for i := 0; i < b.N; i++ {
   186  		bb.WriteString(str)
   187  	}
   188  }