import unittest, pyPdf, sys, os.path
from mock import Mock

SRC = os.path.join(os.path.dirname(__file__), '..', 'src')
sys.path.append(SRC)
import merge

class MockPdfReader:
  def __init__(self):
    self.pages = [None] * 3

  def getNumPages(self):
    return len(self.pages)

  def getPage(self, page_num): pass

class MockPdfWriter:
  def __init__(self):
    self.pages = []

  def write(self, a_file): pass

  def addPage(self, page): self.pages.append(page)

class MergeTest(unittest.TestCase):
  def setUp(self):
    # Stub the global open method inside the merge module
    merge.open = Mock(return_value=True)

    self.front_pages = MockPdfReader()
    self.back_pages = MockPdfReader()
    self.outfile = MockPdfWriter()

    merge.PdfFileReader = Mock(side_effect=[self.front_pages, self.back_pages])
    merge.PdfFileWriter = Mock(return_value=self.outfile)

  def test_merged_file_contains_all_pages(self):
    merge.merge('fake_doc1', 'fake_doc2', 'fake_out', True, False)

    expected_len = len(self.front_pages.pages) + len(self.back_pages.pages)
    self.assertEqual(expected_len, len(self.outfile.pages))

if __name__ == '__main__':
  unittest.main()

@@ -179,32 +179,52 @@
 ef __init__(self
+, pages=(%5BNone%5D * 3)
 ):%0A    self.page
@@ -227,26 +227,21 @@
 pages = 
-%5BNone%5D * 3
+pages
 %0A%0A  def 
@@ -473,16 +473,128 @@
 (page)%0A%0A
+class MockPage:%0A  def __init__(self, text=''):%0A    self.text = text%0A%0A  def extractText(self): return self.text%0A%0A
 class Me
@@ -636,24 +636,70 @@
 etUp(self):%0A
+    def side_effect(arg, *args): return(arg)%0A%0A
     # Stub t
@@ -770,105 +770,169 @@
 ock(
-return_value=True)%0A%0A    self.front_pages = MockPdfReader(
+side_effect=side_effect)%0A%0A    merge.PdfFileReader = Mock(side_effect=side_effect
 )%0A
+%0A
     
-self.back_pages = MockPdfReader()%0A
+# Create a pdf writer and add a handle to self%0A    def create_writer():%0A  
     
@@ -966,88 +966,34 @@
 r()%0A
-%0A
     
-merge.PdfFileReader = Mock(side_effect=%5Bself.front_pages, self.back_pages%5D)
+  return self.outfile%0A
 %0A   
@@ -1024,33 +1024,33 @@
 ock(
-return_value=self.outfile
+side_effect=create_writer
 )%0A%0A 
@@ -1105,44 +1105,111 @@
     
-merge.merge('fake_doc1', 'fake_doc2'
+front_pages = MockPdfReader()%0A    back_pages = MockPdfReader()%0A%0A    merge.merge(front_pages, back_pages
 , 'f
@@ -1255,21 +1255,16 @@
 n = len(
-self.
 front_pa
@@ -1280,21 +1280,16 @@
 ) + len(
-self.
 back_pag
@@ -1359,16 +1359,144 @@
 ages))%0A%0A
+  def test_merged_file_contains_pages_in_correct_order(self):%0A    pass%0A%0A  def test_merging_removes_blank_pages(self):%0A    pass%0A%0A
 if __nam

Make passing Mocks more flexible