1# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"). You
4# may not use this file except in compliance with the License. A copy of
5# the License is located at
6#
7# http://aws.amazon.com/apache2.0/
8#
9# or in the "license" file accompanying this file. This file is
10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11# ANY KIND, either express or implied. See the License for the specific
12# language governing permissions and limitations under the License.
13from tests import unittest, mock
14from botocore.docs.docstring import LazyLoadedDocstring
15from botocore.docs.docstring import ClientMethodDocstring
16from botocore.docs.docstring import WaiterDocstring
17from botocore.docs.docstring import PaginatorDocstring
18
19
20class MockedLazyLoadedDocstring(LazyLoadedDocstring):
21    def __init__(self, *args, **kwargs):
22        super(MockedLazyLoadedDocstring, self).__init__(*args, **kwargs)
23        self.mocked_writer_method = mock.Mock()
24
25    def _write_docstring(self, *args, **kwargs):
26        self.mocked_writer_method(*args, **kwargs)
27
28
29class TestLazyLoadedDocstring(unittest.TestCase):
30    def test_raises_not_implemented(self):
31        with self.assertRaises(NotImplementedError):
32            str(LazyLoadedDocstring())
33
34    def test_expandtabs(self):
35        docstring = MockedLazyLoadedDocstring()
36        docstring.mocked_writer_method.side_effect = (
37            lambda section: section.write('foo\t'))
38        self.assertEqual('foo ', docstring.expandtabs(1))
39
40    def test_str(self):
41        docstring = MockedLazyLoadedDocstring()
42        docstring.mocked_writer_method.side_effect = (
43            lambda section: section.write('foo'))
44        self.assertEqual('foo', str(docstring))
45
46    def test_repr(self):
47        docstring = MockedLazyLoadedDocstring()
48        docstring.mocked_writer_method.side_effect = (
49            lambda section: section.write('foo'))
50        self.assertEqual('foo', repr(docstring))
51
52    def test_is_lazy_loaded(self):
53        docstring = MockedLazyLoadedDocstring()
54        str(docstring)
55        str(docstring)
56        # The mock.ANY represents the DocumentStructure that is filled out.
57        docstring.mocked_writer_method.assert_called_once_with(mock.ANY)
58
59    def test_args_kwargs_passed(self):
60        args = ['foo', 'bar']
61        kwargs = {'biz': 'baz'}
62        docstring = MockedLazyLoadedDocstring(*args, **kwargs)
63        str(docstring)
64        # The mock.ANY represents the DocumentStructure that is filled out.
65        docstring.mocked_writer_method.assert_called_with(
66            mock.ANY, *args, **kwargs)
67
68
69class TestClientMethodDocstring(unittest.TestCase):
70    def test_use_correct_docstring_writer(self):
71        with mock.patch(
72                'botocore.docs.docstring'
73                '.document_model_driven_method') as mock_writer:
74            docstring = ClientMethodDocstring()
75            str(docstring)
76            self.assertTrue(mock_writer.called)
77
78
79class TestWaiterDocstring(unittest.TestCase):
80    def test_use_correct_docstring_writer(self):
81        with mock.patch(
82                'botocore.docs.docstring'
83                '.document_wait_method') as mock_writer:
84            docstring = WaiterDocstring()
85            str(docstring)
86            self.assertTrue(mock_writer.called)
87
88
89class TestPaginatorDocstring(unittest.TestCase):
90    def test_use_correct_docstring_writer(self):
91        with mock.patch(
92                'botocore.docs.docstring'
93                '.document_paginate_method') as mock_writer:
94            docstring = PaginatorDocstring()
95            str(docstring)
96            self.assertTrue(mock_writer.called)
97