Best Python code snippet using nose
test_primitives.py
Source:test_primitives.py
...28 s_qword(0xFFFFFFFFFFFFFFFF // 2, output_format="ascii", signed=True, name="qword_2")29 s_qword(0xFFFFFFFFFFFFFFFF // 2 + 1, output_format="ascii", signed=True, name="qword_3")30 s_qword(0xFFFFFFFFFFFFFFFF, output_format="ascii", signed=True, name="qword_4")31 req = s_get("UNIT TEST 1")32 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="byte_1").render(), b"0")33 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="byte_2").render(), b"127")34 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="byte_3").render(), b"-128")35 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="byte_4").render(), b"-1")36 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="word_1").render(), b"0")37 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="word_2").render(), b"32767")38 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="word_3").render(), b"-32768")39 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="word_4").render(), b"-1")40 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="dword_1").render(), b"0")41 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="dword_2").render(), b"2147483647")42 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="dword_3").render(), b"-2147483648")43 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="dword_4").render(), b"-1")44 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="qword_1").render(), b"0")45 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="qword_2").render(), b"9223372036854775807")46 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="qword_3").render(), b"-9223372036854775808")47 self.assertEqual(req.resolve_name(context_path="UNIT TEST 1", name="qword_4").render(), b"-1")48 def test_string(self):49 s_initialize("STRING UNIT TEST 1")50 s_string("foo", size=200, name="sized_string")51 req = s_get("STRING UNIT TEST 1")52 self.assertEqual(len(req.resolve_name(context_path="STRING UNIT TEST 1", name="sized_string").render()), 200)53 # check that string padding and truncation are working correctly.54 mutations_generator = req.get_mutations()55 for i in xrange(0, 50):56 next(mutations_generator)57 self.assertEqual(58 len(req.resolve_name(context_path="STRING UNIT TEST 1", name="sized_string").render()), 20059 )60 def test_s_mirror(self):61 test_group_values = [b"a", b"bb", b"ccc", b"dddd"]62 s_initialize("test_s_mirror")63 s_size("data", output_format="ascii", fuzzable=False, name="size")64 s_mirror(".size", name="size_mirror")65 with s_block("data"):66 s_static("<")67 s_group("group_start", default_value=b"x", values=test_group_values)68 s_static(">")69 s_static("hello")70 s_static("</")71 s_mirror("data.group_start", name="group_end")72 s_static(">")73 req = s_get("test_s_mirror")74 mutations_generator = req.get_mutations()75 for _ in xrange(len(test_group_values)):76 next(mutations_generator)77 group_start_value = req.resolve_name(context_path="test_s_mirror", name="group_start").render()78 self.assertEqual(79 int(req.resolve_name(context_path="test_s_mirror", name="size").render()),80 len("<{0}>hello</{0}>".format(group_start_value.decode("utf-8"))),81 )82 self.assertEqual(83 req.resolve_name(context_path="test_s_mirror", name="group_end").render(), group_start_value84 )85 self.assertEqual(86 req.resolve_name(context_path="test_s_mirror", name="size_mirror").render(),87 req.resolve_name(context_path="test_s_mirror", name="size").render(),88 )89 def test_bytes(self):90 # test if s_bytes works with empty input91 s_initialize("test_bytes_empty")92 s_bytes(b"", name="bytes_empty")93 req = s_get("test_bytes_empty")94 mutations_generator = req.get_mutations()95 with self.assertRaises(StopIteration):96 while next(mutations_generator):97 req.resolve_name(context_path="test_bytes_empty", name="bytes_empty").render()98 # test if max_len works99 s_initialize("test_bytes_max_len")100 s_bytes(b"12345", name="bytes_max_len", max_len=5)101 req = s_get("test_bytes_max_len")102 mutations_generator = req.get_mutations()103 with self.assertRaises(StopIteration):104 while next(mutations_generator):105 self.assertLessEqual(106 len(req.resolve_name(context_path="test_bytes_max_len", name="bytes_max_len").render()), 5107 )108 # test if size works109 s_initialize("test_bytes_size")110 s_bytes(b"1234567", name="bytes_size", size=7, padding=b"A")111 req = s_get("test_bytes_size")112 mutations_generator = req.get_mutations()113 with self.assertRaises(StopIteration):114 while next(mutations_generator):115 self.assertEqual(len(req.resolve_name(context_path="test_bytes_size", name="bytes_size").render()), 7)116 # test if settting fuzzable to false works117 s_initialize("test_bytes_fuzzable")118 s_bytes(b"1234567", name="bytes_fuzzable", fuzzable=False)119 s_get("test_bytes_fuzzable")120 req = s_get("test_bytes_fuzzable")121 mutations_generator = req.get_mutations()122 with self.assertRaises(StopIteration):123 next(mutations_generator)124 @pytest.mark.skip(reason="Feature not implemented")125 def test_fuzz_extension(self):126 fuzz_strings_path = os.path.join(os.path.dirname(__file__), os.pardir, ".fuzz_strings")127 fuzz_ints_path = os.path.join(os.path.dirname(__file__), os.pardir, ".fuzz_ints")128 # backup existing fuzz extension libraries.129 try:130 shutil.move(fuzz_strings_path, fuzz_strings_path + "_backup")131 shutil.move(fuzz_ints_path, fuzz_ints_path + "_backup")132 except FileNotFoundError:133 pass134 # create extension libraries for unit test.135 with open(fuzz_strings_path, "w") as fh:136 fh.write("pedram\n")137 fh.write("amini\n")138 with open(fuzz_ints_path, "w") as fh:139 fh.write("deadbeef\n")140 fh.write("0xc0cac01a\n")141 s_initialize("EXTENSION TEST")142 s_string("foo", name="string")143 s_int(200, name="int")144 s_char(ord("A"), name="char")145 req = s_get("EXTENSION TEST")146 # restore existing fuzz extension libraries.147 try:148 shutil.move(fuzz_strings_path + "_backup", fuzz_strings_path)149 shutil.move(fuzz_ints_path + "_backup", fuzz_ints_path)150 except FileNotFoundError:151 os.remove(fuzz_strings_path)152 os.remove(fuzz_ints_path)153 # these should be here now.154 self.assertIn("pedram", req.resolve_name(context_path="EXTENSION TEST", name="string")._fuzz_library)155 self.assertIn("amini", req.resolve_name(context_path="EXTENSION TEST", name="string")._fuzz_library)156 self.assertIn(0xDEADBEEF, req.resolve_name(context_path="EXTENSION TEST", name="int")._fuzz_library)157 self.assertIn(0xC0CAC01A, req.resolve_name(context_path="EXTENSION TEST", name="int")._fuzz_library)158 # these should not as a char is too small to store them.159 self.assertNotIn(0xDEADBEEF, req.resolve_name(context_path="EXTENSION TEST", name="char")._fuzz_library)160 self.assertNotIn(0xC0CAC01A, req.resolve_name(context_path="EXTENSION TEST", name="char")._fuzz_library)161if __name__ == "__main__":...
MessageResolver.py
Source:MessageResolver.py
...24 self.dm = dm25 self.dui = dui26 self.true_player = self.dm.get_true_player()27 def monster_killed(self, monster, by_player):28 _name = self.resolve_name(monster)29 _level = self.dm.dungeon_levels[monster.curr_level]30 if by_player:31 if _level.is_cyberspace():32 options = ['delete', 'expunge']33 else:34 options = ['waste', 'dust', 'kill']35 if randrange(2) == 0:36 _mess = _name + ' is toast.'37 else:38 _mess = 'You %s %s' % (choice(options), _name)39 else:40 _mess = _name + ' is killed.'41 42 alert = VisualAlert(monster.row, monster.col, _mess, '')43 alert.show_alert(self.dm, False)44 45 def parse(self, agent, verb):46 if verb not in _verbs:47 if agent == self.dm.player:48 return verb49 else:50 return verb + 's'51 else:52 return _verbs[verb][agent == self.dm.player]53 54 def pick_up_message(self, agent, item, slot):55 _msg = self.resolve_name(agent) + ' ' + self.parse(agent, agent.pick_up_verb())56 _item = item.get_name(1)57 _art = get_correct_article(_item)58 _msg += ' up '59 if _art != '':60 _msg += _art + ' '61 if slot == '':62 _msg += _item + '.'63 else:64 _msg += _item + ' (' + slot + ').'65 alert = VisualAlert(agent.row, agent.col, _msg, '')66 alert.show_alert(self.dm, False)67 68 def resolve_name(self, agent):69 _level = self.dm.dungeon_levels[agent.curr_level]70 if agent == self.dm.player:71 return 'you'72 elif self.dm.is_occupant_visible_to_agent(self.dm.player, agent):73 return agent.get_name()74 elif self.dm.is_occupant_visible_to_agent(self.dm.get_true_player(), agent):75 return agent.get_name()76 else:77 return 'it'78 79 def put_on_item(self, agent, item):80 _msg = self.resolve_name(agent) + ' ' + self.parse(agent, 'put')81 _item = item.get_full_name()82 _msg += ' on the ' + item.get_full_name() + '.'83 alert = VisualAlert(agent.row, agent.col, _msg, '')84 alert.show_alert(self.dm, False)85 def simple_verb_action(self, subject, text, verbs, pause_for_more=False):86 verbs = tuple([self.parse(subject, v) for v in verbs])87 _name = self.resolve_name(subject)88 _mess = _name + (text % verbs)89 90 self.dm.alert_player(subject.row, subject.col, _mess, pause_for_more)91 def shot_message(self, victim):92 _verb = self.parse(victim, 'etre')93 _mess = self.resolve_name(victim) + ' ' + _verb + ' hit.'94 self.dm.alert_player(victim.row, victim.col, _mess)95 96 def show_hit_message(self, tori, uke, verb): 97 if tori == self.dm.player:98 _mess = 'You ' + self.parse(tori, verb) + ' ' + \99 self.resolve_name(uke) + '!'100 elif tori.get_name() in ('the lolcat', 'the ceiling cat'):101 _mess = self.resolve_name(tori) + ' has bited you.'102 elif uke == self.dm.player:103 _mess = self.resolve_name(tori) + ' hits you!'104 elif uke == self.true_player:105 _mess = self.resolve_name(tori) + ' hits your meatsack!'106 else: 107 _mess = self.resolve_name(tori) + ' hits ' + self.resolve_name(uke)108 109 self.dm.alert_player(tori.row, tori.col, _mess)110 111 def show_miss_message(self, tori, uke):112 if uke == self.dm.player or uke == self.true_player:113 _mess = self.resolve_name(tori) + ' misses you!'114 else:115 _mess = "%s %s %s!" % (self.resolve_name(tori), self.parse(tori, 'miss'), self.resolve_name(uke))116 117 self.dm.alert_player(uke.row, uke.col, _mess)118 119 def thrown_message(self, item, target):120 _mess = item.get_name() + ' hits ' + self.resolve_name(target) + '.'121 self.dm.alert_player(target.row, target.col, _mess)...
test_utils.py
Source:test_utils.py
...13 h = add_host('test1')14 d = add_device('node1')15 he = h.add_interface('eth')16 de = d.add_interface('eth1')17 assert h == resolve_name('test1')18 assert d == resolve_name('node1')19 assert he == resolve_name('test1.eth', model=True)20 assert he == resolve_name('test1.eth')21 assert de == resolve_name('node1.eth1', laboratory=True)22 assert de == resolve_name('node1.eth1')23 py.test.raises(Exceptions.ConfigurationError, resolve_name, 'foo')24 py.test.raises(Exceptions.ConfigurationError, resolve_name, 'test1.')25 py.test.raises(Exceptions.ConfigurationError, resolve_name, '.test1')26 py.test.raises(Exceptions.ConfigurationError, resolve_name, 'node1.eth1', model=True)27 py.test.raises(Exceptions.ConfigurationError, resolve_name, 'test1.eth', laboratory=True)28def test_name_resolving():29 destroy_model()30 create_model('test-model')31 destroy_laboratory()32 create_laboratory('test-lab')33 h = add_host('test1')34 d = add_device('node1')35 he = h.add_interface('eth')36 de = d.add_interface('eth1')...
io_service_interface.py
Source:io_service_interface.py
...7from industrial_msgs.msg import ServiceReturnCode8class IOServiceInterface:9 def __init__(self, ip):10 self.lebai_robot_ = LebaiRobot(ip, False)11 self.srv_set_robot_do_ = rospy.Service(rospy.resolve_name('~set_robot_do'), SetDO, self.cmd_set_robot_do)12 self.srv_set_robot_ao_ = rospy.Service(rospy.resolve_name('~set_robot_ao'), SetAO, self.cmd_set_robot_ao)13 self.srv_set_robot_ao_mode_ = rospy.Service(rospy.resolve_name('~set_robot_ao_mode'), SetAMode, self.cmd_set_robot_ao_mode)14 self.srv_set_robot_ai_mode_ = rospy.Service(rospy.resolve_name('~set_robot_ai_mode'), SetAMode, self.cmd_set_robot_ai_mode)15 self.srv_set_flange_do_ = rospy.Service(rospy.resolve_name('~set_flange_do'), SetDO, self.cmd_set_flange_do)16 self.srv_set_gripper_position_ = rospy.Service(rospy.resolve_name('~set_gripper_position'), SetGripper, self.cmd_set_gripper_position)17 self.srv_set_gripper_force_ = rospy.Service(rospy.resolve_name('~set_gripper_force'), SetGripper, self.cmd_set_gripper_force)18 def cmd_set_robot_do(self, req):19 res = SetDOResponse()20 self.lebai_robot_.set_do(req.pin, req.value)21 res.code.val = ServiceReturnCode.SUCCESS22 return res23 def cmd_set_robot_ao(self, req):24 res = SetAOResponse()25 self.lebai_robot_.set_ao(req.pin, req.value)26 res.code.val = ServiceReturnCode.SUCCESS27 return res28 def cmd_set_robot_ao_mode(self, req):29 res = SetAModeResponse()30 self.lebai_robot_.set_ao_mode(req.pin, req.mode)31 res.code.val = ServiceReturnCode.SUCCESS...
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!!