view mde/mergetag/mtunittest.d @ 17:5f90774ea1ef

Applied the GNU GPL v2 to mde. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 15 Mar 2008 15:14:25 +0000
parents 0047b364b6d9
children 611f7b9063c6
line wrap: on
line source

/* LICENSE BLOCK
Part of mde: a Modular D game-oriented Engine
Copyright © 2007-2008 Diggory Hardy

This program is free software; you can redistribute it and/or modify it under the terms of
the GNU General Public License, version 2, as published by the Free Software Foundation.

This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */

/// This module provides a unittest for mergetag.
module mde.mergetag.mtunittest;

import mde.mergetag.Reader;
import mde.mergetag.Writer;
import mde.mergetag.DataSet;
import mde.mergetag.DefaultData;

import tango.scrapple.text.convert.parseTo : parseTo;
import tango.scrapple.text.convert.parseFrom : parseFrom;

import tango.util.log.Log : Log, Logger;

debug (mdeUnitTest) {
    private Logger logger;
    static this() {
        logger = Log.getLogger ("mde.mergetag.mtunittest");
    }

    unittest {
        /* This does a basic write-out and read-in test for each type with its default value.
        * Thus it provides some basic testing for the whole mergetag package. */
        
        const file = "unittest";
        const ID UT_ID = cast (ID) "mdeUT";
        const headInfo = "mde Unit Test";
                
        DataSet dsW = new DataSet();
        
        dsW.header = new DefaultData();
        dsW.header._charA[UT_ID] = headInfo;
                
        DefaultData secW = new DefaultData();
        dsW.sec[UT_ID] = secW;
        
        static char[] genUTCode () {
            char[] ret;
            foreach (type; DefaultData.dataTypes) {
                ret ~= `secW.`~DefaultData.varName(type)~`[UT_ID] = (`~type~`).init;`;
            }
            return ret;
        }
        mixin (genUTCode());	// Add an entry to dd for each type
        
        IWriter w = makeWriter (file, dsW, WriterMethod.Both);
        w.write();
        
        // FIXME: when binary writing is supported, read both formats and check
        IReader r = makeReader (file~".mtt", null, true);
        r.read();
        
        DataSet dsR = r.dataset;
        assert (dsR !is null);
        
        assert (dsR.header !is null);
        char[]* p = UT_ID in dsW.header._charA;
        assert (p);
        assert (*p == headInfo);
                
        IDataSection* sec_p = (UT_ID in dsR.sec);
        assert (sec_p);
        DefaultData secR = cast(DefaultData) *sec_p;
        assert (secR !is null);
        
        // FIXME: when comparing associative arrays works, use that. In the mean-time, format!() should work.
        static char[] genCheckCode (char[] dd1, char[] dd2) {
            const char[] failureMsg = "Assertion failed for type; values: ";
            char[] ret;
            foreach (type; DefaultData.dataTypes) {
                char[] tName = DefaultData.varName(type);
                ret ~= `char[] `~tName~`Val1 = parseFrom!(`~type~`[char[]]) (cast(`~type~`[char[]]) `~dd1~`.`~tName~`);
char[] `~tName~`Val2 = parseFrom!(`~type~`[char[]]) (cast(`~type~`[char[]]) `~dd2~`.`~tName~`);
assert (`~tName~`Val1 == `~tName~`Val2, "Assertion failed for type `~type~`; values: "~`~tName~`Val1~", "~`~tName~`Val2 );
`;
            }
            return ret;
        }
        mixin (genCheckCode (`secW`,`secR`));
    
        logger.info ("Unittest complete (for DefaultData).");
    }
}