1.1 --- a/tests/test_parser.py Mon Jul 30 01:03:50 2018 +0200
1.2 +++ b/tests/test_parser.py Mon Jul 30 17:18:52 2018 +0200
1.3 @@ -1,8 +1,10 @@
1.4 #!/usr/bin/env python
1.5
1.6 -from os.path import abspath, exists, join, split
1.7 +from os.path import abspath, split
1.8 import sys
1.9
1.10 +# Locate and import the moinformat package.
1.11 +
1.12 dirname = split(abspath(sys.argv[0]))[0]
1.13 parent = split(dirname)[0]
1.14
1.15 @@ -12,17 +14,20 @@
1.16 if split(parent)[1] == "MoinLight":
1.17 sys.path.append(parent)
1.18
1.19 -from moinformat import make_serialiser, parse, serialise
1.20 +# Import specific objects.
1.21 +
1.22 +from moinformat import make_input, make_output, make_serialiser, parse, serialise
1.23 from moinformat.tree.moin import Container
1.24 -from glob import glob
1.25
1.26 def test_input(d, s):
1.27
1.28 "Compare serialised output from 'd' with its original form 's'."
1.29
1.30 - o = serialise(d)
1.31 + output = make_output("standalone")
1.32 + expected = output.encode(s)
1.33
1.34 - identical = o == s
1.35 + result = serialise(d, make_serialiser("moin", output))
1.36 + identical = result == expected
1.37
1.38 if quiet:
1.39 return identical
1.40 @@ -31,12 +36,12 @@
1.41
1.42 print identical
1.43 print "-" * 60
1.44 - print o
1.45 + print result
1.46 if not identical:
1.47 print "-" * 60
1.48 - print s
1.49 + print expected
1.50 print "-" * 60
1.51 - print serialise(d, make_serialiser("html"))
1.52 + print serialise(d, make_serialiser("html", output))
1.53 print "-" * 60
1.54 print
1.55
1.56 @@ -161,16 +166,6 @@
1.57
1.58 return branches[0]
1.59
1.60 -def readfile(filename):
1.61 -
1.62 - "Read the contents of 'filename' and return them."
1.63 -
1.64 - f = open(filename)
1.65 - try:
1.66 - return f.read()
1.67 - finally:
1.68 - f.close()
1.69 -
1.70 if __name__ == "__main__":
1.71 args = sys.argv[1:]
1.72
1.73 @@ -178,21 +173,46 @@
1.74 if quiet:
1.75 del args[args.index("-q")]
1.76
1.77 - filenames = args or glob(join(dirname, "test*.txt"))
1.78 + # Make an input context.
1.79 +
1.80 + input = make_input("directory", {"filename" : dirname})
1.81 +
1.82 + # Obtain input filenames.
1.83 +
1.84 + filenames = args or input.select_files("test*.txt*")
1.85 filenames.sort()
1.86
1.87 + # Process each filename, obtaining a corresponding tree definition.
1.88 +
1.89 for filename in filenames:
1.90 - tree_filename = "%s.tree" % filename.rsplit(".", 1)[0]
1.91 +
1.92 + # Test for an explicit encoding suffix.
1.93
1.94 - s = readfile(filename)
1.95 + t = filename.split(".")
1.96 + if len(t) > 2:
1.97 + text_filename = ".".join(t[:2])
1.98 + encoding = t[2]
1.99 + else:
1.100 + text_filename = filename
1.101 + encoding = None
1.102 +
1.103 + tree_filename = "%s.tree" % text_filename.rsplit(".", 1)[0]
1.104 +
1.105 + # Read and parse the input.
1.106 +
1.107 + s = input.readfile(text_filename, encoding)
1.108 d = parse(s)
1.109
1.110 - if exists(tree_filename):
1.111 - ts = readfile(tree_filename)
1.112 + # Read and parse any tree definition.
1.113 +
1.114 + if input.exists(tree_filename):
1.115 + ts = input.readfile(tree_filename)
1.116 t = parse_tree(ts)
1.117 else:
1.118 ts = None
1.119
1.120 + # Report the test results.
1.121 +
1.122 if not quiet:
1.123 print filename
1.124