1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Tests for py_guide_parser.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21import os 22 23from tensorflow.python.platform import test 24from tensorflow.tools.docs import py_guide_parser 25 26 27class TestPyGuideParser(py_guide_parser.PyGuideParser): 28 29 def __init__(self): 30 self.calls = [] 31 py_guide_parser.PyGuideParser.__init__(self) 32 33 def process_title(self, line_number, title): 34 self.calls.append((line_number, 't', title)) 35 36 def process_section(self, line_number, section_title, tag): 37 self.calls.append((line_number, 's', '%s : %s' % (section_title, tag))) 38 39 def process_in_blockquote(self, line_number, line): 40 self.calls.append((line_number, 'b', line)) 41 self.replace_line(line_number, line + ' BQ') 42 43 def process_line(self, line_number, line): 44 self.calls.append((line_number, 'l', line)) 45 46 47class PyGuideParserTest(test.TestCase): 48 49 def testBasics(self): 50 tmp = os.path.join(test.get_temp_dir(), 'py_guide_parser_test.md') 51 f = open(tmp, 'w') 52 f.write("""# a title 53a line 54## a section 55```shell 56in a blockquote 57``` 58out of blockquote 59""") 60 f.close() 61 parser = TestPyGuideParser() 62 result = parser.process(tmp) 63 expected = """# a title 64a line 65## a section 66```shell BQ 67in a blockquote BQ 68``` 69out of blockquote 70""" 71 self.assertEqual(expected, result) 72 expected = [(0, 't', 'a title'), 73 (1, 'l', 'a line'), 74 (2, 's', 'a section : a_section'), 75 (3, 'b', '```shell'), 76 (4, 'b', 'in a blockquote'), 77 (5, 'l', '```'), 78 (6, 'l', 'out of blockquote'), 79 (7, 'l', '')] 80 self.assertEqual(expected, parser.calls) 81 82 83if __name__ == '__main__': 84 test.main() 85