Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / test / shaping / run-tests.py
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4 import sys, os, subprocess
5
6
7 def cmd(command):
8         p = subprocess.Popen (
9                 command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
10         p.wait ()
11         print (p.stderr.read (), end="") # file=sys.stderr
12         return p.stdout.read ().decode ("utf-8").strip (), p.returncode
13
14
15 args = sys.argv[1:]
16 if not args or sys.argv[1].find('hb-shape') == -1 or not os.path.exists (sys.argv[1]):
17         print ("""First argument does not seem to point to usable hb-shape.""")
18         sys.exit (1)
19 hb_shape, args = args[0], args[1:]
20
21 extra_options = "--verify"
22
23 fails = 0
24
25 reference = False
26 if len (args) and args[0] == "--reference":
27         reference = True
28         args = args[1:]
29
30 if not len (args):
31         args = ['-']
32
33 for filename in args:
34         if not reference:
35                 if filename == '-':
36                         print ("Running tests from standard input")
37                 else:
38                         print ("Running tests in " + filename)
39
40         if filename == '-':
41                 f = sys.stdin
42         else:
43                 f = open (filename)
44
45         for line in f:
46                 fontfile, options, unicodes, glyphs_expected = line.split (":")
47                 cwd = os.path.dirname(filename)
48                 fontfile = os.path.normpath (os.path.join (cwd, fontfile))
49
50                 if line.startswith ("#"):
51                         if not reference:
52                                 print ("# %s %s --unicodes %s" % (hb_shape, fontfile, unicodes))
53                         continue
54
55                 if not reference:
56                         print ("%s %s %s %s --unicodes %s" %
57                                          (hb_shape, fontfile, extra_options, options, unicodes))
58
59                 glyphs1, returncode = cmd ([hb_shape, "--font-funcs=ft",
60                         fontfile, extra_options, "--unicodes",
61                         unicodes] + (options.split (' ') if options else []))
62
63                 if returncode:
64                         print ("hb-shape --font-funcs=ft failed.") # file=sys.stderr
65                         fails = fails + 1
66                         #continue
67
68                 glyphs2, returncode = cmd ([hb_shape, "--font-funcs=ot",
69                         fontfile, extra_options, "--unicodes",
70                         unicodes] + (options.split (' ') if options else []))
71
72                 if returncode:
73                         print ("ERROR: hb-shape --font-funcs=ot failed.") # file=sys.stderr
74                         fails = fails + 1
75                         #continue
76
77                 if glyphs1 != glyphs2:
78                         print ("FT funcs: " + glyphs1) # file=sys.stderr
79                         print ("OT funcs: " + glyphs2) # file=sys.stderr
80                         fails = fails + 1
81
82                 if reference:
83                         print (":".join ([fontfile, options, unicodes, glyphs1]))
84                         continue
85
86                 if glyphs1.strip() != glyphs_expected.strip():
87                         print ("Actual:   " + glyphs1) # file=sys.stderr
88                         print ("Expected: " + glyphs_expected) # file=sys.stderr
89                         fails = fails + 1
90
91 if fails != 0:
92         if not reference:
93                 print (str (fails) + " tests failed.") # file=sys.stderr
94         sys.exit (1)
95
96 else:
97         if not reference:
98                 print ("All tests passed.")