Best Python code snippet using tavern
test_fgd.py
Source:test_fgd.py
1"""Test the FGD module."""2from srctools import Vec3from srctools.filesys import VirtualFileSystem4from srctools.fgd import *5import pytest6import io7@pytest.mark.parametrize('name1', ['alpha', 'beta', 'gamma'])8@pytest.mark.parametrize('name2', ['alpha', 'beta', 'gamma'])9def test_autovisgroup_cmp(name1: str, name2: str) -> None:10 """Test the AutoVisgroup class is comparable based on name."""11 vis1 = AutoVisgroup(name1, 'SomeParent')12 vis2 = AutoVisgroup(name2, 'SomeParent')13 assert (vis1 == vis2) == (name1 == name2), 'Mismatch'14 assert (vis1 != vis2) == (name1 != name2), 'Mismatch'15 assert (vis1 < vis2) == (name1 < name2), 'Mismatch'16 assert (vis1 > vis2) == (name1 > name2), 'Mismatch'17 assert (vis1 <= vis2) == (name1 <= name2), 'Mismatch'18 assert (vis1 >= vis2) == (name1 >= name2), 'Mismatch'19def test_entity_parse() -> None:20 """Verify parsing an entity produces the correct results."""21 fsys = VirtualFileSystem({'test.fgd': """22@PointClass base(Base1, Base2, Base3) 23base(Base4, base_5)24sphere(radii) 25line(240 180 50, targetname, target)26autovis(Auto, some, group)27appliesto(tag1, tag2, !tag3)28= some_entity: "The description for this prop, which is spread over " 29+ "multiple lines."30 [31 keyvalue1(string): "Name" : "default": "documentation"32 keyvalue2(int): "Hi": 433 keyvalue1[tag](boolean): "Tagged Name": 034 target(target_destination): "An ent"35 36 spawnflags(flags) : "Flags" = [37 1 : "A" : 038 2 : "B" : 139 4 : "C" : 040 8 : "D value" : 0 [old, !good]41 8 : "E" : 1 [new]42 ]43 44 choicelist(choices) : "A Choice" : 0 : "Blahdy blah" = 45 [46 0: "First"47 1: "Second" [new]48 1: "Old second" [-oLd]49 2: "Third"50 "four": "Fourth"51 ]52"""})53 fgd = FGD()54 with fsys:55 fgd.parse_file(fsys, fsys['test.fgd'], eval_bases=False)56 ent = fgd['Some_ENtity']57 assert ent.type is EntityTypes.POINT58 assert ent.classname == 'some_entity'59 assert ent.desc == 'The description for this prop, which is spread over multiple lines.'60 assert ent.bases == ['Base1', 'Base2', 'Base3', 'Base4', 'base_5']61 assert ent.helpers == [62 HelperSphere(255, 255, 255, 'radii'),63 HelperLine(240, 180, 50, 'targetname', 'target'),64 HelperExtAppliesTo(['tag1', 'tag2', '!tag3'])65 ]66 assert ent.kv['keyvalue1', set()] == KeyValues(67 'keyvalue1',68 ValueTypes.STRING,69 'Name',70 'default',71 'documentation',72 )73 assert ent.kv['keyvalue1', {'tag'}] == KeyValues(74 'keyvalue1',75 ValueTypes.BOOL,76 'Tagged Name',77 '0',78 '',79 )80 assert ent.kv['keyvalue2'] == KeyValues(81 'keyvalue2',82 ValueTypes.INT,83 'Hi',84 '4',85 '',86 )87 assert ent.kv['spawnflags'] == KeyValues(88 'spawnflags',89 ValueTypes.SPAWNFLAGS,90 'Flags',91 val_list=[92 (1, 'A', False, frozenset()),93 (2, 'B', True, frozenset()),94 (4, 'C', False, frozenset()),95 (8, 'D value', False, frozenset({'OLD', '!GOOD'})),96 (8, 'E', True, frozenset({'NEW'})),97 ],98 )99 assert ent.kv['choicelist'] == KeyValues(100 'choicelist',101 ValueTypes.CHOICES,102 'A Choice',103 '0',104 'Blahdy blah',105 val_list=[106 ('0', 'First', frozenset()),107 ('1', 'Second', frozenset({'NEW'})),108 ('1', 'Old second', frozenset({'-OLD'})),109 ('2', 'Third', frozenset()),110 ('four', 'Fourth', frozenset()),111 ],112 )113@pytest.mark.parametrize('code, is_readonly, is_report', [114 ('(int): "None"', False, False),115 ('(int) readonly: "Readonly"', True, False),116 ('(*int): "Old Report"', False, True),117 ('(int) report: "New Report"', False, True),118 ('(*int) readonly: "Both old-style"', True, True),119 ('(int) readonly report: "Both new-style"', True, True),120 # 'report readonly' is not allowed.121 ('(*int) report: "Redundant"', False, True),122 ('(*int) readonly report: "Redundant + readonly"', True, True),123])124def test_parse_kv_flags(code, is_readonly, is_report) -> None:125 """Test the readonly and reportable flags can be set."""126 fsys = VirtualFileSystem({'test.fgd': f"""127 @PointClass = ent128 [129 keyvalue{code}: 1130 ]131 """})132 fgd = FGD()133 fgd.parse_file(fsys, fsys['test.fgd'], eval_bases=False)134 kv = fgd['ent'].kv['keyvalue']135 assert kv.readonly is is_readonly, kv136 assert kv.reportable is is_report, kv137def test_export_regressions(file_regression) -> None:138 """Generate a FGD file to ensure code doesn't unintentionally alter output."""139 fgd = FGD()140 base_origin = EntityDef(EntityTypes.BASE, 'Origin')141 base_angles = EntityDef(EntityTypes.BASE, 'Angles')142 ent = EntityDef(EntityTypes.NPC, 'npc_test')143 ent.bases = [base_origin, base_angles]144 fgd.entities = {145 # 'origin': base_origin,146 'angles': base_angles,147 'npc_test': ent,148 }149 base_origin.keyvalues['origin'] = {frozenset(): KeyValues(150 'origin',151 ValueTypes.VEC_ORIGIN,152 'Origin',153 '0 0 0',154 )}155 base_angles.keyvalues['angles'] = {frozenset(): KeyValues(156 'angles',157 ValueTypes.ANGLES,158 'Angles (Pitch Yaw Roll)',159 '0 0 0',160 )}161 ent.helpers = [162 HelperSphere(255.0, 128.0, 64.0, 'radius'),163 HelperModel('models/editor/a_prop.mdl'),164 HelperSize(Vec(-16, -16, -16), Vec(16, 16, 16)),165 ]166 ent.desc = 'Entity description, extending beyond 1000 characters: ' + ', '.join(map(str, range(500))) + '. Done!'167 ent.keyvalues['test_kv'] = {frozenset(): KeyValues(168 'test_kv',169 ValueTypes.COLOR_255,170 'A test keyvalue',171 '255 255 128',172 'Help text for a keyvalue',173 )}174 # Test exporting with blank defaults and description.175 ent.keyvalues['test_both'] = {frozenset(): KeyValues(176 'test_both',177 ValueTypes.STRING,178 'Both default and desc',179 'defaulted',180 'A description',181 )}182 ent.keyvalues['test_desc'] = {frozenset(): KeyValues(183 'test_desc',184 ValueTypes.STRING,185 'just desc',186 desc='A description',187 )}188 ent.keyvalues['test_def'] = {frozenset(): KeyValues(189 'test_def',190 ValueTypes.STRING,191 'Just default',192 default='defaulted',193 )}194 ent.keyvalues['test_neither'] = {frozenset(): KeyValues(195 'test_neither',196 ValueTypes.STRING,197 'Neither',198 )}199 # Special case, boolean must supply default.200 ent.keyvalues['test_bool_neither'] = {frozenset(): KeyValues(201 'test_bool_neither',202 ValueTypes.BOOL,203 'Neither',204 )}205 ent.keyvalues['test_bool_desc'] = {frozenset(): KeyValues(206 'test_bool_desc',207 ValueTypes.BOOL,208 'Neither',209 desc='A description',210 )}211 buf = io.StringIO()212 fgd.export(buf)...
test_decorators.py
Source:test_decorators.py
...24 self.execute_view()25 def test_both(self):26 self.perms({'admin': True, 'reviewer': True})27 self.execute_view()28 def test_neither(self):29 self.perms({'admin': False, 'reviewer': False})30 with self.assertRaises(PermissionDenied):31 self.execute_view()32 def test_no_data(self):33 # No permissions saved to session.34 with self.assertRaises(PermissionDenied):...
test_fizzbuzz.py
Source:test_fizzbuzz.py
...14 small_num = fizzbuzz(9)15 large_num = fizzbuzz(1503)16 assert small_num == "Fizz"17 assert large_num == "Fizz"18def test_neither():19 small_num = fizzbuzz(8)20 large_num = fizzbuzz(1502)21 assert small_num == 8...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!