| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package stringutils2
- import (
- "reflect"
- "strconv"
- "testing"
- )
- func TestEscapeString(t *testing.T) {
- type args struct {
- str string
- pairs [][]string
- }
- tests := []struct {
- name string
- args args
- want string
- }{
- {
- name: "normalInput",
- args: args{
- str: "abcd\n\"Te\\rst\"ddd\"$Test\"aaa\n$TTT",
- pairs: nil,
- },
- want: `abcd\n\"Te\\rst\"ddd\"\$Test\"aaa\n\$TTT`,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if got := EscapeString(tt.args.str, tt.args.pairs); got != tt.want {
- t.Errorf("EscapeString() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestSplitByQuotation(t *testing.T) {
- type args struct {
- line string
- }
- tests := []struct {
- name string
- args args
- want []string
- wantErr bool
- }{
- {
- name: "normalInput",
- args: args{`"abc" addf "sada"`},
- want: []string{"abc", " addf ", "sada"},
- wantErr: false,
- },
- {
- name: "errorInput",
- args: args{`"abc" "addf "sada"`},
- want: nil,
- wantErr: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := SplitByQuotation(tt.args.line)
- if (err != nil) != tt.wantErr {
- t.Errorf("SplitByQuotation() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("SplitByQuotation() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestEscapeEchoString(t *testing.T) {
- type args struct {
- str string
- }
- tests := []struct {
- name string
- args args
- want string
- wantErr bool
- }{
- {
- name: "normalInput",
- args: args{"abcd\n\"Te\\rst\"ddd\"$Test\"aaa\n$TTT"},
- want: `abcd\n\"Te\\\\rst\"ddd\"\$Test\"aaa\n\$TTT`,
- wantErr: false,
- },
- {
- name: "echoInput",
- args: args{"SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"1d6b\", ATTRS{idProduct}==\"0001\", RUN+=\"/bin/sh -c 'echo enabled > /sys$env{DEVPATH}/../power/wakeup'\""},
- want: `SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"1d6b\", ATTRS{idProduct}==\"0001\", RUN+=\"/bin/sh -c 'echo enabled > /sys\$env{DEVPATH}/../power/wakeup'\"`,
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := EscapeEchoString(tt.args.str)
- if (err != nil) != tt.wantErr {
- t.Errorf("EscapeEchoString() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if got != tt.want {
- t.Errorf("EscapeEchoString() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestGetCharTypeCount(t *testing.T) {
- cases := []struct {
- in string
- want int
- }{
- {
- in: "",
- want: 0,
- },
- {
- in: "123",
- want: 1,
- },
- {
- in: "abc",
- want: 1,
- },
- {
- in: "abcAbc",
- want: 2,
- },
- {
- in: "123dbA",
- want: 3,
- },
- {
- in: "123@Acv",
- want: 4,
- },
- {
- in: "中文",
- want: 1,
- },
- }
- for _, c := range cases {
- got := GetCharTypeCount(c.in)
- if got != c.want {
- t.Errorf("GetCharTypeCount %s want %d got %d", c.in, c.want, got)
- }
- }
- }
- func TestRoleName(t *testing.T) {
- cases := []struct {
- in string
- want string
- random bool
- length int
- }{
- {
- in: "小琪",
- random: true,
- length: 17,
- },
- {
- in: "123^567",
- want: "123567",
- },
- }
- for _, c := range cases {
- got := GenerateRoleName(c.in)
- if c.random {
- if len(got) != c.length {
- t.Errorf("GenerateRoleName %s random want %d length got %d(%s)", c.in, c.length, len(got), got)
- }
- } else if got != c.want {
- t.Errorf("GenerateRoleName %s want %s got %s", c.in, c.want, got)
- }
- }
- }
- func TestFilterEmpty(t *testing.T) {
- cases := []struct {
- input []string
- want []string
- }{
- {
- input: []string{
- "",
- },
- want: []string{},
- },
- }
- for _, c := range cases {
- got := FilterEmpty(c.input)
- if !reflect.DeepEqual(got, c.want) {
- t.Errorf("want: %#v got: %#v", c.want, got)
- }
- }
- }
- func TestPrettyFloat(t *testing.T) {
- cases := []struct {
- in float64
- prec int
- want string
- }{
- {
- in: 3.1415926,
- prec: 2,
- want: "3.14",
- },
- {
- in: 3.1415926,
- prec: 3,
- want: "3.142",
- },
- {
- in: 3.89999999,
- prec: 2,
- want: "3.9",
- },
- {
- in: 3.88999999,
- prec: 2,
- want: "3.89",
- },
- {
- in: 3.99999999,
- prec: 2,
- want: "4",
- },
- {
- in: 0.000020000001,
- prec: 2,
- want: "0.00002",
- },
- {
- in: 0.000021000001,
- prec: 2,
- want: "0.000021",
- },
- {
- in: 0.000021100001,
- prec: 2,
- want: "0.000021",
- },
- {
- in: -3.15,
- prec: 1,
- want: "-3.2",
- },
- {
- in: 3.5,
- prec: 0,
- want: "4",
- },
- {
- in: 0.999999999999,
- prec: 2,
- want: "1",
- },
- }
- for _, c := range cases {
- got := PrettyFloat(c.in, c.prec)
- if got != c.want {
- t.Errorf("%s precision %d want %s got %s", strconv.FormatFloat(c.in, 'f', -1, 64), c.prec, c.want, got)
- }
- }
- }
|