-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.py
More file actions
118 lines (95 loc) · 3.75 KB
/
utils.py
File metadata and controls
118 lines (95 loc) · 3.75 KB
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
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Testing Utilities."""
import pathlib
from typing import Dict
import pytest # pylint: disable=unused-import
import yaml
from babelcode import code_generator
from babelcode import languages
from babelcode import schema_parsing
from babelcode import utils
# Comment this out and replace with [LANG_NAME] to test a single language.
LANGS_TO_TEST = schema_parsing.LanguageSchemaSpecRegistry.list_languages()
# LANGS_TO_TEST = ['Python']
CODE_DIR = pathlib.Path(utils.FIXTURES_PATH, 'language_data')
# Define these shortcuts for ease of use.
DATA_STRUCTURES = [
'TYPE_NAME_1[]',
'list<list<TYPE_NAME_1>>',
'map<TYPE_NAME_1;TYPE_NAME_1>',
'set<TYPE_NAME_1>',
]
class LanguageSpec:
"""A testing specification for a language.
This helper class defines the different testing specs for setting up
automated testing for each language.
Attributes:
name: The name of the language.
lang_dir: The directory where this languages specific features are stored.
testing_spec: The testing specification of inputs and outputs.
func_template_path: The path to the template function to use for testing.
"""
def __init__(self, name: str):
"""Initializes the language spec.
Args:
name: The name of the language.
"""
self.name = name
self.lang_dir = CODE_DIR / self.name
self.testing_spec = yaml.load(
self.lang_dir.joinpath('spec.yaml').open(), yaml.Loader)
self.func_template_path = self.lang_dir / 'func_template.txt'
def __repr__(self) -> str:
"""Gets the name of the language."""
return self.name
def __getitem__(self, key: str) -> Dict[str, schema_parsing.SchemaValueType]:
"""Gets the testing specification for key."""
return self.testing_spec[key]
def get(
self, key: str, default_value: schema_parsing.SchemaValueType
) -> schema_parsing.SchemaValueType:
"""Gets the testing specification for key and with value."""
return self.testing_spec.get(key, default_value)
LANGUAGE_SPECS = {l: LanguageSpec(name=l) for l in LANGS_TO_TEST}
class BaseLanguageTestingClass:
"""Base class for language specific tests."""
def _setup_test(self, lang_name):
"""Performs setup for the language test.
Args:
lang_name: The name of the language.
"""
self.lang_spec = LANGUAGE_SPECS[lang_name]
self.lang_name = lang_name
try:
self.lang = languages.LanguageRegistry.get_language(lang_name)
self.literal_translator = self.lang.make_literal_translator()
except:
self.lang = None
self.literal_translator = None
try:
self.template_map = code_generator.load_template_map(
self.lang.make_template_map())
except:
self.template_map = None
self.schema_spec = schema_parsing.LanguageSchemaSpecRegistry.get_lang_spec(
self.lang_name)
def get_primitive_data(self, convert_type: str) -> Dict[str, str]:
"""Gets the primitive language specification for a generic."""
return self.lang_spec['primitives'][convert_type]
def parse_lang_schema(
self,
schema: schema_parsing.SchemaMapType) -> schema_parsing.SchemaMapType:
"""Parses the language types from the schema."""
return schema_parsing.parse_language_schema(self.schema_spec, schema)