<filename>strings2pot/extractors/arb_test.py
# -*- coding: utf-8 -*-

import os
import unittest
import arb

class ArbExtractorTest(unittest.TestCase):
    def setUp(self):
        self.mock_source_file = 'mock_source_arb.arb'
        self.mock_destination_file = 'mock_destination_arb.pot'
        def mock_context_id_generator(s): return u'MOCK_CONTEXT_ID'
        self.mock_context_id_generator = mock_context_id_generator

        with open(self.mock_source_file, 'a') as source_file:
            source_file.write("""
{
	"testString": "That''s a \\"string\\"",
	"@testString": {
		"type": "text",
		"placeholders": {}
	},
	"withPlaceholderstring": "Hello {name_withSomething}",
	"@withPlaceholderstring": {
		"type": "text",
		"placeholders": {
			"name_withSomething": {}
		}
	},
	"pluralString": "{howMany,plural, =0{Dear {customerName}, your cart is empty}=1{Dear {customerName}, you''ve one item in your cart}other{Dear {customerName}, you have {howMany} items in your chart}}",
	"@pluralString": {
		"type": "text",
		"placeholders": {
			"howMany": {},
			"customerName": {}
		}
	}
}
            """)
    
    def tearDown(self):
        try:
            os.unlink(self.mock_source_file)
            os.unlink(self.mock_destination_file)
        except Exception, e:
            pass

    # test that the ArbExtractor class constructor sets source_file and destination_file attributes
    def test_ctor(self):
        sut = arb.ArbExtractor(
            self.mock_source_file,
            self.mock_destination_file,
            self.mock_context_id_generator
        )

        self.assertEqual(sut.source_file, self.mock_source_file)
        self.assertEqual(sut.destination_file, self.mock_destination_file)

    # test that ArbExtractor parse_string method converts string in POT format 
    def test_parse_string(self):
        sut = arb.ArbExtractor('', '', self.mock_context_id_generator)

        single_line_string = "\' \" {placeholder}"
        self.assertEqual(
            sut.parse_string(single_line_string, ['placeholder']),
            '"\' \" %s"'
        )

        multi_line_string = "\' \" \\n {placeholder}"
        self.assertEqual(
            sut.parse_string(multi_line_string, ['placeholder']),
            '''""
"\' \" \\n"
" %s"'''
        )
    
    # test that ArbExtractor split_plural_string method splits plural string in multiple strings
    def test_split_plural_strings(self):
        sut = arb.ArbExtractor('', '', self.mock_context_id_generator)

        mock_multiple_string = "{howMany,plural, =0{Dear {customerName}, your cart is empty}=1{Dear {customerName}, you''ve one item in your cart}other{Dear {customerName}, you have {howMany} items in your chart}}"
        self.assertEqual(
            sut.split_plural_string(
                mock_multiple_string,
                'howMany',
                ['howMany', 'customerName']
            ),
            [
                "Dear {customerName}, your cart is empty",
                "Dear {customerName}, you''ve one item in your cart",
                "Dear {customerName}, you have {howMany} items in your chart"
            ]
        )

    # test that ArbExtractor run method converts an input file in POT format
    def test_run(self):
        sut = arb.ArbExtractor(
            self.mock_source_file,
            self.mock_destination_file,
            self.mock_context_id_generator
        )

        sut.run()

        with open(self.mock_destination_file, 'r') as destination_file:
            lines = destination_file.readlines()
            pot_content_as_string = "".join(lines)

            self.assertEqual(
                pot_content_as_string,
                """
#: mock_source_arb.arb:@withPlaceholderstring
msgctxt "MOCK_CONTEXT_ID"
msgid "Hello %s"
msgstr ""

#: mock_source_arb.arb:@testString
msgctxt "MOCK_CONTEXT_ID"
msgid "That's a \"string\""
msgstr ""

#: mock_source_arb.arb:@pluralString
msgctxt "MOCK_CONTEXT_ID"
msgid "Dear %s, your cart is empty"
msgstr ""

#: mock_source_arb.arb:@pluralString
msgctxt "MOCK_CONTEXT_ID"
msgid "Dear %s, you've one item in your cart"
msgstr ""

#: mock_source_arb.arb:@pluralString
msgctxt "MOCK_CONTEXT_ID"
msgid "Dear %s, you have %s items in your chart"
msgstr ""
"""
            )


if __name__ == '__main__':
    unittest.main()
<filename>strings2pot/extractors/arb_test.py
# -*- coding: utf-8 -*-

import os
import unittest
import arb

class ArbExtractorTest(unittest.TestCase):
    def setUp(self):
        self.mock_source_file = 'mock_source_arb.arb'
        self.mock_destination_file = 'mock_destination_arb.pot'
        def mock_context_id_generator(s): return u'MOCK_CONTEXT_ID'
        self.mock_context_id_generator = mock_context_id_generator

        with open(self.mock_source_file, 'a') as source_file:
            source_file.write("""
{
	"testString": "That''s a \\"string\\"",
	"@testString": {
		"type": "text",
		"placeholders": {}
	},
	"withPlaceholderstring": "Hello {name_withSomething}",
	"@withPlaceholderstring": {
		"type": "text",
		"placeholders": {
			"name_withSomething": {}
		}
	},
	"pluralString": "{howMany,plural, =0{Dear {customerName}, your cart is empty}=1{Dear {customerName}, you''ve one item in your cart}other{Dear {customerName}, you have {howMany} items in your chart}}",
	"@pluralString": {
		"type": "text",
		"placeholders": {
			"howMany": {},
			"customerName": {}
		}
	}
}
            """)
    
    def tearDown(self):
        try:
            os.unlink(self.mock_source_file)
            os.unlink(self.mock_destination_file)
        except Exception, e:
            pass

    # test that the ArbExtractor class constructor sets source_file and destination_file attributes
    def test_ctor(self):
        sut = arb.ArbExtractor(
            self.mock_source_file,
            self.mock_destination_file,
            self.mock_context_id_generator
        )

        self.assertEqual(sut.source_file, self.mock_source_file)
        self.assertEqual(sut.destination_file, self.mock_destination_file)

    # test that ArbExtractor parse_string method converts string in POT format 
    def test_parse_string(self):
        sut = arb.ArbExtractor('', '', self.mock_context_id_generator)

        single_line_string = "\' \" {placeholder}"
        self.assertEqual(
            sut.parse_string(single_line_string, ['placeholder']),
            '"\' \" %s"'
        )

        multi_line_string = "\' \" \\n {placeholder}"
        self.assertEqual(
            sut.parse_string(multi_line_string, ['placeholder']),
            '''""
"\' \" \\n"
" %s"'''
        )
    
    # test that ArbExtractor split_plural_string method splits plural string in multiple strings
    def test_split_plural_strings(self):
        sut = arb.ArbExtractor('', '', self.mock_context_id_generator)

        mock_multiple_string = "{howMany,plural, =0{Dear {customerName}, your cart is empty}=1{Dear {customerName}, you''ve one item in your cart}other{Dear {customerName}, you have {howMany} items in your chart}}"
        self.assertEqual(
            sut.split_plural_string(
                mock_multiple_string,
                'howMany',
                ['howMany', 'customerName']
            ),
            [
                "Dear {customerName}, your cart is empty",
                "Dear {customerName}, you''ve one item in your cart",
                "Dear {customerName}, you have {howMany} items in your chart"
            ]
        )

    # test that ArbExtractor run method converts an input file in POT format
    def test_run(self):
        sut = arb.ArbExtractor(
            self.mock_source_file,
            self.mock_destination_file,
            self.mock_context_id_generator
        )

        sut.run()

        with open(self.mock_destination_file, 'r') as destination_file:
            lines = destination_file.readlines()
            pot_content_as_string = "".join(lines)

            self.assertEqual(
                pot_content_as_string,
                """
#: mock_source_arb.arb:@withPlaceholderstring
msgctxt "MOCK_CONTEXT_ID"
msgid "Hello %s"
msgstr ""

#: mock_source_arb.arb:@testString
msgctxt "MOCK_CONTEXT_ID"
msgid "That's a \"string\""
msgstr ""

#: mock_source_arb.arb:@pluralString
msgctxt "MOCK_CONTEXT_ID"
msgid "Dear %s, your cart is empty"
msgstr ""

#: mock_source_arb.arb:@pluralString
msgctxt "MOCK_CONTEXT_ID"
msgid "Dear %s, you've one item in your cart"
msgstr ""

#: mock_source_arb.arb:@pluralString
msgctxt "MOCK_CONTEXT_ID"
msgid "Dear %s, you have %s items in your chart"
msgstr ""
"""
            )


if __name__ == '__main__':
    unittest.main()
# -*- coding: utf-8 -*- { 	"testString": "That''s a \\"string\\"", 	"@testString": { 		"type": "text", 		"placeholders": {} 	}, 	"withPlaceholderstring": "Hello {name_withSomething}", 	"@withPlaceholderstring": { 		"type": "text", 		"placeholders": { 			"name_withSomething": {} 		} 	}, 	"pluralString": "{howMany,plural, =0{Dear {customerName}, your cart is empty}=1{Dear {customerName}, you''ve one item in your cart}other{Dear {customerName}, you have {howMany} items in your chart}}", 	"@pluralString": { 		"type": "text", 		"placeholders": { 			"howMany": {}, 			"customerName": {} 		} 	} } # test that the ArbExtractor class constructor sets source_file and destination_file attributes # test that ArbExtractor parse_string method converts string in POT format "" "\' \" \\n" " %s" # test that ArbExtractor split_plural_string method splits plural string in multiple strings # test that ArbExtractor run method converts an input file in POT format #: mock_source_arb.arb:@withPlaceholderstring msgctxt "MOCK_CONTEXT_ID" msgid "Hello %s" msgstr ""  #: mock_source_arb.arb:@testString msgctxt "MOCK_CONTEXT_ID" msgid "That's a \"string\"" msgstr ""  #: mock_source_arb.arb:@pluralString msgctxt "MOCK_CONTEXT_ID" msgid "Dear %s, your cart is empty" msgstr ""  #: mock_source_arb.arb:@pluralString msgctxt "MOCK_CONTEXT_ID" msgid "Dear %s, you've one item in your cart" msgstr ""  #: mock_source_arb.arb:@pluralString msgctxt "MOCK_CONTEXT_ID" msgid "Dear %s, you have %s items in your chart" msgstr ""