ASTeterin / OOP

0 stars 0 forks source link

Замечания по TVSet #8

Open alexey-malov opened 5 years ago

alexey-malov commented 5 years ago
TEST_CASE("Testing channel switching")
{
    CTVSet tv;
    tv.TurnOn();

    CHECK(tv.IsTurnedOn() == true);
    CHECK(tv.GetChannel() == 1);

    CHECK(tv.SelectChannel(0) == false);
    CHECK(tv.SelectChannel(101) == false);
    CHECK(tv.SelectChannel(50) == true);
    CHECK(tv.GetChannel() == 50);

    tv.SelectChannel(2);
    CHECK(tv.SelectPreviousChannel() == true);
    CHECK(tv.GetChannel() == 50);
}

void ExpectOperationFailure(const CTVSet& tv, const function<bool(CTVSet&tv)>& op) { CTVSet tvCopy(tv); // копия телевизора, над которой будет выполняться операция CHECK(!op(tvCopy)); // убедились, что операция (над копией) завершится неуспешно // сравниваем включенность и канал с прежними значениями CHECK(tvCopy.IsTurnedOn() == tv.IsTurnedOn()); CHECK(tvCopy.GetChannel() == tv.GetChannel()); }

TEST_CASE("TV can select channels from 1 to 99 only when it is on") { CTVSet tv; tv.TurnOn();

ExpectOperationSuccess(tv, [](CTVSet& tv){ return tv.SelectChannel(99); }, 99);
ExpectOperationFailure(tv, [](CTVSet& tv){ return tv.SelectChannel(100); });
...

}

alexey-malov commented 5 years ago
alexey-malov commented 5 years ago
alexey-malov commented 5 years ago
alexey-malov commented 5 years ago
bool CTVSet::SetChannelName(int channel, string const& name)
{
    bool isAvailableChannel = (channel >= 1) && (channel <= 99);
    if (!isAvailableChannel || !m_isOn)
    {
        return false;
    }
    if (CTVSet::GetChannelByName(name) != 0)
alexey-malov commented 5 years ago
alexey-malov commented 5 years ago
    for (auto currChannel= m_channelInfo.begin(); currChannel != m_channelInfo.end(); currChannel++)
    {
        if (currChannel->first == channel)
        {
            currChannel->second = name;
            return true;
        }
    }
alexey-malov commented 5 years ago
alexey-malov commented 5 years ago
> Info
TV is turned on
Channel is: 1
4 - TV 3
9 - 1st
> SelectChannel 1st
TV switched on 1 channel.
alexey-malov commented 5 years ago
function<bool(CTVSet& tv)> SelectChannel(int channel)
{
    return [channel](CTVSet& tv) {
        return tv.SelectChannel(channel);
    };
}

function<bool(CTVSet& tv)> SetChannelName(int channel, const string& name)
{
    return [channel, name](CTVSet& tv) {
        return tv.SetChannelName(channel, name); 
    };
}
...
ExpectOperationSuccess(tv, SelectChannel(99), 99);
alexey-malov commented 5 years ago
SCENARIO("deleting channel")
{
    GIVEN("A turned of TV")
    {
        CTVSet tv;
        WHEN("TV is turned on")
        {
            tv.TurnOn();
            THEN("if the channel is named, you can delete the name")
            {
                tv.SetChannelName(1, "ORT");
                CHECK(tv.GetChannel() == 1);
                tv.DeleteChannelName("ORT");
                CHECK(tv.GetChannelName(1) == "");
            }
            AND_THEN("if the channel name is not assigned the status will not change")
            {
                ExpectOperationFailure(tv, [](CTVSet& tv) {return tv.DeleteChannelName("TNT"); });
SCENARIO("Channel deletion")
{
    GIVEN("A turned on TV with associated channel name")
    {
        CTVSet tv;
        tv.TurnOn();
        tv.SetChannelName(3, "TV 3");
        WHEN("deleting the associated channel")
        {
            CHECK(tv.DeleteChannelName("TV 3"));
            THEN("the deleted channel can not be selected")
            {
                ExpectOperationFailure(tv, SelectChannel("TV 3"));
            }
        }
    }
}
alexey-malov commented 5 years ago
int CTVSet::ChannelNumberByName(string const& name, vector<channelInfo> const& channelInfo)
{
    for (auto currChannel = channelInfo.begin(); currChannel != channelInfo.end(); currChannel++)
    {
        if (currChannel->channelName == name)
        {
            return currChannel->channel;
        }
    }
    return 0;
}
alexey-malov commented 5 years ago
alexey-malov commented 5 years ago