Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add erbb unit test target type feature #686

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build-system/build-system.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
# generators/vcvrack
'erbb/generators/vcvrack/project_template.gyp',
'erbb/generators/vcvrack/project.py',
'erbb/generators/vcvrack/test_unit_template.gyp',

# generators/daisy
'erbb/generators/daisy/Makefile_template',
Expand Down
39 changes: 39 additions & 0 deletions build-system/erbb/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def is_data (self): return isinstance (self, Data)
@property
def is_stream (self): return isinstance (self, Stream)

@property
def is_test (self): return isinstance (self, Test)

@property
def is_faust_address (self): return isinstance (self, FaustAddress)

Expand Down Expand Up @@ -207,6 +210,11 @@ def resources (self):
entities = [e for e in self.entities if e.is_resources]
return entities

@property
def tests (self):
entities = [e for e in self.entities if e.is_test]
return entities

@property
def bases (self):
entities = [e for e in self.entities if e.is_base]
Expand Down Expand Up @@ -492,6 +500,37 @@ def source_context_part (self, part):



# -- Test --------------------------------------------------------------------

class Test (Scope):
def __init__ (self, name_identifier):
assert isinstance (name_identifier, adapter.Identifier)
super (Test, self).__init__ ()
self.name_identifier = name_identifier

@staticmethod
def typename (): return 'test'

@property
def name (self): return self.name_identifier.name

@property
def source_context (self):
return self.source_context_part ('name')

def source_context_part (self, part):
if part == 'name':
return adapter.SourceContext.from_token (self.name_identifier)

return super (Test, self).source_context_part (part) # pragma: no cover

@property
def files (self):
entities = [e for e in self.entities if e.is_file]
return entities



# -- Faust -------------------------------------------------------------------

class Faust (Scope):
Expand Down
39 changes: 39 additions & 0 deletions build-system/erbb/generators/vcvrack/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def generate_module (self, path, module, strict):
template = self.replace_bases (template, module, module.bases, path);
template = self.replace_sources (template, module, module.sources, path)
template = self.replace_actions (template, module, path)
template = self.replace_tests (template, module, path)

with open (path_cpp, 'w', encoding='utf-8') as file:
file.write (template)
Expand Down Expand Up @@ -400,3 +401,41 @@ def replace_actions_data (self, module, path):
lines += ' },\n'

return lines


#--------------------------------------------------------------------------

def replace_tests (self, template, module, path):

lines = ''
for test in module.tests:
lines += self.replace_test (module, test, path)

return template.replace ('% tests%', lines)


#--------------------------------------------------------------------------

def replace_test (self, module, test, path):
path_template = os.path.join (PATH_THIS, 'test_unit_template.gyp')
with open (path_template, 'r', encoding='utf-8') as file:
template = file.read ()

template = template.replace ('%test.name%', test.name)
template = self.replace_includes (template, module, path);
template = self.replace_defines (template, module.defines)
template = self.replace_bases (template, module, module.bases, path);
template = self.replace_test_sources (template, test, path)
return template


#--------------------------------------------------------------------------

def replace_test_sources (self, template, test, path):
lines = ''

for file in test.files:
file_path = os.path.relpath (file.path, path)
lines += ' \'%s\',\n' % file_path

return template.replace ('% test.sources%', lines)
1 change: 1 addition & 0 deletions build-system/erbb/generators/vcvrack/project_template.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@
},
],
},
% tests%
],
}
17 changes: 17 additions & 0 deletions build-system/erbb/generators/vcvrack/test_unit_template.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
'target_name': '%test.name%',
'type': 'executable',

'defines': [
% defines.entities%
],

'include_dirs': [
'.',
% bases.entities%
],

'sources': [
% test.sources%
],
},
10 changes: 8 additions & 2 deletions build-system/erbb/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
KEYWORDS = (
'module',
'import', 'define', 'sources', 'resources', 'section',
'file', 'data', 'flash', 'qspi', 'sram', 'stream', 'mono', 'interleaved', 'planar'
'file', 'data', 'test', 'flash', 'qspi', 'sram', 'stream', 'mono', 'interleaved', 'planar'
)

SYMBOLS = (',', '{', '}', '=')
Expand Down Expand Up @@ -71,6 +71,12 @@ def resources_entities (): return ZeroOrMore (data_declaration)
def resources_body (): return '{', resources_entities, '}'
def resources_declaration (): return 'resources', resources_body

# Tests
def test_entities (): return ZeroOrMore (file_declaration)
def test_body (): return '{', test_entities, '}'
def test_name (): return name
def test_declaration (): return 'test', test_name, test_body

# Base
def base_declaration (): return 'base', string_literal

Expand All @@ -79,7 +85,7 @@ def section_name (): return ['flash', 'qspi', 'sram']
def section_declaration (): return 'section', section_name

# Module
def module_entities (): return ZeroOrMore ([section_declaration, import_declaration, define_declaration, sources_declaration, resources_declaration, base_declaration])
def module_entities (): return ZeroOrMore ([section_declaration, import_declaration, define_declaration, sources_declaration, resources_declaration, test_declaration, base_declaration])
def module_body (): return '{', module_entities, '}'
def module_name (): return name
def module_declaration (): return 'module', module_name, module_body
Expand Down
23 changes: 23 additions & 0 deletions build-system/erbb/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,29 @@ def visit_resources_entities (self, node, children):
return list (children)


#-- Test ------------------------------------------------------------------

def visit_test_declaration (self, node, children):
test_name_identifier = children.test_name [0]

test = ast.Test (test_name_identifier)

if children.test_body:
entities = children.test_body [0]
test.add (entities)

return test

def visit_test_name (self, node, children):
return self.visit_identifier (node, children)

def visit_test_body (self, node, children):
return children [0] if children else []

def visit_test_entities (self, node, children):
return list (children)


#-- Base ------------------------------------------------------------------

def visit_base_declaration (self, node, children):
Expand Down
2 changes: 1 addition & 1 deletion build-system/setup/vscode/syntaxes/erbb.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"patterns": [
{
"name": "keyword.control.erbb",
"match": "\\b(module|file|import|define|base|section|resources|sources|data|stream|faust|address|bind|init|use|strict)\\b"
"match": "\\b(module|file|import|define|base|section|resources|sources|data|test|stream|faust|address|bind|init|use|strict)\\b"
},
{
"name": "keyword.control.erbb",
Expand Down
2 changes: 1 addition & 1 deletion build-system/setup/xcode/Erbb.xclangspec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
StartChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
Chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
Words = (
"module", "import", "define", "sources", "file", "resources", "data",
"module", "import", "define", "sources", "file", "resources", "data", "test",
"section", "flash", "qspi", "sram",
"stream", "mono", "interleaved", "planar",
"faust", "bind", "address",
Expand Down
2 changes: 1 addition & 1 deletion documentation/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ErbbLexer(RegexLexer):
'root': [
(r'//.*?$', token.Comment.Singleline),
(r'\".*\"', token.Literal.String),
(r'module|file|import|define|base|section|resources|sources|data|stream|faust|address|bind|init|use|strict', token.Keyword),
(r'module|file|import|define|base|section|resources|sources|data|test|stream|faust|address|bind|init|use|strict', token.Keyword),
(r'flash|qspi|mono|interleaved|planar', token.Name.Builtin),
(r'AudioSample', token.Keyword.Type),
(r'{|}|=', token.Punctuation),
Expand Down
14 changes: 13 additions & 1 deletion documentation/erbb/grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ it is a set of multiple `import`, `sources`, `base`, `define`, etc. _declaration
> _module-entity_ → [define-declaration](#define) \
> _module-entity_ → [sources-declaration](#sources) \
> _module-entity_ → [resources-declaration](#resources) \
> _module-entity_ → [base-declaration](#base)
> _module-entity_ → [base-declaration](#base) \
> _module-entity_ → [test-declaration](#test)


## `import`
Expand Down Expand Up @@ -243,3 +244,14 @@ and `AudioSamplePlanar`.
being an array of channels,
- `AudioSamplePlanar` stores the sample as an array of channels, each channel
being an array of samples.


## `test`

The `test` defines a test target for the module, for example an unit test.

### Grammar

> _test-declaration_ → **`test`** test-name **`{`** test-entity<sub>_0+_</sub> **`}`** \
> _test-name_ → [identifier](./lexical.html#identifiers) \
> _test-entity_ → [file-declaration](#file)
Loading