Created
November 28, 2018 21:48
-
-
Save NevRA/691ec06d60cf3d717bdcbe3bc0ea141c to your computer and use it in GitHub Desktop.
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
// Проверка пинкода | |
// Напиши фнукцию проверки пинкода. Пинкод может содержать 4 или 6 цифр. При правильном пинкоде нужно выводить true, при неправильном false | |
// Сделай так, чтобы прошли тесты | |
bool validatePIN(String input) { | |
return true; | |
} | |
bool hasErrors = false; | |
List<String> messages = []; | |
void main() { | |
test('should fail on wrong PIN', () { | |
expect(validatePIN("1"), equals(false)); | |
expect(validatePIN("12"), equals(false)); | |
expect(validatePIN("123"), equals(false)); | |
expect(validatePIN("12345"), equals(false)); | |
expect(validatePIN("1234567"), equals(false)); | |
expect(validatePIN("-1234"), equals(false)); | |
expect(validatePIN("1.234"), equals(false)); | |
expect(validatePIN("00000000"), equals(false)); | |
expect(validatePIN("a234"), equals(false)); | |
expect(validatePIN(".234"), equals(false)); | |
}); | |
test('should pass on valid PIN', () { | |
expect(validatePIN("1234"), equals(true)); | |
expect(validatePIN("0000"), equals(true)); | |
expect(validatePIN("1111"), equals(true)); | |
expect(validatePIN("123456"), equals(true)); | |
expect(validatePIN("098765"), equals(true)); | |
expect(validatePIN("000000"), equals(true)); | |
expect(validatePIN("123456"), equals(true)); | |
expect(validatePIN("090909"), equals(true)); | |
}); | |
_validatePIN(''); | |
_validatePIN2(''); | |
if(!hasErrors ){ | |
print('💪Tests are passed! \n\n'); | |
} else { | |
print('💩 Tests aren\'t passed! \n\n'); | |
} | |
print(messages.join('\n')); | |
} | |
typedef bool Checker(dynamic input); | |
Checker equals(dynamic input) { | |
return (dynamic internalInput) { | |
input.toString() == internalInput.toString() | |
? true | |
: throw AssertionError('value: $input is not equal: $internalInput'); | |
}; | |
} | |
void test(String name, Function input) { | |
try { | |
input(); | |
messages.add('✓ $name'); | |
} catch (e) { | |
hasErrors = true; | |
if (e is AssertionError) { | |
messages.add('✗ $test failed \n name: $name\n exception: ${e.message}'); | |
} | |
} | |
} | |
void expect(dynamic input, bool validator(dynamic validatorInput)) { | |
validator(input); | |
} | |
bool _validatePIN(String input) { | |
if (input == null) return false; | |
final chars = input.codeUnits; | |
if (chars.length != 4 && chars.length != 6) return false; | |
final first = '0'.codeUnits.first; | |
final last = '9'.codeUnits.first; | |
return chars.every((char) => char >= first && char <= last); | |
} | |
bool _validatePIN2(String input) { | |
return new RegExp(r"^(\d{4}|\d{6})$").hasMatch(input); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment