class InfoPlistTest(unittest.TestCase):
    
    PROPS_TPL = ('%(icon)s<key>CFBundleIdentifier</key>\n'
                 '<string>test.org</string>\n'
                 '<key>CFBundleName</key>\n'
                 '<string>test</string>\n'
                 '<key>CFBundlePackageGetInfoString</key>\n'
                 '<string>Test package</string>\n'
                 '<key>CFBundlePackageType</key>\n'
                 '<string>%(ptype)s</string>\n'
                 '<key>CFBundleVersion</key>\n'
                 '<string>1.0</string>')

    def setUp(self):
        self.info_plist = InfoPlist('test', 'test.org', '1.0',
                                    'Test package')

    def testFormatProperty(self):
        self.assertEquals('<key>Key</key>\n<string>Value</string>',
                self.info_plist._format_property('Key', 'Value'))

    def testGetPropertiesString(self):
        result = self.info_plist._get_properties_string()
        expected = self.PROPS_TPL % {'icon': '', 'ptype': ''}
        self.assertEquals(result, expected)

    def testFrameworkPackageType(self):
        self.info_plist = FrameworkPlist('test', 'test.org', '1.0',
                                         'Test package')
        result = self.info_plist._get_properties_string()
        expected = self.PROPS_TPL % {'ptype': 'FMWK', 'icon': ''}
        self.assertEquals(result, expected)

    def testApplicationPackageType(self):
        self.info_plist = ApplicationPlist('test', 'test.org', '1.0',
                                           'Test package')
        result = self.info_plist._get_properties_string()
        expected = self.PROPS_TPL % {'ptype': 'APPL', 'icon': ''}
        self.assertEquals(result, expected)

    def testGetPropertiesStringWithIcon(self):
        self.info_plist.icon = 'test.ico'
        result = self.info_plist._get_properties_string()
        expected = self.PROPS_TPL % {'ptype': '', 'icon':
            self.info_plist._format_property('CFBundleIconFile', 'test.ico') +
            '\n'}
        self.info_plist.icon = None
        self.assertEquals(result, expected)

    def testSave(self):
        tmp = tempfile.NamedTemporaryFile()
        self.info_plist.save(tmp.name)
        with open(tmp.name, 'r') as f:
            result = f.read()
        expected = INFO_PLIST_TPL % (self.info_plist.BEGIN,
                                     self.info_plist._get_properties_string(),
                                     self.info_plist.END)
        self.assertEquals(result, expected)
class InfoPlistTest(unittest.TestCase):
    
    PROPS_TPL = ('%(icon)s<key>CFBundleIdentifier</key>\n'
                 '<string>test.org</string>\n'
                 '<key>CFBundleName</key>\n'
                 '<string>test</string>\n'
                 '<key>CFBundlePackageGetInfoString</key>\n'
                 '<string>Test package</string>\n'
                 '<key>CFBundlePackageType</key>\n'
                 '<string>%(ptype)s</string>\n'
                 '<key>CFBundleVersion</key>\n'
                 '<string>1.0</string>')

    def setUp(self):
        self.info_plist = InfoPlist('test', 'test.org', '1.0',
                                    'Test package')

    def testFormatProperty(self):
        self.assertEqual('<key>Key</key>\n<string>Value</string>',
                self.info_plist._format_property('Key', 'Value'))

    def testGetPropertiesString(self):
        result = self.info_plist._get_properties_string()
        expected = self.PROPS_TPL % {'icon': '', 'ptype': ''}
        self.assertEqual(result, expected)

    def testFrameworkPackageType(self):
        self.info_plist = FrameworkPlist('test', 'test.org', '1.0',
                                         'Test package')
        result = self.info_plist._get_properties_string()
        expected = self.PROPS_TPL % {'ptype': 'FMWK', 'icon': ''}
        self.assertEqual(result, expected)

    def testApplicationPackageType(self):
        self.info_plist = ApplicationPlist('test', 'test.org', '1.0',
                                           'Test package')
        result = self.info_plist._get_properties_string()
        expected = self.PROPS_TPL % {'ptype': 'APPL', 'icon': ''}
        self.assertEqual(result, expected)

    def testGetPropertiesStringWithIcon(self):
        self.info_plist.icon = 'test.ico'
        result = self.info_plist._get_properties_string()
        expected = self.PROPS_TPL % {'ptype': '', 'icon':
            self.info_plist._format_property('CFBundleIconFile', 'test.ico') +
            '\n'}
        self.info_plist.icon = None
        self.assertEqual(result, expected)

    def testSave(self):
        tmp = tempfile.NamedTemporaryFile()
        self.info_plist.save(tmp.name)
        with open(tmp.name, 'r') as f:
            result = f.read()
        expected = INFO_PLIST_TPL % (self.info_plist.BEGIN,
                                     self.info_plist._get_properties_string(),
                                     self.info_plist.END)
        self.assertEqual(result, expected)