Yaffs site version 1.1
[yaffs-website] / vendor / psy / psysh / test / tools / gen_unvis_fixtures.py
1 #! /usr/bin/env python3
2 import sys
3 from os.path import abspath, expanduser, dirname, join
4 from itertools import chain
5 import json
6 import argparse
7
8 from vis import vis, unvis, VIS_WHITE
9
10
11 __dir__ = dirname(abspath(__file__))
12
13 OUTPUT_FILE = join(__dir__, '..', 'fixtures', 'unvis_fixtures.json')
14
15 # Add custom fixtures here
16 CUSTOM_FIXTURES = [
17     # test long multibyte string
18     ''.join(chr(cp) for cp in range(1024)),
19     'foo bar',
20     'foo\nbar',
21     "$bar = 'baz';",
22     r'$foo = "\x20\\x20\\\x20\\\\x20"',
23     '$foo = function($bar) use($baz) {\n\treturn $baz->getFoo()\n};'
24 ]
25
26 RANGES = {
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
31     'small': chain(
32         # latin blocks
33         range(0x0000, 0x0250),
34         # Greek, Cyrillic
35         range(0x0370, 0x0530),
36         # Hebrew, Arabic
37         range(0x590, 0x0700),
38         # CJK radicals
39         range(0x2E80, 0x2F00),
40         # Hiragana, Katakana
41         range(0x3040, 0x3100)
42     )
43 }
44
45
46 if __name__ == '__main__':
47
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 !
56                       """)
57     argp.add_argument('-r', '--range',
58                       help="""Choose the range of codepoints used to generate
59                       test data.""",
60                       choices=list(RANGES.keys()),
61                       default='small')
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()
66
67     cp_range = RANGES['bmp'] if args.all else RANGES[args.range]
68     indent = 2 if args.format_output else None
69     if args.output_file:
70         OUTPUT_FILE = abspath(expanduser(args.output_file))
71
72     fixtures = []
73
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:
77         char = chr(codepoint)
78         encoded = vis(char, VIS_WHITE)
79         decoded = unvis(encoded)
80         fixtures.append((encoded, decoded))
81
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))
88
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)
93
94     sys.exit(0)