コード例 #1
0
 def test_stringify_results(self):
     docstring = ProviderMethodDocstring(
         app=MagicMock(),
         what='method',
         name='faker.providers.BaseProvider.bothify',
         obj=MagicMock,
         options=MagicMock(),
         lines=[],
     )
     results = [
         '',  # Empty string
         '\'',  # Single quote literal (escaped)
         "'",  # Single quote literal (unescaped)
         '"',  # Double quote literal (unescaped)
         "\"",  # Double quote literal (escaped)
         'aa\taaaaa\r\n',  # String containing \t, \r, \n
         b'abcdef',  # Bytes object
         True,  # Booleans
         False,
         None,  # None types
     ]
     output = [docstring._stringify_result(result) for result in results]
     assert output == [
         "''",  # Ends up as '' when printed
         '"\'"',  # Ends up as "'" when printed
         '"\'"',  # Ends up as "'" when printed
         '\'"\'',  # Ends up as '"' when printed
         '\'"\'',  # Ends up as '"' when printed
         "'aa\\taaaaa\\r\\n'",  # Ends up as 'aa\\taaaaa\\r\\n' when printed
         "b'abcdef'",  # Ends up as b'abcdef' when printed
         'True',  # Ends up as True when printed
         'False',
         'None',  # Ends up as None when printed
     ]
コード例 #2
0
ファイル: test_docstring.py プロジェクト: yonglinZ/faker
    def test_stringify_results(self):
        class TestObject:
            def __repr__(self):
                return 'abcdefg'

        faker = Faker()
        faker.seed_instance(0)
        docstring = ProviderMethodDocstring(
            app=MagicMock(),
            what='method',
            name='faker.providers.BaseProvider.bothify',
            obj=MagicMock,
            options=MagicMock(),
            lines=[],
        )
        results = [
            '',  # Empty string
            '\'',  # Single quote literal (escaped)
            "'",  # Single quote literal (unescaped)
            '"',  # Double quote literal (unescaped)
            "\"",  # Double quote literal (escaped)
            'aa\taaaaa\r\n',  # String containing \t, \r, \n
            b'abcdef',  # Bytes object
            True,  # Booleans
            False,
            None,  # None types
            [1, 2, 3, 4, 5],  # Other non-primitives
            (1, 2, 3, 4, 5),
            {
                1: 2,
                2: 3,
                3: 4,
                4: 5
            },
            faker.uuid4(cast_to=None),
            TestObject(),
        ]
        output = [docstring._stringify_result(result) for result in results]
        assert output == [
            "''",  # Ends up as '' when printed
            '"\'"',  # Ends up as "'" when printed
            '"\'"',  # Ends up as "'" when printed
            '\'"\'',  # Ends up as '"' when printed
            '\'"\'',  # Ends up as '"' when printed
            "'aa\\taaaaa\\r\\n'",  # Ends up as 'aa\\taaaaa\\r\\n' when printed
            "b'abcdef'",  # Ends up as b'abcdef' when printed
            'True',  # Ends up as True when printed
            'False',  # Ends up as False when printed
            'None',  # Ends up as None when printed
            '[1, 2, 3, 4, 5]',  # Ends up using object's __repr__
            '(1, 2, 3, 4, 5)',
            '{1: 2, 2: 3, 3: 4, 4: 5}',
            "UUID('e3e70682-c209-4cac-a29f-6fbed82c07cd')",
            'abcdefg',
        ]
コード例 #3
0
    def test_stringify_results(self, faker):
        class TestObject:
            def __repr__(self):
                return "abcdefg"

        docstring = ProviderMethodDocstring(
            app=MagicMock(),
            what="method",
            name="faker.providers.BaseProvider.bothify",
            obj=MagicMock,
            options=MagicMock(),
            lines=[],
        )
        results = [
            "",  # Empty string
            "'",  # Single quote literal (escaped)
            "'",  # Single quote literal (unescaped)
            '"',  # Double quote literal (unescaped)
            '"',  # Double quote literal (escaped)
            "aa\taaaaa\r\n",  # String containing \t, \r, \n
            b"abcdef",  # Bytes object
            True,  # Booleans
            False,
            None,  # None types
            [1, 2, 3, 4, 5],  # Other non-primitives
            (1, 2, 3, 4, 5),
            {
                1: 2,
                2: 3,
                3: 4,
                4: 5
            },
            faker.uuid4(cast_to=None),
            TestObject(),
        ]
        output = [docstring._stringify_result(result) for result in results]
        assert output == [
            "''",  # Ends up as '' when printed
            '"\'"',  # Ends up as "'" when printed
            '"\'"',  # Ends up as "'" when printed
            "'\"'",  # Ends up as '"' when printed
            "'\"'",  # Ends up as '"' when printed
            "'aa\\taaaaa\\r\\n'",  # Ends up as 'aa\\taaaaa\\r\\n' when printed
            "b'abcdef'",  # Ends up as b'abcdef' when printed
            "True",  # Ends up as True when printed
            "False",  # Ends up as False when printed
            "None",  # Ends up as None when printed
            "[1, 2, 3, 4, 5]",  # Ends up using object's __repr__
            "(1, 2, 3, 4, 5)",
            "{1: 2, 2: 3, 3: 4, 4: 5}",
            "UUID('e3e70682-c209-4cac-a29f-6fbed82c07cd')",
            "abcdefg",
        ]
コード例 #4
0
    def test_end_to_end_sample_generation(self, mock_warning):
        fake = Faker(DEFAULT_LOCALE)
        non_sample_lines = ['lorem', 'ipsum', 'dolor', 'sit', 'amet']
        valid_sample_lines = [
            ":sample 1234jdbvhjdbygdvbhxjhx",  # Will fail during sample section processing, 1st log warning
            ":sample: invalid_arg='value'",  # Will fail during sample generation, 2nd log warning
            ":sample size=3 seed=1000: text='???###'",  # 1st sample generation
            ":sample: number=100**100**100",  # Will fail SampleCodeValidator validation, 3rd log warning
            ":sample seed=3210: letters='abcde'",  # 2nd sample generation
            ":sample size=10 seed=1: abcd='abcd'",  # Will fail during sample generation, 4th log warning
            ":sample size=20 seed=1234: text='???###', ",  # 3rd sample generation
            "         letters='abcde'",
        ]
        lines = non_sample_lines + valid_sample_lines
        docstring = ProviderMethodDocstring(
            app=MagicMock(),
            what='method',
            name='faker.providers.BaseProvider.bothify',
            obj=MagicMock,
            options=MagicMock(),
            lines=lines,
        )

        output = docstring.lines[len(non_sample_lines):]
        assert output[0] == ':examples:'

        # 1st sample generation
        Faker.seed(1000)
        assert output[1] == ''
        assert output[2] == '>>> Faker.seed(1000)'
        assert output[3] == '>>> for _ in range(5):'
        assert output[4] == "...     fake.bothify(text='???###')"
        assert output[5] == '...'
        for i in range(6, 11):
            assert output[i] == docstring._stringify_result(
                fake.bothify(text='???###'))

        # 2nd sample generation
        Faker.seed(3210)
        assert output[11] == ''
        assert output[12] == '>>> Faker.seed(3210)'
        assert output[13] == '>>> for _ in range(5):'
        assert output[14] == "...     fake.bothify(letters='abcde')"
        assert output[15] == '...'
        for i in range(16, 21):
            assert output[i] == docstring._stringify_result(
                fake.bothify(letters='abcde'))

        # 3rd sample generation
        Faker.seed(1234)
        assert output[21] == ''
        assert output[22] == '>>> Faker.seed(1234)'
        assert output[23] == '>>> for _ in range(20):'
        assert output[
            24] == "...     fake.bothify(text='???###', letters='abcde')"
        assert output[25] == '...'
        for i in range(26, 46):
            assert output[i] == docstring._stringify_result(
                fake.bothify(text='???###', letters='abcde'))

        calls = mock_warning.call_args_list
        assert len(calls) == 4

        # 1st call to _log_warning
        args, kwargs = calls[0]
        assert len(args) == 1
        assert not kwargs
        assert args[
            0] == "The section `:sample 1234jdbvhjdbygdvbhxjhx` is malformed and will be discarded."

        # 2nd call to _log_warning
        args, kwargs = calls[1]
        assert len(args) == 1
        assert not kwargs
        assert args[
            0] == "Sample generation failed for method `bothify` with arguments `invalid_arg='value'`."

        # 3rd call to _log_warning
        args, kwargs = calls[2]
        assert len(args) == 1
        assert not kwargs
        assert args[0] == (
            "Invalid code elements detected. Sample generation will be skipped for "
            "method `bothify` with arguments `number=100**100**100`.")

        # 4th call to _log_warning
        args, kwargs = calls[3]
        assert len(args) == 1
        assert not kwargs
        assert args[
            0] == "Sample generation failed for method `bothify` with arguments `abcd='abcd'`."