From b8f0795ebe667a111418351659c600802b7d5c64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Pe=C3=B1aranda?= Date: Tue, 25 Jul 2023 14:07:09 -0300 Subject: [PATCH] [tests] Check bridge prompter --- tests/test_cli_prompters.cpp | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/test_cli_prompters.cpp b/tests/test_cli_prompters.cpp index 1e8143a255e..c3e98f1b426 100644 --- a/tests/test_cli_prompters.cpp +++ b/tests/test_cli_prompters.cpp @@ -164,3 +164,71 @@ TEST_P(CLIPromptersBadCinState, PlainThrows) INSTANTIATE_TEST_SUITE_P(CLIPrompters, CLIPromptersBadCinState, Values(std::ios::eofbit, std::ios::failbit, std::ios::badbit)); + +class BridgePrompterTests : public CLIPrompters, + public WithParamInterface, std::string, bool>> +{ +}; + +TEST_F(CLIPrompters, failsIfNoNetworks) +{ + std::vector nets{}; + + mpt::MockTerminal mock_terminal; + + mp::BridgePrompter prompter{&mock_terminal}; + + ASSERT_DEBUG_DEATH(prompter.bridge_prompt(nets), "[Aa]ssert"); +} + +TEST_P(BridgePrompterTests, correctlyReturns) +{ + auto [nets, answer, ret] = GetParam(); + + mpt::MockTerminal mock_terminal; + EXPECT_CALL(mock_terminal, cout()).WillRepeatedly(ReturnRef(cout)); + EXPECT_CALL(mock_terminal, cout_is_live()).WillOnce(Return(true)); + EXPECT_CALL(mock_terminal, cin()).WillOnce(ReturnRef(cin)); + EXPECT_CALL(mock_terminal, cin_is_live()).WillOnce(Return(true)); + + cin.str(answer + "\n"); + + mp::BridgePrompter prompter{&mock_terminal}; + + EXPECT_EQ(prompter.bridge_prompt(nets), ret); +} + +INSTANTIATE_TEST_SUITE_P(CLIPrompters, BridgePrompterTests, + Values(std::make_tuple(std::vector{"eth1"}, "yes", true), + std::make_tuple(std::vector{"eth1", "eth3"}, "y", true), + std::make_tuple(std::vector{"eth1", "eth3"}, "no", false), + std::make_tuple(std::vector{"eth1"}, "n", false))); + +TEST_F(CLIPrompters, handlesWrongAnswer) +{ + mpt::MockTerminal mock_terminal; + EXPECT_CALL(mock_terminal, cout()).WillRepeatedly(ReturnRef(cout)); + EXPECT_CALL(mock_terminal, cout_is_live()).WillOnce(Return(true)); + EXPECT_CALL(mock_terminal, cin()).WillRepeatedly(ReturnRef(cin)); + EXPECT_CALL(mock_terminal, cin_is_live()).WillOnce(Return(true)); + + cin.str("qqq\nyes\n"); + + mp::BridgePrompter prompter{&mock_terminal}; + + std::vector nets{"eth2"}; + + EXPECT_EQ(prompter.bridge_prompt(nets), true); +} + +TEST_F(CLIPrompters, falseOnNonLiveTerminal) +{ + mpt::MockTerminal mock_terminal; + EXPECT_CALL(mock_terminal, cin_is_live()).WillOnce(Return(false)); + + mp::BridgePrompter prompter{&mock_terminal}; + + std::vector nets{"eth2"}; + + EXPECT_EQ(prompter.bridge_prompt(nets), false); +}