def test_datatotext(self, monkeypatch, capsys): def mock_init(self, *args): print('called editor.__init__()') self.data = [] def mock_init_ruledata(*args): print('called init_ruledata()') return {} def mock_complete_ruledata(*args): print('called complete_ruledata()') return args[0] monkeypatch.setattr(cssedit.Editor, '__init__', mock_init) monkeypatch.setattr(cssedit, 'init_ruledata', mock_init_ruledata) monkeypatch.setattr(cssedit, 'complete_ruledata', mock_complete_ruledata) testobj = cssedit.Editor() testobj.data = [] testobj.datatotext() assert testobj.textdata == [] assert capsys.readouterr().out == ('called editor.__init__()\n') testobj = cssedit.Editor() testobj.data = (types.SimpleNamespace(type='1', typeString='type 1'), ) testobj.datatotext() assert capsys.readouterr().out == ('called editor.__init__()\n' 'called init_ruledata()\n' 'called complete_ruledata()\n') assert testobj.textdata == [('type 1', {'seqnum': 0})]
def setUp(self): "" # treedata for sensible text - one tag self.ed = cssedit.Editor(text=self.one_selector) self.ed.datatotext() self.ed.texttodata() # treedata for sensible text - more tags self.edm = cssedit.Editor(text=self.more_selectors) self.ed.datatotext() self.ed.texttodata()
def test_return_to_source(self, monkeypatch, capsys): def mock_init(self, *args): print('called editor.__init__()') self.data = [] def mock_set_format(*args): print('called cssedit.set_format(`{}`)'.format(args[0])) def mock_save(*args): print('called cssedit.save()') def mock_return(*args): return 'returned by cssedit.return_for_single_tag()' monkeypatch.setattr(cssedit.Editor, '__init__', mock_init) monkeypatch.setattr(cssedit, 'set_format', mock_set_format) monkeypatch.setattr(cssedit, 'save', mock_save) monkeypatch.setattr(cssedit, 'return_for_single_tag', mock_return) testobj = cssedit.Editor() with pytest.raises(AttributeError): testobj.return_to_source(savemode='wrong') assert capsys.readouterr().out == ('called editor.__init__()\n') testobj = cssedit.Editor() testobj.filename = 'x' testobj.data = MockStyleSheet() testobj.return_to_source() assert capsys.readouterr().out == ( 'called editor.__init__()\n' 'called stylesheet.__init__()\n' 'called cssedit.set_format(`compressed`)\n' 'called cssedit.save()\n') testobj = cssedit.Editor() testobj.filename, testobj.tag = '', 'y' testobj.data = MockStyleSheet() testobj.return_to_source() assert testobj.data == 'returned by cssedit.return_for_single_tag()' assert capsys.readouterr().out == ( 'called editor.__init__()\n' 'called stylesheet.__init__()\n' 'called cssedit.set_format(`compressed`)\n') testobj = cssedit.Editor() testobj.filename = testobj.tag = '' testobj.data = MockStyleSheet() testobj.return_to_source() assert testobj.data == 'text from stylesheet' assert capsys.readouterr().out == ( 'called editor.__init__()\n' 'called stylesheet.__init__()\n' 'called cssedit.set_format(`compressed`)\n')
def test_editor_badargs(self): "positional arguments only" # No arguments -- dit gaat niet meer fout vanwege genruik **kwargs # with self.assertRaises(ValueError): # ed = cssedit.Editor() # Positional arg only: 1 with self.assertRaises(TypeError): ed = cssedit.Editor('snork') # Positional arg only: 2 with self.assertRaises(TypeError): ed = cssedit.Editor('snork', 'bork') # Positional arg only: 3 with self.assertRaises(TypeError): ed = cssedit.Editor('snork', 'bork', 'hork') # Positional arg with correct one with self.assertRaises(TypeError): cssedit.Editor('snork', filename=testfiles[0][1])
def newfile(self): "start a new css file" self.gui.tree.remove_root() self.project_file = "" self.css = ed.Editor(new=True) self.gui.tree.init_root() self.gui.tree.set_activeitem(self.gui.tree.root) self.gui.tree.expand_item(self.gui.tree.root) self.mark_dirty(False)
def test_editor_text(self): "cases where at least the text argument is given" # treedata for nonsense text # moet wel een fout uitkomen maar wanneer? ## ed = cssedit.Editor(text="snorckenbocken") # works like text only - raises ValueError # treedata for text without tags ## with self.assertRaises(ValueError): ## ed = cssedit.Editor(text='border: 5px solid red; text-decoration: none; ' ## 'content: "gargl"; ') # treedata for sensible text - one tag ed = cssedit.Editor(text=self.one_selector) self.assertEqual(ed.data.cssText, results.data['editor_text']['one']) ## self.assertEqual(str(ed.data.cssText), self.one_selector) # treedata for sensible text - more tags ed = cssedit.Editor(text=self.more_selectors) self.assertEqual(ed.data.cssText, results.data['editor_text']['more']) ## self.assertEqual(str(ed.data.cssText), self.more_selectors) # treedata voor sensible text - more tags - formatted") ed = cssedit.Editor(text=formatted_css) self.assertEqual(ed.data.cssText, results.data['editor_text']['formatted'])
def test_editor_tag(self): "cases where at least the tag argument is given" # treedata for empty tag - ok because empty text is allowed # with self.assertRaises(ValueError): ed = cssedit.Editor(tag='') self.assertEqual(ed.data.cssText, b'') # treedata for ok tag - ok because empty text is allowed # with self.assertRaises(ValueError): ed = cssedit.Editor(tag=self.tagname) self.assertEqual(ed.data.cssText, b'') # treedata for empty tag and empty text - is actually ok # with self.assertRaises(ValueError): ed = cssedit.Editor(tag="", text="") self.assertEqual(ed.data.cssText, b'') # treedata for ok tag and empty text - this is also ok # with self.assertRaises(ValueError): ed = cssedit.Editor(tag=self.tagname, text="") # treedata for ok tag and nonsense text # with self.assertRaises(ValueError): ed = cssedit.Editor(tag=self.tagname, text=self.bad_tagtext) # No Error? self.assertEqual(ed.data.cssText, b'') # treedata for ok tag and ok text ed = cssedit.Editor(tag=self.tagname, text=self.good_tagtext) self.assertEqual(ed.data.cssText, results.data['editor_tag'])
def test_editor_filename(self): "cases where at least the filename argument is given" # filename as positional arg with self.assertRaises(TypeError): ed = cssedit.Editor(testfiles[0][1]) # treedata for file - empty filename with self.assertRaises(ValueError): ed = cssedit.Editor(filename="") # self.assertEqual(ed.data.cssText, b'') # treedata for file - nonexistant with self.assertRaises(FileNotFoundError): cssedit.Editor(filename="nonexistant") # treedata for file - valid file for method, name in testfiles: if method == 'compressed': result = results.data['editor_file']['compressed'] else: result = results.data['editor_file']['other'] ed = cssedit.Editor(filename=name) self.assertIsInstance(ed.data, cssutils.css.CSSStyleSheet) # TODO: test op inhoud verbeteren print(ed.data.cssText) print(result) self.assertEqual(ed.data.cssText, ''.join(result))
def open(self, **kwargs): "open an existing css file" if 'filename' in kwargs: self.mode = 'file' elif 'tag' in kwargs: self.mode = 'tag' else: self.mode = 'text' self.newfile() fname = kwargs.get('filename', '') self.project_file = os.path.abspath(fname) if fname else '' self.gui.tree.set_root_text(self.project_file or "(no file)") with self.wait_cursor(): self.css = ed.Editor(**kwargs) self.css.datatotext() self.texttotree() self.gui.show_statusmessage(self.build_loaded_message()) self.gui.tree.activate_rootitem() self.gui.tree.set_focus()
def setUp(self): "" self.ed = cssedit.Editor(text=formatted_css) self.ed.datatotext()
def setUp(self): "" self.ed = cssedit.Editor(tag=self.tagname, text=self.good_tagtext) self.ed.datatotext() self.ed.texttodata()
def test_texttodata(self, monkeypatch, capsys): def mock_init(self, *args): print('called editor.__init__()') self.data = [] monkeypatch.setattr(cssedit.Editor, '__init__', mock_init) monkeypatch.setattr(cssedit.cssutils.css, 'CSSStyleSheet', MockStyleSheet) monkeypatch.setattr(cssedit.cssutils.css, 'CSSStyleRule', MockStyleRule) monkeypatch.setattr(cssedit.cssutils.css, 'SelectorList', MockSelectorList) monkeypatch.setattr(cssedit.cssutils.css, 'CSSStyleDeclaration', MockStyleDeclaration) monkeypatch.setattr(cssedit.cssutils.css, 'CSSMediaRule', MockMediaRule) monkeypatch.setattr(cssedit.cssutils.css, 'CSSRuleList', MockRuleList) monkeypatch.setattr(cssedit.cssutils.stylesheets, 'MediaList', MockMediaList) testobj = cssedit.Editor() testobj.textdata = [] testobj.texttodata() assert type(testobj.data) == cssedit.cssutils.css.CSSStyleSheet assert capsys.readouterr().out == ('called editor.__init__()\n' 'called stylesheet.__init__()\n') testobj = cssedit.Editor() testobj.textdata = [('stylerule', { 'selectors': ['x'], 'styles': { 'xx': 'yy' } }), ('mediarule', { 'media': ['y'], 'rules': [('stylerule', { 'selectors': ['x'], 'styles': { 'xx': 'yy' } })] }), (cssedit.cssutils.css.CSSComment().typeString, { 'text': 'z' }), ('textrule_2', { 'text': '/* z */' })] testobj.texttodata() assert type(testobj.data) == cssedit.cssutils.css.CSSStyleSheet assert capsys.readouterr().out == ( 'called editor.__init__()\n' 'called stylesheet.__init__()\n' 'called stylerule.__init__()\n' 'called selectorlist.__init__()\n' 'called styledeclaration.__init__()\n' 'called stylesheet.add()\n' 'called mediarule.__init__()\n' 'called medialist.__init__()\n' 'called rulelist.__init__()\n' 'called stylerule.__init__()\n' 'called selectorlist.__init__()\n' 'called styledeclaration.__init__()\n' 'called stylesheet.add()\n' 'called stylesheet.add()\n' 'called stylesheet.add()\n')
def test_init(self, monkeypatch, capsys): def mock_set_logger(filename): f = open(filename, 'w') f.write('log created when parsing data') return f def mock_load(*args): print('called cssedit.load()') return MockStyleSheet() def mock_get(*args): print('called cssedit.get_for_single_tag()') return MockStyleDeclaration() def mock_parse(*args): print('called cssedit.parse()') return MockStyleSheet() with pytest.raises(ValueError): cssedit.Editor() # Not enough arguments with pytest.raises(TypeError): testobj = cssedit.Editor('snork') # positional argument(s) only testobj = cssedit.Editor(new=True) assert testobj.data == [] assert not hasattr(testobj, 'log') with pytest.raises(ValueError): cssedit.Editor(fake=True) # Wrong arguments with pytest.raises(ValueError): cssedit.Editor(filename='text.css', tag='style') # Ambiguous arguments with pytest.raises(ValueError): cssedit.Editor(filename='text.css', text='style') # Ambiguous arguments with pytest.raises(ValueError): cssedit.Editor(tag='style') # Not enough arguments with pytest.raises(ValueError): cssedit.Editor(filename='') # empty filename monkeypatch.setattr(cssedit, 'set_logger', mock_set_logger) monkeypatch.setattr(cssedit, 'load', mock_load) testobj = cssedit.Editor(filename='text.css') assert type(testobj.data) == MockStyleSheet assert capsys.readouterr().out == ('called cssedit.load()\n' 'called stylesheet.__init__()\n') assert testobj.log == ['log created when parsing data'] monkeypatch.setattr(cssedit, 'get_for_single_tag', mock_get) monkeypatch.setattr(cssedit.cssutils.css, 'CSSStyleSheet', MockStyleSheet) monkeypatch.setattr(cssedit.cssutils.css, 'CSSStyleRule', MockStyleRule) testobj = cssedit.Editor(tag='style', text='x') assert capsys.readouterr().out == ( 'called cssedit.get_for_single_tag()\n' 'called styledeclaration.__init__()\n' 'called stylesheet.__init__()\n' 'called stylerule.__init__()\n' 'called stylesheet.add()\n') assert type(testobj.data) == MockStyleSheet monkeypatch.setattr(cssedit, 'parse', mock_parse) testobj = cssedit.Editor(text='x') assert capsys.readouterr().out == ('called cssedit.parse()\n' 'called stylesheet.__init__()\n') assert type(testobj.data) == MockStyleSheet