home *** CD-ROM | disk | FTP | other *** search
- //
- // $Id: TemplateTranslatorTest.cc,v 1.2 2000/06/07 06:53:17 sergey Exp $
- //
-
- #include <Pilot.h>
- #include <stdio.h>
- #include "../Test.h"
-
- #include "DB/ResourceDatabase.h"
- #include "DB/RecordType.h"
- #include "Util/TemplateTranslator.h"
- #include "TestDataStream.h"
-
-
- namespace Util
- {
- struct Pair
- {
- const char* key;
- const char* value;
- };
-
- static Pair TransTable[] =
- {
- { "k1", "v1" },
- { "k2", "v2" },
- { "k3", "v3" },
- { "k4", "v4" },
- };
-
- class MyTranslationTable: public TemplateTranslator::TranslationTable
- {
- public:
- const char* get(const char* keyword) const
- {
- for (int i = 0; i < sizeof(TransTable)/sizeof(Pair); ++i)
- {
- if (StrCompare(keyword, TransTable[i].key) == 0)
- return TransTable[i].value;
- }
-
- return 0;
- }
- };
-
- // -------------------------------------------------------------------------
-
- class TemplateTranslatorTest: public Test
- {
- public:
- virtual const char* name() const { return "TemplateTranslatorTest"; }
-
- virtual void runTest()
- {
- const char* testText1 = "qwerty\n" "asdfgh\n" "\n" "zxcvbn\n";
- testTranslation(testText1, testText1);
-
- const char* testText2 =
- "qwe${}$rty\n"
- "as ${k2}$ dfg${h\n"
- "k2}$\n"
- "${k3 }$zxc${k1${k4}$vbn${k4}$\n"
- "${k1}$${k3}$\n";
-
- const char* expectedText2 =
- "qwe${}$rty\n"
- "as v2 dfg${h\n"
- "k2}$\n"
- "${k3 }$zxc${k1${k4}$vbnv4\n"
- "v1v3\n";
-
- testTranslation(testText2, expectedText2);
- }
-
- void testTranslation(const char* input, const char* expected)
- {
- TestDataStream out;
-
- MyTranslationTable translationTable;
- TemplateTranslator translator(translationTable);
-
- translator.translate(input, out);
-
- out._buffer[out._size] = 0;
- //printf("Buffer: %s\n", out._buffer);
-
- testAssert(StrCompare(expected, out._buffer) == 0);
- }
- };
- }
-
- Test& GetTemplateTranslatorTest()
- {
- static Util::TemplateTranslatorTest test;
- return test;
- }
-