FreeCAD
213 строк · 8.0 Кб
1# SPDX-License-Identifier: LGPL-2.1-or-later
2# ***************************************************************************
3# * *
4# * Copyright (c) 2022-2023 FreeCAD Project Association *
5# * *
6# * This file is part of FreeCAD. *
7# * *
8# * FreeCAD is free software: you can redistribute it and/or modify it *
9# * under the terms of the GNU Lesser General Public License as *
10# * published by the Free Software Foundation, either version 2.1 of the *
11# * License, or (at your option) any later version. *
12# * *
13# * FreeCAD is distributed in the hope that it will be useful, but *
14# * WITHOUT ANY WARRANTY; without even the implied warranty of *
15# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16# * Lesser General Public License for more details. *
17# * *
18# * You should have received a copy of the GNU Lesser General Public *
19# * License along with FreeCAD. If not, see *
20# * <https://www.gnu.org/licenses/>. *
21# * *
22# ***************************************************************************
23
24import os25import sys26import tempfile27from typing import Dict28import unittest29from unittest.mock import MagicMock30
31sys.path.append("../../") # So the IDE can find the32
33import FreeCAD34
35from addonmanager_macro import Macro36
37
38class TestMacro(unittest.TestCase):39MODULE = "test_macro" # file name without extension40
41def setUp(self):42self.test_dir = os.path.join(43FreeCAD.getHomePath(), "Mod", "AddonManager", "AddonManagerTest", "data"44)45
46def test_basic_metadata(self):47replacements = {48"COMMENT": "test comment",49"WEB": "https://test.url",50"VERSION": "1.2.3",51"AUTHOR": "Test Author",52"DATE": "2022-03-09",53"ICON": "testicon.svg",54}55m = self.generate_macro(replacements)56self.assertEqual(m.comment, replacements["COMMENT"])57self.assertEqual(m.url, replacements["WEB"])58self.assertEqual(m.version, replacements["VERSION"])59self.assertEqual(m.author, replacements["AUTHOR"])60self.assertEqual(m.date, replacements["DATE"])61self.assertEqual(m.icon, replacements["ICON"])62
63def test_other_files(self):64replacements = {65"FILES": "file_a,file_b,file_c",66}67m = self.generate_macro(replacements)68self.assertEqual(len(m.other_files), 3)69self.assertEqual(m.other_files[0], "file_a")70self.assertEqual(m.other_files[1], "file_b")71self.assertEqual(m.other_files[2], "file_c")72
73replacements = {74"FILES": "file_a, file_b, file_c",75}76m = self.generate_macro(replacements)77self.assertEqual(len(m.other_files), 3)78self.assertEqual(m.other_files[0], "file_a")79self.assertEqual(m.other_files[1], "file_b")80self.assertEqual(m.other_files[2], "file_c")81
82replacements = {83"FILES": "file_a file_b file_c",84}85m = self.generate_macro(replacements)86self.assertEqual(len(m.other_files), 1)87self.assertEqual(m.other_files[0], "file_a file_b file_c")88
89def test_version_from_string(self):90replacements = {91"VERSION": "1.2.3",92}93m = self.generate_macro(replacements)94self.assertEqual(m.version, "1.2.3")95
96def test_version_from_date(self):97replacements = {98"DATE": "2022-03-09",99}100outfile = self.generate_macro_file(replacements)101with open(outfile) as f:102lines = f.readlines()103output_lines = []104for line in lines:105if "VERSION" in line:106line = "__Version__ = __Date__"107output_lines.append(line)108with open(outfile, "w") as f:109f.write("\n".join(output_lines))110m = Macro("Unit Test Macro")111m.fill_details_from_file(outfile)112self.assertEqual(m.version, "2022-03-09")113
114def test_version_from_float(self):115outfile = self.generate_macro_file()116with open(outfile) as f:117lines = f.readlines()118output_lines = []119for line in lines:120if "VERSION" in line:121line = "__Version__ = 1.23"122output_lines.append(line)123with open(outfile, "w") as f:124f.write("\n".join(output_lines))125m = Macro("Unit Test Macro")126m.fill_details_from_file(outfile)127self.assertEqual(m.version, "1.23")128
129def test_version_from_int(self):130outfile = self.generate_macro_file()131with open(outfile) as f:132lines = f.readlines()133output_lines = []134for line in lines:135if "VERSION" in line:136line = "__Version__ = 1"137output_lines.append(line)138with open(outfile, "w") as f:139f.write("\n".join(output_lines))140m = Macro("Unit Test Macro")141m.fill_details_from_file(outfile)142self.assertEqual(m.version, "1")143
144def test_xpm(self):145outfile = self.generate_macro_file()146xpm_data = """/* XPM */147static char * blarg_xpm[] = {
148"16 7 2 1",
149"* c #000000",
150". c #ffffff",
151"**..*...........",
152"*.*.*...........",
153"**..*..**.**..**",
154"*.*.*.*.*.*..*.*",
155"**..*..**.*...**",
156"...............*",
157".............**."
158};"""
159with open(outfile) as f:160contents = f.read()161contents += f'\n__xpm__ = """{xpm_data}"""\n'162
163with open(outfile, "w") as f:164f.write(contents)165m = Macro("Unit Test Macro")166m.fill_details_from_file(outfile)167self.assertEqual(m.xpm, xpm_data)168
169def generate_macro_file(self, replacements: Dict[str, str] = {}) -> os.PathLike:170with open(os.path.join(self.test_dir, "macro_template.FCStd")) as f:171lines = f.readlines()172outfile = tempfile.NamedTemporaryFile(mode="wt", delete=False)173for line in lines:174for key, value in replacements.items():175line = line.replace(key, value)176
177outfile.write(line)178outfile.close()179return outfile.name180
181def generate_macro(self, replacements: Dict[str, str] = {}) -> Macro:182outfile = self.generate_macro_file(replacements)183m = Macro("Unit Test Macro")184m.fill_details_from_file(outfile)185os.unlink(outfile)186return m187
188def test_fetch_raw_code_no_data(self):189m = Macro("Unit Test Macro")190Macro.blocking_get = MagicMock(return_value=None)191returned_data = m._fetch_raw_code(192'rawcodeurl <a href="https://fake_url.com">Totally fake</a>'193)194self.assertIsNone(returned_data)195m.blocking_get.assert_called_with("https://fake_url.com")196Macro.blocking_get = None197
198def test_fetch_raw_code_no_url(self):199m = Macro("Unit Test Macro")200Macro.blocking_get = MagicMock(return_value=None)201returned_data = m._fetch_raw_code("Fake pagedata with no URL at all.")202self.assertIsNone(returned_data)203m.blocking_get.assert_not_called()204Macro.blocking_get = None205
206def test_fetch_raw_code_with_data(self):207m = Macro("Unit Test Macro")208Macro.blocking_get = MagicMock(return_value=b"Data returned to _fetch_raw_code")209returned_data = m._fetch_raw_code(210'rawcodeurl <a href="https://fake_url.com">Totally fake</a>'211)212self.assertEqual(returned_data, "Data returned to _fetch_raw_code")213Macro.blocking_get = None214