-
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.
ASCII checker moved to v2 version. (#131)
# Describe Request ASCII checker moved to v2 version. # Change Type New code.
- Loading branch information
Showing
3 changed files
with
120 additions
and
0 deletions.
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,43 @@ | ||
// 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" | ||
"unicode" | ||
) | ||
|
||
const ( | ||
// nameASCII is the name of the ASCII check. | ||
nameASCII = "ascii" | ||
) | ||
|
||
var ( | ||
// ErrNotASCII indicates that the given string contains non-ASCII characters. | ||
ErrNotASCII = NewCheckError("ASCII") | ||
) | ||
|
||
// IsASCII checks if the given string consists of only ASCII characters. | ||
func IsASCII(value string) (string, error) { | ||
for _, c := range value { | ||
if c > unicode.MaxASCII { | ||
return value, ErrNotASCII | ||
} | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
// checkASCII checks if the given string consists of only ASCII characters. | ||
func isASCII(value reflect.Value) (reflect.Value, error) { | ||
_, err := IsASCII(value.Interface().(string)) | ||
return value, err | ||
} | ||
|
||
// makeASCII makes a checker function for the ASCII checker. | ||
func makeASCII(_ string) CheckFunc[reflect.Value] { | ||
return isASCII | ||
} |
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,76 @@ | ||
// 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 ( | ||
"fmt" | ||
"testing" | ||
|
||
v2 "github.com/cinar/checker/v2" | ||
) | ||
|
||
func ExampleIsASCII() { | ||
_, err := v2.IsASCII("Checker") | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func TestIsASCIIInvalid(t *testing.T) { | ||
_, err := v2.IsASCII("𝄞 Music!") | ||
if err == nil { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestIsASCIIValid(t *testing.T) { | ||
_, err := v2.IsASCII("Checker") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestCheckASCIINonString(t *testing.T) { | ||
defer FailIfNoPanic(t, "expected panic") | ||
|
||
type User struct { | ||
Username int `checkers:"ascii"` | ||
} | ||
|
||
user := &User{} | ||
|
||
v2.CheckStruct(user) | ||
} | ||
|
||
func TestCheckASCIIInvalid(t *testing.T) { | ||
type User struct { | ||
Username string `checkers:"ascii"` | ||
} | ||
|
||
user := &User{ | ||
Username: "𝄞 Music!", | ||
} | ||
|
||
_, ok := v2.CheckStruct(user) | ||
if ok { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
func TestCheckASCIIValid(t *testing.T) { | ||
type User struct { | ||
Username string `checkers:"ascii"` | ||
} | ||
|
||
user := &User{ | ||
Username: "checker", | ||
} | ||
|
||
_, ok := v2.CheckStruct(user) | ||
if !ok { | ||
t.Fatal("expected valid") | ||
} | ||
} |
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