Cześć, uczę się testowania kodu (googletest). Chciałem napisać testy do istniejącego projektu ale na samym wstępie poległem, bo w programie użytkownik wprowadza liczbę, której poprawność jest sprawdzana przez funkcję. Ale jak napisać test dla tej funkcji? W ciele jest std::cin więc jak można to zautomatyzować/ominąć?
Funkcje sprawdzające:
bool dataValidation(const std::string& info)
{
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(10, '\n');
std::cout << info;
return true;
}
else {
return false;
}
}
T validateNumber(const std::string& request, const T range_low, const T range_high,
const std::string& info_bad_data = "This is not proper value")
{
T number;
while (true)
{
std::cout << '\n' << request << '\n';
std::cin >> number;
if (dataValidation(info_bad_data)) { // zle dane
continue;
}
else if (number < range_low || number > range_high) { // poza zakresem
std::cout << "You can choose from: " << range_low << " to "
<< range_high << ". Try again\n";
}
else {
break;
}
}
return number;
}