Skip to content

Commit

Permalink
add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 17, 2018
1 parent 37dde2b commit 1fcd541
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Coverage Status](https://coveralls.io/repos/github/gookit/filter/badge.svg?branch=master)](https://coveralls.io/github/gookit/filter?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/filter)](https://goreportcard.com/report/github.com/gookit/filter)

package filter provide filter, sanitize, convert golang data.
package filter provide filtering, sanitizing, and conversion of Golang data.

## GoDoc

Expand Down
6 changes: 6 additions & 0 deletions filtration.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ func (f *Filtration) Safe(key string) (interface{}, bool) {
return GetByPath(key, f.filteredData)
}

// SafeVal get filtered value by key
func (f *Filtration) SafeVal(key string) interface{} {
val, _ := GetByPath(key, f.filteredData)
return val
}

// Get value by key
func (f *Filtration) Get(key string) (interface{}, bool) {
val, ok := GetByPath(key, f.filteredData)
Expand Down
23 changes: 19 additions & 4 deletions filtration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,32 @@ func TestFiltration(t *testing.T) {
is.Equal(nil, val)

f := New(map[string]interface{}{
"name": " inhere ",
"email": " [email protected] ",
"key0": "34",
"name": " inhere ",
"email": " [email protected] ",
"ids": " 1,2, 3",
"jsCode": "<script>var a = 23;</script>",
"htmlCode": "<p>some text</p>",
"strings": []string{" a", " b ", "c "},
})
f.AddRules(map[string]string{
"email": "email",
"name": "trim|ucFirst",
"ids": "strToInts",
"key0": "int64",
"email": "email",
"name": "trim|ucFirst",
"jsCode": "escapeJS",
"htmlCode": "escapeHTML",
"strings": "trimStrings",
})

is.Nil(f.Sanitize())
is.Equal(int64(34), f.SafeVal("key0"))
is.Equal([]int{1, 2, 3}, f.SafeVal("ids"))
is.Equal([]string{"a", "b", "c"}, f.SafeVal("strings"))
is.Equal("Inhere", f.String("name"))
is.Equal("[email protected]", f.String("email"))
is.Equal(`\x3Cscript\x3Evar a = 23;\x3C/script\x3E`, f.SafeVal("jsCode"))
is.Equal("&lt;p&gt;some text&lt;/p&gt;", f.SafeVal("htmlCode"))
}

func TestFiltration_Filtering(t *testing.T) {
Expand Down

0 comments on commit 1fcd541

Please sign in to comment.