4 This script creates a pile of compile-fail tests check that all the
5 derives have spans that point to the fields, rather than the
8 sample usage: src/etc/generate-deriving-span-tests.py
11 import os, datetime, stat, re
13 TEST_DIR = os.path.abspath(
14 os.path.join(os.path.dirname(__file__), '../test/ui/derives/'))
16 YEAR = datetime.datetime.now().year
19 // This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
35 ENUM_STRUCT_VARIANT_STRING = """
49 STRUCT_TUPLE_STRING = """
56 ENUM_TUPLE, ENUM_STRUCT, STRUCT_FIELDS, STRUCT_TUPLE = range(4)
58 def create_test_case(type, trait, super_traits, error_count):
59 string = [ENUM_STRING, ENUM_STRUCT_VARIANT_STRING, STRUCT_STRING, STRUCT_TUPLE_STRING][type]
60 all_traits = ','.join([trait] + super_traits)
61 super_traits = ','.join(super_traits)
62 error_deriving = '#[derive(%s)]' % super_traits if super_traits else ''
64 errors = '\n'.join('//~%s ERROR' % ('^' * n) for n in range(error_count))
65 code = string.format(traits = all_traits, errors = errors)
66 return TEMPLATE.format(year = YEAR, error_deriving=error_deriving, code = code)
68 def write_file(name, string):
69 test_file = os.path.join(TEST_DIR, 'derives-span-%s.rs' % name)
71 with open(test_file) as f:
73 old_str_ignoring_date = re.sub(r'^// Copyright \d+',
74 '// Copyright {year}'.format(year = YEAR), old_str)
75 if old_str_ignoring_date == string:
76 # if all we're doing is updating the copyright year, ignore it
79 # set write permission if file exists, so it can be changed
80 if os.path.exists(test_file):
81 os.chmod(test_file, stat.S_IWUSR)
83 with open(test_file, 'w') as f:
87 os.chmod(test_file, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)
97 'Default': (STRUCT, [], 1),
98 'FromPrimitive': (0, [], 0), # only works for C-like enums
100 'Decodable': (0, [], 0), # FIXME: quoting gives horrible spans
101 'Encodable': (0, [], 0), # FIXME: quoting gives horrible spans
104 for (trait, supers, errs) in [('Clone', [], 1),
105 ('PartialEq', [], 2),
106 ('PartialOrd', ['PartialEq'], 1),
107 ('Eq', ['PartialEq'], 1),
108 ('Ord', ['Eq', 'PartialOrd', 'PartialEq'], 1),
111 traits[trait] = (ALL, supers, errs)
115 for (trait, (types, super_traits, error_count)) in traits.items():
116 mk = lambda ty: create_test_case(ty, trait, super_traits, error_count)
118 files += write_file(trait + '-enum', mk(ENUM_TUPLE))
119 files += write_file(trait + '-enum-struct-variant', mk(ENUM_STRUCT))
121 files += write_file(trait + '-struct', mk(STRUCT_FIELDS))
122 files += write_file(trait + '-tuple-struct', mk(STRUCT_TUPLE))
124 print('Generated {files} deriving span test{}.'.format('s' if files != 1 else '', files = files))