Update Test to lazily parse generated content

This commit is contained in:
Richard Gibson 2021-07-27 13:08:39 -04:00 committed by Rick Waldron
parent 172d75bacd
commit 1925fa1a3b
2 changed files with 19 additions and 9 deletions

View File

@ -210,9 +210,10 @@ class Template:
return '\n'.join(lines) return '\n'.join(lines)
def expand(self, case_filename, case_name, case_values, encoding): def expand(self, case_filename, case_name, case_values, encoding):
assert encoding == 'utf-8'
def get_source():
frontmatter = self._frontmatter(case_filename, case_values) frontmatter = self._frontmatter(case_filename, case_values)
body = self.expand_regions(self.source, case_values) body = self.expand_regions(self.source, case_values)
return codecs.encode(frontmatter + '\n' + body, encoding)
assert encoding == 'utf-8'
return Test(self.attribs['meta']['path'] + case_name + '.js', return Test(self.attribs['meta']['path'] + case_name + '.js',
source=codecs.encode(frontmatter + '\n' + body, encoding)) dynamic_source=get_source)

View File

@ -6,17 +6,23 @@ import os, re
from .util.find_comments import find_comments from .util.find_comments import find_comments
from .util.parse_yaml import parse_yaml from .util.parse_yaml import parse_yaml
def after_parse(fn):
def wrapper(self, *args, **kwargs):
if self.source is None and self.dynamic_source is not None:
self.source = self.dynamic_source()
self._parse()
return fn(self, *args, **kwargs)
return wrapper
class Test: class Test:
"""Representation of a generated test. Specifies a file location which may """Representation of a generated test. Specifies a file location which may
or may not exist.""" or may not exist."""
def __init__(self, file_name, source=None): def __init__(self, file_name, dynamic_source=None):
self.file_name = file_name self.file_name = file_name
self.source = source self.dynamic_source = dynamic_source
self.source = None
self.attribs = dict(meta=None) self.attribs = dict(meta=None)
if self.source:
self._parse()
def load(self, prefix = None): def load(self, prefix = None):
location = os.path.join(prefix or '', self.file_name) location = os.path.join(prefix or '', self.file_name)
with open(location, 'r') as handle: with open(location, 'r') as handle:
@ -30,6 +36,7 @@ class Test:
self.attribs['meta'] = meta self.attribs['meta'] = meta
break break
@after_parse
def is_generated(self): def is_generated(self):
if not self.attribs['meta']: if not self.attribs['meta']:
return False return False
@ -40,6 +47,7 @@ class Test:
return 'generated' in flags return 'generated' in flags
@after_parse
def to_string(self): def to_string(self):
return '\n'.join([ return '\n'.join([
'/**', '/**',
@ -50,6 +58,7 @@ class Test:
self.source, self.source,
'\n']) '\n'])
@after_parse
def write(self, prefix, parents=False): def write(self, prefix, parents=False):
location = os.path.join(prefix, self.file_name) location = os.path.join(prefix, self.file_name)
path = os.path.dirname(location) path = os.path.dirname(location)