libyui-gtk  2.44.9
test.cc
1 /********************************************************************
2  * YaST2-GTK - http://en.opensuse.org/YaST2-GTK *
3  ********************************************************************/
4 
5 #define YUILogComponent "gtk"
6 #include <yui/Libyui_config.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <YUI.h>
10 #include <gtk/gtk.h>
11 #include "YGUtils.h"
12 
13 bool testMapKBAccel()
14 {
15  fprintf (stderr, "Test map KB accels\t");
16  struct {
17  const char *in;
18  const char *out;
19  } aTests[] = {
20  { "Foo&", "Foo_" },
21  { "B&aa", "B_aa" },
22  { "&Foo", "_Foo" },
23  { "_Foo", "__Foo" },
24 // FIXME - unfortunately it seems yast has noescaping policy
25 // for labels (hmm) at least judging from the code at
26 // ncurses/src/NCstring.cc (getHotKey)
27 // { "&&Foo", "&Foo" },
28  { NULL, NULL }
29  };
30  for (int i = 0; aTests[i].in; i++) {
31  std::string mapped = YGUtils::mapKBAccel(aTests[i].in);
32  if (mapped != aTests[i].out) {
33  fprintf (stderr, "Mis-mapped accel '%s' vs '%s'\n",
34  mapped.c_str(), aTests[i].out);
35  return false;
36  }
37  fprintf (stderr, "%d ", i);
38  }
39  fprintf (stderr, "\n");
40  return true;
41 }
42 
43 #include "ygtkrichtext.h"
44 
45 bool testParse(const char *xml)
46 {
47  GError *error = NULL;
48  GMarkupParser parser = { 0, };
49  GMarkupParseContext *ctx = g_markup_parse_context_new (&parser, GMarkupParseFlags (0), NULL, NULL);
50 
51  if (!g_markup_parse_context_parse (ctx, xml, -1, &error))
52  {
53  fprintf (stderr, "Invalid XML: '%s'\n '%s'\n", xml, error->message);
54  return false;
55  }
56  g_markup_parse_context_free (ctx);
57 
58  return true;
59 }
60 
61 bool testXHtmlConvert()
62 {
63  fprintf (stderr, "Test HTML->XML rewrite \t");
64  struct {
65  const char *in;
66  const char *out;
67  } aTests[] = {
68  // preservation
69  { "<p>foo</p>", "<body><p>foo</p></body>" },
70  // outer tag
71  { "some text", "<body>some text</body>" },
72  // unquoted attributes
73  { "<foo baa=Baz></foo>", "<body><foo baa=\"Baz\"></foo></body>" },
74  // break tags
75  { "<br>", "<body><br/></body>" },
76  { "<hr>", "<body><hr/></body>" },
77  // close with different case ...
78  { "<p>Foo</P>", "<body><p>Foo</p></body>" },
79  // unclosed tags
80  { "<p>", "<body><p></p></body>" },
81  { "<b>unclosed", "<body><b>unclosed</b></body>" },
82  { "<b><i>bold</i>", "<body><b><i>bold</i></b></body>" },
83  { "<i><i>bold</i>", "<body><i><i>bold</i></i></body>" },
84  { "<unclosed foo=baa>",
85  "<body><unclosed foo=\"baa\"></unclosed></body>" },
86  // unclosed 'early close' tags
87  { "<i>Some<p>text<p> here<p></i>",
88  "<body><i>Some<p>text</p><p> here</p><p></p></i></body>" },
89  { "<ul><li>foo<li>baa",
90  "<body><ul><li>foo</li><li>baa</li></ul></body>" },
91  { "no outer<p>unclosed<p>several<b>unclosed bold",
92  "<body>no outer<p>unclosed</p><p>several<b>unclosed bold</b></p></body>" },
93  // comment
94  { "we need <b>to <!-- really need to? --> do something</b> about it.",
95  "<body>we need <b>to do something</b> about it.</body>" },
96  { "&amp;", "<body>&amp;</body>" },
97  { "&amp", "<body>&amp;amp</body>" },
98  { "&amp; foo", "<body>&amp; foo</body>" },
99  { "&amp foo", "<body>&amp;amp foo</body>" },
100  { "<pre>https://foo.com/regsvc-1.0/?lang=de-DE&guid=1529f49dc701449fbd854aebf7e40806&command=interactive</pre>\n",
101  "<body><pre>https://foo.com/regsvc-1.0/?lang=de-DE&amp;guid=1529f49dc701449fbd854aebf7e40806&amp;command=interactive</pre> </body>" },
102  { NULL, NULL }
103  };
104  for (int i = 0; aTests[i].in; i++) {
105  gchar *out = ygutils_convert_to_xhtml (aTests[i].in);
106  if (strcmp (out, aTests[i].out)) {
107  fprintf (stderr, "Mis-converted entry %d XML '%s' should be '%s'\n",
108  i, out, aTests[i].out);
109  return false;
110  }
111  if (!testParse (out))
112  return false;
113 
114  fprintf (stderr, "%d ", i);
115  g_free (out);
116  }
117  fprintf (stderr, "\n");
118  return true;
119 }
120 
121 bool testMarkupEscape()
122 {
123  fprintf (stderr, "Test markup escape\t");
124  struct {
125  const char *in;
126  const char *out;
127  bool escape_br;
128  } aTests[] = {
129  { "< text />", "&lt; text /&gt;" },
130  { "um\ndois\ntres", "um\ndois\ntres" },
131  { NULL, NULL }
132  };
133  for (int i = 0; aTests[i].in; i++) {
134  std::string out (YGUtils::escapeMarkup (aTests[i].in));
135  if (out != aTests[i].out) {
136  fprintf (stderr, "Mis-converted entry %d XML '%s' should be '%s'\n",
137  i, out.c_str(), aTests[i].out);
138  return false;
139  }
140  fprintf (stderr, "%d ", i);
141  }
142  fprintf (stderr, "\n");
143  return true;
144 }
145 
146 bool testTruncate()
147 {
148  fprintf (stderr, "Test truncate\t");
149  struct {
150  const char *in;
151  const char *out;
152  int length, pos;
153  } aTests[] = {
154  { "this-is-a-very-long-and-tedious-string", "this-is-a-very-lo...", 20, 1 },
155  { "this-is-a-very-long-and-tedious-string", "...nd-tedious-string", 20, -1 },
156  { "this-is-a-very-long-and-tedious-string", "this-is-a...s-string", 20, 0 },
157  { "this-is-a-very-long-and-tedious-string2", "this-is-...s-string2", 20, 0 },
158  { "abc", "abc", 3, 1 },
159  { "abcd", "...", 3, 1 },
160  { "abcd", "...", 3, -1 },
161  { "abcd", "...", 3, 0 },
162  { "abcdef", "a...", 4, 0 },
163  { "áéíóúçö", "áé...", 5, 1 },
164  { "áéíóúçö", "áéí...", 6, 1 },
165  { "áéíóúçö", "...çö", 5, -1 },
166  { "áéíóúçö", "...úçö", 6, -1 },
167  { "áéíóúçö", "á...ö", 5, 0 },
168  { "áéíóúçö", "á...çö", 6, 0 },
169  { "áéíóú", "áéíóú", 5, 0 },
170  { NULL, NULL }
171  };
172  for (int i = 0; aTests[i].in; i++) {
173  std::string out = YGUtils::truncate (aTests[i].in, aTests[i].length, aTests[i].pos);
174  if (out != aTests[i].out) {
175  fprintf (stderr, "Mis-converted entry %d truncate '%s' should be '%s'\n",
176  i, out.c_str(), aTests[i].out);
177  return false;
178  }
179  fprintf (stderr, "%d ", i);
180  }
181  fprintf (stderr, "\n");
182  return true;
183 }
184 
185 bool testHeaderize()
186 {
187  fprintf (stderr, "Test headerize\t");
188  struct {
189  const char *in;
190  const char *out;
191  gboolean cut;
192  } aTests[] = {
193  { "test", "test", FALSE },
194  { "<h1>Purpose</h1><p>This tool lets you install, remove, and update applications.</p><p>openSUSE's software management",
195  "This tool lets you install, remove, and update applications.", TRUE },
196  { "\n<p><big><b>Section List</b></big><br>\n<P>From <B>Other</B>,\nyou can manually edit the boot loader configuration files, clear the current\n"
197  "configuration and propose a new configuration, start from scratch, or reread\nthe configuration saved on your disk. If you have multiple Linux systems installed,",
198  "From Other, you can manually edit the boot loader configuration files, clear the current configuration and "
199  "propose a new configuration, start from scratch, or reread the configuration saved on your disk.", TRUE },
200  { "<p><big><b>Sound Cards</b><big></p><P>Select an unconfigured card from the list and press <B>Edit</B> to\nconfigure it. If the card was not detected, press <B>Add</B> and\nconfigure the card manually.</P>",
201  "Select an unconfigured card from the list and press Edit to configure it.", TRUE },
202  { "\n<p><b><big>Service Start</big></b><br>\nTo start the service every time your computer is booted, set\n"
203  "<b>Enable firewall</b>. Otherwise set <b>Disable firewall</b>.</p>\n<p><b><big>Switch On or Off</big></b><br>",
204  "To start the service every time your computer is booted, set Enable firewall.", TRUE },
205  { NULL, NULL }
206  };
207  for (int i = 0; aTests[i].in; i++) {
208  gboolean cut = FALSE;
209  char *out = ygutils_headerize_help (aTests[i].in, &cut);
210  if (strcmp (aTests[i].out, out)) {
211  fprintf (stderr, "Mis-headerized test %d:\n-- \n%s\n-- should be --\n%s\n-- xhtml --\n%s\n-- \n",
212  i, out, aTests[i].out, ygutils_convert_to_xhtml (aTests[i].in));
213  return false;
214  }
215  if (!!aTests[i].cut != !!cut)
216  fprintf (stderr, "Mis-labelled as cut (%d)\n", !!cut);
217  fprintf (stderr, "%d ", i);
218  }
219  fprintf (stderr, "\n");
220  return true;
221 }
222 
223 int main (int argc, char **argv)
224 {
225  bool bSuccess = true;
226 
227  bSuccess &= testMapKBAccel();
228  bSuccess &= testXHtmlConvert();
229  bSuccess &= testMarkupEscape();
230  bSuccess &= testTruncate();
231  bSuccess &= testHeaderize();
232 
233  return !bSuccess;
234 }
235