using System.Diagnostics;
using System.Collections.Generic;
static void Main(string[] args)
var sw = new Stopwatch();
var tests = new UnitTests();
tests.Test(new string[] { "--name", "Mike" }, ValidationResult.Valid);
tests.Test(new string[] { "--name", "Mikelangelloverrylongnammeo" }, ValidationResult.Valid);
tests.Test(new string[] { "--name", "mike" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "M1ke" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mike " }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "M" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", " Mike" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "-Mike" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mike_" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mik$e" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mike Mike" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "MikeWazowski" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "Wazowski" }, ValidationResult.Valid);
tests.Test(new string[] { "--surname", "O'Sullivan" }, ValidationResult.Valid);
tests.Test(new string[] { "--surname", "Mike-Wazowski" }, ValidationResult.Valid);
tests.Test(new string[] { "--surname", "Oh'Sullivan" }, ValidationResult.Valid);
tests.Test(new string[] { "--surname", "Sullivan'O" }, ValidationResult.Valid);
tests.Test(new string[] { "--surname", "Wazow$ki" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "O'Sullivan-Wazowski" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "Sullivan-O'Wazowski" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "O' Sullivan" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "O'Hara'Sullivan" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "O'sullivan" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "'Sullivan" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "-Sullivan" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "Sullivan'" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "Sullivan-" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "Mike - Wazowski" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "Mike-Wazowski-Sullivan" }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "wazowski " }, ValidationResult.Invalid);
tests.Test(new string[] { "--surname", "wazowski" }, ValidationResult.Invalid);
tests.Test(new string[] { "--age", "29" }, ValidationResult.Valid);
tests.Test(new string[] { "--age", "1" }, ValidationResult.Valid);
tests.Test(new string[] { "--age", "150" }, ValidationResult.Valid);
tests.Test(new string[] { "--age", "0" }, ValidationResult.Invalid);
tests.Test(new string[] { "--age", "8.5" }, ValidationResult.Invalid);
tests.Test(new string[] { "--age", "0.99" }, ValidationResult.Invalid);
tests.Test(new string[] { "--age", "8,5" }, ValidationResult.Invalid);
tests.Test(new string[] { "--age", "-29" }, ValidationResult.Invalid);
tests.Test(new string[] { "--age", "Mike" }, ValidationResult.Invalid);
tests.Test(new string[] { "--help" }, ValidationResult.HelpRequested);
tests.Test(new string[] { "--help", "me please" }, ValidationResult.Invalid);
tests.Test(new string[] { "--help", "--name", "Mike" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mike", "--help" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mike", "--surname", "Wazowski" }, ValidationResult.Valid);
tests.Test(new string[] { "--name", "Mike", "--surname", "Wazowski", "--age", "149" }, ValidationResult.Valid);
tests.Test(new string[] { "--surname", "Wazowski", "--age", "149", "--name", "Mike" }, ValidationResult.Valid);
tests.Test(new string[] { }, ValidationResult.Invalid);
tests.Test(new string[] { "name", "Mike" }, ValidationResult.Invalid);
tests.Test(new string[] { "--mane", "Mike" }, ValidationResult.Invalid);
tests.Test(new string[] { "Mike", "--name" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mike", "--surname" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "mike", "--surname", "Wazowski" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mike", "--surname", "wazowski" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mike", "--surname", "Wazowski", "--age" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "Mike", "--surname", "wazowski", "--age", "149" }, ValidationResult.Invalid);
tests.Test(new string[] { "--name", "--surname" }, ValidationResult.Invalid);
Console.WriteLine($"Elapsed time: {sw.ElapsedMilliseconds}ms");
public enum ValidationResult
private List<string> results = new List<string>();
public void Test(string[] args, ValidationResult expectedValidationResult)
var validator = new Validator();
var actualValidationResult = validator.Validate(args);
var result = actualValidationResult == expectedValidationResult ? "pass" : "fail";
Console.WriteLine($"{result} | Expected: {expectedValidationResult} - Actual: {actualValidationResult} | {String.Join(' ', args)}");
public void PostResults()
var passed = results.Count(x => x == "pass");
var percentage = (decimal)passed / (decimal)all;
Console.WriteLine($"Final result: {passed}/{all} {percentage:P1}");
public ValidationResult Validate(string[] args)
bool surnameResult = false;
if (CheckIfEmpty(args)) return ValidationResult.Invalid;
for (int i = 0; i < args.Length; i++)
if (i != args.Length - 1)
nameResult = ValidatorService.CheckName(args[i + 1]);
if (i != args.Length - 1)
surnameResult = ValidatorService.CheckSurname(args[i + 1]);
if (i != args.Length - 1)
ageResult = ValidatorService.CheckAge(args[i + 1]);
helpResult = ValidatorService.CheckHelp(args);
return ReturnValidationResult(args, helpResult, nameResult, surnameResult, ageResult);
private ValidationResult ReturnValidationResult(string[] args, bool helpResult, bool nameResult, bool surnameResult, bool ageResult)
if (CountSentence(args) == 1 && helpResult)
return ValidationResult.HelpRequested;
if (CountSentence(args) == 1 && (nameResult || surnameResult || ageResult))
return ValidationResult.Valid;
if (CountSentence(args) == 2 && ((nameResult && surnameResult)
|| (surnameResult && ageResult)
|| (ageResult && nameResult)))
return ValidationResult.Valid;
if (CountSentence(args) == 3 && nameResult && surnameResult && ageResult)
return ValidationResult.Valid;
return ValidationResult.Invalid;
private bool CheckIfEmpty(string[] args)
private int CountSentence(string[] args)
for (int i = 0; i < args.Length; i++)
if (args[i].Contains("--name")
|| args[i].Contains("--surname")
|| args[i].Contains("--age")
|| args[i].Contains("--help"))
public static class ExpressionOperation
private const int maxCountPartsOfSurname = 2;
private const int minMainExpressionLenght = 2;
public static bool CheckMainExpressionLenght(string expression)
=> expression.Length > minMainExpressionLenght;
public static string[] SplitExpression(string expression, char separator)
stringParts = expression.Split(separator);
public static bool CheckProperlySyntax(string expression)
for (int j = 0; j < expression.Length; j++)
if (!char.IsUpper(expression[j]) && j == 0)
if (char.IsUpper(expression[j]) && j != 0)
if (!char.IsLetter(expression[j]))
public static bool CheckExpressionParts(string surname, int minLenghtSurnamePart, char separator)
var surnameParts = SplitExpression(surname, separator);
if (surnameParts.Length > maxCountPartsOfSurname)
for (int i = 0; i < surnameParts.Length; i++)
string surnamePart = surnameParts[i];
if (surnamePart.Length < minLenghtSurnamePart)
if (!CheckProperlySyntax(surnamePart))
public static class ValidatorService
public static bool CheckName(string name)
if (!ExpressionOperation.CheckMainExpressionLenght(name))
if (ExpressionOperation.CheckProperlySyntax(name))
public static bool CheckAge(string age)
var check = int.TryParse(age, out int properlyAge);
return check && properlyAge >= 1 && properlyAge <= 150;
public static bool CheckSurname(string surname)
if (!ExpressionOperation.CheckMainExpressionLenght(surname))
if ( surname.Contains(hyphen) || surname.Contains(quote))
if (surname.Contains(hyphen))
return SeparatorLogic.HyphenOperation(surname, hyphen);
return SeparatorLogic.QuoteOperation(surname, quote);
return ExpressionOperation.CheckProperlySyntax(surname);
public static bool CheckHelp(string[] args)
public static class SeparatorLogic
const int minExpressionLenghtOfQuote = 1;
const int minExpressionLenghtOfHyphen= 2;
public static bool QuoteOperation(string surname, char separator)
=> ExpressionOperation.CheckExpressionParts(surname, minExpressionLenghtOfQuote, separator);
public static bool HyphenOperation(string surname, char separator)
=> ExpressionOperation.CheckExpressionParts(surname, minExpressionLenghtOfHyphen, separator);