home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / printcar / printcar.exe / tests / Util / TemplateTranslatorTest.cc < prev    next >
C/C++ Source or Header  |  2000-06-07  |  2KB  |  97 lines

  1. //
  2. //  $Id: TemplateTranslatorTest.cc,v 1.2 2000/06/07 06:53:17 sergey Exp $
  3. //
  4.  
  5. #include <Pilot.h>
  6. #include <stdio.h>
  7. #include "../Test.h"
  8.  
  9. #include "DB/ResourceDatabase.h"
  10. #include "DB/RecordType.h"
  11. #include "Util/TemplateTranslator.h"
  12. #include "TestDataStream.h"
  13.  
  14.  
  15. namespace Util
  16. {
  17.     struct Pair
  18.     {
  19.         const char* key;
  20.         const char* value;
  21.     };
  22.  
  23.     static Pair TransTable[] =
  24.     {
  25.         { "k1", "v1" },
  26.         { "k2", "v2" },
  27.         { "k3", "v3" },
  28.         { "k4", "v4" },
  29.     };
  30.  
  31.     class MyTranslationTable: public TemplateTranslator::TranslationTable
  32.     {
  33.     public:
  34.         const char* get(const char* keyword) const
  35.         {
  36.             for (int i = 0; i < sizeof(TransTable)/sizeof(Pair); ++i)
  37.             {
  38.                 if (StrCompare(keyword, TransTable[i].key) == 0)
  39.                     return TransTable[i].value;
  40.             }
  41.  
  42.             return 0;
  43.         }
  44.     };
  45.  
  46.     // -------------------------------------------------------------------------
  47.  
  48.     class TemplateTranslatorTest: public Test
  49.     {
  50.     public:
  51.         virtual const char* name() const { return "TemplateTranslatorTest"; }
  52.  
  53.         virtual void runTest()
  54.         {
  55.             const char* testText1 = "qwerty\n" "asdfgh\n" "\n" "zxcvbn\n";
  56.             testTranslation(testText1, testText1);
  57.  
  58.             const char* testText2 =
  59.                 "qwe${}$rty\n"
  60.                 "as ${k2}$ dfg${h\n"
  61.                 "k2}$\n"
  62.                 "${k3 }$zxc${k1${k4}$vbn${k4}$\n"
  63.                 "${k1}$${k3}$\n";
  64.  
  65.             const char* expectedText2 =
  66.                 "qwe${}$rty\n"
  67.                 "as v2 dfg${h\n"
  68.                 "k2}$\n"
  69.                 "${k3 }$zxc${k1${k4}$vbnv4\n"
  70.                 "v1v3\n";
  71.  
  72.             testTranslation(testText2, expectedText2);
  73.         }
  74.  
  75.         void testTranslation(const char* input, const char* expected)
  76.         {
  77.             TestDataStream out;
  78.  
  79.             MyTranslationTable translationTable;
  80.             TemplateTranslator translator(translationTable);
  81.  
  82.             translator.translate(input, out);
  83.  
  84.             out._buffer[out._size] = 0;
  85.             //printf("Buffer: %s\n", out._buffer);
  86.  
  87.             testAssert(StrCompare(expected, out._buffer) == 0);
  88.         }
  89.     };
  90. }
  91.  
  92. Test& GetTemplateTranslatorTest()
  93. {
  94.     static Util::TemplateTranslatorTest test;
  95.     return test;
  96. }
  97.