/*****************************************************************************
 * $LastChangedDate: 2011-04-23 21:07:07 -0400 (Sat, 23 Apr 2011) $
 * @file
 * @author  Jim E. Brooks  http://www.palomino3d.org
 * @brief   Test of ConfTxt class.
 *//*
 * LEGAL:   COPYRIGHT (C) 2008 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
 *****************************************************************************/

#define TESTING 1
#include "test.hh"
#include "base/module.hh"
#include "base/conf_txt.hh"
#include "base/file.hh"
#include <fstream>
using namespace base;

INTERN const string FNAME = "/tmp/base_conf_txt_test.txt";

class ConfTxtTest : public ConfTxt
{
public:
    ConfTxtTest( void )
    :   mLineCount(0),
        mWindowWidth(),
        mWindowHeight()
    {
    }

private:
    virtual void ProcessField( string& field, std::ifstream& config )
    {
        if ( field == "WindowWidth" )
        {
            config >> mWindowWidth;
        }
        else if ( field == "WindowHeight" )
        {
            config >> mWindowHeight;
        }
        else
        {
            Fail();
        }
    }

public:
    int     mLineCount;
    string  mWindowWidth;   // "800"
    string  mWindowHeight;  // "600"
};

/*****************************************************************************
 * Test.
 *****************************************************************************/
bool
Test1( void )
{
    // ------------------------------------------------------------------------
    // First series of tests are for when no configuration file exists yet.
    // ------------------------------------------------------------------------

    RemoveFile( FNAME );  // ensure configuration file doesn't exist

    ConfTxtTest conf;
    bool exception = false;

    // Test that the error of empty filename causes throwing an exception.
    exception = false;
    try
    {
        conf.Read( "" );
    }
    catch (...)
    {
        // ok
        exception = true;
    }
    if ( not exception )
    {
        CDEBUG << "FAIL: empty filename \n";
        return Fail();
    }

    return true;
}

/*****************************************************************************
 * Test reading/parsing lines of textual configuration.
 *****************************************************************************/
bool
Test2( void )
{
    RemoveFile( FNAME );  // ensure configuration file doesn't exist

    // Create a data file.
    std::fstream fs;
//  fs.open( FNAME.c_str(), std::ios::in | std::ios::out | std::ios::binary );  // wrong
    fs.open( FNAME.c_str(), std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc );
    fs << "WindowWidth   800" << std::endl
       << "WindowHeight  600" << std::endl;
    if ( (not fs.good()) or (not fs.is_open()) )
    {
        return Fail();
    }

    // Read data file.
    ConfTxtTest conf;
    try
    {
        conf.Read( FNAME );
    }
    catch (...)
    {
        return Fail();
    }

    if ( conf.mWindowWidth == "800"
     and conf.mWindowHeight == "600" )
    {
        return true;
    }
    else
    {
        return Fail();
    }
}

/*****************************************************************************
 * Called if all other tests passed else no cleanup.
 *****************************************************************************/
bool
Cleanup( void )
{
    RemoveFile( FNAME );
    return true;
}

/*****************************************************************************
 * main.
 *****************************************************************************/
int
main( int argc, char** argv )
{
    RUN_TEST( "ConfTxt", TEST_ERROR_MODE_CATCH,  // this test expects an exception
              Test1()
          and Test2()
          and Cleanup() )
}
