def test_cut_calls_copy_then_cuts_and_remembers_some_stuff(self):
        clipboard = Clipboard()
        sheet = Sheet()
        user = User()
        user.username = '******'
        user.save()
        sheet.owner = user
        clipboard.copy = Mock(wraps=clipboard.copy)

        worksheet = Worksheet()
        worksheet.A1.formula = 'outside'
        worksheet.A2.formula = 'inside'
        sheet.jsonify_worksheet(worksheet)
        sheet.save()

        clipboard.cut(sheet, (1, 2), (3, 4))

        self.assertCalledOnce(clipboard.copy, sheet, (1, 2), (3, 4))

        worksheet = sheet.unjsonify_worksheet()

        self.assertEquals(worksheet.A1.formula, 'outside')
        self.assertEquals(worksheet.A2, Cell())

        self.assertEquals(clipboard.source_left, 1)
        self.assertEquals(clipboard.source_top, 2)
        self.assertEquals(clipboard.source_right, 3)
        self.assertEquals(clipboard.source_bottom, 4)

        self.assertEquals(clipboard.is_cut, True)
        self.assertEquals(clipboard.source_sheet, sheet)
    def test_cut_then_paste_same_sheet(self):
        sheet = Sheet()
        worksheet = Worksheet()
        worksheet.A1.formula = 'safe'
        worksheet.A2.formula = 'topleft'
        worksheet.B2.formula = 'topright'
        worksheet.A3.formula = 'bottomleft'
        worksheet.B3.formula = 'bottomright'
        sheet.jsonify_worksheet(worksheet)

        clipboard = Clipboard()
        clipboard.cut(sheet, (1, 2), (2, 3))

        updated_worksheet = sheet.unjsonify_worksheet()
        self.assertEquals(updated_worksheet.A1.formula, 'safe')
        self.assertEquals(updated_worksheet.A2, Cell() )
        self.assertEquals(updated_worksheet.B2, Cell() )
        self.assertEquals(updated_worksheet.A3, Cell() )

        clipboard.paste_to(sheet, (2, 3), (2, 3))

        updated_worksheet = sheet.unjsonify_worksheet()

        self.assertEquals(updated_worksheet.A1.formula, 'safe')
        self.assertEquals(updated_worksheet.A2, Cell() )
        self.assertEquals(updated_worksheet.B2, Cell() )
        self.assertEquals(updated_worksheet.A3, Cell() )
        self.assertEquals(updated_worksheet.B3.formula, 'topleft')
        self.assertEquals(updated_worksheet.C3.formula, 'topright')
        self.assertEquals(updated_worksheet.C4.formula, 'bottomright')
        self.assertEquals(updated_worksheet.B4.formula, 'bottomleft')
    def test_cut_calls_copy_then_cuts_and_remembers_some_stuff(self):
        clipboard = Clipboard()
        sheet = Sheet()
        user = User()
        user.username = '******'
        user.save()
        sheet.owner = user
        clipboard.copy = Mock(wraps=clipboard.copy)

        worksheet = Worksheet()
        worksheet.A1.formula = 'outside'
        worksheet.A2.formula = 'inside'
        sheet.jsonify_worksheet(worksheet)
        sheet.save()

        clipboard.cut(sheet, (1, 2), (3, 4))

        self.assertCalledOnce(clipboard.copy, sheet, (1, 2), (3, 4))

        worksheet = sheet.unjsonify_worksheet()

        self.assertEquals(worksheet.A1.formula, 'outside')
        self.assertEquals(worksheet.A2, Cell())

        self.assertEquals(clipboard.source_left, 1)
        self.assertEquals(clipboard.source_top, 2)
        self.assertEquals(clipboard.source_right, 3)
        self.assertEquals(clipboard.source_bottom, 4)

        self.assertEquals(clipboard.is_cut, True)
        self.assertEquals(clipboard.source_sheet, sheet)
    def test_cut_then_paste_same_sheet(self):
        sheet = Sheet()
        worksheet = Worksheet()
        worksheet.A1.formula = 'safe'
        worksheet.A2.formula = 'topleft'
        worksheet.B2.formula = 'topright'
        worksheet.A3.formula = 'bottomleft'
        worksheet.B3.formula = 'bottomright'
        sheet.jsonify_worksheet(worksheet)

        clipboard = Clipboard()
        clipboard.cut(sheet, (1, 2), (2, 3))

        updated_worksheet = sheet.unjsonify_worksheet()
        self.assertEquals(updated_worksheet.A1.formula, 'safe')
        self.assertEquals(updated_worksheet.A2, Cell())
        self.assertEquals(updated_worksheet.B2, Cell())
        self.assertEquals(updated_worksheet.A3, Cell())

        clipboard.paste_to(sheet, (2, 3), (2, 3))

        updated_worksheet = sheet.unjsonify_worksheet()

        self.assertEquals(updated_worksheet.A1.formula, 'safe')
        self.assertEquals(updated_worksheet.A2, Cell())
        self.assertEquals(updated_worksheet.B2, Cell())
        self.assertEquals(updated_worksheet.A3, Cell())
        self.assertEquals(updated_worksheet.B3.formula, 'topleft')
        self.assertEquals(updated_worksheet.C3.formula, 'topright')
        self.assertEquals(updated_worksheet.C4.formula, 'bottomright')
        self.assertEquals(updated_worksheet.B4.formula, 'bottomleft')
    def test_paste_from_cut_rewrites_source_worksheet_formulae_before_pasting(
            self, mock_rewrite_source_sheet_formulae):
        source_worksheet = Worksheet()
        source_worksheet.A1.formula = '=pre-rewrite'
        source_sheet = Sheet()
        source_sheet.jsonify_worksheet(source_worksheet)

        clipboard = Clipboard()
        clipboard.cut(source_sheet, (1, 2), (3, 5))
        clipboard.to_cells = Mock()
        clipboard.to_cells.return_value = {}.items()

        def check_to_cells_not_run(*args):
            self.assertFalse(clipboard.to_cells.call_args_list)

        mock_rewrite_source_sheet_formulae.side_effect = check_to_cells_not_run

        clipboard.paste_to(source_sheet, (2, 3), (2, 3))

        self.assertCalledOnce(mock_rewrite_source_sheet_formulae,
                              source_worksheet, (1, 2, 3, 5), 2, 3)
    def test_paste_onto_different_sheet_from_cut_does_not_rewrite_source_sheet_formulae(
            self, mock_rewrite_source_sheet_formulae
        ):
        source_worksheet = Worksheet()
        source_worksheet.A1.formula = '=pre-rewrite'
        source_sheet = Sheet()
        source_sheet.jsonify_worksheet(source_worksheet)

        dest_sheet = Mock()

        clipboard = Clipboard()
        clipboard.cut(source_sheet, (1, 2), (3, 5))
        clipboard.to_cells = Mock()
        clipboard.to_cells.return_value = {}.items()

        def check_to_cells_not_run(*args):
            self.assertFalse(clipboard.to_cells.call_args_list)

        mock_rewrite_source_sheet_formulae.side_effect = check_to_cells_not_run

        clipboard.paste_to(dest_sheet, (2, 3), (2, 3))

        self.assertFalse( mock_rewrite_source_sheet_formulae.called )