Skip to content

Commit

Permalink
Add Examples. Fixes #111
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Jun 25, 2023
1 parent f76f14c commit 7fc4eaf
Show file tree
Hide file tree
Showing 21 changed files with 292 additions and 86 deletions.
15 changes: 11 additions & 4 deletions alphanumeric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test

import (
Expand All @@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)

func ExampleIsAlphanumeric() {
result := checker.IsAlphanumeric("ABcd1234")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsAlphanumericInvalid(t *testing.T) {
if checker.IsAlphanumeric("-/") == checker.ResultValid {
t.Fail()
Expand Down
15 changes: 11 additions & 4 deletions ascii_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test

import (
Expand All @@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)

func ExampleIsASCII() {
result := checker.IsASCII("Checker")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsASCIIInvalid(t *testing.T) {
if checker.IsASCII("𝄞 Music!") == checker.ResultValid {
t.Fail()
Expand Down
15 changes: 11 additions & 4 deletions cidr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test

import (
Expand All @@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)

func ExampleIsCidr() {
result := checker.IsCidr("2001:db8::/32")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsCidrInvalid(t *testing.T) {
if checker.IsCidr("900.800.200.100//24") == checker.ResultValid {
t.Fail()
Expand Down
11 changes: 5 additions & 6 deletions credit_card.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker

import (
Expand Down Expand Up @@ -85,8 +84,8 @@ func IsDinersCreditCard(number string) Result {
return isCreditCard(number, dinersPattern)
}

// IsDiscoveryCreditCard checks if the given valie is a valid Discovery credit card.
func IsDiscoveryCreditCard(number string) Result {
// IsDiscoverCreditCard checks if the given valie is a valid Discover credit card.
func IsDiscoverCreditCard(number string) Result {
return isCreditCard(number, discoverPattern)
}

Expand Down
74 changes: 67 additions & 7 deletions credit_card_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test

import (
Expand Down Expand Up @@ -37,6 +36,14 @@ func changeToInvalidLuhn(number string) string {
return number[:len(number)-1] + strconv.Itoa(luhn)
}

func ExampleIsAnyCreditCard() {
result := checker.IsAnyCreditCard("6011111111111117")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsAnyCreditCardValid(t *testing.T) {
if checker.IsAnyCreditCard(amexCard) != checker.ResultValid {
t.Fail()
Expand All @@ -55,6 +62,14 @@ func TestIsAnyCreditCardInvalidLuhn(t *testing.T) {
}
}

func ExampleIsAmexCreditCard() {
result := checker.IsAmexCreditCard("378282246310005")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsAmexCreditCardValid(t *testing.T) {
if checker.IsAmexCreditCard(amexCard) != checker.ResultValid {
t.Fail()
Expand All @@ -73,6 +88,13 @@ func TestIsAmexCreditCardInvalidLuhn(t *testing.T) {
}
}

func ExampleIsDinersCreditCard() {
result := checker.IsDinersCreditCard("36227206271667")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsDinersCreditCardValid(t *testing.T) {
if checker.IsDinersCreditCard(dinersCard) != checker.ResultValid {
t.Fail()
Expand All @@ -91,24 +113,39 @@ func TestIsDinersCreditCardInvalidLuhn(t *testing.T) {
}
}

func ExampleIsDiscoverCreditCard() {
result := checker.IsDiscoverCreditCard("6011111111111117")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsDiscoverCreditCardValid(t *testing.T) {
if checker.IsDiscoveryCreditCard(discoverCard) != checker.ResultValid {
if checker.IsDiscoverCreditCard(discoverCard) != checker.ResultValid {
t.Fail()
}
}

func TestIsDiscoverCreditCardInvalidPattern(t *testing.T) {
if checker.IsDiscoveryCreditCard(invalidCard) == checker.ResultValid {
if checker.IsDiscoverCreditCard(invalidCard) == checker.ResultValid {
t.Fail()
}
}

func TestIsDiscoverCreditCardInvalidLuhn(t *testing.T) {
if checker.IsDiscoveryCreditCard(changeToInvalidLuhn(discoverCard)) == checker.ResultValid {
if checker.IsDiscoverCreditCard(changeToInvalidLuhn(discoverCard)) == checker.ResultValid {
t.Fail()
}
}

func ExampleIsJcbCreditCard() {
result := checker.IsJcbCreditCard("3530111333300000")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsJcbCreditCardValid(t *testing.T) {
if checker.IsJcbCreditCard(jcbCard) != checker.ResultValid {
t.Fail()
Expand All @@ -127,6 +164,14 @@ func TestIsJcbCreditCardInvalidLuhn(t *testing.T) {
}
}

func ExampleIsMasterCardCreditCard() {
result := checker.IsMasterCardCreditCard("5555555555554444")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsMasterCardCreditCardValid(t *testing.T) {
if checker.IsMasterCardCreditCard(masterCard) != checker.ResultValid {
t.Fail()
Expand All @@ -145,6 +190,14 @@ func TestIsMasterCardCreditCardInvalidLuhn(t *testing.T) {
}
}

func ExampleIsUnionPayCreditCard() {
result := checker.IsUnionPayCreditCard("6200000000000005")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsUnionPayCreditCardValid(t *testing.T) {
if checker.IsUnionPayCreditCard(unionPayCard) != checker.ResultValid {
t.Fail()
Expand All @@ -163,6 +216,13 @@ func TestIsUnionPayCreditCardInvalidLuhn(t *testing.T) {
}
}

func ExampleIsVisaCreditCard() {
result := checker.IsVisaCreditCard("4111111111111111")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}
func TestIsVisaCreditCardValid(t *testing.T) {
if checker.IsVisaCreditCard(visaCard) != checker.ResultValid {
t.Fail()
Expand Down
15 changes: 11 additions & 4 deletions digits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test

import (
Expand All @@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)

func ExampleIsDigits() {
result := checker.IsDigits("1234")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsDigitsInvalid(t *testing.T) {
if checker.IsDigits("checker") == checker.ResultValid {
t.Fail()
Expand Down
2 changes: 1 addition & 1 deletion doc/checkers/required.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if !valid {
If you do not want to validate user input stored in a struct, you can individually call the `required` checker function [`IsRequired`](https://pkg.go.dev/github.com/cinar/checker#IsRequired) to validate the user input. Here is an example:

```golang
var name
var name string

result := checker.IsRequired(name)
if result != checker.ResultValid {
Expand Down
15 changes: 11 additions & 4 deletions email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test

import (
Expand All @@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)

func ExampleIsEmail() {
result := checker.IsEmail("[email protected]")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestCheckEmailNonString(t *testing.T) {
defer checker.FailIfNoPanic(t)

Expand Down
15 changes: 11 additions & 4 deletions fqdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test

import (
Expand All @@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)

func ExampleIsFqdn() {
result := checker.IsFqdn("zdo.com")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestCheckFdqnWithoutTld(t *testing.T) {
if checker.IsFqdn("abcd") != checker.ResultNotFqdn {
t.Fail()
Expand Down
15 changes: 11 additions & 4 deletions ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//
// https://github.com/cinar/checker
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// Copyright 2023 Onur Cinar. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package checker_test

import (
Expand All @@ -14,6 +13,14 @@ import (
"github.com/cinar/checker"
)

func ExampleIsIP() {
result := checker.IsIP("2001:db8::68")

if result != checker.ResultValid {
// Send the mistakes back to the user
}
}

func TestIsIPInvalid(t *testing.T) {
if checker.IsIP("900.800.200.100") == checker.ResultValid {
t.Fail()
Expand Down
Loading

0 comments on commit 7fc4eaf

Please sign in to comment.