2014-01-27 15:28:04 +11:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
|
|
"""
|
2020-12-28 20:15:16 +03:00
|
|
|
This script creates a pile of UI tests check that all the
|
2015-01-12 02:02:38 -08:00
|
|
|
derives have spans that point to the fields, rather than the
|
|
|
|
|
#[derive(...)] line.
|
2014-01-27 15:28:04 +11:00
|
|
|
|
|
|
|
|
sample usage: src/etc/generate-deriving-span-tests.py
|
|
|
|
|
"""
|
|
|
|
|
|
2020-02-08 00:02:11 -05:00
|
|
|
import os
|
|
|
|
|
import stat
|
2014-01-27 15:28:04 +11:00
|
|
|
|
|
|
|
|
TEST_DIR = os.path.abspath(
|
2024-12-04 23:02:25 +01:00
|
|
|
os.path.join(os.path.dirname(__file__), "../test/ui/derives/")
|
|
|
|
|
)
|
2014-01-27 15:28:04 +11:00
|
|
|
|
2019-02-08 09:48:12 +01:00
|
|
|
TEMPLATE = """\
|
2014-05-29 17:45:07 -07:00
|
|
|
// This file was auto-generated using 'src/etc/generate-deriving-span-tests.py'
|
2014-01-27 15:28:04 +11:00
|
|
|
|
|
|
|
|
{error_deriving}
|
|
|
|
|
struct Error;
|
|
|
|
|
{code}
|
|
|
|
|
fn main() {{}}
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
ENUM_STRING = """
|
2015-01-12 02:02:38 -08:00
|
|
|
#[derive({traits})]
|
2014-01-27 15:28:04 +11:00
|
|
|
enum Enum {{
|
|
|
|
|
A(
|
|
|
|
|
Error {errors}
|
|
|
|
|
)
|
|
|
|
|
}}
|
|
|
|
|
"""
|
|
|
|
|
ENUM_STRUCT_VARIANT_STRING = """
|
2015-01-12 02:02:38 -08:00
|
|
|
#[derive({traits})]
|
2014-01-27 15:28:04 +11:00
|
|
|
enum Enum {{
|
|
|
|
|
A {{
|
|
|
|
|
x: Error {errors}
|
|
|
|
|
}}
|
|
|
|
|
}}
|
|
|
|
|
"""
|
|
|
|
|
STRUCT_STRING = """
|
2015-01-12 02:02:38 -08:00
|
|
|
#[derive({traits})]
|
2014-01-27 15:28:04 +11:00
|
|
|
struct Struct {{
|
|
|
|
|
x: Error {errors}
|
|
|
|
|
}}
|
|
|
|
|
"""
|
|
|
|
|
STRUCT_TUPLE_STRING = """
|
2015-01-12 02:02:38 -08:00
|
|
|
#[derive({traits})]
|
2014-01-27 15:28:04 +11:00
|
|
|
struct Struct(
|
|
|
|
|
Error {errors}
|
|
|
|
|
);
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
ENUM_TUPLE, ENUM_STRUCT, STRUCT_FIELDS, STRUCT_TUPLE = range(4)
|
|
|
|
|
|
2020-02-08 00:02:11 -05:00
|
|
|
|
2017-10-25 15:02:26 +02:00
|
|
|
def create_test_case(type, trait, super_traits, error_count):
|
2024-12-04 23:02:25 +01:00
|
|
|
string = [
|
|
|
|
|
ENUM_STRING,
|
|
|
|
|
ENUM_STRUCT_VARIANT_STRING,
|
|
|
|
|
STRUCT_STRING,
|
|
|
|
|
STRUCT_TUPLE_STRING,
|
|
|
|
|
][type]
|
|
|
|
|
all_traits = ",".join([trait] + super_traits)
|
|
|
|
|
super_traits = ",".join(super_traits)
|
|
|
|
|
error_deriving = "#[derive(%s)]" % super_traits if super_traits else ""
|
|
|
|
|
|
|
|
|
|
errors = "\n".join("//~%s ERROR" % ("^" * n) for n in range(error_count))
|
2020-02-08 00:02:11 -05:00
|
|
|
code = string.format(traits=all_traits, errors=errors)
|
|
|
|
|
return TEMPLATE.format(error_deriving=error_deriving, code=code)
|
|
|
|
|
|
2014-01-27 15:28:04 +11:00
|
|
|
|
|
|
|
|
def write_file(name, string):
|
2024-12-04 23:02:25 +01:00
|
|
|
test_file = os.path.join(TEST_DIR, "derives-span-%s.rs" % name)
|
2014-01-27 15:28:04 +11:00
|
|
|
|
|
|
|
|
# set write permission if file exists, so it can be changed
|
|
|
|
|
if os.path.exists(test_file):
|
|
|
|
|
os.chmod(test_file, stat.S_IWUSR)
|
|
|
|
|
|
2024-12-04 23:02:25 +01:00
|
|
|
with open(test_file, "w") as f:
|
2014-01-27 15:28:04 +11:00
|
|
|
f.write(string)
|
|
|
|
|
|
|
|
|
|
# mark file read-only
|
2024-12-04 23:02:25 +01:00
|
|
|
os.chmod(test_file, stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH)
|
2014-01-27 15:28:04 +11:00
|
|
|
|
|
|
|
|
|
|
|
|
|
ENUM = 1
|
|
|
|
|
STRUCT = 2
|
|
|
|
|
ALL = STRUCT | ENUM
|
|
|
|
|
|
|
|
|
|
traits = {
|
2024-12-04 23:02:25 +01:00
|
|
|
"Default": (STRUCT, [], 1),
|
|
|
|
|
"FromPrimitive": (0, [], 0), # only works for C-like enums
|
|
|
|
|
"Decodable": (0, [], 0), # FIXME: quoting gives horrible spans
|
|
|
|
|
"Encodable": (0, [], 0), # FIXME: quoting gives horrible spans
|
2014-01-27 15:28:04 +11:00
|
|
|
}
|
|
|
|
|
|
2024-12-04 23:02:25 +01:00
|
|
|
for trait, supers, errs in [
|
|
|
|
|
("Clone", [], 1),
|
|
|
|
|
("PartialEq", [], 2),
|
|
|
|
|
("PartialOrd", ["PartialEq"], 1),
|
|
|
|
|
("Eq", ["PartialEq"], 1),
|
|
|
|
|
("Ord", ["Eq", "PartialOrd", "PartialEq"], 1),
|
|
|
|
|
("Debug", [], 1),
|
|
|
|
|
("Hash", [], 1),
|
|
|
|
|
]:
|
2014-01-27 15:28:04 +11:00
|
|
|
traits[trait] = (ALL, supers, errs)
|
|
|
|
|
|
2024-12-04 23:02:25 +01:00
|
|
|
for trait, (types, super_traits, error_count) in traits.items():
|
|
|
|
|
|
2023-06-10 12:06:17 -04:00
|
|
|
def mk(ty, t=trait, st=super_traits, ec=error_count):
|
|
|
|
|
return create_test_case(ty, t, st, ec)
|
|
|
|
|
|
2014-01-27 15:28:04 +11:00
|
|
|
if types & ENUM:
|
2024-12-04 23:02:25 +01:00
|
|
|
write_file(trait + "-enum", mk(ENUM_TUPLE))
|
|
|
|
|
write_file(trait + "-enum-struct-variant", mk(ENUM_STRUCT))
|
2014-01-27 15:28:04 +11:00
|
|
|
if types & STRUCT:
|
2024-12-04 23:02:25 +01:00
|
|
|
write_file(trait + "-struct", mk(STRUCT_FIELDS))
|
|
|
|
|
write_file(trait + "-tuple-struct", mk(STRUCT_TUPLE))
|