#!/usr/bin/env python
"""Test suite for mediawiki->dokuwiki conversions.

Goes through every subdirectory of the tests/ directory. Each subdirectory contains:

* mediawiki.txt -> snippet of Mediawiki syntax to be converted.
* dokuwiki.txt -> correct Dokuwiki output to be expected.
* notes.txt -> (optional) file describing what's special about this test.

Converts mediawiki.txt and compares output to dokuwiki.txt, prints an
error (and contents of notes.txt) if the output does not match.

Copyright (C) 2014 Angus Gratton
Licensed under New BSD License as described in the file LICENSE.

"""
from __future__ import print_function, unicode_literals, absolute_import, division
import sys, os, codecs, inspect, traceback
from pprint import pprint
import wikicontent, yamdwe

DELIMITER="*"*40

def run_test(testdir):
    """
    Run the test contained in the directory 'testdir'

    Return True on success
    """
    print("Running %s..." % testdir)
    mw = _readfile(testdir, "mediawiki.txt").strip() # ignore leading/trailing whitespace, too annoying
    dw = _readfile(testdir, "dokuwiki.txt").strip() # ignore leading/trailing whitespace, too annoying
    notes = _readfile(testdir, "notes.txt")

    # use directory name as the page name
    pagename = os.path.split(testdir)[1]

    if len(mw) == 0:
        print("WARNING: No mediawiki input!!!")

    try:
        converted = wikicontent.convert_pagecontent(pagename, mw).strip()
        if converted == dw:
            return True
    except:
        print("CONVERSION ERROR")
        traceback.print_exc()
        print(DELIMITER)
        if len(notes):
            print("Test notes:")
            print(notes)
        return False

    print("OUTPUT MISMATCH")
    if len(notes):
        print("Test notes:")
        print(notes)
    print(DELIMITER)
    print("Input Mediawiki:")
    print(mw)
    print(DELIMITER)
    print("Expected Output:")
    print(DELIMITER)
    print(dw)
    print(DELIMITER)
    print("Actual Output:")
    print(converted)
    print(DELIMITER)
    return False

def run_all_tests():
    """
    Run all tests. Return True on success.
    """
    successes = 0
    testsrun = 0
    execdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
    testsdir = os.path.join(execdir, "tests")
    for test in os.listdir(testsdir):
        path = os.path.join(testsdir, test)
        if os.path.isdir(path):
            testsrun += 1
            if run_test(path):
                successes += 1
    print("--- %d/%d TESTS PASSED ---" % (successes, testsrun))
    return successes == testsrun

def _readfile(dirpath, filename):
    """
    Read a complete file and return content as a unicode string, or
    empty string if file not found
    """
    try:
        with codecs.open(os.path.join(dirpath, filename), "r", "utf-8") as f:
            return f.read()
    except IOError:
        return u""

if __name__ == "__main__":
    yamdwe.enable_unicode_output()
    if run_all_tests():
        sys.exit(0)
    else:
        sys.exit(1)

@@ -2055,118 +2055,70 @@
 def 
-run_all_tests():%0A    %22%22%22%0A    Run all tests. Return True on success.%0A    %22%22%22%0A    successes = 0%0A    testsrun = 0
+tests_dirpath():%0A    %22%22%22 Return path to the test directory %22%22%22
 %0A   
@@ -2210,18 +2210,14 @@
     
-testsdir =
+return
  os.
@@ -2243,16 +2243,163 @@
 %22tests%22)
+%0A%0Adef run_all_tests():%0A    %22%22%22%0A    Run all tests. Return True on success.%0A    %22%22%22%0A    successes = 0%0A    testsrun = 0%0A    testsdir = tests_dirpath()
 %0A    for
@@ -3061,26 +3061,433 @@
 utput()%0A    
-if
+try:%0A        if sys.argv%5B1%5D in %5B%22-h%22, %22--help%22%5D:%0A            print(%22Usage: %25s %3Coptional test name%3E%22 %25 (sys.argv%5B0%5D))%0A            print(%22(If test name not specified, all tests in tests/ directory will be run.)%22)%0A            sys.exit(0)%0A    except IndexError:%0A        pass%0A%0A    try:%0A        res = run_test(os.path.join(tests_dirpath(), sys.argv%5B1%5D))%0A    except IndexError: # no argv%5B1%5D, so run all%0A        res =
  run_all_tes
@@ -3486,24 +3486,35 @@
 _all_tests()
+%0A    if res
 :%0A        sy

Support running just one test from the suite, by supplying it on the command line