Skip to content

Instantly share code, notes, and snippets.

@donno
Created January 3, 2011 10:33
Show Gist options
  • Save donno/763333 to your computer and use it in GitHub Desktop.
Save donno/763333 to your computer and use it in GitHub Desktop.
Mineserver Test case using catch for Config class in Mineserver.
/*
Copyright (c) 2010, The Mineserver Project
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the The Mineserver Project nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
This expects config.cfg to be with the test program.
The config.cfg needs at least the following defined:
* system.server_name to "Mineserver alpha testserver"
* net.port should be 25565
* system.user_validation = false
* system.physics.enabled = true
* system.something= 13.37;
Consider getting the test runner to generate a test.cfg at the start.
*/
#include "catch/catch.hpp"
#include "../src/config.h"
TEST_CASE( "config/loading", "Can a config file be loaded" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
config.dump();
}
TEST_CASE( "config/loading", "Loading non-existant file" )
{
Config config;
REQUIRE( config.load("this-file_does_not_exist.cfg") == false );
}
TEST_CASE( "config/has=true", "")
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.has("system.server_name") == true );
}
TEST_CASE( "config/has=false", "" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.has("system.this_key_does_not_exist") == false );
}
TEST_CASE( "config/sData", "" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.sData("system.server_name") == std::string("Mineserver alpha testserver") );
}
TEST_CASE( "config/str2int", "Attempt to extract integer from a string" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.iData("system.server_name") == 0);
}
TEST_CASE( "config/str2double", "Attempt to extract double from a string" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.iData("system.server_name") == 0.0);
}
TEST_CASE( "config/str2float", "Attempt to extract float from a string" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.iData("system.server_name") == 0.0f);
}
TEST_CASE( "config/str2long", "Attempt to extract long from a string" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.iData("system.server_name") == 0L);
}
TEST_CASE( "config/str2bool", "Attempt to extract boolean from a string" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.bData("system.server_name") == false);
}
TEST_CASE( "config/boolean=true", "Read true value from boolean" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.has("system.physics.enabled") == true);
REQUIRE( config.bData("system.physics.enabled") == true);
}
TEST_CASE( "config/boolean=false", "Read false value from boolean" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.bData("system.user_validation") == false);
}
TEST_CASE( "config/port=25565", "Check integer value" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.iData("net.port") == 25565);
}
TEST_CASE( "config/port=25565", "Check integer as float value" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.fData("net.port") == 25565.0f);
}
TEST_CASE( "config/port=25565", "Check integer as double value" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.dData("net.port") == 25565.0);
}
TEST_CASE( "config/port=25565", "Check integer as long value" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.lData("net.port") == 25565L);
}
TEST_CASE( "config/something=13.37", "Check double value" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.dData("system.something") == 13.37);
}
TEST_CASE( "config/something=13.37", "Check double as int value" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.iData("system.something") == 13);
}
TEST_CASE( "config/something=13.37", "Check double as long value" )
{
Config config;
REQUIRE( config.load("config.cfg") == true );
REQUIRE( config.lData("system.something") == 13L);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment