README 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. Writing Tests
  2. =============
  3. Here's how to write tests.
  4. 1. Create a new file in the t directory, called foo.t. Start with
  5. the following lines:
  6. require 't/test.pl';
  7. package OddMuse;
  8. use Test::More tests => $num
  9. clear_pages();
  10. This will load the testing library, make all its functions
  11. available to you, announce that you plan to make $num tests, and
  12. clear all the pages from the test wiki.
  13. The test wiki will be created in /tmp/oddmuse.
  14. 2. The wiki is accessed via the command line only. You don't need to
  15. have your code installed on a webserver! Just run the test from
  16. the parent directory:
  17. perl t/foo.t
  18. 3. Write $num tests. :)
  19. 4. To examine the situation after having run some tests, you can
  20. also call the script from the command line. The only problem is
  21. that the tests use a specific data directory that you need to
  22. provide via an environment variable:
  23. WikiDataDir=test-data perl wiki.pl action=index raw=1
  24. add_module, remove_module, and remove_rule
  25. ------------------------------------------
  26. Load a module before you run any tests:
  27. add_module('usemod.pl');
  28. If the module has important setup code, you might have to add the
  29. following:
  30. InitVariables();
  31. The reason is this: If you add a module and the run the script
  32. again, you're fine. But if you run tests that don't invoke another
  33. copy of the script, then the init code will not have run.
  34. Modules and rules need rarely be removed, since every *.t file
  35. starts in a new process. If you then want to run additional tests
  36. without the module you added (in the same *.t file!), then remove
  37. both the module and the rules it added. You'll have to do this
  38. manually, unfortunately.
  39. remove_module('usemod.pl');
  40. remove_rule(\&UsemodRule);
  41. update_page
  42. -----------
  43. $page = update_page($pagename, $content);
  44. update_page($pagename, $content, $summary, $minor, $admin, @rest);
  45. @rest is a list of parameter=value string pairs:
  46. @rest = ('username=joe', 'ns=Moore');
  47. If updating the page resultet in a redirect, the redirect is stored
  48. in the variable $redirect, and you still get the result page
  49. returned.
  50. test_page($redirect, split('\n',<<'EOT'));
  51. banned text
  52. wiki administrator
  53. matched
  54. See .*BannedContent.* for more information
  55. EOT
  56. You can even create pages containing file uploads directly:
  57. $page = update_page('alex pic', "#FILE image/png\niVBORw0KGgoAAAA");
  58. get_page
  59. --------
  60. $page = get_page('action=calendar');
  61. $page = get_page('action=rc all=1 showedit=1');
  62. $page = get_page('action=rc', 'all=1', 'showedit=1');
  63. test_page(get_page('action=all'),
  64. 'restricted to administrators');
  65. Return the text of the page. The parameters are the parameters
  66. available to you from the command line when using the CGI library:
  67. keyword1 keyword2 keyword3
  68. keyword1+keyword2+keyword3
  69. name1=value1 name2=value2
  70. name1=value1&name2=value2
  71. "name1='I am a long value'" "name2=two\ words"
  72. /your/path/here?name1=value1&name2=value2
  73. run_tests
  74. ---------
  75. Takes a list of alternating input and output strings, applies rules
  76. (and thus @MyRules) to input and compares it to the output. If you
  77. have html attributes in the output you want to test, use
  78. xpath_run_tests, because the order of the attributes is not
  79. guaranteed and varies with CGI library version.
  80. run_tests(q{"Get lost!", they say.},
  81. q{&#x201c;Get lost!&#x201d;, they say.});
  82. run_tests(split('\n', <<'EOT'));
  83. input1
  84. output1
  85. input2
  86. output2
  87. EOT
  88. Newline excapes \n in the input and output are translated to real
  89. newlines when running the tests.
  90. run_tests(split('\n',<<'EOT'));
  91. * ''one\n** two
  92. <ul><li><em>one</em><ul><li>two</li></ul></li></ul>
  93. EOT
  94. test_page and test_page_negative
  95. --------------------------------
  96. Tests any string for regular expression matches:
  97. test_page($string, $regexp1, $regexp2, ...);
  98. test_page(update_page($pagename, $content), $re1, $re2);
  99. Or make sure that none of the regular expressions match:
  100. test_page_negative($page,
  101. "rollback",
  102. "Rollback",
  103. "EvilPage",
  104. "AnotherEvilPage",
  105. );
  106. xpath_run_tests
  107. ---------------
  108. This is the equivalent of run_tests using XPath instead of simple
  109. string comparison. It takes a list of alternating input and xpath
  110. tests, applies rules (and thus @MyRules) to the input and applies
  111. the test to the output.
  112. xpath_run_tests(split('\n',<<'EOT'));
  113. WikiWord
  114. //a[@class="edit"][@title="Click to edit this page"]
  115. This is a [:day for fun and laughter].
  116. //a[@class="anchor"][@name="day_for_fun_and_laughter"]
  117. EOT
  118. XPath is harder to write, but is ideal when the output contains tags
  119. with more than one attribute, since the order of attributes is
  120. undefined. And you don't even have to test for all the attributes.
  121. xpath_test and negative_xpath_test
  122. ----------------------------------
  123. The equivalent of test_page, but using xpath instead of exact
  124. matches.
  125. xpath_test(get_page('action=all pwd=foo'),
  126. '//p/a[@href="#HomePage"][text()="HomePage"]',
  127. '//h1/a[@name="foo"][text()="foo"]',
  128. '//a[@class="local"][@href="#bar"][text()="bar"]',
  129. '//h1/a[@name="bar"][text()="bar"]')
  130. And the same thing for negative matches, of course:
  131. negative_xpath_test($page, '//h1/a[not(text())]');
  132. run_macro_tests
  133. ---------------
  134. run_macro_tests(split('\n',<<'EOT'));
  135. $input1
  136. $output2
  137. $input2
  138. $output2
  139. EOT
  140. Tests @MyMacros.