1#!/usr/bin/env python
2
3from __future__ import print_function
4import sys, os, subprocess
5
6
7try:
8	input = raw_input
9except NameError:
10	pass
11
12
13def cmd(command):
14	p = subprocess.Popen (
15		command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
16	p.wait ()
17	print (p.stderr.read (), file=sys.stderr)
18	return p.stdout.read ().decode ("utf-8"), p.returncode
19
20
21srcdir = os.environ.get ("srcdir", ".")
22builddir = os.environ.get ("builddir", ".")
23top_builddir = os.environ.get ("top_builddir",
24	os.path.normpath (os.path.join (os.getcwd (), "..", "..")))
25EXEEXT = os.environ.get ("EXEEXT", "")
26
27extra_options = "--verify"
28hb_shape = os.path.join (top_builddir, "util", "hb-shape" + EXEEXT)
29
30fails = 0
31args = sys.argv[1:]
32
33reference = False
34if len (args) and args[0] == "--reference":
35	reference = True
36	args = args[1:]
37
38if not len (args):
39	args = [sys.stdin]
40
41for f in args:
42	if not reference:
43		if f == sys.stdin:
44			print ("Running tests from standard input")
45		else:
46			print ("Running tests in " + f)
47
48	if f == sys.stdin:
49		def f():
50			while True:
51				try:
52					line = input ()
53				except EOFError:
54					break
55
56				if len (line):
57					yield line
58				else:
59					break
60		f = f()
61	else:
62		f = open (f)
63
64	for line in f:
65		fontfile, options, unicodes, glyphs_expected = line.split (":")
66
67		if line.startswith ("#"):
68			if not reference:
69				print ("# hb-shape %s --unicodes %s" % (fontfile, unicodes))
70			continue
71
72		if not reference:
73			print ("hb-shape %s %s %s --unicodes %s" %
74					 (fontfile, extra_options, options, unicodes))
75
76		glyphs1, returncode = cmd ([hb_shape, "--font-funcs=ft",
77			os.path.join (srcdir, fontfile), extra_options, "--unicodes",
78			unicodes] + (options.split (' ') if len(options) else []))
79
80		if returncode:
81			print ("hb-shape --font-funcs=ft failed.", file=sys.stderr)
82			fails = fails + 1
83			#continue
84
85		glyphs2, returncode = cmd ([hb_shape, "--font-funcs=ot",
86			os.path.join (srcdir, fontfile), extra_options, "--unicodes",
87			unicodes] + (options.split (' ') if len(options) else []))
88
89		if returncode:
90			print ("hb-shape --font-funcs=ot failed.", file=sys.stderr)
91			fails = fails + 1
92			#continue
93
94		if glyphs1 != glyphs2:
95			print ("FT funcs: " + glyphs1, file=sys.stderr)
96			print ("OT funcs: " + glyphs2, file=sys.stderr)
97			fails = fails + 1
98
99		if reference:
100			print (":".join ([fontfile, options, unicodes, glyphs1]))
101			continue
102
103		if glyphs1.strip() != glyphs_expected.strip():
104			print ("Actual:   " + glyphs1, file=sys.stderr)
105			print ("Expected: " + glyphs_expected, file=sys.stderr)
106			fails = fails + 1
107
108if fails != 0:
109	if not reference:
110		print (str (fails) + " tests failed.")
111	sys.exit (1)
112
113else:
114	if not reference:
115		print ("All tests passed.")
116