#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2015 the TicketML authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE.md file.

from __future__ import division, absolute_import, print_function, unicode_literals

import ticketml
from ticketml.ticketml import Emphasis
import unittest
try:
    import unittest.mock as mock
except ImportError:
    import mock
from nose.tools import *


class BackendMixin(object):
    def __init__(self, *args, **kwargs):
        super(BackendMixin, self).__init__(*args, **kwargs)
        self.mock_serial = mock.MagicMock()

    def test_init_sets_left_alignment(self):
        self.mock_serial.write.assert_any_call(b'\x1ba\x00')  # sets alignment to left

    def test_print_text_prints_text(self):
        self.mock_serial.reset_mock()
        self.backend.print_text('ASDF')
        self.mock_serial.write.assert_called_once_with(b'ASDF')

    def test_print_text_encodes_as_cp850(self):
        self.mock_serial.reset_mock()
        self.backend.print_text('£')
        self.mock_serial.write.assert_called_once_with(b'\x9c')

    @raises(UnicodeEncodeError)
    def test_print_text_fails_with_unencodable_text(self):
        self.mock_serial.reset_mock()
        self.backend.print_text('ルーク')


class CbmBackendTests(BackendMixin, unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(CbmBackendTests, self).__init__(*args, **kwargs)
        self.backend = ticketml.CbmBackend(self.mock_serial)

    def test_init_sets_default_printing_mode(self):
        self.mock_serial.write.assert_any_call(b'\x1b!\x00')  # sets printing mode to 0

    def test_printing_mode_change_midline(self):
        self.backend.print_text('hello')
        self.backend.set_emphasis(Emphasis.on)
        self.mock_serial.write.assert_any_call(b'\x1b!\x08')  # sets printing mode to 0


class Ibm4610BackendTests(BackendMixin, unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super(Ibm4610BackendTests, self).__init__(*args, **kwargs)
        self.backend = ticketml.Ibm4610Backend(self.mock_serial)

@@ -348,16 +348,27 @@
 Emphasis
+, Alignment
 %0Aimport 
@@ -1154,24 +1154,396 @@
 h(b'%5Cx9c')%0A%0A
+    def test_cut_considered_as_linebreak(self):%0A        self.backend.print_text('hi')%0A        self.backend.feed_and_cut()%0A        self.assertTrue(self.backend._at_linebreak)%0A        self.mock_serial.reset_mock()%0A        self.backend.set_alignment(Alignment.right)%0A        self.assertTrue(self.mock_serial.write.called)%0A        self.assertTrue(self.backend._at_linebreak)%0A%0A
     @raises(

Test that cutting sets the end-of-line marker