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