pyEDAA.Reports.CodeCoverage.UCIS.Bins

pyEDAA/Reports/CodeCoverage/UCIS/Bins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# ==================================================================================================================== #
#              _____ ____    _        _      ____                       _                                              #
#  _ __  _   _| ____|  _ \  / \      / \    |  _ \ ___ _ __   ___  _ __| |_ ___                                        #
# | '_ \| | | |  _| | | | |/ _ \    / _ \   | |_) / _ \ '_ \ / _ \| '__| __/ __|                                       #
# | |_) | |_| | |___| |_| / ___ \  / ___ \ _|  _ <  __/ |_) | (_) | |  | |_\__ \                                       #
# | .__/ \__, |_____|____/_/   \_\/_/   \_(_)_| \_\___| .__/ \___/|_|   \__|___/                                       #
# |_|    |___/                                        |_|                                                              #
# ==================================================================================================================== #
# Authors:                                                                                                             #
#   Patrick Lehmann                                                                                                    #
#                                                                                                                      #
# License:                                                                                                             #
# ==================================================================================================================== #
# Copyright 2026-2026 Electronic Design Automation Abstraction (EDA²)                                                  #
#                                                                                                                      #
# Licensed under the Apache License, Version 2.0 (the "License");                                                      #
# you may not use this file except in compliance with the License.                                                     #
# You may obtain a copy of the License at                                                                              #
#                                                                                                                      #
#   http://www.apache.org/licenses/LICENSE-2.0                                                                         #
#                                                                                                                      #
# Unless required by applicable law or agreed to in writing, software                                                  #
# distributed under the License is distributed on an "AS IS" BASIS,                                                    #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.                                             #
# See the License for the specific language governing permissions and                                                  #
# limitations under the License.                                                                                       #
#                                                                                                                      #
# SPDX-License-Identifier: Apache-2.0                                                                                  #
# ==================================================================================================================== #
#
"""
The bins of the UCIS XML interchange format: a coverage item's count - how often it was covered -, the tests, which
covered it, and its goal.
"""
from __future__                                import annotations

from typing                                    import Iterable, Optional as Nullable, Self

from lxml.etree                                import _Element
from pyTooling.Common                          import getFullyQualifiedName
from pyTooling.Decorators                      import export, readonly
from pyTooling.MetaClasses                     import ExtendedType

from pyEDAA.Reports.CodeCoverage.UCIS.Elements import Base, ObjectAttributesMixin, UserAttribute


@export
class BinContents(metaclass=ExtendedType, slots=True):
	"""
	A ``<contents>`` of a bin: how often its coverage item was covered, the tests, which covered it, and the components
	of its unique ID.

	It is a value, as a path is: it names no parent.
	"""

	_coverageCount:  int            #: How often the coverage item was covered.
	_historyNodeIDs: list[int]      #: The IDs of the history nodes, which covered the coverage item.
	_nameComponent:  Nullable[str]  #: The name component of the coverage item's unique ID.
	_typeComponent:  Nullable[str]  #: The type component of the coverage item's unique ID.

	def __init__(
		self,
		coverageCount: int,
		historyNodeIDs: Nullable[Iterable[int]] = None,
		nameComponent: Nullable[str] = None,
		typeComponent: Nullable[str] = None
	) -> None:
		"""
		Initialize the contents of a bin.

		:param coverageCount:  How often the coverage item was covered.
		:param historyNodeIDs: Optional, the IDs of the history nodes, which covered the coverage item. Default: ``None``.
		:param nameComponent:  Optional, the name component of the coverage item's unique ID. Default: ``None``.
		:param typeComponent:  Optional, the type component of the coverage item's unique ID, e.g. ``:5:`` for a
		                       statement. Default: ``None``.
		:raises ValueError:    If parameter ``coverageCount`` is ``None``.
		:raises TypeError:     If parameter ``coverageCount`` isn't of type :class:`int`.
		:raises ValueError:    If parameter ``coverageCount`` is negative.
		:raises TypeError:     If parameter ``historyNodeIDs`` isn't iterable.
		:raises TypeError:     If parameter ``historyNodeIDs`` contains an element not of type :class:`int`.
		:raises TypeError:     If parameter ``nameComponent`` isn't of type :class:`str`.
		:raises TypeError:     If parameter ``typeComponent`` isn't of type :class:`str`.
		"""
		if coverageCount is None:
			raise ValueError("Parameter 'coverageCount' is None.")
		elif not isinstance(coverageCount, int) or isinstance(coverageCount, bool):
			ex = TypeError("Parameter 'coverageCount' is not of type 'int'.")
			ex.add_note(f"Got type '{getFullyQualifiedName(coverageCount)}'.")
			raise ex
		elif coverageCount < 0:
			ex = ValueError("Parameter 'coverageCount' is negative.")
			ex.add_note(f"Got value '{coverageCount}'.")
			raise ex

		if nameComponent is not None and not isinstance(nameComponent, str):
			ex = TypeError("Parameter 'nameComponent' is not of type 'str'.")
			ex.add_note(f"Got type '{getFullyQualifiedName(nameComponent)}'.")
			raise ex

		if typeComponent is not None and not isinstance(typeComponent, str):
			ex = TypeError("Parameter 'typeComponent' is not of type 'str'.")
			ex.add_note(f"Got type '{getFullyQualifiedName(typeComponent)}'.")
			raise ex

		self._coverageCount =  coverageCount
		self._historyNodeIDs = []
		self._nameComponent =  nameComponent
		self._typeComponent =  typeComponent

		if historyNodeIDs is not None:
			if not isinstance(historyNodeIDs, Iterable):
				ex = TypeError("Parameter 'historyNodeIDs' is not iterable.")
				ex.add_note(f"Got type '{getFullyQualifiedName(historyNodeIDs)}'.")
				raise ex

			for historyNodeID in historyNodeIDs:
				if not isinstance(historyNodeID, int) or isinstance(historyNodeID, bool):
					ex = TypeError("Parameter 'historyNodeIDs' contains an element not of type 'int'.")
					ex.add_note(f"Got type '{getFullyQualifiedName(historyNodeID)}'.")
					raise ex

				self._historyNodeIDs.append(historyNodeID)

	@classmethod
	def Parse(cls, element: _Element) -> Self:
		"""
		Parse the contents of a bin from its ``<contents>`` element.

		:param element: The ``<contents>`` element.
		:returns:       The contents.
		"""
		return cls(
			int(element.attrib["coverageCount"]),
			[int(idElement.text) for idElement in element.iterfind("{*}historyNodeId")],
			element.attrib.get("nameComponent"),
			element.attrib.get("typeComponent")
		)

	@readonly
	def CoverageCount(self) -> int:
		"""
		Read-only property to access how often the coverage item was covered (:attr:`_coverageCount`).

		:returns: The count, e.g. how often a statement ran.
		"""
		return self._coverageCount

	@readonly
	def HistoryNodeIDs(self) -> list[int]:
		"""
		Read-only property to access the IDs of the history nodes, which covered the item (:attr:`_historyNodeIDs`).

		:returns: The IDs, in the order the report lists them.
		"""
		return self._historyNodeIDs

	@readonly
	def NameComponent(self) -> Nullable[str]:
		"""
		Read-only property to access the name component of the coverage item's unique ID (:attr:`_nameComponent`).

		:returns: The name component; ``None``, if the report states none.
		"""
		return self._nameComponent

	@readonly
	def TypeComponent(self) -> Nullable[str]:
		"""
		Read-only property to access the type component of the coverage item's unique ID (:attr:`_typeComponent`).

		:returns: The type component, e.g. ``:5:``; ``None``, if the report states none.
		"""
		return self._typeComponent


@export
class Bin(Base, ObjectAttributesMixin):
	"""
	A bin of a coverage item: its contents, its goal and its attributes.

	A statement has ``<bin>`` elements, a branch ``<branchBin>`` elements. The element owning the bin takes it as a
	parameter and becomes its parent.
	"""

	_parent:            Nullable[Base]  #: The element owning the bin.
	_contents:          BinContents     #: How often the coverage item was covered, and by which tests.
	_coverageCountGoal: Nullable[int]   #: How often the coverage item must be covered.

	def __init__(
		self,
		contents: BinContents,
		coverageCountGoal: Nullable[int] = None,
		alias: Nullable[str] = None,
		excluded: bool = False,
		excludedReason: Nullable[str] = None,
		weight: int = 1,
		userAttributes: Nullable[Iterable[UserAttribute]] = None
	) -> None:
		"""
		Initialize the bin.

		:param contents:          How often the coverage item was covered, and by which tests.
		:param coverageCountGoal: Optional, how often the coverage item must be covered. Default: ``None``.
		:param alias:             Optional, an alias of the bin's name. Default: ``None``.
		:param excluded:          Optional, whether the bin is excluded from the coverage. Default: ``False``.
		:param excludedReason:    Optional, why the bin is excluded. Default: ``None``.
		:param weight:            Optional, weight of the bin in computing the coverage. Default: ``1``.
		:param userAttributes:    Optional, the user-defined attributes. Default: ``None``.
		:raises TypeError:        If parameter ``userAttributes`` isn't iterable.
		:raises TypeError:        If parameter ``userAttributes`` contains an element not of type
		                          :class:`~pyEDAA.Reports.CodeCoverage.UCIS.Elements.UserAttribute`.
		:raises TypeError:        If parameter ``alias`` isn't of type :class:`str`.
		:raises ValueError:       If parameter ``excluded`` is ``None``.
		:raises TypeError:        If parameter ``excluded`` isn't of type :class:`bool`.
		:raises TypeError:        If parameter ``excludedReason`` isn't of type :class:`str`.
		:raises ValueError:       If parameter ``weight`` is ``None``.
		:raises TypeError:        If parameter ``weight`` isn't of type :class:`int`.
		:raises ValueError:       If parameter ``weight`` is negative.
		:raises ValueError:       If parameter ``contents`` is ``None``.
		:raises TypeError:        If parameter ``contents`` isn't of type :class:`BinContents`.
		:raises TypeError:        If parameter ``coverageCountGoal`` isn't of type :class:`int`.
		:raises ValueError:       If parameter ``coverageCountGoal`` is negative.
		"""
		super().__init__(userAttributes)
		ObjectAttributesMixin.__init__(self, alias, excluded, excludedReason, weight)

		if contents is None:
			raise ValueError("Parameter 'contents' is None.")
		elif not isinstance(contents, BinContents):
			ex = TypeError("Parameter 'contents' is not of type 'BinContents'.")
			ex.add_note(f"Got type '{getFullyQualifiedName(contents)}'.")
			raise ex

		if coverageCountGoal is not None:
			if not isinstance(coverageCountGoal, int) or isinstance(coverageCountGoal, bool):
				ex = TypeError("Parameter 'coverageCountGoal' is not of type 'int'.")
				ex.add_note(f"Got type '{getFullyQualifiedName(coverageCountGoal)}'.")
				raise ex
			elif coverageCountGoal < 0:
				ex = ValueError("Parameter 'coverageCountGoal' is negative.")
				ex.add_note(f"Got value '{coverageCountGoal}'.")
				raise ex

		self._parent =            None
		self._contents =          contents
		self._coverageCountGoal = coverageCountGoal

	@classmethod
	def Parse(cls, element: _Element) -> Self:
		"""
		Parse a bin, its contents and its user-defined attributes from its element.

		The element is e.g. a ``<bin>`` or a ``<branchBin>``.

		:param element:            The bin's element.
		:returns:                  The bin.
		:raises CodeCoverageError: If a user-defined attribute's value isn't of the type the attribute states.
		"""
		goal = element.attrib.get("coverageCountGoal")
		return cls(
			BinContents.Parse(element.find("{*}contents")),
			None if goal is None else int(goal),
			element.attrib.get("alias"),
			cls._ParseBoolean(element.attrib.get("excluded", "false")),
			element.attrib.get("excludedReason"),
			int(element.attrib.get("weight", "1")),
			cls._ParseUserAttributes(element)
		)

	@readonly
	def Parent(self) -> Nullable[Base]:
		"""
		Read-only property to access the element owning the bin (:attr:`_parent`).

		:returns: The element, e.g. a :class:`~pyEDAA.Reports.CodeCoverage.UCIS.Blocks.Statement`; ``None``, if the bin
		          belongs to none.
		"""
		return self._parent

	@readonly
	def Contents(self) -> BinContents:
		"""
		Read-only property to access the contents of the bin (:attr:`_contents`).

		:returns: How often the coverage item was covered, and by which tests.
		"""
		return self._contents

	@readonly
	def CoverageCountGoal(self) -> Nullable[int]:
		"""
		Read-only property to access how often the coverage item must be covered (:attr:`_coverageCountGoal`).

		:returns: The goal; ``None``, if the report states none.
		"""
		return self._coverageCountGoal