This commit is contained in:
John 2019-04-22 15:48:16 +08:00
commit 9d1063c6b2
4 changed files with 409 additions and 0 deletions

View File

@ -0,0 +1,32 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package ghtml_test
import (
"github.com/gogf/gf/g/encoding/ghtml"
"github.com/gogf/gf/g/test/gtest"
"testing"
)
func TestStripTags(t *testing.T) {
src := `<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>`
dst := `Test paragraph. Other text`
gtest.Assert(ghtml.StripTags(src), dst)
}
func TestEntities(t *testing.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
gtest.Assert(ghtml.Entities(src), dst)
gtest.Assert(ghtml.EntitiesDecode(dst), src)
}
func TestSpecialChars(t *testing.T) {
src := `A 'quote' "is" <b>bold</b>`
dst := `A &#39;quote&#39; &#34;is&#34; &lt;b&gt;bold&lt;/b&gt;`
gtest.Assert(ghtml.SpecialChars(src), dst)
gtest.Assert(ghtml.SpecialCharsDecode(dst), src)
}

View File

@ -0,0 +1,142 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gtoml_test
import (
"github.com/gogf/gf/g/encoding/gparser"
"github.com/gogf/gf/g/encoding/gtoml"
"github.com/gogf/gf/g/test/gtest"
"testing"
)
var tomlStr string = `
# 模板引擎目录
viewpath = "/home/www/templates/"
# MySQL数据库配置
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
var tomlErr string = `
# 模板引擎目录
viewpath = "/home/www/templates/"
# MySQL数据库配置
[redis]
dd = 11
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
func TestEncode(t *testing.T) {
gtest.Case(t, func() {
m := make(map[string]string)
m["toml"] = tomlStr
res, err := gtoml.Encode(m)
if err != nil {
t.Errorf("encode failed. %v", err)
return
}
p, err := gparser.LoadContent(res)
if err != nil {
t.Errorf("parser failed. %v", err)
return
}
gtest.Assert(p.GetString("toml"), tomlStr)
})
gtest.Case(t, func() {
_, err := gtoml.Encode(tomlErr)
if err == nil {
t.Errorf("encode should be failed. %v", err)
return
}
})
}
func TestDecode(t *testing.T) {
gtest.Case(t, func() {
m := make(map[string]string)
m["toml"] = tomlStr
res, err := gtoml.Encode(m)
if err != nil {
t.Errorf("encode failed. %v", err)
return
}
decodeStr, err := gtoml.Decode(res)
if err != nil {
t.Errorf("decode failed. %v", err)
return
}
gtest.Assert(decodeStr.(map[string]interface{})["toml"], tomlStr)
decodeStr1 := make(map[string]interface{})
err = gtoml.DecodeTo(res, &decodeStr1)
if err != nil {
t.Errorf("decodeTo failed. %v", err)
return
}
gtest.Assert(decodeStr1["toml"], tomlStr)
})
gtest.Case(t, func() {
_, err := gtoml.Decode([]byte(tomlErr))
if err == nil {
t.Errorf("decode failed. %v", err)
return
}
decodeStr1 := make(map[string]interface{})
err = gtoml.DecodeTo([]byte(tomlErr), &decodeStr1)
if err == nil {
t.Errorf("decodeTo failed. %v", err)
return
}
})
}
func TestToJson(t *testing.T) {
gtest.Case(t, func() {
m := make(map[string]string)
m["toml"] = tomlStr
res, err := gtoml.Encode(m)
if err != nil {
t.Errorf("encode failed. %v", err)
return
}
jsonToml, err := gtoml.ToJson(res)
if err != nil {
t.Errorf("ToJson failed. %v", err)
return
}
p, err := gparser.LoadContent(res)
if err != nil {
t.Errorf("parser failed. %v", err)
return
}
expectJson, err := p.ToJson()
if err != nil {
t.Errorf("parser ToJson failed. %v", err)
return
}
gtest.Assert(jsonToml, expectJson)
})
gtest.Case(t, func() {
_, err := gtoml.ToJson([]byte(tomlErr))
if err == nil {
t.Errorf("ToJson failed. %v", err)
return
}
})
}

View File

@ -0,0 +1,92 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gurl_test
import (
"github.com/gogf/gf/g/encoding/gurl"
"github.com/gogf/gf/g/test/gtest"
"net/url"
"testing"
)
var urlStr string = `https://golang.org/x/crypto?go-get=1 +`
var urlEncode string = `https%3A%2F%2Fgolang.org%2Fx%2Fcrypto%3Fgo-get%3D1+%2B`
var rawUrlEncode string = `https%3A%2F%2Fgolang.org%2Fx%2Fcrypto%3Fgo-get%3D1%20%2B`
func TestEncodeAndDecode(t *testing.T) {
gtest.Case(t, func() {
gtest.Assert(gurl.Encode(urlStr), urlEncode)
res, err := gurl.Decode(urlEncode)
if err != nil {
t.Errorf("decode failed. %v", err)
return
}
gtest.Assert(res, urlStr)
})
}
func TestRowEncodeAndDecode(t *testing.T) {
gtest.Case(t, func() {
gtest.Assert(gurl.RawEncode(urlStr), rawUrlEncode)
res, err := gurl.RawDecode(rawUrlEncode)
if err != nil {
t.Errorf("decode failed. %v", err)
return
}
gtest.Assert(res, urlStr)
})
}
func TestBuildQuery(t *testing.T) {
src := url.Values{
"a": {"a2", "a1"},
"b": {"b2", "b1"},
"c": {"c1", "c2"},
}
expect := "a=a2&a=a1&b=b2&b=b1&c=c1&c=c2"
gtest.Assert(gurl.BuildQuery(src), expect)
}
func TestParseURL(t *testing.T) {
src := `http://username:password@hostname:9090/path?arg=value#anchor`
expect := map[string]string{
"scheme": "http",
"host": "hostname",
"port": "9090",
"user": "username",
"pass": "password",
"path": "/path",
"query": "arg=value",
"fragment": "anchor",
}
gtest.Case(t, func() {
component := 0
for k, v := range []string{"all", "scheme", "host", "port", "user", "pass", "path", "query", "fragment"} {
if v == "all" {
component = -1
} else {
component = 1 << (uint(k - 1))
}
res, err := gurl.ParseURL(src, component)
if err != nil {
t.Errorf("ParseURL failed. component:%v, err:%v", component, err)
return
}
if v == "all" {
gtest.Assert(res, expect)
} else {
gtest.Assert(res[v], expect[v])
}
}
})
}

View File

@ -0,0 +1,143 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gyaml_test
import (
"github.com/gogf/gf/g/encoding/gparser"
"github.com/gogf/gf/g/encoding/gyaml"
"github.com/gogf/gf/g/test/gtest"
"testing"
)
var yamlStr string = `
#即表示url属性值
url: http://www.wolfcode.cn
#即表示server.host属性的值
server:
host: http://www.wolfcode.cn
#数组即表示server为[a,b,c]
server:
- 120.168.117.21
- 120.168.117.22
- 120.168.117.23
#常量
pi: 3.14 #定义一个数值3.14
hasChild: true #定义一个boolean值
name: '你好YAML' #定义一个字符串
`
var yamlErr string = `
# 模板引擎目录
viewpath = "/home/www/templates/"
# MySQL数据库配置
[redis]
dd = 11
[redis]
disk = "127.0.0.1:6379,0"
cache = "127.0.0.1:6379,1"
`
func TestEncode(t *testing.T) {
gtest.Case(t, func() {
m := make(map[string]string)
m["yaml"] = yamlStr
res, err := gyaml.Encode(m)
if err != nil {
t.Errorf("encode failed. %v", err)
return
}
p, err := gparser.LoadContent(res)
if err != nil {
t.Errorf("parser failed. %v", err)
return
}
gtest.Assert(p.GetString("yaml"), yamlStr)
})
}
func TestDecode(t *testing.T) {
gtest.Case(t, func() {
m := make(map[string]string)
m["yaml"] = yamlStr
res, err := gyaml.Encode(m)
if err != nil {
t.Errorf("encode failed. %v", err)
return
}
decodeStr, err := gyaml.Decode(res)
if err != nil {
t.Errorf("decode failed. %v", err)
return
}
gtest.Assert(decodeStr.(map[string]interface{})["yaml"], yamlStr)
decodeStr1 := make(map[string]interface{})
err = gyaml.DecodeTo(res, &decodeStr1)
if err != nil {
t.Errorf("decodeTo failed. %v", err)
return
}
gtest.Assert(decodeStr1["yaml"], yamlStr)
})
gtest.Case(t, func() {
_, err := gyaml.Decode([]byte(yamlErr))
if err == nil {
t.Errorf("decode failed. %v", err)
return
}
decodeStr1 := make(map[string]interface{})
err = gyaml.DecodeTo([]byte(yamlErr), &decodeStr1)
if err == nil {
t.Errorf("decodeTo failed. %v", err)
return
}
})
}
func TestToJson(t *testing.T) {
gtest.Case(t, func() {
m := make(map[string]string)
m["yaml"] = yamlStr
res, err := gyaml.Encode(m)
if err != nil {
t.Errorf("encode failed. %v", err)
return
}
jsonyaml, err := gyaml.ToJson(res)
if err != nil {
t.Errorf("ToJson failed. %v", err)
return
}
p, err := gparser.LoadContent(res)
if err != nil {
t.Errorf("parser failed. %v", err)
return
}
expectJson, err := p.ToJson()
if err != nil {
t.Errorf("parser ToJson failed. %v", err)
return
}
gtest.Assert(jsonyaml, expectJson)
})
gtest.Case(t, func() {
_, err := gyaml.ToJson([]byte(yamlErr))
if err == nil {
t.Errorf("ToJson failed. %v", err)
return
}
})
}