# Path: tests/test_helpers.py
# def write_designspace_and_UFOs(designspace, path):
#     for source in designspace.sources:
#         basename = os.path.basename(source.filename)
#         ufo_path = os.path.join(os.path.dirname(path), basename)
#         source.filename = basename
#         source.path = ufo_path
#         _save_overwrite_ufo(source.font, ufo_path)
# 
#     designspace.write(path)


import os
import glyphsLib
import pytest
import py.path
from fontTools.designspaceLib import DesignSpaceDocument
from glyphsLib.builder.instances import apply_instance_data
from ..test_helpers import write_designspace_and_UFOs
    from glyphsLib.interpolation import apply_instance_data as reexported
    # Reencode Glyphs: A.alt=0041, C=
    assert ufos[1]["A"].unicode is None
    assert ufos[1]["A.alt"].unicode == 0x0041
    assert ufos[1]["C"].unicode is None


def test_glyphs3_mapping():
    font = glyphsLib.GSFont(os.path.join(DATA, "Glyphs3Instances.glyphs"))
    # Instance1: designspace 200 -> userspace 400
    # Instance2: designspace 800 -> userspace 900
    # Instance2: designspace 600 -> userspace 650
    doc = glyphsLib.to_designspace(font)
    assert doc.axes[0].map == [(400, 200), (600, 650), (900, 800)]
    assert doc.instances[0].location == {"Weight": 200}
    assert doc.instances[1].location == {"Weight": 800}
    assert doc.instances[2].location == {"Weight": 650}


def test_glyphs3_instance_filtering():
    font = glyphsLib.GSFont(os.path.join(DATA, "InstanceFamilyName-G3.glyphs"))
    assert len(font.instances) == 6

    # Loaded from default font family name
    assert not font.instances[0].properties
    assert not font.instances[1].properties
    assert font.instances[0].familyName == "MyFamily"
    assert font.instances[1].familyName == "MyFamily"

    # Loaded from .properties
    assert font.instances[2].familyName == "MyFamily 12pt"

# Path: tests/test_helpers.py
# def write_designspace_and_UFOs(designspace, path):
#     for source in designspace.sources:
#         basename = os.path.basename(source.filename)
#         ufo_path = os.path.join(os.path.dirname(path), basename)
#         source.filename = basename
#         source.path = ufo_path
#         _save_overwrite_ufo(source.font, ufo_path)
# 
#     designspace.write(path)


# Path: tests/builder/instances_test.py
import os
import glyphsLib
import pytest
import py.path
from fontTools.designspaceLib import DesignSpaceDocument
from glyphsLib.builder.instances import apply_instance_data
from ..test_helpers import write_designspace_and_UFOs
    from glyphsLib.interpolation import apply_instance_data as reexported

    # Reencode Glyphs: A.alt=0041, C=
    assert ufos[1]["A"].unicode is None
    assert ufos[1]["A.alt"].unicode == 0x0041
    assert ufos[1]["C"].unicode is None


def test_glyphs3_mapping():
    font = glyphsLib.GSFont(os.path.join(DATA, "Glyphs3Instances.glyphs"))
    # Instance1: designspace 200 -> userspace 400
    # Instance2: designspace 800 -> userspace 900
    # Instance2: designspace 600 -> userspace 650
    doc = glyphsLib.to_designspace(font)
    assert doc.axes[0].map == [(400, 200), (600, 650), (900, 800)]
    assert doc.instances[0].location == {"Weight": 200}
    assert doc.instances[1].location == {"Weight": 800}
    assert doc.instances[2].location == {"Weight": 650}


def test_glyphs3_instance_filtering():
    font = glyphsLib.GSFont(os.path.join(DATA, "InstanceFamilyName-G3.glyphs"))
    assert len(font.instances) == 6

    # Loaded from default font family name
    assert not font.instances[0].properties
    assert not font.instances[1].properties
    assert font.instances[0].familyName == "MyFamily"
    assert font.instances[1].familyName == "MyFamily"

    # Loaded from .properties
    assert font.instances[2].familyName == "MyFamily 12pt"

    assert font.instances[3].familyName == "MyFamily 12pt"