test_util_comment.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # coding=utf-8
  2. """
  3. Unit tests for ddns.util.comment module
  4. @author: GitHub Copilot
  5. """
  6. from __future__ import unicode_literals
  7. from __init__ import unittest
  8. from ddns.util.comment import remove_comment
  9. class TestRemoveComment(unittest.TestCase):
  10. """Test cases for comment removal functionality"""
  11. def test_remove_comment_empty_string(self):
  12. """测试空字符串"""
  13. result = remove_comment("")
  14. self.assertEqual(result, "")
  15. def test_remove_comment_no_comments(self):
  16. """测试没有注释的内容"""
  17. content = '{"key": "value", "number": 123}'
  18. result = remove_comment(content)
  19. self.assertEqual(result, content)
  20. def test_remove_comment_hash_full_line(self):
  21. """测试整行 # 注释"""
  22. content = '# This is a comment\n{"key": "value"}'
  23. expected = '\n{"key": "value"}'
  24. result = remove_comment(content)
  25. self.assertEqual(result, expected)
  26. def test_remove_comment_double_slash_full_line(self):
  27. """测试整行 // 注释"""
  28. content = '// This is a comment\n{"key": "value"}'
  29. expected = '\n{"key": "value"}'
  30. result = remove_comment(content)
  31. self.assertEqual(result, expected)
  32. def test_remove_comment_hash_with_leading_whitespace(self):
  33. """测试带前导空白的 # 注释"""
  34. content = ' # This is a comment\n{"key": "value"}'
  35. expected = '\n{"key": "value"}'
  36. result = remove_comment(content)
  37. self.assertEqual(result, expected)
  38. def test_remove_comment_double_slash_with_leading_whitespace(self):
  39. """测试带前导空白的 // 注释"""
  40. content = ' // This is a comment\n{"key": "value"}'
  41. expected = '\n{"key": "value"}'
  42. result = remove_comment(content)
  43. self.assertEqual(result, expected)
  44. def test_remove_comment_hash_end_of_line(self):
  45. """测试行尾 # 注释"""
  46. content = '{"key": "value"} # this is a comment'
  47. expected = '{"key": "value"}'
  48. result = remove_comment(content)
  49. self.assertEqual(result, expected)
  50. def test_remove_comment_double_slash_end_of_line(self):
  51. """测试行尾 // 注释"""
  52. content = '{"key": "value"} // this is a comment'
  53. expected = '{"key": "value"}'
  54. result = remove_comment(content)
  55. self.assertEqual(result, expected)
  56. def test_remove_comment_hash_in_string_should_not_remove(self):
  57. """测试字符串内的 # 不应该被移除"""
  58. content = '{"url": "http://example.com#anchor"}'
  59. result = remove_comment(content)
  60. self.assertEqual(result, content)
  61. def test_remove_comment_double_slash_in_string_should_not_remove(self):
  62. """测试字符串内的 // 不应该被移除"""
  63. content = '{"url": "http://example.com/path"}'
  64. result = remove_comment(content)
  65. self.assertEqual(result, content)
  66. def test_remove_comment_single_quoted_string_with_hash(self):
  67. """测试单引号字符串内的 # 不应该被移除"""
  68. content = "{'url': 'http://example.com#anchor'}"
  69. result = remove_comment(content)
  70. self.assertEqual(result, content)
  71. def test_remove_comment_single_quoted_string_with_double_slash(self):
  72. """测试单引号字符串内的 // 不应该被移除"""
  73. content = "{'url': 'http://example.com/path'}"
  74. result = remove_comment(content)
  75. self.assertEqual(result, content)
  76. def test_remove_comment_escaped_quotes_in_string(self):
  77. """测试字符串内转义引号的处理"""
  78. content = '{"message": "He said \\"Hello#World\\""} # comment'
  79. expected = '{"message": "He said \\"Hello#World\\""}'
  80. result = remove_comment(content)
  81. self.assertEqual(result, expected)
  82. def test_remove_comment_complex_json_with_comments(self):
  83. """测试复杂JSON配置与多种注释"""
  84. content = """{
  85. // Configuration file for DDNS
  86. "$schema": "https://ddns.newfuture.cc/schema/v4.0.json", // Schema validation
  87. "debug": false, # false=disable, true=enable
  88. "dns": "dnspod_com", // DNS provider
  89. "id": "1008666", # ID or Email
  90. "token": "ae86$cbbcctv666666666666666", // API Token or Key
  91. "ipv4": ["test.lorzl.ml"], # IPv4 domains to update
  92. "ipv6": ["test.lorzl.ml"], // IPv6 domains to update
  93. "index4": "public", # IPv4 update method
  94. "index6": "url:https://iptest.com", # IPv6 update method
  95. "proxy": null // Proxy settings
  96. }"""
  97. expected = """{
  98. "$schema": "https://ddns.newfuture.cc/schema/v4.0.json",
  99. "debug": false,
  100. "dns": "dnspod_com",
  101. "id": "1008666",
  102. "token": "ae86$cbbcctv666666666666666",
  103. "ipv4": ["test.lorzl.ml"],
  104. "ipv6": ["test.lorzl.ml"],
  105. "index4": "public",
  106. "index6": "url:https://iptest.com",
  107. "proxy": null
  108. }"""
  109. result = remove_comment(content)
  110. self.assertEqual(result, expected)
  111. def test_remove_comment_mixed_comment_styles(self):
  112. """测试混合注释风格"""
  113. content = """// Header comment
  114. {
  115. # This is a hash comment
  116. "key1": "value1", // End of line comment
  117. "key2": "value2" # Another end of line comment
  118. }
  119. # Footer comment"""
  120. expected = """
  121. {
  122. "key1": "value1",
  123. "key2": "value2"
  124. }
  125. """
  126. result = remove_comment(content)
  127. self.assertEqual(result, expected)
  128. def test_remove_comment_comments_with_special_chars(self):
  129. """测试包含特殊字符的注释"""
  130. content = """// Comment with 中文字符 and émojis 🚀
  131. {
  132. "test": "value" # Comment with symbols !@#$%^&*()
  133. }"""
  134. expected = """
  135. {
  136. "test": "value"
  137. }"""
  138. result = remove_comment(content)
  139. self.assertEqual(result, expected)
  140. def test_remove_comment_preserve_empty_lines(self):
  141. """测试保留空行"""
  142. content = """// Comment
  143. {
  144. "key": "value"
  145. }
  146. // Another comment"""
  147. expected = """
  148. {
  149. "key": "value"
  150. }
  151. """
  152. result = remove_comment(content)
  153. self.assertEqual(result, expected)
  154. def test_remove_comment_url_with_hash_and_comment(self):
  155. """测试URL中包含#,行尾有注释的情况"""
  156. content = '{"url": "https://example.com#section"} # This is a comment'
  157. expected = '{"url": "https://example.com#section"}'
  158. result = remove_comment(content)
  159. self.assertEqual(result, expected)
  160. def test_remove_comment_json_array_with_comments(self):
  161. """测试JSON数组与注释"""
  162. content = """[
  163. // First item
  164. "item1", # Comment 1
  165. "item2", // Comment 2
  166. "item3" # Last item
  167. ]"""
  168. expected = """[
  169. "item1",
  170. "item2",
  171. "item3"
  172. ]"""
  173. result = remove_comment(content)
  174. self.assertEqual(result, expected)
  175. def test_remove_comment_nested_quotes(self):
  176. """测试嵌套引号的处理"""
  177. content = """{"message": "She said: \\"Don't use // or # here\\""} // comment"""
  178. expected = """{"message": "She said: \\"Don't use // or # here\\""} """
  179. result = remove_comment(content)
  180. # Note: we expect a trailing space where the comment was removed
  181. self.assertEqual(result, expected.rstrip())
  182. def test_remove_comment_multiple_slashes(self):
  183. """测试多个斜杠的情况"""
  184. content = '{"path": "C:\\\\Program Files\\\\App"} // Windows path'
  185. expected = '{"path": "C:\\\\Program Files\\\\App"}'
  186. result = remove_comment(content)
  187. self.assertEqual(result, expected)
  188. def test_remove_comment_hash_after_double_slash_comment(self):
  189. """测试 // 注释中包含 # 的情况"""
  190. content = '{"key": "value"} // Comment with # symbol'
  191. expected = '{"key": "value"}'
  192. result = remove_comment(content)
  193. self.assertEqual(result, expected)
  194. def test_remove_comment_single_line_various_formats(self):
  195. """测试单行多种格式"""
  196. test_cases = [
  197. ('{"key": "value"}', '{"key": "value"}'), # No comment
  198. ("# Full line comment", ""), # Full line hash
  199. ("// Full line comment", ""), # Full line double slash
  200. (" # Indented comment", ""), # Indented hash
  201. (" // Indented comment", ""), # Indented double slash
  202. ('{"a": "b"} # End comment', '{"a": "b"}'), # End hash
  203. ('{"a": "b"} // End comment', '{"a": "b"}'), # End double slash
  204. ]
  205. for i, (input_content, expected) in enumerate(test_cases):
  206. result = remove_comment(input_content)
  207. self.assertEqual(
  208. result,
  209. expected,
  210. "Failed for test case %d: %r -> expected %r, got %r" % (i, input_content, expected, result),
  211. )
  212. if __name__ == "__main__":
  213. unittest.main()