]> git.lizzy.rs Git - rust.git/blob - src/etc/generate-deriving-span-tests.py
Auto merge of #29498 - wthrowe:replace-pattern, r=alexcrichton
[rust.git] / src / etc / generate-deriving-span-tests.py
1 #!/usr/bin/env python
2 #
3 # Copyright 2013 The Rust Project Developers. See the COPYRIGHT
4 # file at the top-level directory of this distribution and at
5 # http://rust-lang.org/COPYRIGHT.
6 #
7 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 # option. This file may not be copied, modified, or distributed
11 # except according to those terms.
12
13 """
14 This script creates a pile of compile-fail tests check that all the
15 derives have spans that point to the fields, rather than the
16 #[derive(...)] line.
17
18 sample usage: src/etc/generate-deriving-span-tests.py
19 """
20
21 import sys, os, datetime, stat
22
23 TEST_DIR = os.path.abspath(
24     os.path.join(os.path.dirname(__file__), '../test/compile-fail'))
25
26 YEAR = datetime.datetime.now().year
27
28 TEMPLATE = """// Copyright {year} The Rust Project Developers. See the COPYRIGHT
29 // file at the top-level directory of this distribution and at
30 // http://rust-lang.org/COPYRIGHT.
31 //
32 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
33 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
34 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
35 // option. This file may not be copied, modified, or distributed
36 // except according to those terms.
37
38 // This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
39
40 extern crate rand;
41
42 {error_deriving}
43 struct Error;
44 {code}
45 fn main() {{}}
46 """
47
48 ENUM_STRING = """
49 #[derive({traits})]
50 enum Enum {{
51    A(
52      Error {errors}
53      )
54 }}
55 """
56 ENUM_STRUCT_VARIANT_STRING = """
57 #[derive({traits})]
58 enum Enum {{
59    A {{
60      x: Error {errors}
61    }}
62 }}
63 """
64 STRUCT_STRING = """
65 #[derive({traits})]
66 struct Struct {{
67     x: Error {errors}
68 }}
69 """
70 STRUCT_TUPLE_STRING = """
71 #[derive({traits})]
72 struct Struct(
73     Error {errors}
74 );
75 """
76
77 ENUM_TUPLE, ENUM_STRUCT, STRUCT_FIELDS, STRUCT_TUPLE = range(4)
78
79 def create_test_case(type, trait, super_traits, number_of_errors):
80     string = [ENUM_STRING, ENUM_STRUCT_VARIANT_STRING, STRUCT_STRING, STRUCT_TUPLE_STRING][type]
81     all_traits = ','.join([trait] + super_traits)
82     super_traits = ','.join(super_traits)
83     error_deriving = '#[derive(%s)]' % super_traits if super_traits else ''
84
85     errors = '\n'.join('//~%s ERROR' % ('^' * n) for n in range(error_count))
86     code = string.format(traits = all_traits, errors = errors)
87     return TEMPLATE.format(year = YEAR, error_deriving=error_deriving, code = code)
88
89 def write_file(name, string):
90     test_file = os.path.join(TEST_DIR, 'derives-span-%s.rs' % name)
91
92     # set write permission if file exists, so it can be changed
93     if os.path.exists(test_file):
94         os.chmod(test_file, stat.S_IWUSR)
95
96     with open(test_file, 'wt') as f:
97         f.write(string)
98
99     # mark file read-only
100     os.chmod(test_file, stat.S_IRUSR|stat.S_IRGRP|stat.S_IROTH)
101
102
103
104 ENUM = 1
105 STRUCT = 2
106 ALL = STRUCT | ENUM
107
108 traits = {
109     'Zero': (STRUCT, [], 1),
110     'Default': (STRUCT, [], 1),
111     'FromPrimitive': (0, [], 0), # only works for C-like enums
112
113     'Decodable': (0, [], 0), # FIXME: quoting gives horrible spans
114     'Encodable': (0, [], 0), # FIXME: quoting gives horrible spans
115 }
116
117 for (trait, supers, errs) in [('Clone', [], 1),
118                               ('PartialEq', [], 2),
119                               ('PartialOrd', ['PartialEq'], 8),
120                               ('Eq', ['PartialEq'], 1),
121                               ('Ord', ['Eq', 'PartialOrd', 'PartialEq'], 1),
122                               ('Debug', [], 1),
123                               ('Hash', [], 1)]:
124     traits[trait] = (ALL, supers, errs)
125
126 for (trait, (types, super_traits, error_count)) in traits.items():
127     mk = lambda ty: create_test_case(ty, trait, super_traits, error_count)
128     if types & ENUM:
129         write_file(trait + '-enum', mk(ENUM_TUPLE))
130         write_file(trait + '-enum-struct-variant', mk(ENUM_STRUCT))
131     if types & STRUCT:
132         write_file(trait + '-struct', mk(STRUCT_FIELDS))
133         write_file(trait + '-tuple-struct', mk(STRUCT_TUPLE))