Quellcode durchsuchen

Tests refactored (#76).

main
offa vor 7 Jahren
Ursprung
Commit
420441285e
4 geänderte Dateien mit 90 neuen und 173 gelöschten Zeilen
  1. +85
    -0
      test/CallMocks.h
  2. +2
    -58
      test/ScopeExitTest.cpp
  3. +2
    -58
      test/ScopeFailTest.cpp
  4. +1
    -57
      test/ScopeSuccessTest.cpp

+ 85
- 0
test/CallMocks.h Datei anzeigen

@@ -0,0 +1,85 @@
/*
* Scope Guard
* Copyright (C) 2017 offa
*
* This file is part of Scope Guard.
*
* Scope Guard is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scope Guard is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scope Guard. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <trompeloeil.hpp>

namespace
{
struct CallMock
{
MAKE_MOCK0(deleter, void());
};


struct ThrowOnCopyMock
{
ThrowOnCopyMock() { }

ThrowOnCopyMock(const ThrowOnCopyMock&)
{
throw std::exception{};
}

MAKE_CONST_MOCK0(deleter, void());

void operator()() const
{
this->deleter();
}

ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
{
throw std::exception{};
}
};


struct NotNothrowMoveMock
{
NotNothrowMoveMock(CallMock* m) : m_mock(m) { }
NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock) { }
NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock) { }

void operator()() const
{
m_mock->deleter();
}

NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
{
throw "Not implemented";
}

NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
{
throw "Not implemented";
}

CallMock* m_mock;

};


}




+ 2
- 58
test/ScopeExitTest.cpp Datei anzeigen

@@ -19,71 +19,15 @@
*/

#include "scope_exit.h"
#include "CallMocks.h"
#include <catch.hpp>
#include <trompeloeil.hpp>

using namespace trompeloeil;

namespace
{
struct CallMock
{
MAKE_MOCK0(deleter, void());
};


struct ThrowOnCopyMock
{
ThrowOnCopyMock() { }

ThrowOnCopyMock(const ThrowOnCopyMock&)
{
throw std::exception{};
}

MAKE_CONST_MOCK0(deleter, void());

void operator()() const
{
this->deleter();
}

ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
{
throw std::exception{};
}
};


struct NotNothrowMoveMock
{
NotNothrowMoveMock(CallMock* m) : m_mock(m) { }
NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock) { }
NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock) { }

void operator()() const
{
m_mock->deleter();
}

NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
{
throw "Not implemented";
}

NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
{
throw "Not implemented";
}

CallMock* m_mock;

};


CallMock m;


void deleter()
{
m.deleter();
@@ -113,7 +57,7 @@ TEST_CASE("exit function called and rethrow on copy exception", "[ScopeExit]")
REQUIRE_CALL(noMove, deleter());

sr::scope_exit<decltype(noMove)> guard{noMove};
}());
}());
}

TEST_CASE("exit function is not called if released", "[ScopeExit]")

+ 2
- 58
test/ScopeFailTest.cpp Datei anzeigen

@@ -19,71 +19,15 @@
*/

#include "scope_fail.h"
#include "CallMocks.h"
#include <catch.hpp>
#include <trompeloeil.hpp>

using namespace trompeloeil;

namespace
{
struct CallMock
{
MAKE_MOCK0(deleter, void());
};


struct ThrowOnCopyMock
{
ThrowOnCopyMock() { }

ThrowOnCopyMock(const ThrowOnCopyMock&)
{
throw std::exception{};
}

MAKE_CONST_MOCK0(deleter, void());

void operator()() const
{
this->deleter();
}

ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
{
throw std::exception{};
}
};


struct NotNothrowMoveMock
{
NotNothrowMoveMock(CallMock* m) : m_mock(m) { }
NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock) { }
NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock) { }

void operator()() const
{
m_mock->deleter();
}

NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
{
throw "Not implemented";
}

NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
{
throw "Not implemented";
}

CallMock* m_mock;

};


CallMock m;


void deleter()
{
m.deleter();
@@ -113,7 +57,7 @@ TEST_CASE("exit function called and rethrow on copy exception", "[ScopeFail]")
REQUIRE_CALL(noMove, deleter());

sr::scope_fail<decltype(noMove)> guard{noMove};
}());
}());
}

TEST_CASE("exit function is not called if released", "[ScopeFail]")

+ 1
- 57
test/ScopeSuccessTest.cpp Datei anzeigen

@@ -19,71 +19,15 @@
*/

#include "scope_success.h"
#include "CallMocks.h"
#include <catch.hpp>
#include <trompeloeil.hpp>

using namespace trompeloeil;

namespace
{
struct CallMock
{
MAKE_MOCK0(deleter, void());
};


struct ThrowOnCopyMock
{
ThrowOnCopyMock() { }

ThrowOnCopyMock(const ThrowOnCopyMock&)
{
throw std::exception{};
}

MAKE_CONST_MOCK0(deleter, void());

void operator()() const
{
this->deleter();
}

ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
{
throw std::exception{};
}
};


struct NotNothrowMoveMock
{
NotNothrowMoveMock(CallMock* m) : m_mock(m) { }
NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock) { }
NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock) { }

void operator()() const
{
m_mock->deleter();
}

NotNothrowMoveMock& operator=(const NotNothrowMoveMock&)
{
throw "Not implemented";
}

NotNothrowMoveMock& operator=(NotNothrowMoveMock&&)
{
throw "Not implemented";
}

CallMock* m_mock;

};


CallMock m;


void deleter()
{
m.deleter();

Laden…
Abbrechen
Speichern