###############################################################################
#
# Tests for XlsxWriter.
#
# Copyright (c), 2013-2016, John McNamara, jmcnamara@cpan.org
#

import unittest
from ...compatibility import StringIO
from ...styles import Styles
from ...format import Format


class TestWriteXf(unittest.TestCase):
    """
    Test the Styles _write_xf() method.

    """

    def setUp(self):
        self.fh = StringIO()
        self.styles = Styles()
        self.styles._set_filehandle(self.fh)

    def test_write_xf_1(self):
        """Test the _write_xf() method. Default properties."""
        properties = {}

        xf_format = Format(properties)

        self.styles._write_xf(xf_format)

        exp = """<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>"""
        got = self.fh.getvalue()

        self.assertEqual(got, exp)

    def test_write_xf_2(self):
        """Test the _write_xf() method. Has font but is first XF."""
        properties = {'has_font': 1}

        xf_format = Format(properties)

        self.styles._write_xf(xf_format)

        exp = """<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>"""
        got = self.fh.getvalue()

        self.assertEqual(got, exp)

    def test_write_xf_3(self):
        """Test the _write_xf() method. Has font but isn't first XF."""
        properties = {'has_font': 1, 'font_index': 1}

        xf_format = Format(properties)

        self.styles._write_xf(xf_format)

        exp = """<xf numFmtId="0" fontId="1" fillId="0" borderId="0" xfId="0" applyFont="1"/>"""
        got = self.fh.getvalue()

        self.assertEqual(got, exp)

    def test_write_xf_4(self):
        """Test the _write_xf() method. Uses built-in number format."""
        properties = {'num_format_index': 2}

        xf_format = Format(properties)

        self.styles._write_xf(xf_format)

        exp = """<xf numFmtId="2" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>"""
        got = self.fh.getvalue()

        self.assertEqual(got, exp)

    def test_write_xf_5(self):
        """Test the _write_xf() method. Uses built-in number format + font."""
        properties = {'num_format_index': 2, 'has_font': 1, 'font_index': 1}

        xf_format = Format(properties)

        self.styles._write_xf(xf_format)

        exp = """<xf numFmtId="2" fontId="1" fillId="0" borderId="0" xfId="0" applyNumberFormat="1" applyFont="1"/>"""
        got = self.fh.getvalue()

        self.assertEqual(got, exp)

    def test_write_xf_6(self):
        """Test the _write_xf() method. Vertical alignment = top."""
        properties = {'align': 'top'}

        xf_format = Format(properties)

        self.styles._write_xf(xf_format)

        exp = """<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyAlignment="1"><alignment vertical="top"/></xf>"""
        got = self.fh.getvalue()