mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 03:02:28 +08:00
Compare commits
7 Commits
a8a92844f3
...
0ef45b533b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0ef45b533b | ||
|
|
169f280c9c | ||
|
|
e74126a0bd | ||
|
|
f7be4190f1 | ||
|
|
8089b71bfd | ||
|
|
5b9543255a | ||
|
|
bf859387f4 |
@@ -1,51 +1,51 @@
|
||||
-----BEGIN rsa private key-----
|
||||
MIIJKAIBAAKCAgEA0HaqqGWnNqwRVKoJUUjYiH/dwkSK61La4RGYkvDfcuo9pDgc
|
||||
zHVhFUb/MYWeb3bAlFAGcZLYSBh6WAGxbeMjSkaLroaXnafhKZ2oXXUy8CzyYxZw
|
||||
pLCDgJLG7Pf0jer3STZW93ZT8UJixbKwbqD6b8fkpMANwCYrTlXDWBuZVaEKuQNn
|
||||
C4XufQis4fwRxRgfrZuLMvuQVtnyYmssmnp8JHovYkr87giDjEgvlrC84Lez32Zh
|
||||
EhORRu5NGEDeZ1OewbkXGyQtbcBQYNAxgnOcO44MTTPhEdz4/A0xwzEe+yIOxWIA
|
||||
WuEM2rclXpZBW/MEK41gZ3C04KcX5R0KRdj2PTTH7vJ33yRC4hKtWetwMvsUrewr
|
||||
RKVcUgaeFykDAlgIDoSWvG0nzb70AWSoNcj7krLlwEc2WLGy/kCYWppnfYKafEJX
|
||||
pgblHrDFKlWvte68yrvGdCsWRLQ2uUF0xokQIg4u2bApyWNroxEtjHwUp1VeJuZB
|
||||
Qkfg+vIW+t3moQpMtlhy4aai/oGIxI5TIB82nv8TPVczjHmmE3hwL6nZ5OfbFyWp
|
||||
RFvKLtXcaAKMi2HqOLRkQhqjjJUCO9Wyhu1BkX8RAeKLl3S7gRyhuOB5JNUGEpQ3
|
||||
89MdbUX5S12Qp1xTDS+otbC8tYS1gu0OOOWowxbuF5+Ci2SYgSHv/3zXKoMCAwEA
|
||||
AQKCAgBASCdt0BLVABBEDD7dStpClTNbwWkZEa6e8p8ayJ4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JMn7R9K/2y8pXmvPb1C8KBADKALFRLdCfbMT+5PU7o5G/J/bLQZEgpw+4lqPZ
|
||||
SUi+wmzh6ZfsaE2TCGNKL+XmL3km6BpbFVCvW/ftHaC5LQIz1XDWBQYtSDEodNiV
|
||||
rT+JonBk1W8SmcQMu2AeDk4EWYfSCRksL4OqbYaxsjSMeAT10W4zkIVfum0jBy+v
|
||||
Ey9EhZ04nVxorHjk+BunYu4NRlkhEUlP7O4g1XbKbM4NhnBhptZp1w1Nell/hlNQ
|
||||
UyEDSjLpnChY+PzfeocpLrXZg8ez83r5AoIBAQCrufc7Cu36/WoSKoMwd5n9kOQ9
|
||||
W4kzu7ZCv/0PxXj8o3nyNkHD/uZHtGGPowhzt8cZR8JKfmDqrCM28WWyGk79x9bN
|
||||
s/IVyTc5CHe0/zPJooTgpFA8etMhdc5JIYorSZaAgLiOqzDyGuNi9oyfotbLqiFZ
|
||||
4uiagMt+hdTP0yEwE1JvzQAwdJvOfMxhiNka6szKb+0QGwBLwCB71OAMXeIfzmBK
|
||||
i0PLomu7tlogS5cePt45lp3nAowreSru/DnfZobVlVFLV9rV+xFuLmxC4zB86cto
|
||||
wfZvKgSI/hP6rg0RkcQbBad0ZsVm52NtCWYTB+bISke8wDN552wZVC6lyPat
|
||||
-----END rsa private key-----
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
-----BEGIN rsa public key-----
|
||||
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA0HaqqGWnNqwRVKoJUUjY
|
||||
iH/dwkSK61La4RGYkvDfcuo9pDgczHVhFUb/MYWeb3bAlFAGcZLYSBh6WAGxbeMj
|
||||
SkaLroaXnafhKZ2oXXUy8CzyYxZwpLCDgJLG7Pf0jer3STZW93ZT8UJixbKwbqD6
|
||||
b8fkpMANwCYrTlXDWBuZVaEKuQNnC4XufQis4fwRxRgfrZuLMvuQVtnyYmssmnp8
|
||||
JHovYkr87giDjEgvlrC84Lez32ZhEhORRu5NGEDeZ1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gRl0PtmqD7EfKwgREcA20zWeQtYkioOUclX30uG
|
||||
3vVSf0j2KyoAqWmES52KqUp9pwNdopWvRtQGAey2b9mpdSgl69rM2RmbzFOIDrfw
|
||||
/1ftPpaUi2CVILviSeYtN2egkeB+ghCPCcIS9VUpnNTYcW8Y+cTnfi9FwSEwn13W
|
||||
pjiYO/K2412uWjNVNvvW3RECAwEAAQ==
|
||||
-----END rsa public key-----
|
||||
|
||||
@@ -35,6 +35,7 @@ import (
|
||||
- [IsDir](#IsDir)
|
||||
- [ListFileNames](#ListFileNames)
|
||||
- [RemoveFile](#RemoveFile)
|
||||
- [RemoveDir](#RemoveDir)
|
||||
- [ReadFileToString](#ReadFileToString)
|
||||
- [ReadFileByLine](#ReadFileByLine)
|
||||
- [Zip](#Zip)
|
||||
@@ -390,12 +391,12 @@ func main() {
|
||||
|
||||
### <span id="RemoveFile">RemoveFile</span>
|
||||
|
||||
<p>删除文件</p>
|
||||
<p>删除文件,支持传入删除前的回调函数。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func RemoveFile(path string) error
|
||||
func RemoveFile(path string, onDelete ...func(path string)) error
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/P2y0XW8a1SH)</span></b>
|
||||
@@ -416,6 +417,41 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="RemoveDir">RemoveDir</span>
|
||||
|
||||
<p>删除目录,支持传入删除前的回调函数。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func RemoveDir(path string, onDelete ...func(path string)) error
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/fileutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var deletedPaths []string
|
||||
|
||||
err := fileutil.RemoveDir("./tempdir", func(p string) {
|
||||
deletedPaths = append(deletedPaths, p)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
fmt.Println(deletedPaths)
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="ReadFileToString">ReadFileToString</span>
|
||||
|
||||
<p>读取文件内容并返回字符串</p>
|
||||
|
||||
@@ -48,6 +48,9 @@ import (
|
||||
- [UploadFile](#UploadFile)
|
||||
- [IsPingConnected](#IsPingConnected)
|
||||
- [IsTelnetConnected](#IsTelnetConnected)
|
||||
- [IsTelnetConnected](#IsTelnetConnected)
|
||||
- [BuildUrl](#BuildUrl)
|
||||
- [AddQueryParams](#AddQueryParams)
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -1031,3 +1034,83 @@ func main() {
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="BuildUrl">BuildUrl</span>
|
||||
|
||||
<p>创建url字符串。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func BuildUrl(scheme, host, path string, query map[string][]string) (string, error)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/netutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
urlStr, err := netutil.BuildUrl(
|
||||
"https",
|
||||
"example.com",
|
||||
"query",
|
||||
map[string][]string{
|
||||
"a": {"foo", "bar"},
|
||||
"b": {"baz"},
|
||||
},
|
||||
)
|
||||
|
||||
fmt.Println(urlStr)
|
||||
fmt.Println(err)
|
||||
|
||||
// Output:
|
||||
// https://example.com/query?a=foo&a=bar&b=baz
|
||||
// <nil>
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="AddQueryParams">AddQueryParams</span>
|
||||
|
||||
<p>向url添加查询参数。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func AddQueryParams(urlStr string, params map[string][]string) (string, error)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/netutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
urlStr, err := netutil.BuildUrl(
|
||||
"https",
|
||||
"example.com",
|
||||
"query",
|
||||
map[string][]string{
|
||||
"a": {"foo", "bar"},
|
||||
"b": {"baz"},
|
||||
},
|
||||
)
|
||||
|
||||
fmt.Println(urlStr)
|
||||
fmt.Println(err)
|
||||
|
||||
// Output:
|
||||
// https://example.com/query?a=foo&a=bar&b=baz
|
||||
// <nil>
|
||||
}
|
||||
```
|
||||
@@ -35,6 +35,7 @@ import (
|
||||
- [IsDir](#IsDir)
|
||||
- [ListFileNames](#ListFileNames)
|
||||
- [RemoveFile](#RemoveFile)
|
||||
- [RemoveDir](#RemoveDir)
|
||||
- [ReadFileToString](#ReadFileToString)
|
||||
- [ReadFileByLine](#ReadFileByLine)
|
||||
- [Zip](#Zip)
|
||||
@@ -395,7 +396,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func RemoveFile(path string) error
|
||||
func RemoveFile(path string, onDelete ...func(path string)) error
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/P2y0XW8a1SH)</span></b>
|
||||
@@ -416,6 +417,41 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="RemoveDir">RemoveDir</span>
|
||||
|
||||
<p>Delete directory.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func RemoveDir(path string, onDelete ...func(path string)) error
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/fileutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var deletedPaths []string
|
||||
|
||||
err := fileutil.RemoveDir("./tempdir", func(p string) {
|
||||
deletedPaths = append(deletedPaths, p)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
fmt.Println(deletedPaths)
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="ReadFileToString">ReadFileToString</span>
|
||||
|
||||
<p>Return string of file content.</p>
|
||||
|
||||
@@ -48,6 +48,9 @@ import (
|
||||
- [UploadFile](#UploadFile)
|
||||
- [IsPingConnected](#IsPingConnected)
|
||||
- [IsTelnetConnected](#IsTelnetConnected)
|
||||
- [BuildUrl](#BuildUrl)
|
||||
- [AddQueryParams](#AddQueryParams)
|
||||
|
||||
|
||||
<div STYLE="page-break-after: always;"></div>
|
||||
|
||||
@@ -1031,3 +1034,83 @@ func main() {
|
||||
// false
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="BuildUrl">BuildUrl</span>
|
||||
|
||||
<p>Builds a URL from the given params.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func BuildUrl(scheme, host, path string, query map[string][]string) (string, error)
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/netutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
urlStr, err := netutil.BuildUrl(
|
||||
"https",
|
||||
"example.com",
|
||||
"query",
|
||||
map[string][]string{
|
||||
"a": {"foo", "bar"},
|
||||
"b": {"baz"},
|
||||
},
|
||||
)
|
||||
|
||||
fmt.Println(urlStr)
|
||||
fmt.Println(err)
|
||||
|
||||
// Output:
|
||||
// https://example.com/query?a=foo&a=bar&b=baz
|
||||
// <nil>
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="AddQueryParams">AddQueryParams</span>
|
||||
|
||||
<p>Adds query parameters to the given URL.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func AddQueryParams(urlStr string, params map[string][]string) (string, error)
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/netutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
urlStr, err := netutil.BuildUrl(
|
||||
"https",
|
||||
"example.com",
|
||||
"query",
|
||||
map[string][]string{
|
||||
"a": {"foo", "bar"},
|
||||
"b": {"baz"},
|
||||
},
|
||||
)
|
||||
|
||||
fmt.Println(urlStr)
|
||||
fmt.Println(err)
|
||||
|
||||
// Output:
|
||||
// https://example.com/query?a=foo&a=bar&b=baz
|
||||
// <nil>
|
||||
}
|
||||
```
|
||||
@@ -172,10 +172,54 @@ func IsDir(path string) bool {
|
||||
|
||||
// RemoveFile remove the path file.
|
||||
// Play: https://go.dev/play/p/P2y0XW8a1SH
|
||||
func RemoveFile(path string) error {
|
||||
func RemoveFile(path string, onDelete ...func(path string)) error {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if info.IsDir() {
|
||||
return fmt.Errorf("%s is a directory", path)
|
||||
}
|
||||
|
||||
if len(onDelete) > 0 && onDelete[0] != nil {
|
||||
onDelete[0](path)
|
||||
}
|
||||
|
||||
return os.Remove(path)
|
||||
}
|
||||
|
||||
// RemoveDir remove the path directory.
|
||||
// Play: todo
|
||||
func RemoveDir(path string, onDelete ...func(path string)) error {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !info.IsDir() {
|
||||
return fmt.Errorf("%s is not a directory", path)
|
||||
}
|
||||
|
||||
var callback func(string)
|
||||
if len(onDelete) > 0 {
|
||||
callback = onDelete[0]
|
||||
}
|
||||
|
||||
err = filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
|
||||
if err == nil && callback != nil {
|
||||
callback(p)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.RemoveAll(path)
|
||||
}
|
||||
|
||||
// CopyFile copy src file to dest file.
|
||||
// Play: https://go.dev/play/p/Jg9AMJMLrJi
|
||||
func CopyFile(srcPath string, dstPath string) error {
|
||||
|
||||
@@ -85,14 +85,37 @@ func TestIsDir(t *testing.T) {
|
||||
|
||||
func TestRemoveFile(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestRemoveFile")
|
||||
|
||||
f := "./text.txt"
|
||||
if !IsExist(f) {
|
||||
CreateFile(f)
|
||||
err := RemoveFile(f)
|
||||
assert.IsNil(err)
|
||||
CreateFile(f)
|
||||
err := RemoveFile(f)
|
||||
assert.IsNil(err)
|
||||
}
|
||||
|
||||
func TestRemoveDir(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestRemoveDir")
|
||||
|
||||
err := os.MkdirAll("./tempdir/a/b", 0755)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
var deletedPaths []string
|
||||
|
||||
err = RemoveDir("./tempdir", func(p string) {
|
||||
deletedPaths = append(deletedPaths, p)
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
assert.Equal([]string{"./tempdir", "tempdir/a", "tempdir/a/b"}, deletedPaths)
|
||||
assert.Equal(false, IsExist("./tempdir"))
|
||||
}
|
||||
|
||||
func TestCopyFile(t *testing.T) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"regexp"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -305,3 +307,97 @@ func IsTelnetConnected(host string, port string) bool {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// BuildUrl builds a URL from the given params.
|
||||
// Play: todo
|
||||
func BuildUrl(scheme, host, path string, query map[string][]string) (string, error) {
|
||||
if err := validateScheme(scheme); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if path != "" {
|
||||
if !hostRegex.MatchString(host) {
|
||||
return "", fmt.Errorf("invalid host: '%s' is not a valid host", host)
|
||||
}
|
||||
}
|
||||
|
||||
parsedUrl := &url.URL{
|
||||
Scheme: scheme,
|
||||
Host: host,
|
||||
}
|
||||
|
||||
if path == "" {
|
||||
parsedUrl.Path = "/"
|
||||
} else if !strings.HasPrefix(path, "/") {
|
||||
parsedUrl.Path = "/" + path
|
||||
} else {
|
||||
parsedUrl.Path = path
|
||||
}
|
||||
|
||||
queryParams := parsedUrl.Query()
|
||||
|
||||
for key, values := range query {
|
||||
for _, value := range values {
|
||||
queryParams.Add(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
parsedUrl.RawQuery = queryParams.Encode()
|
||||
|
||||
return parsedUrl.String(), nil
|
||||
}
|
||||
|
||||
var supportedSchemes = map[string]bool{
|
||||
"http": true,
|
||||
"https": true,
|
||||
"ftp": true,
|
||||
"file": true,
|
||||
"mailto": true,
|
||||
"ws": true, // WebSocket
|
||||
"wss": true, // WebSocket Secure
|
||||
"data": true, // Data URL
|
||||
}
|
||||
|
||||
func validateScheme(scheme string) error {
|
||||
if _, exists := supportedSchemes[scheme]; !exists {
|
||||
return fmt.Errorf("invalid scheme: '%s' is not supported", scheme)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var hostRegex = regexp.MustCompile(`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])(\.[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])*$`)
|
||||
var pathRegex = regexp.MustCompile(`^\/([a-zA-Z0-9%_-]+(?:\/[a-zA-Z0-9%_-]+)*)$`)
|
||||
|
||||
var alphaNumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]+$`)
|
||||
|
||||
// AddQueryParams adds query parameters to the given URL.
|
||||
// Play: todoå
|
||||
func AddQueryParams(urlStr string, params map[string][]string) (string, error) {
|
||||
parsedUrl, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
queryParams := parsedUrl.Query()
|
||||
|
||||
for k, values := range params {
|
||||
if k == "" {
|
||||
return "", errors.New("empty key is not allowed")
|
||||
}
|
||||
|
||||
if !alphaNumericRegex.MatchString(k) {
|
||||
return "", fmt.Errorf("query parameter key %s must be alphanumeric", k)
|
||||
}
|
||||
|
||||
for _, v := range values {
|
||||
if !alphaNumericRegex.MatchString(v) {
|
||||
return "", fmt.Errorf("query parameter value %s must be alphanumeric", v)
|
||||
}
|
||||
queryParams.Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
parsedUrl.RawQuery = queryParams.Encode()
|
||||
|
||||
return parsedUrl.String(), nil
|
||||
}
|
||||
|
||||
@@ -201,3 +201,40 @@ func ExampleIsTelnetConnected() {
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
func ExampleBuildUrl() {
|
||||
urlStr, err := BuildUrl(
|
||||
"https",
|
||||
"example.com",
|
||||
"query",
|
||||
map[string][]string{
|
||||
"a": {"foo", "bar"},
|
||||
"b": {"baz"},
|
||||
},
|
||||
)
|
||||
|
||||
fmt.Println(urlStr)
|
||||
fmt.Println(err)
|
||||
|
||||
// Output:
|
||||
// https://example.com/query?a=foo&a=bar&b=baz
|
||||
// <nil>
|
||||
}
|
||||
|
||||
func ExampleAddQueryParams() {
|
||||
urlStr := "https://example.com"
|
||||
|
||||
params := map[string][]string{
|
||||
"a": {"foo", "bar"},
|
||||
"b": {"baz"},
|
||||
}
|
||||
|
||||
urlStr, err := AddQueryParams(urlStr, params)
|
||||
|
||||
fmt.Println(urlStr)
|
||||
fmt.Println(err)
|
||||
|
||||
// Output:
|
||||
// https://example.com?a=foo&a=bar&b=baz
|
||||
// <nil>
|
||||
}
|
||||
|
||||
@@ -142,3 +142,110 @@ func TestTelnetConnected(t *testing.T) {
|
||||
result2 := IsTelnetConnected("www.baidu.com", "123")
|
||||
assert.Equal(false, result2)
|
||||
}
|
||||
|
||||
func TestBuildUrl(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestBuildUrl")
|
||||
|
||||
tests := []struct {
|
||||
scheme string
|
||||
host string
|
||||
path string
|
||||
query map[string][]string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
scheme: "http",
|
||||
host: "www.test.com",
|
||||
path: "/path/subpath",
|
||||
query: map[string][]string{"a": {"1"}, "b": {"2"}},
|
||||
want: "http://www.test.com/path/subpath?a=1&b=2",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
scheme: "http",
|
||||
host: "www.test.com",
|
||||
path: "/simple-path",
|
||||
query: map[string][]string{"a": {"1"}, "b": {"2"}},
|
||||
want: "http://www.test.com/simple-path?a=1&b=2",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
scheme: "http",
|
||||
host: "www.test.com",
|
||||
path: "",
|
||||
query: map[string][]string{"a": {"foo", "bar"}, "b": {"baz"}},
|
||||
want: "http://www.test.com/?a=foo&a=bar&b=baz",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
scheme: "https",
|
||||
host: "www.test. com",
|
||||
path: "/path",
|
||||
query: nil,
|
||||
want: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
scheme: "https",
|
||||
host: "www.test.com",
|
||||
path: "/path with spaces",
|
||||
query: nil,
|
||||
want: "https://www.test.com/path%20with%20spaces",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got, err := BuildUrl(tt.scheme, tt.host, tt.path, tt.query)
|
||||
assert.Equal(tt.want, got)
|
||||
assert.Equal(tt.wantErr, err != nil)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestAddQueryParams(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestAddQueryParams")
|
||||
|
||||
tests := []struct {
|
||||
url string
|
||||
query map[string][]string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
url: "http://www.test.com",
|
||||
query: map[string][]string{"a": {"1"}, "b": {"2"}},
|
||||
want: "http://www.test.com?a=1&b=2",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
url: "http://www.test.com",
|
||||
query: map[string][]string{"a": {"foo", "bar"}, "b": {"baz"}},
|
||||
want: "http://www.test.com?a=foo&a=bar&b=baz",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
url: "http://www.test.com",
|
||||
query: map[string][]string{},
|
||||
want: "http://www.test.com",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
url: "http://www.test.com",
|
||||
query: map[string][]string{"a": {"$%"}},
|
||||
want: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
got, err := AddQueryParams(tt.url, tt.query)
|
||||
assert.Equal(tt.want, got)
|
||||
assert.Equal(tt.wantErr, err != nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -307,10 +307,21 @@ func TestBeforeLast(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestBeforeLast")
|
||||
|
||||
assert.Equal("lancet", BeforeLast("lancet", ""))
|
||||
assert.Equal("lancet", BeforeLast("lancet", "abcdef"))
|
||||
assert.Equal("github.com/test", BeforeLast("github.com/test/lancet", "/"))
|
||||
assert.Equal("github.com/test/", BeforeLast("github.com/test/test/lancet", "test"))
|
||||
tests := []struct {
|
||||
input string
|
||||
char string
|
||||
expected string
|
||||
}{
|
||||
{"lancet", "", "lancet"},
|
||||
{"lancet", "lancet", ""},
|
||||
{"lancet", "abcdef", "lancet"},
|
||||
{"github.com/test/lancet", "/", "github.com/test"},
|
||||
{"github.com/test/test/lancet", "test", "github.com/test/"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, BeforeLast(tt.input, tt.char))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAfter(t *testing.T) {
|
||||
@@ -318,11 +329,21 @@ func TestAfter(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestAfter")
|
||||
|
||||
assert.Equal("lancet", After("lancet", ""))
|
||||
assert.Equal("", After("lancet", "lancet"))
|
||||
assert.Equal("test/lancet", After("github.com/test/lancet", "/"))
|
||||
assert.Equal("/lancet", After("github.com/test/lancet", "test"))
|
||||
assert.Equal("lancet", After("lancet", "abcdef"))
|
||||
tests := []struct {
|
||||
input string
|
||||
char string
|
||||
expected string
|
||||
}{
|
||||
{"lancet", "", "lancet"},
|
||||
{"lancet", "lancet", ""},
|
||||
{"lancet", "abcdef", "lancet"},
|
||||
{"github.com/test/lancet", "/", "test/lancet"},
|
||||
{"github.com/test/lancet", "test", "/lancet"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, After(tt.input, tt.char))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAfterLast(t *testing.T) {
|
||||
@@ -330,11 +351,21 @@ func TestAfterLast(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestAfterLast")
|
||||
|
||||
assert.Equal("lancet", AfterLast("lancet", ""))
|
||||
assert.Equal("lancet", AfterLast("github.com/test/lancet", "/"))
|
||||
assert.Equal("/lancet", AfterLast("github.com/test/lancet", "test"))
|
||||
assert.Equal("/lancet", AfterLast("github.com/test/test/lancet", "test"))
|
||||
assert.Equal("lancet", AfterLast("lancet", "abcdef"))
|
||||
tests := []struct {
|
||||
input string
|
||||
char string
|
||||
expected string
|
||||
}{
|
||||
{"lancet", "", "lancet"},
|
||||
{"lancet", "lancet", ""},
|
||||
{"lancet", "abcdef", "lancet"},
|
||||
{"github.com/test/lancet", "/", "lancet"},
|
||||
{"github.com/test/test/lancet", "test", "/lancet"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, AfterLast(tt.input, tt.char))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsString(t *testing.T) {
|
||||
@@ -342,11 +373,20 @@ func TestIsString(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsString")
|
||||
|
||||
assert.Equal(true, IsString("lancet"))
|
||||
assert.Equal(true, IsString(""))
|
||||
assert.Equal(false, IsString(1))
|
||||
assert.Equal(false, IsString(true))
|
||||
assert.Equal(false, IsString([]string{}))
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expected bool
|
||||
}{
|
||||
{"lancet", true},
|
||||
{"", true},
|
||||
{1, false},
|
||||
{true, false},
|
||||
{[]string{}, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsString(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestReverse(t *testing.T) {
|
||||
@@ -363,11 +403,21 @@ func TestWrap(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestWrap")
|
||||
|
||||
assert.Equal("ab", Wrap("ab", ""))
|
||||
assert.Equal("", Wrap("", "*"))
|
||||
assert.Equal("*ab*", Wrap("ab", "*"))
|
||||
assert.Equal("\"ab\"", Wrap("ab", "\""))
|
||||
assert.Equal("'ab'", Wrap("ab", "'"))
|
||||
tests := []struct {
|
||||
input string
|
||||
wrapper string
|
||||
expected string
|
||||
}{
|
||||
{"", "", ""},
|
||||
{"ab", "", "ab"},
|
||||
{"ab", "*", "*ab*"},
|
||||
{"ab", "\"", "\"ab\""},
|
||||
{"ab", "'", "'ab'"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, Wrap(tt.input, tt.wrapper))
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnwrap(t *testing.T) {
|
||||
@@ -485,10 +535,21 @@ func TestIsBlank(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsBlank")
|
||||
|
||||
assert.Equal(IsBlank(""), true)
|
||||
assert.Equal(IsBlank("\t\v\f\n"), true)
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"", true},
|
||||
{" ", true},
|
||||
{"\t\v\f\n", true},
|
||||
{"\t\v\f\nabc", false},
|
||||
{"abc", false},
|
||||
{" 中文", false},
|
||||
}
|
||||
|
||||
assert.Equal(IsBlank(" 中文"), false)
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsBlank(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsNotBlank(t *testing.T) {
|
||||
@@ -496,12 +557,22 @@ func TestIsNotBlank(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsBlank")
|
||||
|
||||
assert.Equal(IsNotBlank(""), false)
|
||||
assert.Equal(IsNotBlank(" "), false)
|
||||
assert.Equal(IsNotBlank("\t\v\f\n"), false)
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"", false},
|
||||
{" ", false},
|
||||
{"\t\v\f\n", false},
|
||||
{"\t\v\f\nabc", true},
|
||||
{"abc", true},
|
||||
{" 中文", true},
|
||||
{" world ", true},
|
||||
}
|
||||
|
||||
assert.Equal(IsNotBlank(" 中文"), true)
|
||||
assert.Equal(IsNotBlank(" world "), true)
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsNotBlank(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasPrefixAny(t *testing.T) {
|
||||
@@ -509,12 +580,19 @@ func TestHasPrefixAny(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestHasPrefixAny")
|
||||
|
||||
str := "foo bar"
|
||||
prefixes := []string{"fo", "xyz", "hello"}
|
||||
notMatches := []string{"oom", "world"}
|
||||
tests := []struct {
|
||||
str string
|
||||
prefixes []string
|
||||
expected bool
|
||||
}{
|
||||
{"foo bar", []string{"fo", "xyz", "hello"}, true},
|
||||
{"foo bar", []string{"oom", "world"}, false},
|
||||
{"foo bar", []string{}, false},
|
||||
}
|
||||
|
||||
assert.Equal(HasPrefixAny(str, prefixes), true)
|
||||
assert.Equal(HasPrefixAny(str, notMatches), false)
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, HasPrefixAny(tt.str, tt.prefixes))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasSuffixAny(t *testing.T) {
|
||||
@@ -522,25 +600,44 @@ func TestHasSuffixAny(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestHasSuffixAny")
|
||||
|
||||
str := "foo bar"
|
||||
suffixes := []string{"bar", "xyz", "hello"}
|
||||
notMatches := []string{"oom", "world"}
|
||||
tests := []struct {
|
||||
str string
|
||||
suffixes []string
|
||||
expected bool
|
||||
}{
|
||||
{"foo bar", []string{"bar", "xyz", "hello"}, true},
|
||||
{"foo bar", []string{"oom", "world"}, false},
|
||||
{"foo bar", []string{}, false},
|
||||
}
|
||||
|
||||
assert.Equal(HasSuffixAny(str, suffixes), true)
|
||||
assert.Equal(HasSuffixAny(str, notMatches), false)
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, HasSuffixAny(tt.str, tt.suffixes))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndexOffset(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestIndexOffset")
|
||||
|
||||
str := "foo bar hello world"
|
||||
|
||||
assert.Equal(IndexOffset(str, "o", 5), 12)
|
||||
assert.Equal(IndexOffset(str, "o", 0), 1)
|
||||
assert.Equal(IndexOffset(str, "d", len(str)-1), len(str)-1)
|
||||
assert.Equal(IndexOffset(str, "d", len(str)), -1)
|
||||
assert.Equal(IndexOffset(str, "f", -1), -1)
|
||||
tests := []struct {
|
||||
str string
|
||||
substr string
|
||||
offset int
|
||||
expected int
|
||||
}{
|
||||
{str, "o", 5, 12},
|
||||
{str, "o", 0, 1},
|
||||
{str, "d", len(str) - 1, len(str) - 1},
|
||||
{str, "d", len(str), -1},
|
||||
{str, "f", -1, -1},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IndexOffset(tt.str, tt.substr, tt.offset))
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplaceWithMap(t *testing.T) {
|
||||
@@ -648,13 +745,23 @@ func TestSubInBetween(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestSubInBetween")
|
||||
|
||||
str := "abcde"
|
||||
tests := []struct {
|
||||
input string
|
||||
start string
|
||||
end string
|
||||
expected string
|
||||
}{
|
||||
{"abcde", "", "", ""},
|
||||
{"abcde", "a", "d", "bc"},
|
||||
{"abcde", "a", "e", "bcd"},
|
||||
{"abcde", "a", "f", ""},
|
||||
{"abcde", "a", "", ""},
|
||||
{"abcde", "", "e", "abcd"},
|
||||
}
|
||||
|
||||
assert.Equal("", SubInBetween(str, "", ""))
|
||||
assert.Equal("ab", SubInBetween(str, "", "c"))
|
||||
assert.Equal("bc", SubInBetween(str, "a", "d"))
|
||||
assert.Equal("", SubInBetween(str, "a", ""))
|
||||
assert.Equal("", SubInBetween(str, "a", "f"))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, SubInBetween(tt.input, tt.start, tt.end))
|
||||
}
|
||||
}
|
||||
|
||||
func TestHammingDistance(t *testing.T) {
|
||||
@@ -696,13 +803,22 @@ func TestConcat(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestConcat")
|
||||
|
||||
assert.Equal("", Concat(0))
|
||||
assert.Equal("a", Concat(1, "a"))
|
||||
assert.Equal("ab", Concat(2, "a", "b"))
|
||||
assert.Equal("abc", Concat(3, "a", "b", "c"))
|
||||
assert.Equal("abc", Concat(3, "a", "", "b", "c", ""))
|
||||
assert.Equal("你好,世界!", Concat(0, "你好", ",", "", "世界!", ""))
|
||||
assert.Equal("Hello World!", Concat(0, "Hello", " Wo", "r", "ld!", ""))
|
||||
tests := []struct {
|
||||
args []string
|
||||
expected string
|
||||
}{
|
||||
{[]string{}, ""},
|
||||
{[]string{"a"}, "a"},
|
||||
{[]string{"a", "b"}, "ab"},
|
||||
{[]string{"a", "b", "c"}, "abc"},
|
||||
{[]string{"a", "", "b", "c", ""}, "abc"},
|
||||
{[]string{"你好", ",", "", "世界!", ""}, "你好,世界!"},
|
||||
{[]string{"Hello", " Wo", "r", "ld!", ""}, "Hello World!"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, Concat(0, tt.args...))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEllipsis(t *testing.T) {
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
var (
|
||||
alphaMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z]+$`)
|
||||
letterRegexMatcher *regexp.Regexp = regexp.MustCompile(`[a-zA-Z]`)
|
||||
alphaNumericMatcher *regexp.Regexp = regexp.MustCompile(`^[a-zA-Z0-9-]+$`)
|
||||
numberRegexMatcher *regexp.Regexp = regexp.MustCompile(`\d`)
|
||||
intStrMatcher *regexp.Regexp = regexp.MustCompile(`^[\+-]?\d+$`)
|
||||
urlMatcher *regexp.Regexp = regexp.MustCompile(`^((ftp|http|https?):\/\/)?(\S+(:\S*)?@)?((([1-9]\d?|1\d\d|2[01]\d|22[0-3])(\.(1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.([0-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(([a-zA-Z0-9]+([-\.][a-zA-Z0-9]+)*)|((www\.)?))?(([a-z\x{00a1}-\x{ffff}0-9]+-?-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.([a-z\x{00a1}-\x{ffff}]{2,}))?))(:(\d{1,5}))?((\/|\?|#)[^\s]*)?$`)
|
||||
@@ -181,6 +182,12 @@ func IsJSON(str string) bool {
|
||||
return json.Unmarshal([]byte(str), &js) == nil
|
||||
}
|
||||
|
||||
// IsAlphaNumericStr check if the string is alphanumeric.
|
||||
// Play: todo
|
||||
func IsAlphaNumeric(s string) bool {
|
||||
return alphaNumericMatcher.MatchString(s)
|
||||
}
|
||||
|
||||
// IsNumberStr check if the string can convert to a number.
|
||||
// Play: https://go.dev/play/p/LzaKocSV79u
|
||||
func IsNumberStr(s string) bool {
|
||||
|
||||
@@ -15,16 +15,25 @@ func TestIsAllUpper(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsAllUpper")
|
||||
|
||||
assert.Equal(true, IsAllUpper("ABC"))
|
||||
assert.Equal(false, IsAllUpper(""))
|
||||
assert.Equal(false, IsAllUpper("abc"))
|
||||
assert.Equal(false, IsAllUpper("aBC"))
|
||||
assert.Equal(false, IsAllUpper("1BC"))
|
||||
assert.Equal(false, IsAllUpper("1bc"))
|
||||
assert.Equal(false, IsAllUpper("123"))
|
||||
assert.Equal(false, IsAllUpper("你好"))
|
||||
assert.Equal(false, IsAllUpper("A&"))
|
||||
assert.Equal(false, IsAllUpper("&@#$%^&*"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"ABC", true},
|
||||
{"", false},
|
||||
{"abc", false},
|
||||
{"aBC", false},
|
||||
{"1BC", false},
|
||||
{"1bc", false},
|
||||
{"123", false},
|
||||
{"你好", false},
|
||||
{"A&", false},
|
||||
{"&@#$%^&*", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsAllUpper(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAllLower(t *testing.T) {
|
||||
@@ -32,16 +41,25 @@ func TestIsAllLower(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsAllLower")
|
||||
|
||||
assert.Equal(true, IsAllLower("abc"))
|
||||
assert.Equal(false, IsAllLower("ABC"))
|
||||
assert.Equal(false, IsAllLower(""))
|
||||
assert.Equal(false, IsAllLower("aBC"))
|
||||
assert.Equal(false, IsAllLower("1BC"))
|
||||
assert.Equal(false, IsAllLower("1bc"))
|
||||
assert.Equal(false, IsAllLower("123"))
|
||||
assert.Equal(false, IsAllLower("你好"))
|
||||
assert.Equal(false, IsAllLower("A&"))
|
||||
assert.Equal(false, IsAllLower("&@#$%^&*"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"abc", true},
|
||||
{"", false},
|
||||
{"ABC", false},
|
||||
{"aBC", false},
|
||||
{"1BC", false},
|
||||
{"1bc", false},
|
||||
{"123", false},
|
||||
{"你好", false},
|
||||
{"A&", false},
|
||||
{"&@#$%^&*", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsAllLower(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainLower(t *testing.T) {
|
||||
@@ -49,17 +67,25 @@ func TestContainLower(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestContainLower")
|
||||
|
||||
assert.Equal(true, ContainLower("abc"))
|
||||
assert.Equal(true, ContainLower("aBC"))
|
||||
assert.Equal(true, ContainLower("1bc"))
|
||||
assert.Equal(true, ContainLower("a&"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"abc", true},
|
||||
{"aBC", true},
|
||||
{"1bc", true},
|
||||
{"a&", true},
|
||||
{"ABC", false},
|
||||
{"", false},
|
||||
{"1BC", false},
|
||||
{"123", false},
|
||||
{"你好", false},
|
||||
{"&@#$%^&*", false},
|
||||
}
|
||||
|
||||
assert.Equal(false, ContainLower("ABC"))
|
||||
assert.Equal(false, ContainLower(""))
|
||||
assert.Equal(false, ContainLower("1BC"))
|
||||
assert.Equal(false, ContainLower("123"))
|
||||
assert.Equal(false, ContainLower("你好"))
|
||||
assert.Equal(false, ContainLower("&@#$%^&*"))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, ContainLower(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainUpper(t *testing.T) {
|
||||
@@ -67,17 +93,25 @@ func TestContainUpper(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestContainUpper")
|
||||
|
||||
assert.Equal(true, ContainUpper("ABC"))
|
||||
assert.Equal(true, ContainUpper("aBC"))
|
||||
assert.Equal(true, ContainUpper("1BC"))
|
||||
assert.Equal(true, ContainUpper("A&"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"ABC", true},
|
||||
{"aBC", true},
|
||||
{"1BC", true},
|
||||
{"A&", true},
|
||||
{"abc", false},
|
||||
{"", false},
|
||||
{"1bc", false},
|
||||
{"123", false},
|
||||
{"你好", false},
|
||||
{"&@#$%^&*", false},
|
||||
}
|
||||
|
||||
assert.Equal(false, ContainUpper("abc"))
|
||||
assert.Equal(false, ContainUpper(""))
|
||||
assert.Equal(false, ContainUpper("1bc"))
|
||||
assert.Equal(false, ContainUpper("123"))
|
||||
assert.Equal(false, ContainUpper("你好"))
|
||||
assert.Equal(false, ContainUpper("&@#$%^&*"))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, ContainUpper(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainLetter(t *testing.T) {
|
||||
@@ -85,15 +119,23 @@ func TestContainLetter(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestContainLetter")
|
||||
|
||||
assert.Equal(true, ContainLetter("ABC"))
|
||||
assert.Equal(true, ContainLetter("1Bc"))
|
||||
assert.Equal(true, ContainLetter("1ab"))
|
||||
assert.Equal(true, ContainLetter("A&"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"ABC", true},
|
||||
{"1Bc", true},
|
||||
{"1ab", true},
|
||||
{"A&", true},
|
||||
{"", false},
|
||||
{"123", false},
|
||||
{"你好", false},
|
||||
{"&@#$%^&*", false},
|
||||
}
|
||||
|
||||
assert.Equal(false, ContainLetter(""))
|
||||
assert.Equal(false, ContainLetter("123"))
|
||||
assert.Equal(false, ContainLetter("你好"))
|
||||
assert.Equal(false, ContainLetter("&@#$%^&*"))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, ContainLetter(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestContainNumber(t *testing.T) {
|
||||
@@ -101,18 +143,26 @@ func TestContainNumber(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestContainNumber")
|
||||
|
||||
assert.Equal(true, ContainNumber("123"))
|
||||
assert.Equal(true, ContainNumber("1Bc"))
|
||||
assert.Equal(true, ContainNumber("a2c"))
|
||||
assert.Equal(true, ContainNumber("ab3"))
|
||||
assert.Equal(true, ContainNumber("a23"))
|
||||
assert.Equal(true, ContainNumber("a23c"))
|
||||
assert.Equal(true, ContainNumber("1%%%"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"123", true},
|
||||
{"1Bc", true},
|
||||
{"a2c", true},
|
||||
{"ab3", true},
|
||||
{"a23", true},
|
||||
{"a23c", true},
|
||||
{"1%%%", true},
|
||||
{"ABC", false},
|
||||
{"", false},
|
||||
{"你好", false},
|
||||
{"&@#$%^&*", false},
|
||||
}
|
||||
|
||||
assert.Equal(false, ContainNumber("ABC"))
|
||||
assert.Equal(false, ContainNumber(""))
|
||||
assert.Equal(false, ContainNumber("你好"))
|
||||
assert.Equal(false, ContainNumber("&@#$%^&*"))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, ContainNumber(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsJSON(t *testing.T) {
|
||||
@@ -120,15 +170,23 @@ func TestIsJSON(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsJSON")
|
||||
|
||||
assert.Equal(true, IsJSON("{}"))
|
||||
assert.Equal(true, IsJSON("{\"name\": \"test\"}"))
|
||||
assert.Equal(true, IsJSON("[]"))
|
||||
assert.Equal(true, IsJSON("123"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"{}", true},
|
||||
{"{\"name\": \"test\"}", true},
|
||||
{"[]", true},
|
||||
{"123", true},
|
||||
{"", false},
|
||||
{"abc", false},
|
||||
{"你好", false},
|
||||
{"&@#$%^&*", false},
|
||||
}
|
||||
|
||||
assert.Equal(false, IsJSON(""))
|
||||
assert.Equal(false, IsJSON("abc"))
|
||||
assert.Equal(false, IsJSON("你好"))
|
||||
assert.Equal(false, IsJSON("&@#$%^&*"))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsJSON(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsNumber(t *testing.T) {
|
||||
@@ -136,10 +194,19 @@ func TestIsNumber(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsNumber")
|
||||
|
||||
assert.Equal(false, IsNumber(""))
|
||||
assert.Equal(false, IsNumber("3"))
|
||||
assert.Equal(true, IsNumber(0))
|
||||
assert.Equal(true, IsNumber(0.1))
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expected bool
|
||||
}{
|
||||
{"", false},
|
||||
{"3", false},
|
||||
{0, true},
|
||||
{0.1, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsNumber(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsFloat(t *testing.T) {
|
||||
@@ -147,10 +214,19 @@ func TestIsFloat(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsFloat")
|
||||
|
||||
assert.Equal(false, IsFloat(""))
|
||||
assert.Equal(false, IsFloat("3"))
|
||||
assert.Equal(false, IsFloat(0))
|
||||
assert.Equal(true, IsFloat(0.1))
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expected bool
|
||||
}{
|
||||
{"", false},
|
||||
{"3", false},
|
||||
{0, false},
|
||||
{0.1, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsFloat(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsInt(t *testing.T) {
|
||||
@@ -158,11 +234,20 @@ func TestIsInt(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsInt")
|
||||
|
||||
assert.Equal(false, IsInt(""))
|
||||
assert.Equal(false, IsInt("3"))
|
||||
assert.Equal(false, IsInt(0.1))
|
||||
assert.Equal(true, IsInt(0))
|
||||
assert.Equal(true, IsInt(-1))
|
||||
tests := []struct {
|
||||
input interface{}
|
||||
expected bool
|
||||
}{
|
||||
{"", false},
|
||||
{"3", false},
|
||||
{0.1, false},
|
||||
{0, true},
|
||||
{-1, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsInt(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsNumberStr(t *testing.T) {
|
||||
@@ -170,11 +255,21 @@ func TestIsNumberStr(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsNumberStr")
|
||||
|
||||
assert.Equal(true, IsNumberStr("3."))
|
||||
assert.Equal(true, IsNumberStr("+3."))
|
||||
assert.Equal(true, IsNumberStr("-3."))
|
||||
assert.Equal(true, IsNumberStr("+3e2"))
|
||||
assert.Equal(false, IsNumberStr("abc"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"3", true},
|
||||
{"3.", true},
|
||||
{"+3.", true},
|
||||
{"-3.", true},
|
||||
{"+3e2", true},
|
||||
{"abc", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsNumberStr(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsFloatStr(t *testing.T) {
|
||||
@@ -182,11 +277,21 @@ func TestIsFloatStr(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsFloatStr")
|
||||
|
||||
assert.Equal(true, IsFloatStr("3."))
|
||||
assert.Equal(true, IsFloatStr("+3."))
|
||||
assert.Equal(true, IsFloatStr("-3."))
|
||||
assert.Equal(true, IsFloatStr("12"))
|
||||
assert.Equal(false, IsFloatStr("abc"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"3", true},
|
||||
{"3.", true},
|
||||
{"+3.", true},
|
||||
{"-3.", true},
|
||||
{"12", true},
|
||||
{"abc", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsFloatStr(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIntStr(t *testing.T) {
|
||||
@@ -194,10 +299,20 @@ func TestIsIntStr(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsIntStr")
|
||||
|
||||
assert.Equal(true, IsIntStr("+3"))
|
||||
assert.Equal(true, IsIntStr("-3"))
|
||||
assert.Equal(false, IsIntStr("3."))
|
||||
assert.Equal(false, IsIntStr("abc"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"3", true},
|
||||
{"3.", false},
|
||||
{"+3", true},
|
||||
{"-3", true},
|
||||
{"abc", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsIntStr(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsPort(t *testing.T) {
|
||||
@@ -205,13 +320,22 @@ func TestIsPort(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsPort")
|
||||
|
||||
assert.Equal(true, IsPort("1"))
|
||||
assert.Equal(true, IsPort("65535"))
|
||||
assert.Equal(false, IsPort("abc"))
|
||||
assert.Equal(false, IsPort("123abc"))
|
||||
assert.Equal(false, IsPort(""))
|
||||
assert.Equal(false, IsPort("-1"))
|
||||
assert.Equal(false, IsPort("65536"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"1", true},
|
||||
{"65535", true},
|
||||
{"abc", false},
|
||||
{"123abc", false},
|
||||
{"", false},
|
||||
{"-1", false},
|
||||
{"65536", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsPort(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIp(t *testing.T) {
|
||||
@@ -219,10 +343,19 @@ func TestIsIp(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsIntStr")
|
||||
|
||||
assert.Equal(true, IsIp("127.0.0.1"))
|
||||
assert.Equal(true, IsIp("::0:0:0:0:0:0:1"))
|
||||
assert.Equal(false, IsIp("127.0.0"))
|
||||
assert.Equal(false, IsIp("127"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"127.0.0.1", true},
|
||||
{"::0:0:0:0:0:0:1", true},
|
||||
{"127.0.0", false},
|
||||
{"127", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsIp(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIpPort(t *testing.T) {
|
||||
@@ -230,16 +363,25 @@ func TestIsIpPort(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsIpPort")
|
||||
|
||||
assert.Equal(true, IsIpPort("[0:0:0:0:0:0:0:1]:8080")) // valid IPv6 and port
|
||||
assert.Equal(true, IsIpPort("127.0.0.1:8080")) // valid IP and port
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"[::0:0:0:0:0:0:1]:8080", true},
|
||||
{"127.0.0.1:8080", true},
|
||||
|
||||
assert.Equal(false, IsIpPort("")) // empty string
|
||||
assert.Equal(false, IsIpPort(":8080")) // only port
|
||||
assert.Equal(false, IsIpPort("127.0.0.1")) // valid IP without port
|
||||
assert.Equal(false, IsIpPort("0:0:0:0:0:0:0:1")) // valid IPv6 without port
|
||||
assert.Equal(false, IsIpPort("256.256.256.256:8080")) // invalid IP with valid port
|
||||
assert.Equal(false, IsIpPort("256.256.256.256:abc")) // invalid IP and invalid port
|
||||
assert.Equal(false, IsIpPort("127.0.0.1:70000")) // valid IP with invalid port
|
||||
{"", false},
|
||||
{":8080", false},
|
||||
{"127.0.0.1", false},
|
||||
{"0:0:0:0:0:0:0:1", false},
|
||||
{"256.256.256.256:8080", false},
|
||||
{"256.256.256.256:abc", false},
|
||||
{"127.0.0.1:70000", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsIpPort(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIpV4(t *testing.T) {
|
||||
@@ -247,13 +389,23 @@ func TestIsIpV4(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsIpV4")
|
||||
|
||||
assert.Equal(true, IsIpV4("127.0.0.1"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"127.0.0.1", true},
|
||||
{"::0:0:0:0:0:0:1", false},
|
||||
|
||||
assert.Equal(false, IsIpV4("::0:0:0:0:0:0:1"))
|
||||
assert.Equal(false, IsIpV4("127.0.0.1.1"))
|
||||
assert.Equal(false, IsIpV4("256.0.0.1"))
|
||||
assert.Equal(false, IsIpV4("127.0.0.a"))
|
||||
assert.Equal(false, IsIpV4(""))
|
||||
{"::0:0:0:0:0:0:1", false},
|
||||
{"127.0.0.1.1", false},
|
||||
{"256.0.0.1", false},
|
||||
{"127.0.0.a", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsIpV4(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsIpV6(t *testing.T) {
|
||||
@@ -261,14 +413,23 @@ func TestIsIpV6(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsIpV6")
|
||||
|
||||
assert.Equal(true, IsIpV6("::0:0:0:0:0:0:1"))
|
||||
assert.Equal(true, IsIpV6("::1"))
|
||||
assert.Equal(true, IsIpV6("::"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"::0:0:0:0:0:0:1", true},
|
||||
{"::1", true},
|
||||
{"::", true},
|
||||
{"127.0.0.1", false},
|
||||
{"2001:db8::8a2e:37023:7334", false},
|
||||
{"2001::25de::cade", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsIpV6(tt.input))
|
||||
}
|
||||
|
||||
assert.Equal(false, IsIpV6("127.0.0.1"))
|
||||
assert.Equal(false, IsIpV6("2001:db8::8a2e:37023:7334"))
|
||||
assert.Equal(false, IsIpV6("2001::25de::cade"))
|
||||
assert.Equal(false, IsIpV6(""))
|
||||
}
|
||||
|
||||
func TestIsUrl(t *testing.T) {
|
||||
@@ -287,13 +448,21 @@ func TestIsDns(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsDns")
|
||||
|
||||
assert.Equal(true, IsDns("abc.com"))
|
||||
assert.Equal(true, IsDns("123.cn"))
|
||||
assert.Equal(true, IsDns("a.b.com"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"abc.com", true},
|
||||
{"123.cn", true},
|
||||
{"a.b.com", true},
|
||||
{"a.b.c", false},
|
||||
{"a@b.com", false},
|
||||
{"http://abc.com", false},
|
||||
}
|
||||
|
||||
assert.Equal(false, IsDns("a.b.c"))
|
||||
assert.Equal(false, IsDns("a@b.com"))
|
||||
assert.Equal(false, IsDns("http://abc.com"))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsDns(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsEmail(t *testing.T) {
|
||||
@@ -330,11 +499,19 @@ func TestIsChinesePhone(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsChinesePhone")
|
||||
|
||||
assert.Equal(true, IsChinesePhone("010-32116675"))
|
||||
assert.Equal(true, IsChinesePhone("0464-8756213"))
|
||||
assert.Equal(true, IsChinesePhone("0731-82251545")) // 长沙晚报电话
|
||||
assert.Equal(false, IsChinesePhone("123-87562"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"010-32116675", true},
|
||||
{"0464-8756213", true},
|
||||
{"0731-82251545", true},
|
||||
{"123-87562", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsChinesePhone(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsChineseIdNum(t *testing.T) {
|
||||
@@ -342,13 +519,23 @@ func TestIsChineseIdNum(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsChineseIdNum")
|
||||
|
||||
assert.Equal(true, IsChineseIdNum("210911192105130714"))
|
||||
assert.Equal(true, IsChineseIdNum("11010519491231002X"))
|
||||
assert.Equal(true, IsChineseIdNum("11010519491231002x"))
|
||||
assert.Equal(false, IsChineseIdNum("123456"))
|
||||
assert.Equal(false, IsChineseIdNum("990911192105130714"))
|
||||
assert.Equal(false, IsChineseIdNum("990911189905130714"))
|
||||
assert.Equal(false, IsChineseIdNum("210911222205130714"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"210911192105130714", true},
|
||||
{"11010519491231002X", true},
|
||||
{"11010519491231002x", true},
|
||||
{"123456", false},
|
||||
{"990911192105130714", false},
|
||||
{"990911189905130714", false},
|
||||
{"210911222205130714", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsChineseIdNum(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsCreditCard(t *testing.T) {
|
||||
@@ -407,13 +594,23 @@ func TestIsStrongPassword(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsStrongPassword")
|
||||
|
||||
assert.Equal(false, IsStrongPassword("abc", 3))
|
||||
assert.Equal(false, IsStrongPassword("abc123", 6))
|
||||
assert.Equal(false, IsStrongPassword("abcABC", 6))
|
||||
assert.Equal(false, IsStrongPassword("abc123@#$", 9))
|
||||
assert.Equal(false, IsStrongPassword("abcABC123@#$", 16))
|
||||
assert.Equal(true, IsStrongPassword("abcABC123@#$", 12))
|
||||
assert.Equal(true, IsStrongPassword("abcABC123@#$", 10))
|
||||
tests := []struct {
|
||||
input string
|
||||
length int
|
||||
expected bool
|
||||
}{
|
||||
{"abc", 3, false},
|
||||
{"abc123", 6, false},
|
||||
{"abcABC", 6, false},
|
||||
{"abc123@#$", 9, false},
|
||||
{"abcABC123@#$", 16, false},
|
||||
{"abcABC123@#$", 12, true},
|
||||
{"abcABC123@#$", 10, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsStrongPassword(tt.input, tt.length))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsWeakPassword(t *testing.T) {
|
||||
@@ -421,11 +618,20 @@ func TestIsWeakPassword(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsWeakPassword")
|
||||
|
||||
assert.Equal(true, IsWeakPassword("abc"))
|
||||
assert.Equal(true, IsWeakPassword("123"))
|
||||
assert.Equal(true, IsWeakPassword("abc123"))
|
||||
assert.Equal(true, IsWeakPassword("abcABC123"))
|
||||
assert.Equal(false, IsWeakPassword("abc123@#$"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"abc", true},
|
||||
{"123", true},
|
||||
{"abc123", true},
|
||||
{"abcABC123", true},
|
||||
{"abc123@#$", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsWeakPassword(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsZeroValue(t *testing.T) {
|
||||
@@ -566,50 +772,83 @@ func TestIsPrintable(t *testing.T) {
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsPrintable")
|
||||
|
||||
assert.Equal(true, IsPrintable("ABC"))
|
||||
assert.Equal(true, IsPrintable("{id: 123}"))
|
||||
assert.Equal(true, IsPrintable(""))
|
||||
assert.Equal(true, IsPrintable("😄"))
|
||||
assert.Equal(false, IsPrintable("\u0000"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"ABC", true},
|
||||
{"123", true},
|
||||
{"你好", true},
|
||||
{"", true},
|
||||
{"😄", true},
|
||||
{"\u0000", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsPrintable(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsBin(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsBin")
|
||||
|
||||
assert.Equal(true, IsBin("0101"))
|
||||
assert.Equal(true, IsBin("0b1101"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"0101", true},
|
||||
{"0b1101", true},
|
||||
{"b1101", false},
|
||||
{"1201", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
assert.Equal(false, IsBin("b1101"))
|
||||
assert.Equal(false, IsBin("1201"))
|
||||
assert.Equal(false, IsBin(""))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsBin(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsHex(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsHex")
|
||||
|
||||
assert.Equal(true, IsHex("ABCDE"))
|
||||
assert.Equal(true, IsHex("abcde"))
|
||||
assert.Equal(true, IsHex("0xabcde"))
|
||||
assert.Equal(true, IsHex("0Xabcde"))
|
||||
assert.Equal(true, IsHex("#abcde"))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"ABCDE", true},
|
||||
{"abcde", true},
|
||||
{"0xabcde", true},
|
||||
{"0Xabcde", true},
|
||||
{"#abcde", true},
|
||||
{"cdfeg", false},
|
||||
{"0xcdfeg", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
assert.Equal(false, IsHex("cdfeg"))
|
||||
assert.Equal(false, IsHex("0xcdfeg"))
|
||||
assert.Equal(false, IsHex(""))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsHex(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsBase64URL(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert := internal.NewAssert(t, "TestIsBase64URL")
|
||||
|
||||
assert.Equal(true, IsBase64URL("SAGsbG8sIHdvcmxkIQ"))
|
||||
assert.Equal(true, IsBase64URL("SAGsbG8sIHdvcmxkIQ=="))
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"SAGsbG8sIHdvcmxkIQ", true},
|
||||
{"SAGsbG8sIHdvcmxkIQ==", true},
|
||||
{"SAGsbG8sIHdvcmxkIQ=", false},
|
||||
{"SAGsbG8sIHdvcmxkIQ===", false},
|
||||
}
|
||||
|
||||
assert.Equal(false, IsBase64URL("SAGsbG8sIHdvcmxkIQ="))
|
||||
assert.Equal(false, IsBase64URL("SAGsbG8sIHdvcmxkIQ==="))
|
||||
// assert.Equal(false, IsBase64URL(""))
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsBase64URL(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsJWT(t *testing.T) {
|
||||
@@ -659,3 +898,29 @@ func TestIsChinaUnionPay(t *testing.T) {
|
||||
assert.Equal(true, IsChinaUnionPay("6250941006528599"))
|
||||
assert.Equal(false, IsChinaUnionPay("3782822463100007"))
|
||||
}
|
||||
|
||||
func TestIsAlphaNumeric(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert := internal.NewAssert(t, "TestIsAlphaNumeric")
|
||||
|
||||
tests := []struct {
|
||||
input string
|
||||
expected bool
|
||||
}{
|
||||
{"ABC", true},
|
||||
{"abc", true},
|
||||
{"aBC", true},
|
||||
{"1BC", true},
|
||||
{"1bc", true},
|
||||
{"123", true},
|
||||
{"你好", false},
|
||||
{"A&", false},
|
||||
{"&@#$%^&*", false},
|
||||
{"", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
assert.Equal(tt.expected, IsAlphaNumeric(tt.input))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user