Skip to content

Instantly share code, notes, and snippets.

@Groogy
Created May 25, 2013 21:52
Show Gist options
  • Save Groogy/5650899 to your computer and use it in GitHub Desktop.
Save Groogy/5650899 to your computer and use it in GitHub Desktop.
/* Copyright (C) 2013 Henrik Valter Vogelius Hansson ([email protected])
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it freely,
* subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <igloo/igloo.h>
#include <Tyr/ResourceHolder.hpp>
using namespace igloo;
class MockResource
{
public:
MockResource() : myData() {}
~MockResource() {}
const std::string& getData() const { return myData; }
void setData(const std::string& data) { myData = data; }
private:
std::string myData;
};
class MockHolder : public tyr::ResourceHolder<std::string, MockResource>
{
public:
MockHolder() {}
virtual ~MockHolder() {}
private:
virtual bool loadResource(MockResource& resource, const std::string& source)
{
resource.setData(source);
return true;
}
};
Context(ANewlyCreatedResourceHolder)
{
MockHolder Subject;
Spec(ShouldHaveNoResources)
{
AssertThat(Subject.getNumResources(), Equals(0u));
}
Spec(ShouldHaveOneResourceAfterLoad)
{
Subject.load("foo", "bar");
AssertThat(Subject.getNumResources(), Equals(1u));
AssertThat(Subject.get("foo").getData(), Equals("bar"));
}
};
Context(AFilledResourceHolder)
{
MockHolder Subject;
void SetUp()
{
Subject.load("1", "foo");
Subject.load("2", "bar");
Subject.load("3", "foobar");
}
Spec(ShouldHaveThreeResources)
{
AssertThat(Subject.getNumResources(), Equals(3u));
}
Spec(ShouldReceiveCorrectObjectsWithIdentifiers)
{
AssertThat(Subject.get("1").getData(), Equals("foo"));
AssertThat(Subject.get("2").getData(), Equals("bar"));
AssertThat(Subject.get("3").getData(), Equals("foobar"));
}
Spec(ShouldThrowExceptionWhenObjectNotFound)
{
AssertThrows(std::out_of_range, Subject.get("fail"));
}
Spec(ShouldThrowExceptionWhenIdentifierAlreadyUsed)
{
AssertThrows(std::logic_error, Subject.load("1", "fail"));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment