Home | History | Annotate | Download | only in localize
      1 #include "XMLHandler.h"
      2 #include <stdio.h>
      3 #include <unistd.h>
      4 #include <fcntl.h>
      5 
      6 const char *const NS_MAP[] = {
      7     "xml", XMLNS_XMLNS,
      8     NULL, NULL
      9 };
     10 
     11 const XMLNamespaceMap NO_NAMESPACES(NS_MAP);
     12 
     13 char const*const EXPECTED_EXACT =
     14        "<ASDF>\n"
     15         "    <a id=\"system\" old-cl=\"1\" new-cl=\"43019\">\n"
     16         "        <app dir=\"apps/common\" />\n"
     17         "    </a>\n"
     18         "    <a id=\"samples\" old-cl=\"1\" new-cl=\"43019\">asdf\n"
     19         "        <app dir=\"samples/NotePad\" />\n"
     20         "        <app dir=\"samples/LunarLander\" />\n"
     21         "        <something>a<b>,</b>b </something>\n"
     22         "        <exact xml:space=\"preserve\">a<b>,</b>b </exact>\n"
     23         "    </a>\n"
     24         "</ASDF>\n";
     25 
     26 char const*const EXPECTED_PRETTY =
     27         "<ASDF>\n"
     28         "  <a id=\"system\"\n"
     29         "      old-cl=\"1\"\n"
     30         "      new-cl=\"43019\">\n"
     31         "    <app dir=\"apps/common\" />\n"
     32         "  </a>\n"
     33         "  <a id=\"samples\"\n"
     34         "      old-cl=\"1\"\n"
     35         "      new-cl=\"43019\">asdf\n"
     36         "    <app dir=\"samples/NotePad\" />\n"
     37         "    <app dir=\"samples/LunarLander\" />\n"
     38         "    <something>a\n"
     39         "      <b>,\n"
     40         "      </b>b \n"
     41         "    </something>\n"
     42         "    <exact xml:space=\"preserve\">a<b>,</b>b </exact>\n"
     43         "  </a>\n"
     44         "</ASDF>\n";
     45 
     46 static string
     47 read_file(const string& filename)
     48 {
     49     char buf[1024];
     50     int fd = open(filename.c_str(), O_RDONLY);
     51     if (fd < 0) {
     52         return "";
     53     }
     54     string result;
     55     while (true) {
     56         ssize_t len = read(fd, buf, sizeof(buf)-1);
     57         buf[len] = '\0';
     58         if (len <= 0) {
     59             break;
     60         }
     61         result.append(buf, len);
     62     }
     63     close(fd);
     64     return result;
     65 }
     66 
     67 static int
     68 ParseFile_EXACT_test()
     69 {
     70     XMLNode* root = NodeHandler::ParseFile("testdata/xml.xml", XMLNode::EXACT);
     71     if (root == NULL) {
     72         return 1;
     73     }
     74     string result = root->ToString(NO_NAMESPACES);
     75     delete root;
     76     //printf("[[%s]]\n", result.c_str());
     77     return result == EXPECTED_EXACT;
     78 }
     79 
     80 static int
     81 ParseFile_PRETTY_test()
     82 {
     83     XMLNode* root = NodeHandler::ParseFile("testdata/xml.xml", XMLNode::PRETTY);
     84     if (root == NULL) {
     85         return 1;
     86     }
     87     string result = root->ToString(NO_NAMESPACES);
     88     delete root;
     89     //printf("[[%s]]\n", result.c_str());
     90     return result == EXPECTED_PRETTY;
     91 }
     92 
     93 static int
     94 ParseString_EXACT_test()
     95 {
     96     string text = read_file("testdata/xml.xml");
     97     XMLNode* root = NodeHandler::ParseString("testdata/xml.xml", text, XMLNode::EXACT);
     98     if (root == NULL) {
     99         return 1;
    100     }
    101     string result = root->ToString(NO_NAMESPACES);
    102     delete root;
    103     //printf("[[%s]]\n", result.c_str());
    104     return result == EXPECTED_EXACT;
    105 }
    106 
    107 static int
    108 ParseString_PRETTY_test()
    109 {
    110     string text = read_file("testdata/xml.xml");
    111     XMLNode* root = NodeHandler::ParseString("testdata/xml.xml", text, XMLNode::PRETTY);
    112     if (root == NULL) {
    113         return 1;
    114     }
    115     string result = root->ToString(NO_NAMESPACES);
    116     delete root;
    117     //printf("[[%s]]\n", result.c_str());
    118     return result == EXPECTED_PRETTY;
    119 }
    120 
    121 int
    122 XMLHandler_test()
    123 {
    124     int err = 0;
    125     bool all = true;
    126 
    127     if (all) err |= ParseFile_EXACT_test();
    128     if (all) err |= ParseFile_PRETTY_test();
    129     if (all) err |= ParseString_EXACT_test();
    130     if (all) err |= ParseString_PRETTY_test();
    131 
    132     return err;
    133 }
    134