import unittest

import pyglet

import pywavefront

class TestWavefront(unittest.TestCase):
    def setUp(self):
        pyglet.resource.path.append('@' + __name__)
        pyglet.resource.reindex()
        self.meshes = pywavefront.Wavefront('simple.obj')

    def testMaterials(self):
        "Ensure parsed wavefront materials match known values."
        self.assertEqual(len(self.meshes.materials), 2)
        self.assertEqual(self.meshes.materials['Material.simple'].__class__,
                pywavefront.material.Material)

    def testMeshes(self):
        "Ensure parsed wavefront meshes match known values."
        self.assertEqual(len(self.meshes.meshes), 2)
        self.assertEqual(self.meshes.meshes['Simple'].__class__,
                pywavefront.mesh.Mesh)

    def testMeshList(self):
        "Ensure parsed wavefront mesh list matches known values."
        self.assertEqual(len(self.meshes.mesh_list), 2)
        self.assertEqual(self.meshes.mesh_list[0].__class__,
                pywavefront.mesh.Mesh)

    def testAddDuplicateMesh(self):
        "Adding a duplicate mesh should increment the mesh list, but not the meshes hash."
        self.meshes.add_mesh(self.meshes.meshes['Simple'])
        self.assertEqual(len(self.meshes.meshes), 2)
        self.assertEqual(len(self.meshes.mesh_list), 3)

    def testMeshMaterialVertices(self):
        "Mesh vertices should have known values."
        self.assertEqual(len(self.meshes.meshes['Simple'].materials[0].vertices), 24)

class TestBrokenWavefront(unittest.TestCase):
    def setUp(self):
        pyglet.resource.path.append('@' + __name__)
        pyglet.resource.reindex()

    def testUnknownUsemtl(self):
        "Referencing an unknown material with usemtl should raise an exception."
        self.assertRaises(pywavefront.PywavefrontException,
                pywavefront.Wavefront, 'simple_unknown_usemtl.obj')

    def testMissingNormals(self):
        "If there are texture coordinates but no normals, should raise an exception."
        self.assertRaises(pywavefront.PywavefrontException,
                pywavefront.Wavefront, 'simple_missing_normals.obj')

class TestNoMaterial(TestWavefront):
    def setUp(self):
        pyglet.resource.path.append('@' + __name__)
        pyglet.resource.reindex()
        # reset the obj file to new file with no mtl line
        self.meshes = pywavefront.Wavefront('simple_no_mtl.obj')

    def testMaterials(self):
        """Override this test"""
        pass

    def testMeshMaterialVertices(self):
        "Mesh vertices should have known values."
        self.assertEqual(len(self.meshes.meshes['Simple'].materials[0].vertices), 48)

@@ -184,32 +184,150 @@
 ource.reindex()%0A
+        self.mesh_names = %5B'Simple', 'SimpleB'%5D%0A        self.material_names = %5B'Material.simple', 'Material2.simple'%5D%0A
         self.mes
@@ -511,25 +511,48 @@
 materials), 
-2
+len(self.material_names)
 )%0A        se
@@ -588,33 +588,38 @@
 terials%5B
-'M
+self.m
 aterial
-.simple'
+_names%5B0%5D
 %5D.__clas
@@ -800,33 +800,52 @@
 meshes.meshes), 
-2
+len(self.mesh_names)
 )%0A        self.a
@@ -874,24 +874,34 @@
 .meshes%5B
-'Simple'
+self.mesh_names%5B0%5D
 %5D.__clas
@@ -1092,17 +1092,36 @@
 _list), 
-2
+len(self.mesh_names)
 )%0A      
@@ -1390,24 +1390,34 @@
 .meshes%5B
-'Simple'
+self.mesh_names%5B0%5D
 %5D)%0A     
@@ -1461,17 +1461,36 @@
 eshes), 
-2
+len(self.mesh_names)
 )%0A      
@@ -1540,9 +1540,32 @@
 t), 
-3
+len(self.mesh_names) + 1
 )%0A%0A 
@@ -1697,32 +1697,42 @@
 shes.meshes%5B
-'Simple'
+self.mesh_names%5B0%5D
 %5D.materials%5B
@@ -2620,131 +2620,140 @@
 mesh
+_nam
 es = 
-pywavefront.Wavefront('simple_no_mtl.obj')%0A%0A    def testMaterials(self):%0A        %22%22%22Override this test%22%22%22%0A        pass
+%5B'Simple', 'SimpleB'%5D%0A        self.material_names = %5BNone%5D%0A        self.meshes = pywavefront.Wavefront('simple_no_mtl.obj')
 %0A%0A  
@@ -2888,24 +2888,34 @@
 .meshes%5B
-'Simple'
+self.mesh_names%5B0%5D
 %5D.materi

Refactor wavefront unit tests