From 574025799339310b84c12076cc3097066f66439a Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Wed, 30 Oct 2024 14:46:28 +0300 Subject: [PATCH] [terminal/input] Add input validation feature --- terminal/input/input_test.go | 79 ++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 terminal/input/input_test.go diff --git a/terminal/input/input_test.go b/terminal/input/input_test.go new file mode 100644 index 00000000..d0de34c7 --- /dev/null +++ b/terminal/input/input_test.go @@ -0,0 +1,79 @@ +package input + +// ////////////////////////////////////////////////////////////////////////////////// // +// // +// Copyright (c) 2024 ESSENTIAL KAOS // +// Apache License, Version 2.0 // +// // +// ////////////////////////////////////////////////////////////////////////////////// // + +import ( + "testing" + + . "github.com/essentialkaos/check" +) + +// ////////////////////////////////////////////////////////////////////////////////// // + +func Test(t *testing.T) { TestingT(t) } + +type InputSuite struct{} + +// ////////////////////////////////////////////////////////////////////////////////// // + +var _ = Suite(&InputSuite{}) + +// ////////////////////////////////////////////////////////////////////////////////// // + +func (s *InputSuite) TestValidators(c *C) { + _, err := validatorNotEmpty("") + c.Assert(err, Equals, ErrIsEmpty) + _, err = validatorNotEmpty(" ") + c.Assert(err, Equals, ErrIsEmpty) + _, err = validatorNotEmpty("test") + c.Assert(err, IsNil) + + _, err = validatorIsNumber("ABCD") + c.Assert(err, Equals, ErrInvalidNumber) + _, err = validatorIsNumber("1234") + c.Assert(err, IsNil) + _, err = validatorIsNumber("-1234") + c.Assert(err, IsNil) + _, err = validatorIsNumber("") + c.Assert(err, IsNil) + + _, err = validatorIsFloat("ABCD") + c.Assert(err, Equals, ErrInvalidFloat) + _, err = validatorIsFloat("1234.56") + c.Assert(err, IsNil) + _, err = validatorIsFloat("-1234.56") + c.Assert(err, IsNil) + _, err = validatorIsFloat("") + c.Assert(err, IsNil) + + _, err = validatorIsEmail("ABCD") + c.Assert(err, Equals, ErrInvalidEmail) + _, err = validatorIsEmail("@test") + c.Assert(err, Equals, ErrInvalidEmail) + _, err = validatorIsEmail("abcd@") + c.Assert(err, Equals, ErrInvalidEmail) + _, err = validatorIsEmail("abcd@test") + c.Assert(err, Equals, ErrInvalidEmail) + _, err = validatorIsEmail("") + c.Assert(err, IsNil) + _, err = validatorIsEmail("test@domain.com") + c.Assert(err, IsNil) + + _, err = validatorIsURL("abcd") + c.Assert(err, Equals, ErrInvalidURL) + _, err = validatorIsURL("abcd.com") + c.Assert(err, Equals, ErrInvalidURL) + _, err = validatorIsURL("https://abcd") + c.Assert(err, Equals, ErrInvalidURL) + _, err = validatorIsURL("test://abcd.com") + c.Assert(err, Equals, ErrInvalidURL) + _, err = validatorIsURL("") + c.Assert(err, IsNil) + _, err = validatorIsURL("https://domain.com") + c.Assert(err, IsNil) +}