<commit_before># -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals
from fontTools.misc.py23 import *
from fontTools.misc import sstruct
from fontTools.misc.xmlWriter import XMLWriter
import struct
import unittest
from ._n_a_m_e import table__n_a_m_e, NameRecord, nameRecordFormat, nameRecordSize


def makeName(text, nameID, platformID, platEncID, langID):
	name = NameRecord()
	name.nameID, name.platformID, name.platEncID, name.langID = (
		nameID, platformID, platEncID, langID)
	name.string = tobytes(text, encoding=name.getEncoding())
	return name


class NameTableTest(unittest.TestCase):

	def test_getDebugName(self):
		table = table__n_a_m_e()
		table.names = [
			makeName("Bold", 258, 1, 0, 0),  # Mac, MacRoman, English
			makeName("Gras", 258, 1, 0, 1),  # Mac, MacRoman, French
			makeName("Fett", 258, 1, 0, 2),  # Mac, MacRoman, German
			makeName("Sem Fracções", 292, 1, 0, 8)  # Mac, MacRoman, Portuguese
		]
		self.assertEqual("Bold", table.getDebugName(258))
		self.assertEqual("Sem Fracções", table.getDebugName(292))
		self.assertEqual(None, table.getDebugName(999))

	def test_setName(self):
		table = table__n_a_m_e()
		table.setName("Regular", 2, 1, 0, 0)
		table.setName("Version 1.000", 5, 3, 1, 0x409)
		table.setName("寬鬆", 276, 1, 2, 0x13)
		self.assertEqual("Regular", table.getName(2, 1, 0, 0).toUnicode())
		self.assertEqual("Version 1.000", table.getName(5, 3, 1, 0x409).toUnicode())
		self.assertEqual("寬鬆", table.getName(276, 1, 2, 0x13).toUnicode())
		self.assertTrue(len(table.names) == 3)
		table.setName("緊縮", 276, 1, 2, 0x13)
		self.assertEqual("緊縮", table.getName(276, 1, 2, 0x13).toUnicode())
		self.assertTrue(len(table.names) == 3)

	def test_decompile_badOffset(self):
                # https://github.com/behdad/fonttools/issues/525
		table = table__n_a_m_e()
		badRecord = {
			"platformID": 1,
			"platEncID": 3,
			"langID": 7,
			"nameID": 1,
			"length": 3,
			"offset": 8765  # out of range
		}
		data = bytesjoin([
                        struct.pack(">HHH", 1, 1, 6 + nameRecordSize),<commit_msg>[_n_a_m_e_test] add tests for addName method; test that warning is logged from setName if input is bytes<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function, division, absolute_import, unicode_literals
from fontTools.misc.py23 import *
from fontTools.misc import sstruct
from fontTools.misc.xmlWriter import XMLWriter
from fontTools.misc.loggingTools import CapturingLogHandler
import struct
import unittest
from ._n_a_m_e import table__n_a_m_e, NameRecord, nameRecordFormat, nameRecordSize
from ._n_a_m_e import makeName, log


class NameTableTest(unittest.TestCase):

	def test_getDebugName(self):
		table = table__n_a_m_e()
		table.names = [
			makeName("Bold", 258, 1, 0, 0),  # Mac, MacRoman, English
			makeName("Gras", 258, 1, 0, 1),  # Mac, MacRoman, French
			makeName("Fett", 258, 1, 0, 2),  # Mac, MacRoman, German
			makeName("Sem Fracções", 292, 1, 0, 8)  # Mac, MacRoman, Portuguese
		]
		self.assertEqual("Bold", table.getDebugName(258))
		self.assertEqual("Sem Fracções", table.getDebugName(292))
		self.assertEqual(None, table.getDebugName(999))

	def test_setName(self):
		table = table__n_a_m_e()
		table.setName("Regular", 2, 1, 0, 0)
		table.setName("Version 1.000", 5, 3, 1, 0x409)
		table.setName("寬鬆", 276, 1, 2, 0x13)
		self.assertEqual("Regular", table.getName(2, 1, 0, 0).toUnicode())
		self.assertEqual("Version 1.000", table.getName(5, 3, 1, 0x409).toUnicode())
		self.assertEqual("寬鬆", table.getName(276, 1, 2, 0x13).toUnicode())
		self.assertTrue(len(table.names) == 3)
		table.setName("緊縮", 276, 1, 2, 0x13)
		self.assertEqual("緊縮", table.getName(276, 1, 2, 0x13).toUnicode())
		self.assertTrue(len(table.names) == 3)
		# passing bytes issues a warning
		with CapturingLogHandler(log, "WARNING") as captor:
			table.setName(b"abc", 0, 1, 0, 0)
		self.assertTrue(
			len([r for r in captor.records if "string is bytes" in r.msg]) == 1)
		# anything other than unicode or bytes raises an error
		with self.assertRaises(TypeError):
			table.setName(1.000, 5, 1, 0, 0)

	def test_addName(self):
		table = table__n_a_m_e()
		nameIDs = []
		for string in ("Width", "Weight", "Custom"):
			nameIDs.append(table.addName(string))

		self.assertEqual(nameIDs[0], 256)
		self.assertEqual(nameIDs[1], 257)
		self.assertEqual(nameIDs[2], 258)
		self.assertEqual(len(table.names), 6)
		self.assertEqual(table.names[0].string, "Width")
		self.assertEqual(table.names[1].string, "Width")
		self.assertEqual(table.names[2].string, "Weight")
		self.assertEqual(table.names[3].string, "Weight")
		self.assertEqual(table.names[4].string, "Custom")
		self.assertEqual(table.names[5].string, "Custom")

		with self.assertRaises(ValueError):
			table.addName('Invalid nameID', minNameID=32767)
		with self.assertRaises(TypeError):
			table.addName(b"abc")  # must be unicode string

	def test_decompile_badOffset(self):
                # https://github.com/behdad/fonttools/issues/525
		table = table__n_a_m_e()
		badRecord = {
			"platformID": 1,
			"platEncID": 3,
			"langID": 7,
			"nameID": 1,
			"length": 3,
			"offset": 8765  # out of range
		}
		data = bytesjoin([
                        struct.pack(">HHH", 1, 1, 6 + nameRecordSize),
{'fraction_non_alphanumeric': 0.14902480590797199, 'fraction_numerical': 0.05055860632455974, 'mean_word_length': 4.467908902691511, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '402138', 'n_tokens_mistral': 2293, 'n_tokens_neox': 1926, 'n_words': 447}