1 /* $Id: makeimagedl-info-test.cc,v 1.7 2004/09/11 23:26:30 atterer Exp $ -*- C++ -*-
2   __   _
3   |_) /|  Copyright (C) 2003  |  richard@
4   | \/�|  Richard Atterer     |  atterer.net
5   � '` �
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License, version 2. See
8   the file COPYING for details.
9 
10   #test-deps job/makeimagedl-info.o net/uri.o
11 
12 */
13 
14 #include <config.h>
15 
16 #include <string>
17 
18 #include <debug.hh>
19 #include <log.hh>
20 #include <makeimagedl.hh>
21 #include <url-mapping.hh>
22 //______________________________________________________________________
23 
24 using namespace Job;
25 
MakeImageDl(const string & jigdoUri,const string & destination)26 MakeImageDl::MakeImageDl(/*IO* ioPtr,*/ const string& jigdoUri,
27                          const string& destination)
28     : io(/*ioPtr*/), stateVal(DOWNLOADING_JIGDO),
29       jigdoUrl(jigdoUri), childrenVal(), dest(destination),
30       tmpDirVal(), mi(),
31       imageNameVal(), imageInfoVal(), imageShortInfoVal(), templateUrls(0),
32       templateMd5Val(0) { }
~MakeImageDl()33 Job::MakeImageDl::~MakeImageDl() { }
34 
setImageSection(string * imageName,string * imageInfo,string * imageShortInfo,PartUrlMapping * templateUrls,MD5 ** templateMd5)35 void MakeImageDl::setImageSection(string* imageName, string* imageInfo,
36     string* imageShortInfo, PartUrlMapping* templateUrls, MD5** templateMd5) {
37   msg("setImageSection");
38   Paranoid(!haveImageSection());
39   imageNameVal.swap(*imageName);
40   imageInfoVal.swap(*imageInfo);
41   imageShortInfoVal.swap(*imageShortInfo);
42   this->templateUrls = templateUrls;
43   templateMd5Val = *templateMd5; *templateMd5 = 0;
44 
45   IOSOURCE_SEND(IO, io, makeImageDl_haveImageSection, ());
46 }
47 //======================================================================
48 
49 namespace {
50 
51   const char* const hexDigits = "0123456789abcdef";
escapedChar(string * o,byte c)52   void escapedChar(string* o, byte c) {
53     switch (c) {
54     case 0: *o += "\\0"; break;
55     case '\n': *o += "\\n"; break;
56     case '\t': *o += "\\t"; break;
57     case '"': case '\\': *o += '\\'; *o += c; break;
58     default:
59       if (c >= ' ' && c <= '~') {
60         *o += c;
61       } else {
62         *o += "\\x";
63         *o += hexDigits[unsigned(c) >> 4];
64         *o += hexDigits[unsigned(c) & 0xfU];
65       }
66     }
67   }
68 
escapedString(const string & s)69   inline string escapedString(const string& s) {
70     string result;
71     for (unsigned i = 0; i < s.length(); ++i)
72       escapedChar(&result, s[i]);
73     return result;
74   }
75 
76 }
77 //______________________________________________________________________
78 
testImageInfo(const char * subst[],bool escapedText,const char * text,const char * expected)79 string testImageInfo(const char* subst[], bool escapedText,
80                      const char* text, const char* expected) {
81   string imageShortInfo;
82   PartUrlMapping* templateUrls = 0;
83   MD5* templateMd5 = 0;
84 
85   MakeImageDl m("http://url/", "");
86   string imageName = "image.iso";
87   string imageInfo = text;
88   m.setImageSection(&imageName, &imageInfo, &imageShortInfo, templateUrls,
89                     &templateMd5);
90   string info = " ";
91   m.imageInfo(&info, escapedText, subst);
92   msg("\"%1\"", escapedString(info));
93   Assert(info == expected);
94   return info;
95 }
96 //______________________________________________________________________
97 
main(int argc,char * argv[])98 int main(int argc, char* argv[]) {
99   if (argc == 2) Logger::scanOptions(argv[1], argv[0]);
100 
101   const char* gtk[] = {
102     "<b>", "</b>", // <b>, </b>
103     "<i>", "</i>", // <i>, </i>
104     "<tt>", "</tt>", // <tt>, </tt>
105     "<u>", "</u>", // <u>, </u>
106     "<span size=\"large\">", "</span>", // <big>, </big>
107     "<span size=\"small\">", "</span>", // <small>, </small>
108     "\n" // <br/>
109   };
110   const char* tex[] = {
111     "\\textbf{", "}", // <b>, </b>
112     "\\textit{", "}", // <i>, </i>
113     "\\texttt{", "}", // <tt>, </tt>
114     "\\underline{", "}", // <u>, </u>
115     "\\large{", "}", // <big>, </big>
116     "\\small{", "}", // <small>, </small>
117     "\\\\" // <br/>
118   };
119 
120   testImageInfo(gtk, true,
121                 "'Sun, <16> \"Mar 2003 & 04:45:40 -0700'",
122                 " 'Sun, &lt;16&gt; \"Mar 2003 &amp; 04:45:40 -0700'");
123   testImageInfo(tex, false,
124                 "'Sun, <16> \"Mar 2003 & 04:45:40 -0700'",
125                 " 'Sun, <16> \"Mar 2003 & 04:45:40 -0700'");
126 
127   testImageInfo(gtk, true,
128                 "<b>Let</b> <big>him</big> <small>who</small><br/><br></br>"
129                 "<i>hath</i> <tt>understanding</tt> <u>reckon</u>",
130                 " <b>Let</b> <span size=\"large\">him</span> "
131                 "<span size=\"small\">who</span>\n\n<i>hath</i> "
132                 "<tt>understanding</tt> <u>reckon</u>");
133   testImageInfo(tex, false,
134                 "<b>Let</b> <big>him</big> <small>who</small><br/><br></br>"
135                 "<i>hath</i> <tt>understanding</tt> <u>reckon</u>",
136                 " \\textbf{Let} \\large{him} \\small{who}\\\\\\\\"
137                 "\\textit{hath} \\texttt{understanding} \\underline{reckon}");
138 
139   testImageInfo(gtk, true,
140                 "br must be empty <br> </br>",
141                 " br must be empty &lt;br&gt; &lt;/br&gt;");
142 
143   testImageInfo(tex, false,
144                 "Ugh, </x> does no good",
145                 " Ugh, </x> does no good");
146 
147   testImageInfo(gtk, true,
148                 "<b>Now <i>let <big>mee<br/>ee</big> entertain</i> you</b>",
149                 " <b>Now <i>let <span size=\"large\">mee\nee</span> "
150                 "entertain</i> you</b>");
151 
152   testImageInfo(tex, false,
153                 "<blubb>x</blubb>",
154                 " <blubb>x</blubb>");
155 
156   testImageInfo(gtk, true,
157                 "<b x=\"\">nobold</b>",
158                 " &lt;b x=\"\"&gt;nobold&lt;/b&gt;");
159 
160   testImageInfo(gtk, true,
161                 "nocomment<!-- x -->",
162                 " nocomment&lt;!-- x --&gt;");
163 
164   /* CDATA sections are supported by glib. However, our parseComment()
165      creates an error if it sees one, because glib doesn't strip the
166      "<![CDATA[" and "]]>" strings - not useful! */
167   testImageInfo(gtk, true,
168                 "<b><![CDATA[This <i>is</i> & quoted]]></b>",
169                 " &lt;b&gt;&lt;![CDATA[This &lt;i&gt;is&lt;/i&gt; &amp; "
170                 "quoted]]&gt;&lt;/b&gt;");
171   // " <b>This &lt;i&gt;is&lt;/i&gt; &amp; quoted</b>"
172 
173   return 0;
174 }
175