summaryrefslogtreecommitdiffhomepage
path: root/tools/go_generics/tests/defs.bzl
blob: 6277c394765b3fee22415892f1b39455b6f40fe3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Generics tests."""

load("//tools/go_generics:defs.bzl", "go_template", "go_template_instance")

def _go_generics_test_impl(ctx):
    runner = ctx.actions.declare_file(ctx.label.name)
    runner_content = "\n".join([
        "#!/bin/bash",
        "exec diff --ignore-blank-lines --ignore-matching-lines=^[[:space:]]*// %s %s" % (
            ctx.files.template_output[0].short_path,
            ctx.files.expected_output[0].short_path,
        ),
        "",
    ])
    ctx.actions.write(runner, runner_content, is_executable = True)
    return [DefaultInfo(
        executable = runner,
        runfiles = ctx.runfiles(
            files = ctx.files.template_output + ctx.files.expected_output,
            collect_default = True,
            collect_data = True,
        ),
    )]

_go_generics_test = rule(
    implementation = _go_generics_test_impl,
    attrs = {
        "template_output": attr.label(mandatory = True, allow_single_file = True),
        "expected_output": attr.label(mandatory = True, allow_single_file = True),
    },
    test = True,
)

def go_generics_test(name, inputs, output, types = None, consts = None, **kwargs):
    """Instantiates a generics test.

    Args:
        name: the name of the test.
        inputs: all the input files.
        output: the output files.
        types: the template types (dictionary).
        consts: the template consts (dictionary).
        **kwargs: additional arguments for the template_instance.
    """
    if types == None:
        types = dict()
    if consts == None:
        consts = dict()
    go_template(
        name = name + "_template",
        srcs = inputs,
        types = types.keys(),
        consts = consts.keys(),
    )
    go_template_instance(
        name = name + "_output",
        template = ":" + name + "_template",
        out = name + "_output.go",
        types = types,
        consts = consts,
        **kwargs
    )
    _go_generics_test(
        name = name + "_test",
        template_output = name + "_output.go",
        expected_output = output,
    )