Coverage for core\test_leoRst.py: 100%

65 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# -*- coding: utf-8 -*- 

2#@+leo-ver=5-thin 

3#@+node:ekr.20210902055206.1: * @file ../unittests/core/test_leoRst.py 

4#@@first 

5"""Tests of leo/core/leoRst.py""" 

6 

7import textwrap 

8try: 

9 import docutils 

10except Exception: # pragma: no cover 

11 docutils = None 

12import leo.core.leoRst as leoRst # Required for coverage tests. 

13assert leoRst 

14from leo.core.leoTest2 import LeoUnitTest 

15 

16#@+others 

17#@+node:ekr.20210327072030.1: ** class TestRst (LeoUnitTest) 

18class TestRst(LeoUnitTest): 

19 """A class to run rst-related unit tests.""" 

20 

21 def setUp(self): 

22 super().setUp() 

23 if not docutils: 

24 self.skipTest('no docutils') # pragma: no cover 

25 

26 #@+others 

27 #@+node:ekr.20210902211919.12: *3* TestRst.test_at_no_head 

28 def test_at_no_head(self): 

29 c = self.c 

30 rc = c.rstCommands 

31 # Create the *input* tree. 

32 root = c.rootPosition().insertAfter() 

33 root.h = fn = '@rst test.html' 

34 child = root.insertAsLastChild() 

35 child.h = '@rst-no-head section' 

36 # Insert the body texts. Overindent to eliminate @verbatim sentinels. 

37 root.b = textwrap.dedent("""\ 

38 ##### 

39 Title 

40 ##### 

41 

42 This is test.html 

43 

44 """) 

45 child.b = textwrap.dedent("""\ 

46 This is the body of the section. 

47 """) 

48 # Define the expected output. 

49 expected = textwrap.dedent(f"""\ 

50 .. rst3: filename: {fn} 

51 

52 .. _http-node-marker-1: 

53 

54 ##### 

55 Title 

56 ##### 

57 

58 This is test.html 

59 

60 This is the body of the section. 

61 

62 """) 

63 # Get and check the rst result. 

64 rc.nodeNumber = 0 

65 rc.http_server_support = True # Override setting for testing. 

66 source = rc.write_rst_tree(root, fn) 

67 self.assertEqual(source, expected) 

68 # Get the html from docutils. 

69 html = rc.writeToDocutils(source, ext='.html') 

70 # Don't bother testing the html. It will depend on docutils. 

71 assert html and html.startswith('<?xml') and html.strip().endswith('</html>') 

72 #@+node:ekr.20210902211919.9: *3* TestRst.test_handleMissingStyleSheetArgs 

73 def test_handleMissingStyleSheetArgs(self): 

74 c = self.c 

75 x = c.rstCommands 

76 result = x.handleMissingStyleSheetArgs(s=None) 

77 self.assertEqual(result, {}) 

78 expected = { 

79 'documentoptions': '[english,12pt,lettersize]', 

80 'language': 'ca', 

81 'use-latex-toc': '1', 

82 } 

83 for s in ( 

84 '--language=ca, --use-latex-toc,--documentoptions=[english,12pt,lettersize]', 

85 '--documentoptions=[english,12pt,lettersize],--language=ca, --use-latex-toc', 

86 '--use-latex-toc,--documentoptions=[english,12pt,lettersize],--language=ca, ', 

87 ): 

88 result = x.handleMissingStyleSheetArgs(s=s) 

89 self.assertEqual(result, expected) 

90 #@+node:ekr.20210902211919.11: *3* TestRst.test_unicode_characters 

91 def test_unicode_characters(self): 

92 c = self.c 

93 rc = c.rstCommands 

94 # Create the *input* tree. 

95 root = c.rootPosition().insertAfter() 

96 root.h = fn = '@rst unicode_test.html' 

97 # Insert the body text. Overindent to eliminate @verbatim sentinels. 

98 root.b = textwrap.dedent("""\ 

99 Test of unicode characters: ÀNjϢﻙ 

100 

101 End of test. 

102 """) 

103 # Define the expected output. 

104 expected = textwrap.dedent(f"""\ 

105 .. rst3: filename: {fn} 

106 

107 .. _http-node-marker-1: 

108 

109 Test of unicode characters: ÀNjϢﻙ 

110 

111 End of test. 

112 

113 """) 

114 # Get and check the rst result. 

115 rc.nodeNumber = 0 

116 rc.http_server_support = True # Override setting for testing. 

117 source = rc.write_rst_tree(root, fn) 

118 self.assertEqual(source, expected) 

119 # Get the html from docutils. 

120 html = rc.writeToDocutils(source, ext='.html') 

121 # Don't bother testing the html. It will depend on docutils. 

122 assert html and html.startswith('<?xml') and html.strip().endswith('</html>') 

123 #@+node:ekr.20210327092009.1: *3* TestRst.write_logic 

124 def test_write_to_docutils(self): 

125 c = self.c 

126 rc = c.rstCommands 

127 # Create the *input* tree. 

128 root = c.rootPosition().insertAfter() 

129 root.h = fn = '@rst test.html' 

130 child = root.insertAsLastChild() 

131 child.h = 'section' 

132 # Insert the body texts. Overindent to eliminate @verbatim sentinels. 

133 root.b = textwrap.dedent("""\ 

134 @language rest 

135 

136 ##### 

137 Title 

138 ##### 

139 

140 This is test.html 

141 """) 

142 child.b = textwrap.dedent("""\ 

143 @ This is a doc part 

144 it has two lines. 

145 @c 

146 This is the body of the section. 

147 """) 

148 # Define the expected output. 

149 expected = textwrap.dedent(f"""\ 

150 .. rst3: filename: {fn} 

151 

152 .. _http-node-marker-1: 

153 

154 @language rest 

155 

156 ##### 

157 Title 

158 ##### 

159 

160 This is test.html 

161 

162 .. _http-node-marker-2: 

163 

164 section 

165 +++++++ 

166 

167 @ This is a doc part 

168 it has two lines. 

169 @c 

170 This is the body of the section. 

171 

172 """) 

173 # Get and check the rst result. 

174 rc.nodeNumber = 0 

175 rc.http_server_support = True # Override setting for testing. 

176 source = rc.write_rst_tree(root, fn) 

177 self.assertEqual(source, expected) 

178 # Get the html from docutils. 

179 html = rc.writeToDocutils(source, ext='.html') 

180 # Don't bother testing the html. It will depend on docutils. 

181 assert html and html.startswith('<?xml') and html.strip().endswith('</html>') 

182 #@-others 

183#@-others 

184#@-leo