-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import os | ||
|
||
SPACES = ' ' * 4 | ||
|
||
|
||
class CodeGenerator: | ||
def __init__(self): | ||
self.lines = [] | ||
self.level = 0 | ||
|
||
def indent(self): | ||
self.level += 1 | ||
|
||
def undent(self): | ||
if self.level > 0: | ||
self.level -= 1 | ||
|
||
@property | ||
def indent_string(self): | ||
return self.level * SPACES | ||
|
||
def write(self, value: str, *args, **kwargs): | ||
value = self.indent_string + value | ||
if args or kwargs: | ||
value = value.format(*args, **kwargs) | ||
|
||
self.lines.append(value) | ||
|
||
def write_lines(self, lines): | ||
for line in lines: | ||
self.lines.append(self.indent_string + line) | ||
|
||
def __add__(self, value: str): | ||
self.write(value) | ||
|
||
def __str__(self): | ||
return os.linesep.join(self.lines) |
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,55 @@ | ||
import pytest | ||
from gql.utils_codegen import CodeGenerator | ||
|
||
|
||
def test_codegen_write_simple_strings(module_compiler): | ||
gen = CodeGenerator() | ||
gen.write('def sum(a, b):') | ||
gen.indent() | ||
gen.write('return a + b') | ||
|
||
code = str(gen) | ||
|
||
m = module_compiler(code) | ||
assert m.sum(2, 3) == 5 | ||
|
||
|
||
def test_codegen_write_template_strings_args(module_compiler): | ||
gen = CodeGenerator() | ||
gen.write('def {0}(a, b):', 'sum') | ||
gen.indent() | ||
gen.write('return a + b') | ||
|
||
code = str(gen) | ||
|
||
m = module_compiler(code) | ||
assert m.sum(2, 3) == 5 | ||
|
||
|
||
def test_codegen_write_template_strings_kwargs(module_compiler): | ||
gen = CodeGenerator() | ||
gen.write('def {method}(a, b):', method='sum') | ||
gen.indent() | ||
gen.write('return a + b') | ||
|
||
code = str(gen) | ||
|
||
m = module_compiler(code) | ||
assert m.sum(2, 3) == 5 | ||
|
||
|
||
def test_codegen_write_lines(module_compiler): | ||
lines = [ | ||
'@staticmethod', | ||
'def sum(a, b):' | ||
' return a + b' | ||
] | ||
gen = CodeGenerator() | ||
gen.write('class Math:') | ||
gen.indent() | ||
gen.write_lines(lines) | ||
|
||
code = str(gen) | ||
|
||
m = module_compiler(code) | ||
assert m.Math.sum(2, 3) == 5 |