File: test_cpp_codegen.py

package info (click to toggle)
python-schema-salad 8.9.20251102115403-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,060 kB
  • sloc: python: 19,247; cpp: 2,631; cs: 1,869; java: 1,341; makefile: 187; xml: 184; sh: 103; javascript: 46
file content (126 lines) | stat: -rw-r--r-- 4,293 bytes parent folder | download | duplicates (2)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""Test C++ code generation."""

import filecmp
import os
from pathlib import Path
from typing import Any, Optional, cast

import pytest

from schema_salad import codegen
from schema_salad.avro.schema import Names
from schema_salad.schema import load_schema

from .util import cwl_file_uri, get_data_uri, get_path


def test_cwl_cpp_gen(tmp_path: Path) -> None:
    """End to end test of C++ generator using the CWL v1.0 schema."""
    src_target = tmp_path / "cwl_v1_0.h"
    cpp_codegen(cwl_file_uri, src_target)
    assert os.path.exists(src_target)


@pytest.mark.parametrize(
    "filename",
    [
        "01_single_record.yml",
        "02_two_records.yml",
        "03_simple_inheritance.yml",
        "04_abstract_inheritance.yml",
        "05_specialization.yml",
    ],
)
def test_cwl_cpp_generations(tmp_path: Path, filename: str) -> None:
    """End to end test of C++ generator using small scenarios."""

    file = get_path(f"cpp_tests/{filename}")

    # file with generated cpp output
    src_target = tmp_path / "test.h"
    # file with expected cpp output
    expected = file.with_suffix(".h")

    cpp_codegen("file://" + os.fspath(file), src_target)

    assert os.path.isfile(expected)
    assert os.path.isfile(src_target)
    assert filecmp.cmp(expected, src_target, shallow=False)


def test_cwl_cpp_generations_with_spdx(tmp_path: Path) -> None:
    """End to end test of C++ generator checking for SPDX headers"""

    src_target = tmp_path / "test.h"

    input_file_uri = get_data_uri("cpp_tests/01_single_record.yml")

    """Generating different combinations of license headers"""
    """Generate License Identifier"""
    cpp_codegen(input_file_uri, src_target, spdx_license_identifier="Apache-2.0")
    lines = open(src_target).readlines()[0:2]
    assert lines[0] == "// SPDX-License-Identifier: Apache-2.0\n"
    assert lines[1] == "#pragma once\n"

    """Generate single CopyrightText"""
    cpp_codegen(
        input_file_uri,
        src_target,
        spdx_copyright_text=["Copyright 2016 Some People <email@example.com>"],
    )
    lines = open(src_target).readlines()[0:2]
    assert lines[0] == "// SPDX-FileCopyrightText: Copyright 2016 Some People <email@example.com>\n"
    assert lines[1] == "#pragma once\n"

    """Generate two CopyrightText entries"""
    cpp_codegen(
        input_file_uri,
        src_target,
        spdx_copyright_text=[
            "Copyright 2016 Person A <person_a@example.com>",
            "Copyright 2017 Person B <person_b@example.com>",
        ],
    )
    lines = open(src_target).readlines()[0:3]
    assert lines[0] == "// SPDX-FileCopyrightText: Copyright 2016 Person A <person_a@example.com>\n"
    assert lines[1] == "// SPDX-FileCopyrightText: Copyright 2017 Person B <person_b@example.com>\n"
    assert lines[2] == "#pragma once\n"

    """Generate CopyrightText and License Identifier"""
    cpp_codegen(
        input_file_uri,
        src_target,
        spdx_license_identifier="Apache-2.0",
        spdx_copyright_text=[
            "Copyright 2016 Person A <person_a@example.com>",
            "Copyright 2017 Person B <person_b@example.com>",
        ],
    )
    lines = open(src_target).readlines()[0:4]

    assert lines[0] == "// SPDX-FileCopyrightText: Copyright 2016 Person A <person_a@example.com>\n"
    assert lines[1] == "// SPDX-FileCopyrightText: Copyright 2017 Person B <person_b@example.com>\n"
    assert lines[2] == "// SPDX-License-Identifier: Apache-2.0\n"
    assert lines[3] == "#pragma once\n"


def cpp_codegen(
    file_uri: str,
    target: Path,
    spdx_copyright_text: Optional[list[str]] = None,
    spdx_license_identifier: Optional[str] = None,
) -> None:
    """Help using the C++ code generation function."""
    document_loader, avsc_names, schema_metadata, metaschema_loader = load_schema(file_uri)
    assert isinstance(avsc_names, Names)
    schema_raw_doc = metaschema_loader.fetch(file_uri)
    schema_doc, schema_metadata = metaschema_loader.resolve_all(schema_raw_doc, file_uri)
    codegen.codegen(
        "cpp",
        cast(list[dict[str, Any]], schema_doc),
        schema_metadata,
        document_loader,
        target=str(target),
        spdx_copyright_text=spdx_copyright_text,
        spdx_license_identifier=spdx_license_identifier,
    )