From 7fc4eaf234568dfeadb51faef512341ebedc680b Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Sun, 25 Jun 2023 01:33:27 +0000 Subject: [PATCH] Add Examples. Fixes #111 --- alphanumeric_test.go | 15 +++++--- ascii_test.go | 15 +++++--- cidr_test.go | 15 +++++--- credit_card.go | 11 +++--- credit_card_test.go | 74 ++++++++++++++++++++++++++++++++++++---- digits_test.go | 15 +++++--- doc/checkers/required.md | 2 +- email_test.go | 15 +++++--- fqdn_test.go | 15 +++++--- ip_test.go | 15 +++++--- ipv4_test.go | 15 +++++--- ipv6_test.go | 15 +++++--- isbn_test.go | 28 ++++++++++++--- luhn_test.go | 15 +++++--- mac_test.go | 15 +++++--- max_test.go | 17 ++++++--- maxlength_test.go | 17 ++++++--- min_test.go | 17 ++++++--- minlength_test.go | 17 ++++++--- required_test.go | 16 ++++++--- url_test.go | 14 +++++--- 21 files changed, 292 insertions(+), 86 deletions(-) diff --git a/alphanumeric_test.go b/alphanumeric_test.go index 11d44db..a47fbe6 100644 --- a/alphanumeric_test.go +++ b/alphanumeric_test.go @@ -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 ( @@ -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() diff --git a/ascii_test.go b/ascii_test.go index cbdc0b2..8fb7bd1 100644 --- a/ascii_test.go +++ b/ascii_test.go @@ -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 ( @@ -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() diff --git a/cidr_test.go b/cidr_test.go index 7775cda..f529df3 100644 --- a/cidr_test.go +++ b/cidr_test.go @@ -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 ( @@ -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() diff --git a/credit_card.go b/credit_card.go index 057a7a5..4e9b5c7 100644 --- a/credit_card.go +++ b/credit_card.go @@ -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 ( @@ -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) } diff --git a/credit_card_test.go b/credit_card_test.go index a5a044e..2730650 100644 --- a/credit_card_test.go +++ b/credit_card_test.go @@ -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 ( @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() @@ -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() diff --git a/digits_test.go b/digits_test.go index c7bc39b..232e83a 100644 --- a/digits_test.go +++ b/digits_test.go @@ -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 ( @@ -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() diff --git a/doc/checkers/required.md b/doc/checkers/required.md index f59dad2..5b4638b 100644 --- a/doc/checkers/required.md +++ b/doc/checkers/required.md @@ -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 { diff --git a/email_test.go b/email_test.go index ca7a4de..3b5e016 100644 --- a/email_test.go +++ b/email_test.go @@ -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 ( @@ -14,6 +13,14 @@ import ( "github.com/cinar/checker" ) +func ExampleIsEmail() { + result := checker.IsEmail("user@zdo.com") + + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestCheckEmailNonString(t *testing.T) { defer checker.FailIfNoPanic(t) diff --git a/fqdn_test.go b/fqdn_test.go index aabf42c..3b54d85 100644 --- a/fqdn_test.go +++ b/fqdn_test.go @@ -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 ( @@ -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() diff --git a/ip_test.go b/ip_test.go index f299193..0d1d8c4 100644 --- a/ip_test.go +++ b/ip_test.go @@ -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 ( @@ -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() diff --git a/ipv4_test.go b/ipv4_test.go index a8ae93f..bb88375 100644 --- a/ipv4_test.go +++ b/ipv4_test.go @@ -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 ( @@ -14,6 +13,14 @@ import ( "github.com/cinar/checker" ) +func ExampleIsIPV4() { + result := checker.IsIPV4("192.168.1.1") + + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsIPV4Invalid(t *testing.T) { if checker.IsIPV4("900.800.200.100") == checker.ResultValid { t.Fail() diff --git a/ipv6_test.go b/ipv6_test.go index 0437e2e..f336f5a 100644 --- a/ipv6_test.go +++ b/ipv6_test.go @@ -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 ( @@ -14,6 +13,14 @@ import ( "github.com/cinar/checker" ) +func ExampleIsIPV6() { + result := checker.IsIPV6("2001:db8::68") + + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsIPV6Invalid(t *testing.T) { if checker.IsIPV6("900.800.200.100") == checker.ResultValid { t.Fail() diff --git a/isbn_test.go b/isbn_test.go index 4f12d44..1d8a3e1 100644 --- a/isbn_test.go +++ b/isbn_test.go @@ -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 ( @@ -14,6 +13,13 @@ import ( "github.com/cinar/checker" ) +func ExampleIsISBN10() { + result := checker.IsISBN10("1430248270") + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsISBN10Valid(t *testing.T) { result := checker.IsISBN10("1430248270") if result != checker.ResultValid { @@ -49,6 +55,13 @@ func TestIsISBN10InvalidCheck(t *testing.T) { } } +func ExampleIsISBN13() { + result := checker.IsISBN13("9781430248279") + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsISBN13Valid(t *testing.T) { result := checker.IsISBN13("9781430248279") if result != checker.ResultValid { @@ -77,6 +90,13 @@ func TestIsISBN13InvalidCheck(t *testing.T) { } } +func ExampleIsISBN() { + result := checker.IsISBN("1430248270") + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsISBNValid10(t *testing.T) { result := checker.IsISBN("1430248270") if result != checker.ResultValid { diff --git a/luhn_test.go b/luhn_test.go index 80a0e84..7940d0d 100644 --- a/luhn_test.go +++ b/luhn_test.go @@ -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 ( @@ -14,6 +13,14 @@ import ( "github.com/cinar/checker" ) +func ExampleIsLuhn() { + result := checker.IsLuhn("4012888888881881") + + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsLuhnValid(t *testing.T) { numbers := []string{ "4012888888881881", diff --git a/mac_test.go b/mac_test.go index b4bcf65..be230aa 100644 --- a/mac_test.go +++ b/mac_test.go @@ -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 ( @@ -14,6 +13,14 @@ import ( "github.com/cinar/checker" ) +func ExampleIsMac() { + result := checker.IsMac("00:00:5e:00:53:01") + + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsMacInvalid(t *testing.T) { if checker.IsMac("1234") == checker.ResultValid { t.Fail() diff --git a/max_test.go b/max_test.go index de1bc28..a072636 100644 --- a/max_test.go +++ b/max_test.go @@ -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 ( @@ -14,6 +13,16 @@ import ( "github.com/cinar/checker" ) +func ExampleIsMax() { + quantity := 5 + + result := checker.IsMax(quantity, 10) + + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsMaxValid(t *testing.T) { n := 5 diff --git a/maxlength_test.go b/maxlength_test.go index 18e520a..da3fa4b 100644 --- a/maxlength_test.go +++ b/maxlength_test.go @@ -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 ( @@ -14,6 +13,16 @@ import ( "github.com/cinar/checker" ) +func ExampleIsMaxLength() { + s := "1234" + + result := checker.IsMaxLength(s, 4) + + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsMaxLengthValid(t *testing.T) { s := "1234" diff --git a/min_test.go b/min_test.go index f7c08ea..0a8e4c3 100644 --- a/min_test.go +++ b/min_test.go @@ -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 ( @@ -14,6 +13,16 @@ import ( "github.com/cinar/checker" ) +func ExampleIsMin() { + age := 45 + + result := checker.IsMin(age, 21) + + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsMinValid(t *testing.T) { n := 45 diff --git a/minlength_test.go b/minlength_test.go index e102239..68b10ea 100644 --- a/minlength_test.go +++ b/minlength_test.go @@ -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 ( @@ -14,6 +13,16 @@ import ( "github.com/cinar/checker" ) +func ExampleIsMinLength() { + s := "1234" + + result := checker.IsMinLength(s, 4) + + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsMinLengthValid(t *testing.T) { s := "1234" diff --git a/required_test.go b/required_test.go index 527e03c..677499f 100644 --- a/required_test.go +++ b/required_test.go @@ -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 ( @@ -14,6 +13,15 @@ import ( "github.com/cinar/checker" ) +func ExampleIsRequired() { + var name string + + result := checker.IsRequired(name) + if result != checker.ResultValid { + // Send the result back to the user + } +} + func TestIsRequired(t *testing.T) { s := "valid" diff --git a/url_test.go b/url_test.go index d4c8bde..8c08b2f 100644 --- a/url_test.go +++ b/url_test.go @@ -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 ( @@ -14,6 +13,13 @@ import ( "github.com/cinar/checker" ) +func ExampleIsURL() { + result := checker.IsURL("https://zdo.com") + if result != checker.ResultValid { + // Send the mistakes back to the user + } +} + func TestIsURLValid(t *testing.T) { result := checker.IsURL("https://zdo.com") if result != checker.ResultValid {