-
-
Save zolotyh/cc970bd63da94031418df6c2ca32c50d 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
// Поиск файла. | |
// Среди иерархии папок затерялся файл. Напиши функцию для поиска файла. Файловая система представлена в виде мапки. | |
// Гарантируем, что на файловой системе не может быть более одного файла. | |
// Результат нужно вернуть в ввиде строки: пути к файлу, либо 'File not found' в случае отсутствия файла. | |
// Пример: | |
// { | |
// 'f1': { | |
// 'f11': { | |
// 'f111': { | |
// 'file': '' | |
// } | |
// }, | |
// 'f12': {} | |
// }, | |
// 'f2': {}, | |
// 'f3': { | |
// 'f31': {} | |
// } | |
// }; | |
// должно вернуть: 'f1/f11/f111/file' | |
// Требуемую мапку в дарте можно закодировать как Map<String, Object>. | |
// Object - базовый класс, наследником которого являются как String, так и Map. | |
// Проверить тип можно следующим образом: | |
// value is Map | |
// value is String | |
// Получить ключи мапки можно следующим образом: | |
// map.keys.toList() | |
// Map<String, Object> | |
String findFileName(Map fs) { | |
return ''; | |
} | |
bool hasErrors = false; | |
List<String> messages = []; | |
Map<String, Object> fs; | |
void main() { | |
test('should return correct path for exsisting file', () { | |
fs = { | |
'f1': { | |
'f11': { | |
'f111': { | |
'file': '' | |
} | |
}, | |
'f12': {} | |
}, | |
'f2': {}, | |
'f3': { | |
'f31': {} | |
} | |
}; | |
expect(findFileName(fs), equals('f1/f11/f111/file')); | |
}); | |
test('should return "File not found" for non existing file', () { | |
fs = { | |
'f1': { | |
'f11': { | |
'f111': {} | |
}, | |
'f12': {} | |
}, | |
'f2': {}, | |
'f3': { | |
'f31': {} | |
} | |
}; | |
expect(findFileName(fs), equals('File not found')); | |
}); | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment