From 1c19b83d611ef789d5d513f357abc12d3057af51 Mon Sep 17 00:00:00 2001 From: Martin Lehmann Date: Tue, 17 Oct 2023 17:19:20 +0200 Subject: [PATCH] fix(metrics): Don't crash if there are no diagrams or semantic objects --- capellambse/extensions/metrics/composer.py | 5 ++++- tests/test_aird_parser.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/capellambse/extensions/metrics/composer.py b/capellambse/extensions/metrics/composer.py index 02fd4cf4c..a4a4a2637 100644 --- a/capellambse/extensions/metrics/composer.py +++ b/capellambse/extensions/metrics/composer.py @@ -145,7 +145,10 @@ def draw_labeled_bar( ) -> str: """Construct a bar with label and icon (SVG string).""" total = sum(segments) - normalized_segments = [x / total for x in segments] + if total: + normalized_segments = [x / total for x in segments] + else: + normalized_segments = [0 for _ in segments] return ( draw_icon(x, y) + draw_text(x + 5, y + 3, f"{total}") diff --git a/tests/test_aird_parser.py b/tests/test_aird_parser.py index 63e6fa1e0..bafe17469 100644 --- a/tests/test_aird_parser.py +++ b/tests/test_aird_parser.py @@ -10,6 +10,10 @@ import capellambse from capellambse import aird, diagram, loader +EMPTY_MODEL = pathlib.Path(__file__).parent.joinpath( + "data/decl/empty_project_52/empty_project_52.aird" +) + class TestAIRDBasicFunctionality: test_model = ( @@ -81,6 +85,11 @@ def test_python_code_representation_matches_expected_output( actual = repr(diagram_under_test) assert actual == expected + def test_enumerate_diagrams_doesnt_crash_if_there_are_no_diagrams(self): + model = loader.MelodyLoader(EMPTY_MODEL) + for _ in aird.enumerate_diagrams(model): + pass + def test_airdparser_msm_produces_valid_json_without_error( model: capellambse.MelodyModel,