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

Parser tests #26

Merged
merged 4 commits into from
Aug 23, 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
2 changes: 1 addition & 1 deletion src/fcp/specs/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class FcpV2(Model):
devices: fields.List(device.Device)
broadcasts: fields.List(broadcast.Broadcast)
logs: fields.List(log.Log)
version: fields.Str(default="1.0")
version: fields.Str()

def add_device(self, device):
self.devices.append(device)
Expand Down
10 changes: 6 additions & 4 deletions src/fcp/v2_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def convert_params(params):

values = {}
for name, value in params.items():
values = values | convertion_table[name](value)
values.update(convertion_table[name](value))

return values

Expand Down Expand Up @@ -566,7 +566,7 @@ def deduplicate(module):
def merge(fcp, fpi):
fcp = {key: fcp[key] for key in fcp.keys() & {"struct", "enum"}}
fpi = {key: fpi[key] for key in fpi.keys() & {"device", "broadcast", "log"}}
fcp = fcp | fpi
fcp.update(fpi)
return Ok(fcp)


Expand All @@ -578,6 +578,7 @@ def convert(module):
structs=module["struct"].values(),
enums=module["enum"].values(),
logs=module["log"].values(),
version="3.0",
)
)

Expand All @@ -586,7 +587,7 @@ def convert(module):
def get_sources(module):
sources = {module.filename: module.source}
for mod in module.imports:
sources = sources | get_sources(mod)
sources.update(get_sources(mod))

return sources

Expand Down Expand Up @@ -624,4 +625,5 @@ def get_fcp(fcp, fpi):
fpi_sources = get_sources(fpi)
fpi = deduplicate(resolve_imports(fpi).Q()).Q()

return Ok((convert(merge(fcp, fpi).Q()), fcp_sources | fpi_sources))
fcp_sources.update(fpi_sources)
return Ok((convert(merge(fcp, fpi).Q()), fcp_sources))
5 changes: 5 additions & 0 deletions tests/configs/001.fcp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: "3"

struct foo {
bar @ 0: u8;
};
43 changes: 43 additions & 0 deletions tests/configs/001.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"structs": [
{
"name": "foo",
"signals": [
{
"name": "bar",
"comment": {
"value": ""
},
"type": "u8",
"field_id": 0,
"meta": {
"line": 4,
"end_line": 4,
"column": 5,
"end_column": 17,
"start_pos": 31,
"end_pos": 43,
"filename": "001.fcp"
}
}
],
"meta": {
"line": 3,
"end_line": 5,
"column": 1,
"end_column": 3,
"start_pos": 14,
"end_pos": 46,
"filename": "001.fcp"
},
"comment": {
"value": ""
}
}
],
"enums": [],
"devices": [],
"broadcasts": [],
"logs": [],
"version": "3.0"
}
6 changes: 6 additions & 0 deletions tests/configs/002.fcp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "3"

enum foo {
bar = 0;
baz = 1;
};
52 changes: 52 additions & 0 deletions tests/configs/002.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"structs": [],
"enums": [
{
"name": "foo",
"enumeration": [
{
"name": "bar",
"value": 0,
"meta": {
"line": 4,
"end_line": 4,
"column": 5,
"end_column": 13,
"start_pos": 29,
"end_pos": 37,
"filename": "002.fcp"
}
},
{
"name": "baz",
"value": 1,
"meta": {
"line": 5,
"end_line": 5,
"column": 5,
"end_column": 13,
"start_pos": 42,
"end_pos": 50,
"filename": "002.fcp"
}
}
],
"meta": {
"line": 3,
"end_line": 6,
"column": 1,
"end_column": 3,
"start_pos": 14,
"end_pos": 53,
"filename": "002.fcp"
},
"comment": {
"value": ""
}
}
],
"devices": [],
"broadcasts": [],
"logs": [],
"version": "3.0"
}
1 change: 1 addition & 0 deletions tests/configs/test.fpi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version: "3"
23 changes: 23 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest
import os
import json

from fcp.v2_parser import get_fcp

THIS_DIR = os.path.dirname(os.path.abspath(__file__))


@pytest.mark.parametrize("test_name", ["001", "002"])
def test_parser(test_name):
config_dir = os.path.join(THIS_DIR, "configs")
print("config dir", config_dir)
fpi_config = os.path.join(config_dir, "test.fpi")
fcp_config = os.path.join(config_dir, test_name + ".fcp")
result = os.path.join(config_dir, test_name + ".json")

fcp_v2, sources = get_fcp(fcp_config, fpi_config).unwrap()

fcp_json = fcp_v2.unwrap().to_dict()
result = json.loads(open(result).read())

assert fcp_json == result
12 changes: 7 additions & 5 deletions tests/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

@pytest.fixture
def fcp_v2():
return FcpV2(devices=[], structs=[], broadcasts=[], enums=[], logs=[])
return FcpV2(
devices=[], structs=[], broadcasts=[], enums=[], logs=[], version="3.0"
)


@pytest.fixture
Expand All @@ -15,13 +17,13 @@ def fcp_v2_dict():
"devices": [],
"broadcasts": [],
"logs": [],
"version": "1.0",
"version": "3.0",
}


@pytest.fixture
def fcp_v2_json():
return '{"structs": [], "enums": [], "devices": [], "broadcasts": [], "logs": [], "version": "1.0"}'
return '{"structs": [], "enums": [], "devices": [], "broadcasts": [], "logs": [], "version": "3.0"}'


def test_fcp_v2_init(fcp_v2):
Expand All @@ -31,7 +33,7 @@ def test_fcp_v2_init(fcp_v2):
def test_fcp_v2_to_json(fcp_v2):
assert (
fcp_v2.to_json()
== '{"structs": [], "enums": [], "devices": [], "broadcasts": [], "logs": [], "version": "1.0"}'
== '{"structs": [], "enums": [], "devices": [], "broadcasts": [], "logs": [], "version": "3.0"}'
)


Expand All @@ -42,7 +44,7 @@ def test_fcp_v2_to_dict(fcp_v2):
"devices": [],
"broadcasts": [],
"logs": [],
"version": "1.0",
"version": "3.0",
}


Expand Down