[docs]defcompare_code(code:str,expected:str,dedent:bool=True,debug:bool=False,)->bool:""" Compares whether two pieces of code are identical, ignoring whitespace and indentation. :param code: The generated code string. :param expected: The expected code string. :return: Returns True if the two code strings are identical after ignoring whitespace and indentation; otherwise, returns False. """s1=normalize_code(code,dedent)s2=normalize_code(expected,dedent)ifdebug:print(f"--- code ---")print(s1)print(f"--- expected ---")print(s2)returns1==s2
[docs]defwrite(path:Path,content:str):""" Write content to a file, creating parent directories if they do not exist. """try:path.write_text(content,encoding="utf-8")exceptFileNotFoundError:path.parent.mkdir(parents=True,exist_ok=True)path.write_text(content,encoding="utf-8")
[docs]@dataclasses.dataclassclassSemVer:""" Semantic Versioning (SemVer) representation. """major:int=dataclasses.field()minor:int=dataclasses.field()patch:int=dataclasses.field()dev_id:str|None=dataclasses.field(default=None)@propertydefversion(self)->str:returnf"{self.major}.{self.minor}.{self.patch}"@propertydeflower_version(self)->str:returnf"{self.major}.{self.minor}.0"@propertydefupper_version(self)->str:returnf"{self.major}.{self.minor+1}.0"@classmethoddefparse(cls,s:str):parts=s.split(".")iflen(parts)==3:major,minor,patch=map(int,parts)returncls(major,minor,patch)eliflen(parts)==4:major,minor,patch=map(int,parts[:3])dev_id=parts[3]returncls(major,minor,patch,dev_id)else:# pragma: no coverraiseValueError(f"Invalid version string: {s}")