1 #! /usr/bin/env python3
3 from os.path import abspath, expanduser, dirname, join
4 from itertools import chain
8 from vis import vis, unvis, VIS_WHITE
11 __dir__ = dirname(abspath(__file__))
13 OUTPUT_FILE = join(__dir__, '..', 'fixtures', 'unvis_fixtures.json')
15 # Add custom fixtures here
17 # test long multibyte string
18 ''.join(chr(cp) for cp in range(1024)),
22 r'$foo = "\x20\\x20\\\x20\\\\x20"',
23 '$foo = function($bar) use($baz) {\n\treturn $baz->getFoo()\n};'
27 # All valid codepoints in the BMP
28 'bmp': chain(range(0x0000, 0xD800), range(0xE000, 0xFFFF)),
29 # Smaller set of pertinent? codepoints inside BMP
30 # see: http://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane
33 range(0x0000, 0x0250),
35 range(0x0370, 0x0530),
39 range(0x2E80, 0x2F00),
46 if __name__ == '__main__':
48 argp = argparse.ArgumentParser(
49 description='Generates test data for Psy\\Test\\Util\\StrTest')
50 argp.add_argument('-f', '--format-output', action='store_true',
51 help='Indent JSON output to ease debugging')
52 argp.add_argument('-a', '--all', action='store_true',
53 help="""Generates test data for all codepoints of the BMP.
54 (same as --range=bmp). WARNING: You will need quite
55 a lot of RAM to run the testsuite !
57 argp.add_argument('-r', '--range',
58 help="""Choose the range of codepoints used to generate
60 choices=list(RANGES.keys()),
62 argp.add_argument('-o', '--output-file',
63 help="""Write test data to OUTPUT_FILE
64 (defaults to PSYSH_DIR/test/fixtures)""")
65 args = argp.parse_args()
67 cp_range = RANGES['bmp'] if args.all else RANGES[args.range]
68 indent = 2 if args.format_output else None
70 OUTPUT_FILE = abspath(expanduser(args.output_file))
74 # use SMALL_RANGE by default, it should be enough.
75 # use BMP_RANGE for a more complete smoke test
76 for codepoint in cp_range:
78 encoded = vis(char, VIS_WHITE)
79 decoded = unvis(encoded)
80 fixtures.append((encoded, decoded))
82 # Add our own custom fixtures at the end,
83 # since they would fail anyway if one of the previous did.
84 for fixture in CUSTOM_FIXTURES:
85 encoded = vis(fixture, VIS_WHITE)
86 decoded = unvis(encoded)
87 fixtures.append((encoded, decoded))
89 with open(OUTPUT_FILE, 'w') as fp:
90 # dump as json to avoid backslashin and quotin nightmare
91 # between php and python
92 json.dump(fixtures, fp, indent=indent)