def test_set_continue_with_breaks(self): """ Tests the behavior of continue when there are breakpoints in the mix. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) line_offset = 8 # The difference in the set_break call and line_2. db.set_break( self.filename, sys._getframe().f_lineno + line_offset, ) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True line_2 = True line_3 = True # Assert we only got to line_1 because of the breakpoint. # These are split up to give more helpful messages if the test fails. self.assertTrue(line_1) self.assertFalse(line_2) self.assertFalse(line_3) # We are still in stepping mode so we should be reporting the stack. self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' db.get_line(self.filename, db.curframe.f_lineno),') sys.settrace(None) db = Qdb(cmd_manager=QueueCommandManager) line_2_offset = 13 # The difference in the set_break call and line_2. line_3_offset = 10 # THe difference in the set_break call and line_3. db.set_break( self.filename, sys._getframe().f_lineno + line_2_offset, ) db.set_break( self.filename, sys._getframe().f_lineno + line_3_offset, ) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True line_2 = True line_3 = True self.assertTrue(line_1) self.assertTrue(line_2) self.assertFalse(line_3) self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' db.get_line(self.filename, db.curframe.f_lineno),')
def test_function_call_next_step(self): """ Tests the functionality of next and step when calling functions. This checks to make sure the function is stepped into and can be stepped over. """ cmd_manager = QueueCommandManager() db = Qdb(cmd_manager=cmd_manager) # Queue up a next command to next over the function call. cmd_manager.enqueue(lambda t: t.set_next(t.curframe)) # Queue up a sleep so that we block after calling next. # This would cause us to NOT execute the f_called[0] = True line of f # had we only called set_step. This is asserted afterwards. cmd_manager.user_wait(0.2) # A mutable structure to check if f is called. f_called = NonLocal(False) def f(): f_called.value = True with Timeout(0.1, False): db.set_trace() f() # We hit that line in f, so it should now be True. self.assertTrue(f_called.value) # Assert that we are currently executing the line we think we should # be executing. Since we are just stepping, this should be setting # curframe each time. self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' db.get_line(self.filename, db.curframe.f_lineno),' ) db.disable() cmd_manager = QueueCommandManager() db = Qdb(cmd_manager=cmd_manager) f_called = NonLocal(False) # This time we will be only stepping, so we should not execute the # entire call to f. cmd_manager.enqueue(lambda t: t.set_step()) cmd_manager.user_wait(1.2) with Timeout(0.1, False): db.set_trace() f() # We should not have hit this line in f. self.assertFalse(f_called.value) # Since we only stepped once, this is the last time we set the frame. self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' f_called.value = True' )
def test_set_continue_no_breaks(self): """ Asserts that set_continue works with no breakpoints. """ db = Qdb(cmd_manager=QueueCommandManager) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True # EDIT IN BOTH PLACES line_2 = True line_3 = True # Assert that we hit all three lines event though interaction is # blocked. self.assertTrue(line_1 and line_2 and line_3) # As this was the last time we were supposed to stop, this should be # the curframe data. self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' line_1 = True # EDIT IN BOTH PLACES' )
def test_set_continue_no_breaks(self): """ Asserts that set_continue works with no breakpoints. """ cmd_manager = QueueCommandManager() db = Qdb(cmd_manager=cmd_manager) cmd_manager.enqueue(lambda t: t.set_continue()) cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True # EDIT IN BOTH PLACES line_2 = True line_3 = True # Assert that we hit all three lines event though interaction is # blocked. self.assertTrue(line_1 and line_2 and line_3) # As this was the last time we were supposed to stop, this should be # the curframe data. self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' line_1 = True # EDIT IN BOTH PLACES' )
def test_file_cache_from_string(self): """ Asserts that manual caching from a string works. """ contents = dedent("""\ line 1 line 2 line 3 line 4 """) db = Qdb(cmd_manager=NopCommandManager()) db.cache_file('file', contents=contents) # Check the whole 'file'. self.assertEquals(db.get_file('file'), contents[:-1]) # drop '\n' for n in range(1, 5): # Check all the lines. self.assertEquals('line %d' % n, db.get_line('file', n))
def test_file_cache_from_string(self): """ Asserts that manual caching from a string works. """ contents = dedent( """\ line 1 line 2 line 3 line 4 """ ) db = Qdb(cmd_manager=NopCommandManager()) db.cache_file('file', contents=contents) # Check the whole 'file'. self.assertEquals(db.get_file('file'), contents[:-1]) # drop '\n' for n in range(1, 5): # Check all the lines. self.assertEquals('line %d' % n, db.get_line('file', n))
def test_file_cache_from_disk(self): """ Asserts that the disk caching works. """ # We will use this file, as it is the only file we know that exists. # The first time this is run after a change, __file__ will point to # the source code file; however, if we run this twice in a row, it # points to the byte-compiled file. filename = fix_filename(__file__) db = Qdb(cmd_manager=NopCommandManager()) db.cache_file(filename) with open(filename) as f: contents = f.read()[:-1] # Drop the last newline. # Assert that querying the entire file works. self.assertEqual(db.get_file(filename), contents) for n, line in zip(count(start=1), contents.splitlines()): # Iterate over all the lines of the file, asserting that we # have saved them correctly. This also asserts that the line # indexing is working as intended. self.assertEqual(db.get_line(filename, n), line)
def test_file_cache_from_disk(self): """ Asserts that the disk caching works. """ # We will use this file, as it is the only file we know that exists. # The first time this is run after a change, __file__ will point to # the source code file; however, if we run this twice in a row, it # points to the byte-compiled file. filename = fix_filename(__file__) db = Qdb(cmd_manager=NopCommandManager()) db.cache_file(filename) with open(filename) as f: contents = f.read()[:-1] # Drop the last newline. # Assert that querying the entire file works. self.assertEquals(db.get_file(filename), contents) for n, line in zip(count(start=1), contents.splitlines()): # Iterate over all the lines of the file, asserting that we # have saved them correctly. This also asserts that the line # indexing is working as intended. self.assertEquals(db.get_line(filename, n), line)
def test_set_continue_with_breaks(self): """ Tests the behavior of continue when there are breakpoints in the mix. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) line_offset = 8 # The difference in the set_break call and line_2. db.set_break( self.filename, sys._getframe().f_lineno + line_offset, ) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True line_2 = True line_3 = True # Assert we only got to line_1 because of the breakpoint. # These are split up to give more helpful messages if the test fails. self.assertTrue(line_1) self.assertFalse(line_2) self.assertFalse(line_3) # We are still in stepping mode so we should be reporting the stack. self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' db.get_line(self.filename, db.curframe.f_lineno),' ) sys.settrace(None) db = Qdb(cmd_manager=QueueCommandManager) line_2_offset = 13 # The difference in the set_break call and line_2. line_3_offset = 10 # THe difference in the set_break call and line_3. db.set_break( self.filename, sys._getframe().f_lineno + line_2_offset, ) db.set_break( self.filename, sys._getframe().f_lineno + line_3_offset, ) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True line_2 = True line_3 = True self.assertTrue(line_1) self.assertTrue(line_2) self.assertFalse(line_3) self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' db.get_line(self.filename, db.curframe.f_lineno),' )