-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
159 additions
and
45 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
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,37 @@ | ||
package checker | ||
|
||
import ( | ||
"reflect" | ||
"unicode" | ||
) | ||
|
||
// CheckerAscii is the name of the checker. | ||
const CheckerAscii = "ascii" | ||
|
||
// ResultNotAscii indicates that the given string contains non-ASCII characters. | ||
const ResultNotAscii = "NOT_ASCII" | ||
|
||
// IsAscii checks if the given string consists of only ASCII characters. | ||
func IsAscii(value string) Result { | ||
for i := 0; i < len(value); i++ { | ||
if value[i] > unicode.MaxASCII { | ||
return ResultNotAscii | ||
} | ||
} | ||
|
||
return ResultValid | ||
} | ||
|
||
// makeAscii makes a checker function for the ascii checker. | ||
func makeAscii(_ string) CheckFunc { | ||
return checkAscii | ||
} | ||
|
||
// checkAscii checks if the given string consists of only ASCII characters. | ||
func checkAscii(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
return IsAscii(value.String()) | ||
} |
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,57 @@ | ||
package checker | ||
|
||
import "testing" | ||
|
||
func TestIsAsciiInvalid(t *testing.T) { | ||
if IsAscii("𝄞 Music!") == ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestIsAsciiValid(t *testing.T) { | ||
if IsAscii("Checker") != ResultValid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckAsciiNonString(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
|
||
type User struct { | ||
Age int `checkers:"ascii"` | ||
} | ||
|
||
user := &User{} | ||
|
||
Check(user) | ||
} | ||
|
||
func TestCheckAsciiInvalid(t *testing.T) { | ||
type User struct { | ||
Username string `checkers:"ascii"` | ||
} | ||
|
||
user := &User{ | ||
Username: "𝄞 Music!", | ||
} | ||
|
||
_, valid := Check(user) | ||
if valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestCheckAsciiValid(t *testing.T) { | ||
type User struct { | ||
Username string `checkers:"ascii"` | ||
} | ||
|
||
user := &User{ | ||
Username: "checker", | ||
} | ||
|
||
_, valid := Check(user) | ||
if !valid { | ||
t.Fail() | ||
} | ||
} |
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
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,28 @@ | ||
# ASCII Checker | ||
|
||
The ```ascii``` checkr checks if the given string consists of only ASCII characters. If the string contains non-ASCII characters, the checker will return the ```NOT_ASCII``` result. Here is an example: | ||
|
||
```golang | ||
type User struct { | ||
Username string `checkers:"ascii"` | ||
} | ||
|
||
user := &User{ | ||
Username: "checker", | ||
} | ||
|
||
_, valid := Check(user) | ||
if !valid { | ||
// Send the mistakes back to the user | ||
} | ||
``` | ||
|
||
In your custom checkers, you can call the ```ascii``` checker function ```IsAscii``` to validate the user input. Here is an example: | ||
|
||
```golang | ||
result := IsAscii("Checker") | ||
|
||
if result != ResultValid { | ||
// Send the mistakes back to the user | ||
} | ||
``` |
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
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
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,13 @@ | ||
//go:build !test | ||
// +build !test | ||
|
||
package checker | ||
|
||
import "testing" | ||
|
||
// FailIfNoPanic fails if test didn't panic. Use this function with the defer. | ||
func FailIfNoPanic(t *testing.T) { | ||
if r := recover(); r == nil { | ||
t.Fail() | ||
} | ||
} |
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,13 @@ | ||
package checker | ||
|
||
import "testing" | ||
|
||
func TestFailIfNoPanicValid(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
panic("") | ||
} | ||
|
||
func TestFailIfNoPanicInvalid(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
defer FailIfNoPanic(nil) | ||
} |