class Shell(object): def __init__(self): self.subject = Subject() def add_run_observer(self, observer): self.subject.add_observer(observer) def remove_run_observer(self, observer): self.subject.remove_observer(self, observer) def check_open(self, args, input_string=None): self.subject.notify(' '.join(args)) return check_open(args, input_string)
class SubjectTestCase(unittest.TestCase): def setUp(self): self.count1 = 0 self.count2 = 0 self.count3 = 0 self.subject = Subject() def event1(self): self.count1 += 1 def event2(self): self.count2 += 1 def remove_during_event(self): self.count3 += 1 self.subject.remove_observer(self.remove_during_event) def test_simple(self): self.subject.add_observer(self.event1) self.subject.notify() self.assertEqual(1, self.count1) self.subject.remove_observer(self.event1) self.subject.notify() self.assertEqual(1, self.count1) self.subject.add_observer(self.event1) self.subject.add_observer(self.event2) self.subject.notify() self.assertEqual(2, self.count1) self.assertEqual(1, self.count2) def test_multiple(self): self.subject.add_observer(self.event1) self.subject.add_observer(self.event1) self.subject.notify() self.assertEqual(2, self.count1) def test_remove_during_event(self): self.subject.add_observer(self.event1) self.subject.add_observer(self.remove_during_event) self.subject.add_observer(self.event2) self.subject.notify() self.assertEqual(1, self.count1) self.assertEqual(1, self.count2) self.assertEqual(1, self.count3) self.subject.notify() self.assertEqual(2, self.count1) self.assertEqual(2, self.count2) self.assertEqual(1, self.count3)
class GDBDebugger(object): def __init__(self, gdb_stub): self.gdb_stub = gdb_stub self.gdb_stub.execute_sync('set breakpoint pending on') self.exit_subject = Subject() gdb_stub.add_event_observer(self.exit_subject.notify) def add_symbol_file(self, symbol_file, sections): out = StringIO() out.write('add-symbol-file /home/pag/Code/drk/src/core/%s ' % (symbol_file)) out.write(sections['.text']) del sections['.text'] for section in sections.iteritems(): out.write(' -s %s %s' % (section[0], section[1])) out.write(' -readnow') self.gdb_stub.execute_sync(out.getvalue()) def set_main_symbol_file(self, symbol_file): self.gdb_stub.execute_sync('file %s' % symbol_file) def add_breakpoint(self, symbol): self.gdb_stub.execute_sync('break %s' % symbol) def clear_symbols(self): self.gdb_stub.execute_sync('file') def clear_breakpoints(self): self.gdb_stub.execute('delete') def attach_tcp(self, host, port): self.gdb_stub.execute_sync('target remote %s:%s' % (host, str(port))) def detach(self): self.clear_breakpoints() self.clear_symbols() self.gdb_stub.execute_sync('detach') def add_exit_observer(self, observer): self.exit_subject.add_observer(observer) def remove_exit_observer(self, observer): self.exit_subject.remove_observer(observer) def pause(self): self.gdb_stub.pause() def resume(self): self.gdb_stub.execute('continue')