-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swift: add unit tests to code generation
Tests can be run with ``` bazel test //swift/codegen:tests ``` Coverage can be checked installing `pytest-cov` and running ``` pytest --cov=swift/codegen swift/codegen/test ```
- Loading branch information
Showing
19 changed files
with
1,008 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# this empty file adds the repo root to PYTHON_PATH when running pytest |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,31 @@ | ||
py_binary( | ||
name = "codegen", | ||
srcs = glob(["**/*.py"]), | ||
srcs = glob([ | ||
"lib/*.py", | ||
"*.py", | ||
]), | ||
) | ||
|
||
py_library( | ||
name = "test_utils", | ||
testonly = True, | ||
srcs = ["test/utils.py"], | ||
deps = [":codegen"], | ||
) | ||
|
||
[ | ||
py_test( | ||
name = src[len("test/"):-len(".py")], | ||
size = "small", | ||
srcs = [src], | ||
deps = [ | ||
":codegen", | ||
":test_utils", | ||
], | ||
) | ||
for src in glob(["test/test_*.py"]) | ||
] | ||
|
||
test_suite( | ||
name = "tests", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import pathlib | ||
from dataclasses import dataclass, field | ||
from typing import List, ClassVar | ||
|
||
import inflection | ||
|
||
|
||
@dataclass | ||
class QlParam: | ||
param: str | ||
type: str = None | ||
first: bool = False | ||
|
||
|
||
@dataclass | ||
class QlProperty: | ||
singular: str | ||
type: str | ||
tablename: str | ||
tableparams: List[QlParam] | ||
plural: str = None | ||
params: List[QlParam] = field(default_factory=list) | ||
first: bool = False | ||
local_var: str = "x" | ||
|
||
def __post_init__(self): | ||
if self.params: | ||
self.params[0].first = True | ||
while self.local_var in (p.param for p in self.params): | ||
self.local_var += "_" | ||
assert self.tableparams | ||
if self.type_is_class: | ||
self.tableparams = [x if x != "result" else self.local_var for x in self.tableparams] | ||
self.tableparams = [QlParam(x) for x in self.tableparams] | ||
self.tableparams[0].first = True | ||
|
||
@property | ||
def indefinite_article(self): | ||
if self.plural: | ||
return "An" if self.singular[0] in "AEIO" else "A" | ||
|
||
@property | ||
def type_is_class(self): | ||
return self.type[0].isupper() | ||
|
||
|
||
@dataclass | ||
class QlClass: | ||
template: ClassVar = 'ql_class' | ||
|
||
name: str | ||
bases: List[str] = field(default_factory=list) | ||
final: bool = False | ||
properties: List[QlProperty] = field(default_factory=list) | ||
dir: pathlib.Path = pathlib.Path() | ||
imports: List[str] = field(default_factory=list) | ||
|
||
def __post_init__(self): | ||
self.bases = sorted(self.bases) | ||
if self.properties: | ||
self.properties[0].first = True | ||
|
||
@property | ||
def db_id(self): | ||
return "@" + inflection.underscore(self.name) | ||
|
||
@property | ||
def root(self): | ||
return not self.bases | ||
|
||
@property | ||
def path(self): | ||
return self.dir / self.name | ||
|
||
|
||
@dataclass | ||
class QlStub: | ||
template: ClassVar = 'ql_stub' | ||
|
||
name: str | ||
base_import: str | ||
|
||
|
||
@dataclass | ||
class QlImportList: | ||
template: ClassVar = 'ql_imports' | ||
|
||
imports: List[str] = field(default_factory=list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
#10833