/test/lib/regular_expression/test_perl_syntax_01.e

http://github.com/tybor/Liberty · Specman e · 305 lines · 257 code · 16 blank · 32 comment · 30 complexity · 3e6331200187a6f114707a3c26c5b604 MD5 · raw file

  1. -- This file is part of SmartEiffel The GNU Eiffel Compiler Tools and Libraries.
  2. -- See the Copyright notice at the end of this file.
  3. --
  4. class TEST_PERL_SYNTAX_01
  5. --
  6. -- Testing regular expressions with perl syntax
  7. -- Input test file is from perl project (in perl-5.9.2.devel.tar.gz: perl-5.9.2/t/op/re_tests)
  8. -- It has been renamed as perl_re_tests
  9. insert
  10. EIFFELTEST_TOOLS
  11. create {}
  12. make
  13. feature {}
  14. print_extra_info: BOOLEAN False
  15. comments_allowed: BOOLEAN True
  16. print_ok: BOOLEAN False
  17. print_error: BOOLEAN True
  18. make
  19. local
  20. re_tests: TEXT_FILE_READ
  21. do
  22. pattern := ""
  23. input_text := ""
  24. matching_result := ""
  25. function := ""
  26. function_result := ""
  27. comment := ""
  28. from
  29. create re_tests.connect_to("perl_re_tests")
  30. if not re_tests.is_connected then
  31. io.put_string("Failed to open file re_tests.%N")
  32. crash
  33. end
  34. re_tests.read_line
  35. until
  36. re_tests.end_of_input
  37. loop
  38. line_number := line_number + 1
  39. if comments_allowed and then re_tests.last_string.has_prefix(once "--") then
  40. if print_extra_info then
  41. io.put_string(once "Warning line skipped (comment): ")
  42. io.put_integer(line_number)
  43. io.put_new_line
  44. end
  45. else
  46. substitute_new_lines(re_tests.last_string)
  47. build_one_test(re_tests.last_string)
  48. run_test
  49. end
  50. re_tests.read_line
  51. end
  52. re_tests.disconnect
  53. end
  54. substitute_new_lines (line: STRING)
  55. local
  56. pos: INTEGER
  57. do
  58. from
  59. pos := line.first_substring_index("\n")
  60. until
  61. pos = 0
  62. loop
  63. line.remove(pos)
  64. line.put('%N', pos)
  65. pos := line.first_substring_index("\n")
  66. end
  67. end
  68. build_one_test (line: STRING)
  69. local
  70. first, last, i: INTEGER
  71. do
  72. from
  73. i := line_elements.lower
  74. first := 1
  75. until
  76. i >= line_elements.upper
  77. loop
  78. last := line.index_of('%T', first)
  79. if last = 0 then
  80. last := line.count + 1
  81. end
  82. line_elements.item(i).clear_count
  83. line_elements.item(i).append_substring(line, first, last - 1)
  84. first := last + 1
  85. i := i + 1
  86. end
  87. line_elements.last.clear_count
  88. if first <= line.count then
  89. line_elements.last.append_substring(line, first, line.count)
  90. end
  91. end
  92. run_test
  93. local
  94. factory: REGULAR_EXPRESSION_BUILDER; reg_exp: REGULAR_EXPRESSION
  95. do
  96. if pattern.first = '%'' then
  97. pattern.remove_head(1)
  98. from
  99. until
  100. pattern.last = '%''
  101. loop
  102. inspect
  103. pattern.last
  104. when 'i' then
  105. factory.set_case_insensitive
  106. when 'x' then
  107. factory.set_extended_legibility
  108. when 'm' then
  109. factory.set_match_line_boundary
  110. when 's' then
  111. factory.set_any_match_newline
  112. end
  113. pattern.remove_tail(1)
  114. end
  115. pattern.remove_tail(1)
  116. end
  117. reg_exp := factory.convert_perl_pattern(pattern)
  118. if reg_exp = Void then
  119. if not matching_result.is_equal(once "c") then
  120. if print_error then
  121. io.put_string(once "Failed to understand pattern for line ")
  122. io.put_integer(line_number)
  123. io.put_new_line
  124. io.put_string(once "%TError is : %"")
  125. io.put_string(factory.last_error_message)
  126. io.put_string(once "%"%N")
  127. end
  128. assert(False)
  129. else
  130. if print_extra_info then
  131. if print_ok then
  132. io.put_string(once "Ligne OK: ")
  133. io.put_integer(line_number)
  134. io.put_string(once "%Terror message: ")
  135. io.put_string(factory.last_error_message)
  136. io.put_new_line
  137. end
  138. end
  139. assert(True)
  140. end
  141. else
  142. if matching_result.is_equal(once "c") then
  143. if print_error then
  144. io.put_string(once "Undetected syntax error for pattern line ")
  145. io.put_integer(line_number)
  146. io.put_new_line
  147. end
  148. assert(False)
  149. elseif reg_exp.match(input_text) then
  150. if matching_result.is_equal(once "n") then
  151. if print_error then
  152. io.put_string(once "Wrong match success for input line ")
  153. io.put_integer(line_number)
  154. io.put_new_line
  155. end
  156. assert(False)
  157. else
  158. eval_perl_function(function, function_result, reg_exp, input_text)
  159. end
  160. elseif matching_result.is_equal(once "y") then
  161. if print_error then
  162. io.put_string(once "Wrong match failure for input line ")
  163. io.put_integer(line_number)
  164. io.put_new_line
  165. end
  166. assert(False)
  167. else
  168. if print_extra_info then
  169. if print_ok then
  170. io.put_string(once "Ligne OK: ")
  171. io.put_integer(line_number)
  172. io.put_new_line
  173. end
  174. end
  175. assert(True)
  176. end
  177. end
  178. end
  179. eval_perl_function (perl_function, expected_result: STRING; reg_exp: REGULAR_EXPRESSION; text: STRING)
  180. local
  181. build_result, buffer: STRING; i: INTEGER
  182. do
  183. build_result := once ""
  184. build_result.copy(perl_function)
  185. buffer := once ""
  186. -- $&
  187. buffer.clear_count
  188. reg_exp.append_pattern_text(text, buffer)
  189. substitute(build_result, "$&", buffer)
  190. -- $i
  191. from
  192. i := reg_exp.group_count.max(9)
  193. until
  194. i < 1
  195. loop
  196. buffer.clear_count
  197. if i <= reg_exp.group_count and then reg_exp.ith_group_matched(i) then
  198. reg_exp.append_ith_group(text, buffer, i)
  199. end
  200. --memory leak
  201. substitute(build_result, once "$" + i.to_string, buffer)
  202. i := i - 1
  203. end
  204. -- $-[i]
  205. from
  206. i := 0
  207. until
  208. i > reg_exp.group_count
  209. loop
  210. --memory leak
  211. if reg_exp.ith_group_matched(i) then
  212. substitute(build_result, once "$-[" + i.to_string + once "]", (reg_exp.ith_group_first_index(i) - 1).to_string)
  213. else
  214. substitute(build_result, once "$-[" + i.to_string + once "]", once "")
  215. end
  216. i := i + 1
  217. end
  218. -- $+[0]
  219. from
  220. i := 0
  221. until
  222. i > reg_exp.group_count
  223. loop
  224. --memory leak
  225. if reg_exp.ith_group_matched(i) then
  226. substitute(build_result, once "$+[" + i.to_string + once "]", reg_exp.ith_group_last_index(i).to_string)
  227. else
  228. substitute(build_result, once "$+[" + i.to_string + once "]", once "")
  229. end
  230. i := i + 1
  231. end
  232. if not expected_result.is_equal(build_result) then
  233. if print_error then
  234. io.put_string(once "Error in function evaluation for input line ")
  235. io.put_integer(line_number)
  236. io.put_string("%N%TFunction is: %"")
  237. io.put_string(perl_function)
  238. io.put_string(once "%"%N%TExpected is: %"")
  239. io.put_string(expected_result)
  240. io.put_string(once "%"%N%TFound result is: %"")
  241. io.put_string(build_result)
  242. io.put_string(once "%"%N")
  243. end
  244. assert(False)
  245. else
  246. if print_ok then
  247. io.put_string(once "Ligne OK: ")
  248. io.put_integer(line_number)
  249. io.put_new_line
  250. end
  251. assert(True)
  252. end
  253. end
  254. substitute (text, search, replacement: STRING)
  255. local
  256. pos: INTEGER
  257. do
  258. pos := text.first_substring_index(search)
  259. if pos /= 0 then
  260. text.replace_substring(replacement, pos, pos + search.count - 1)
  261. end
  262. end
  263. line_number: INTEGER
  264. pattern, input_text, matching_result, function, function_result, comment: STRING
  265. line_elements: FAST_ARRAY[STRING]
  266. require
  267. pattern /= Void
  268. once
  269. Result := {FAST_ARRAY[STRING] << pattern, input_text, matching_result, function, function_result, comment >> }
  270. end
  271. end -- class TEST_PERL_SYNTAX_01
  272. --
  273. -- ------------------------------------------------------------------------------------------------------------------------------
  274. -- Copyright notice below. Please read.
  275. --
  276. -- SmartEiffel is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License,
  277. -- as published by the Free Software Foundation; either version 2, or (at your option) any later version.
  278. -- SmartEiffel is distributed in the hope that it will be useful but WITHOUT ANY WARRANTY; without even the implied warranty
  279. -- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have
  280. -- received a copy of the GNU General Public License along with SmartEiffel; see the file COPYING. If not, write to the Free
  281. -- Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  282. --
  283. -- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P. - University of Nancy 1 - FRANCE
  284. -- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
  285. --
  286. -- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
  287. --
  288. -- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
  289. -- ------------------------------------------------------------------------------------------------------------------------------