-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add upper and lower normalizers and tests to v2. (#146)
# Describe Request Add upper and lower normalizers and tests to v2. # Change Type New code.
- Loading branch information
Showing
5 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2023-2024 Onur Cinar. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
// https://github.com/cinar/checker | ||
|
||
package v2 | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// nameLower is the name of the lower normalizer. | ||
nameLower = "lower" | ||
) | ||
|
||
// Lower maps all Unicode letters in the given value to their lower case. | ||
func Lower(value string) (string, error) { | ||
return strings.ToLower(value), nil | ||
} | ||
|
||
// reflectLower maps all Unicode letters in the given value to their lower case. | ||
func reflectLower(value reflect.Value) (reflect.Value, error) { | ||
newValue, err := Lower(value.Interface().(string)) | ||
return reflect.ValueOf(newValue), err | ||
} | ||
|
||
// makeLower returns the lower normalizer function. | ||
func makeLower(_ string) CheckFunc[reflect.Value] { | ||
return reflectLower | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2023-2024 Onur Cinar. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
// https://github.com/cinar/checker | ||
|
||
package v2_test | ||
|
||
import ( | ||
"testing" | ||
|
||
v2 "github.com/cinar/checker/v2" | ||
) | ||
|
||
func TestLower(t *testing.T) { | ||
input := "CHECKER" | ||
expected := "checker" | ||
|
||
actual, err := v2.Lower(input) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if actual != expected { | ||
t.Fatalf("actual %s expected %s", actual, expected) | ||
} | ||
} | ||
|
||
func TestReflectLower(t *testing.T) { | ||
type Person struct { | ||
Name string `checkers:"lower"` | ||
} | ||
|
||
person := &Person{ | ||
Name: "CHECKER", | ||
} | ||
|
||
expected := "checker" | ||
|
||
errs, ok := v2.CheckStruct(person) | ||
if !ok { | ||
t.Fatalf("got unexpected errors %v", errs) | ||
} | ||
|
||
if person.Name != expected { | ||
t.Fatalf("actual %s expected %s", person.Name, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2023-2024 Onur Cinar. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
// https://github.com/cinar/checker | ||
|
||
package v2 | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// nameUpper is the name of the upper normalizer. | ||
nameUpper = "upper" | ||
) | ||
|
||
// Upper maps all Unicode letters in the given value to their upper case. | ||
func Upper(value string) (string, error) { | ||
return strings.ToUpper(value), nil | ||
} | ||
|
||
// reflectUpper maps all Unicode letters in the given value to their upper case. | ||
func reflectUpper(value reflect.Value) (reflect.Value, error) { | ||
newValue, err := Upper(value.Interface().(string)) | ||
return reflect.ValueOf(newValue), err | ||
} | ||
|
||
// makeUpper returns the upper normalizer function. | ||
func makeUpper(_ string) CheckFunc[reflect.Value] { | ||
return reflectUpper | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) 2023-2024 Onur Cinar. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
// https://github.com/cinar/checker | ||
|
||
package v2_test | ||
|
||
import ( | ||
"testing" | ||
|
||
v2 "github.com/cinar/checker/v2" | ||
) | ||
|
||
func TestUpper(t *testing.T) { | ||
input := "checker" | ||
expected := "CHECKER" | ||
|
||
actual, err := v2.Upper(input) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if actual != expected { | ||
t.Fatalf("actual %s expected %s", actual, expected) | ||
} | ||
} | ||
|
||
func TestReflectUpper(t *testing.T) { | ||
type Person struct { | ||
Name string `checkers:"upper"` | ||
} | ||
|
||
person := &Person{ | ||
Name: "checker", | ||
} | ||
|
||
expected := "CHECKER" | ||
|
||
errs, ok := v2.CheckStruct(person) | ||
if !ok { | ||
t.Fatalf("got unexpected errors %v", errs) | ||
} | ||
|
||
if person.Name != expected { | ||
t.Fatalf("actual %s expected %s", person.Name, expected) | ||
} | ||
} |