import codecs
import unittest

from ..sm import SMSimfile
from ..ssc import SSCSimfile
from ..convert import *


class TestConvert(unittest.TestCase):
    def test_sm_to_ssc(self):
        with codecs.open('testdata/Kryptix.sm', 'r', 'utf-8') as infile:
            sm = SMSimfile(file=infile)
        
        ssc = sm_to_ssc(sm)

        for property, value in sm.items():
            self.assertEqual(value, ssc[property])
        self.assertEqual('0.83', ssc.version)
        self.assertEqual(len(sm.charts), len(ssc.charts))
        for sm_chart, ssc_chart in zip(sm.charts, ssc.charts):
            for property, value in sm_chart.items():
                self.assertEqual(value, ssc_chart[property])
    
    def test_ssc_to_sm_raises_by_default(self):
        with codecs.open('testdata/Springtime.ssc', 'r', 'utf-8') as infile:
            ssc = SSCSimfile(file=infile)
        
        self.assertRaises(InvalidPropertyException, ssc_to_sm, ssc)

    def test_ssc_to_sm_with_lenient_invalid_property_behaviors(self):
        with codecs.open('testdata/Springtime.ssc', 'r', 'utf-8') as infile:
            ssc = SSCSimfile(file=infile)
        
        sm = ssc_to_sm(
            ssc,
            invalid_property_behaviors={
                KnownProperty.GAMEPLAY_EVENT: InvalidPropertyBehavior.IGNORE,
                KnownProperty.TIMING_DATA: InvalidPropertyBehavior.IGNORE,
            },
        )

        for key, value in ssc.items():
            if key == 'VERSION':
                self.assertNotIn(key, sm)
            else:
                self.assertEqual(value, ssc[key])
        self.assertEqual(len(ssc.charts), len(sm.charts))
        for ssc_chart, sm_chart in zip(ssc.charts, sm.charts):
            # We're iterating over the output sm_chart on purpose:
            # SMChart cannot accept fields that it doesn't know about
            for property, value in sm_chart.items():
                self.assertEqual(value, ssc_chart[property])
@@ -24,16 +24,31 @@
 ittest%0A%0A
+import simfile%0A
 from ..s
@@ -193,35 +193,47 @@
 f):%0A        
-with codecs
+sm: SMSimfile = simfile
 .open('testd
@@ -251,73 +251,8 @@
 .sm'
-, 'r', 'utf-8') as infile:%0A            sm = SMSimfile(file=infile
 )%0A  
@@ -719,35 +719,49 @@
 f):%0A        
-with codecs
+ssc: SSCSimfile = simfile
 .open('testd
@@ -783,75 +783,8 @@
 ssc'
-, 'r', 'utf-8') as infile:%0A            ssc = SSCSimfile(file=infile
 )%0A  
@@ -941,19 +941,33 @@
     
-with codecs
+ssc: SSCSimfile = simfile
 .ope
@@ -997,75 +997,8 @@
 ssc'
-, 'r', 'utf-8') as infile:%0A            ssc = SSCSimfile(file=infile
 )%0A  
@@ -1102,29 +1102,24 @@
         
-Known
 Property
 .GAMEPLA
@@ -1110,16 +1110,20 @@
 Property
+Type
 .GAMEPLA
@@ -1183,21 +1183,16 @@
     
-Known
 Property
 .TIM
@@ -1187,16 +1187,20 @@
 Property
+Type
 .TIMING_

fix tests