view mde/file/mergetag/mdeUT.d @ 82:ac1e3fd07275

New ssi file format. (De)serializer now supports non-ascii wide characters (encoded to UTF-8) and no longer supports non-ascii 8-bit chars which would result in bad UTF-8. Moved/renamed a few things left over from the last commit.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 30 Aug 2008 09:37:35 +0100
parents d8fccaa45d5f
children 97e6dce08037
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 as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

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, see <http://www.gnu.org/licenses/>. */

/// This module provides a unittest for mergetag.
module mde.file.mergetag.mdeUT;

debug (mdeUnitTest) {
    import mde.file.mergetag.Reader;
    import mde.file.mergetag.Writer;
    import mde.file.mergetag.DataSet;
    import mde.file.mergetag.DefaultData;
    import mde.file.deserialize;
    import mde.file.serialize;
    
    import tango.io.FilePath;
    import tango.util.log.Log : Log, Logger;
    
    private Logger logger;
    static this() {
        logger = Log.getLogger ("mde.file.mergetag.mdeUT");
    }
    
    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 (FilePath (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`));
        
        // Delete the unittest file now
        FilePath (file~".mtt").remove;
        
        logger.info ("Unittest complete (for DefaultData).");
    }
}