import unittest
import collections
from fontTools.misc.py23 import basestring
from fontParts.base import FontPartsError


class TestComponent(unittest.TestCase):

    def getComponent_generic(self):
        layer, _ = self.objectGenerator("layer")
        glyph = layer.newGlyph("A")
        pen = glyph.getPen()
        pen.moveTo((0, 0))
        pen.lineTo((0, 100))
        pen.lineTo((100, 100))
        pen.lineTo((100, 0))
        pen.closePath()
        for i, point in enumerate(glyph[0].points):
            point.name = "point %d" % i
        glyph = layer.newGlyph("B")
        component = glyph.appendComponent("A")
        component.transformation = (1, 0, 0, 1, 0, 0)
        return component

    # ----
    # repr
    # ----

    def test_reprContents(self):
        component = self.getComponent_generic()
        value = component._reprContents()
        self.assertIsInstance(value, list)
        for i in value:
            self.assertIsInstance(i, basestring)

    def test_reprContents_noGlyph(self):
        component, _ = self.objectGenerator("component")
        value = component._reprContents()
        self.assertIsInstance(value, list)
        for i in value:
            self.assertIsInstance(i, basestring)

    # -------
    # Parents
    # -------

    def test_get_parent_font(self):
        font, _ = self.objectGenerator("font")
        layer = font.newLayer("L")
        glyph = layer.newGlyph("X")
        component = glyph.appendComponent("A")
        self.assertIsNotNone(component.font)
        self.assertEqual(
            component.font,
            font
        )

    def test_get_parent_noFont(self):
        layer, _ = self.objectGenerator("layer")
        glyph = layer.newGlyph("X")
        component = glyph.appendComponent("A")
        self.assertIsNone(component.font)

    def test_get_parent_layer(self):
        layer, _ = self.objectGenerator("layer")
        glyph = layer.newGlyph("X")
        component = glyph.appendComponent("A")
        self.assertIsNotNone(component.layer)
        self.assertEqual(
            component.layer,
            layer
        )

    def test_get_parent_noLayer(self):
        glyph, _ = self.objectGenerator("glyph")