PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/test-unit-2.1.1/test/test-diff.rb

http://github.com/IronLanguages/main
Ruby | 518 lines | 460 code | 58 blank | 0 comment | 5 complexity | 697acc68beded39c526facdcaa11a74e MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. class TestUnitDiff < Test::Unit::TestCase
  2. def test_binary_search_ranges
  3. assert_found_binary_search_ranges(5, [1..2, 4..5, 7..9])
  4. assert_not_found_binary_search_ranges(3, [1..2, 4..5, 7..9])
  5. end
  6. def test_to_indexes
  7. assert_to_indexes({"abc def" => [0, 2], "abc" => [1]},
  8. ["abc def", "abc", "abc def"])
  9. assert_to_indexes({?a => [0, 3], ?b => [1], ?c => [2], ?d => [4]},
  10. "abcad")
  11. assert_to_indexes({
  12. ?1 => [0, 35],
  13. ?t => [2, 5, 16],
  14. ?e => [3, 14, 31, 38],
  15. ?s => [4, 6, 12, 13, 20, 32, 44],
  16. ?, => [7, 21, 33],
  17. ?0 => [9, 23],
  18. ?a => [11, 26],
  19. ?r => [15, 30],
  20. ?i => [17, 27, 41],
  21. ?o => [18],
  22. ?n => [19, 39, 42],
  23. ?f => [25],
  24. ?l => [28],
  25. ?u => [29],
  26. ?p => [37],
  27. ?d => [40],
  28. ?g => [43],
  29. },
  30. "1 tests, 0 assertions, 0 failures, 1 pendings") do |x|
  31. x == " "[0]
  32. end
  33. end
  34. def test_longest_match
  35. assert_longest_match([0, 1, 3],
  36. %w(b c d), %w(a b c d x y z),
  37. 0, 2, 0, 7)
  38. assert_longest_match([1, 2, 2],
  39. %w(b c d), %w(a b c d x y z),
  40. 1, 2, 0, 6)
  41. assert_longest_match([0, 0, 0],
  42. %w(a b), %w(c),
  43. 0, 1, 0, 0)
  44. assert_longest_match([1, 0, 2],
  45. %w(q a b x c d), %w(a b y c d f),
  46. 0, 5, 0, 5)
  47. assert_longest_match([4, 3, 2],
  48. %w(q a b x c d), %w(a b y c d f),
  49. 3, 5, 2, 5)
  50. assert_longest_match([1, 0, 2], "qabxcd", "abycdf", 0, 5, 0, 5)
  51. assert_longest_match([0, 0, 1], "efg", "eg", 0, 2, 0, 1)
  52. assert_longest_match([2, 1, 1], "efg", "eg", 1, 2, 1, 1)
  53. end
  54. def test_longest_match_with_junk_predicate
  55. assert_longest_match([0, 4, 5], " abcd", "abcd abcd", 0, 4, 0, 8)
  56. assert_longest_match([1, 0, 4], " abcd", "abcd abcd", 0, 4, 0, 8) do |x|
  57. x == ' '[0]
  58. end
  59. end
  60. def test_matches
  61. assert_matches([[0, 0, 2],
  62. [3, 2, 2]],
  63. %w(a b x c d), %w(a b c d))
  64. assert_matches([[1, 0, 2],
  65. [4, 3, 2]],
  66. %w(q a b x c d), %w(a b y c d f))
  67. assert_matches([[1, 0, 2],
  68. [4, 3, 2]],
  69. "qabxcd", "abycdf")
  70. assert_matches([[0, 0, 1],
  71. [2, 1, 1]],
  72. "efg", "eg")
  73. end
  74. def test_matches_with_junk_predicate
  75. assert_matches([[0, 0, 23],
  76. [24, 24, 11],
  77. [36, 36, 9]],
  78. "1 tests, 0 assertions, 1 failures, 0 pendings",
  79. "1 tests, 0 assertions, 0 failures, 1 pendings")
  80. assert_matches([[0, 0, 1],
  81. [1, 1, 8],
  82. [9, 9, 1],
  83. [10, 10, 13],
  84. [24, 24, 11],
  85. [36, 36, 9]],
  86. "1 tests, 0 assertions, 1 failures, 0 pendings",
  87. "1 tests, 0 assertions, 0 failures, 1 pendings") do |x|
  88. x == " "[0]
  89. end
  90. end
  91. def test_blocks
  92. assert_blocks([[0, 0, 2],
  93. [3, 2, 2],
  94. [5, 4, 0]],
  95. %w(a b x c d), %w(a b c d))
  96. assert_blocks([[1, 0, 2],
  97. [4, 3, 2],
  98. [6, 6, 0]],
  99. %w(q a b x c d), %w(a b y c d f))
  100. assert_blocks([[1, 0, 2],
  101. [4, 3, 2],
  102. [6, 6, 0]],
  103. "qabxcd", "abycdf")
  104. assert_blocks([[0, 0, 1],
  105. [2, 1, 1],
  106. [3, 2, 0]],
  107. "efg", "eg")
  108. end
  109. def test_blocks_with_junk_predicate
  110. assert_blocks([[0, 0, 23],
  111. [24, 24, 11],
  112. [36, 36, 9],
  113. [45, 45, 0]],
  114. "1 tests, 0 assertions, 1 failures, 0 pendings",
  115. "1 tests, 0 assertions, 0 failures, 1 pendings") do |x|
  116. x == " "[0]
  117. end
  118. end
  119. def test_operations
  120. assert_operations([], %w(), %w())
  121. assert_operations([[:delete, 0, 1, 0, 0],
  122. [:equal, 1, 3, 0, 2],
  123. [:replace, 3, 4, 2, 3],
  124. [:equal, 4, 6, 3, 5],
  125. [:insert, 6, 6, 5, 6]],
  126. %w(q a b x c d), %w(a b y c d f))
  127. assert_operations([[:delete, 0, 1, 0, 0],
  128. [:equal, 1, 3, 0, 2],
  129. [:replace, 3, 4, 2, 3],
  130. [:equal, 4, 6, 3, 5],
  131. [:insert, 6, 6, 5, 6]],
  132. "qabxcd", "abycdf")
  133. assert_operations([[:equal, 0, 23, 0, 23],
  134. [:replace, 23, 24, 23, 24],
  135. [:equal, 24, 35, 24, 35],
  136. [:replace, 35, 36, 35, 36],
  137. [:equal, 36, 45, 36, 45]],
  138. "1 tests, 0 assertions, 1 failures, 0 pendings",
  139. "1 tests, 0 assertions, 0 failures, 1 pendings")
  140. assert_operations([[:equal, 0, 23, 0, 23],
  141. [:replace, 23, 24, 23, 24],
  142. [:equal, 24, 35, 24, 35],
  143. [:replace, 35, 36, 35, 36],
  144. [:equal, 36, 45, 36, 45]],
  145. "1 tests, 0 assertions, 1 failures, 0 pendings",
  146. "1 tests, 0 assertions, 0 failures, 1 pendings") do |x|
  147. x == " "[0]
  148. end
  149. end
  150. def test_grouped_operations
  151. assert_grouped_operations([[[:equal, 0, 0, 0, 0]]],
  152. %w(),
  153. %w())
  154. assert_grouped_operations([[[:equal, 0, 3, 0, 3]]],
  155. %w(a b c),
  156. %w(a b c))
  157. assert_grouped_operations([[[:equal, 0, 1, 0, 1],
  158. [:replace, 1, 2, 1, 2],
  159. [:equal, 2, 5, 2, 5]],
  160. [[:equal, 8, 11, 8, 11],
  161. [:replace, 11, 12, 11, 12],
  162. [:equal, 12, 13, 12, 13],
  163. [:delete, 13, 16, 13, 13],
  164. [:equal, 16, 17, 13, 14],
  165. [:replace, 17, 18, 14, 15],
  166. [:equal, 18, 20, 15, 17]]],
  167. %w(1 2 3 4 5 6 7 8 9 a b c d e f g h i j k),
  168. %w(1 i 3 4 5 6 7 8 9 a b cX d h iX j k))
  169. end
  170. def test_ratio
  171. assert_ratio(0.75, "abcd", "bcde")
  172. assert_ratio(0.80, "efg", "eg")
  173. end
  174. def test_1_length_readable_diff
  175. differ = Test::Unit::Diff::ReadableDiffer.new(["0"], ["1"])
  176. def differ.cut_off_ratio
  177. 0
  178. end
  179. def differ.default_ratio
  180. 0
  181. end
  182. assert_equal("- 0\n" +
  183. "? ^\n" +
  184. "+ 1\n" +
  185. "? ^",
  186. differ.diff.join("\n"))
  187. end
  188. def test_same_contents_readable_diff
  189. assert_readable_diff(" aaa", ["aaa"], ["aaa"])
  190. assert_readable_diff(" aaa\n" \
  191. " bbb",
  192. ["aaa", "bbb"], ["aaa", "bbb"])
  193. end
  194. def test_deleted_readable_diff
  195. assert_readable_diff(" aaa\n" \
  196. "- bbb",
  197. ["aaa", "bbb"], ["aaa"])
  198. assert_readable_diff(" aaa\n" \
  199. "- bbb\n" \
  200. "- ccc\n" \
  201. "- ddd",
  202. ["aaa", "bbb", "ccc", "ddd"], ["aaa"])
  203. end
  204. def test_inserted_readable_diff
  205. assert_readable_diff(" aaa\n" \
  206. "+ bbb\n" \
  207. "+ ccc\n" \
  208. "+ ddd",
  209. ["aaa"], ["aaa", "bbb", "ccc", "ddd"])
  210. end
  211. def test_replace_readable_diff
  212. assert_readable_diff(" aaa\n" \
  213. "- bbb\n" \
  214. "+ BbB\n" \
  215. " ccc\n" \
  216. "- ddd\n" \
  217. "- efg\n" \
  218. "? -\n" \
  219. "+ eg",
  220. ["aaa", "bbb", "ccc", "ddd", "efg"],
  221. ["aaa", "BbB", "ccc", "eg"])
  222. assert_readable_diff("- abcd xyz abc\n" \
  223. "? -\n" \
  224. "+ abcd abcd xyz abc\n" \
  225. "? +++++",
  226. [" abcd xyz abc"],
  227. ["abcd abcd xyz abc"])
  228. end
  229. def test_difference_readable_diff
  230. assert_readable_diff("- 1 tests, 0 assertions, 1 failures, 0 pendings\n" \
  231. "? ^ ^\n" \
  232. "+ 1 tests, 0 assertions, 0 failures, 1 pendings\n" \
  233. "? ^ ^",
  234. ["1 tests, 0 assertions, 1 failures, 0 pendings"],
  235. ["1 tests, 0 assertions, 0 failures, 1 pendings"])
  236. end
  237. def test_complex_readable_diff
  238. assert_readable_diff(" aaa\n" \
  239. "- bbb\n" \
  240. "- ccc\n" \
  241. "+ \n" \
  242. "+ # \n" \
  243. " ddd",
  244. ["aaa", "bbb", "ccc", "ddd"],
  245. ["aaa", "", " # ", "ddd"])
  246. assert_readable_diff("- one1\n" \
  247. "? ^\n" \
  248. "+ ore1\n" \
  249. "? ^\n" \
  250. "- two2\n" \
  251. "- three3\n" \
  252. "? - -\n" \
  253. "+ tree\n" \
  254. "+ emu",
  255. ["one1", "two2", "three3"],
  256. ["ore1", "tree", "emu"])
  257. end
  258. def test_empty_readable_diff
  259. assert_readable_diff("", [""], [""])
  260. end
  261. def test_unified_diff
  262. assert_unified_diff("",
  263. ["one", "two", "three"],
  264. ["one", "two", "three"],
  265. "content 1",
  266. "content 2")
  267. assert_unified_diff("--- Original Sat Jan 26 23:30:50 1991\n" \
  268. "+++ Current Fri Jun 06 10:20:52 2003\n" \
  269. "@@ -1,4 +1,4 @@\n" \
  270. "+zero\n" \
  271. " one\n" \
  272. "-two\n" \
  273. "-three\n" \
  274. "+tree\n" \
  275. " four",
  276. ["one", "two", "three", "four"],
  277. ["zero", "one", "tree", "four"],
  278. "Original Sat Jan 26 23:30:50 1991",
  279. "Current Fri Jun 06 10:20:52 2003",
  280. :show_context => false)
  281. from = File.read(__FILE__).split(/\n/)
  282. to = from.dup
  283. target_line = __LINE__
  284. to[target_line - 1, 1] = []
  285. context = " def test_unified_diff"
  286. summary = "@@ -#{target_line - 3},7 +#{target_line - 3},6 @@ #{context}"
  287. assert_unified_diff((["--- revision 10",
  288. "+++ revision 11",
  289. summary] +
  290. from[target_line - 4, 3].collect {|line| " #{line}"} +
  291. ["-#{from[target_line - 1]}"] +
  292. from[target_line, 3].collect {|line| " #{line}"}
  293. ).join("\n"),
  294. from, to,
  295. "revision 10",
  296. "revision 11")
  297. end
  298. def test_empty_unified_diff
  299. assert_unified_diff("", [""], [""], "From", "To")
  300. assert_unified_diff("", [], [], "From", "To")
  301. end
  302. def test_diff_lines
  303. assert_diff_lines(["- ddd",
  304. "- efg",
  305. "? -",
  306. "+ eg"],
  307. ["aaa", "bbb", "ccc", "ddd", "efg"],
  308. ["aaa", "BbB", "ccc", "eg"],
  309. 3, 5, 3, 4)
  310. end
  311. def test_diff_line
  312. assert_diff_line(["- abcDefghiJkl",
  313. "? ^ ^ ^",
  314. "+ abcdefGhijkl",
  315. "? ^ ^ ^"],
  316. "abcDefghiJkl",
  317. "abcdefGhijkl")
  318. assert_diff_line(["- bcDefghiJklx",
  319. "? ^ ^ ^ -",
  320. "+ abcdefGhijkl",
  321. "? + ^ ^ ^"],
  322. "bcDefghiJklx",
  323. "abcdefGhijkl")
  324. end
  325. def test_empty_diff_line
  326. assert_diff_line(["- ",
  327. "+ "],
  328. "", "")
  329. end
  330. def test_format_diff_point
  331. assert_format_diff_point(["- \tabcDefghiJkl",
  332. "? \t ^ ^ ^",
  333. "+ \t\tabcdefGhijkl",
  334. "? \t ^ ^ ^"],
  335. "\tabcDefghiJkl",
  336. "\t\tabcdefGhijkl",
  337. " ^ ^ ^ ",
  338. "+ ^ ^ ^ ")
  339. assert_format_diff_point(["- efg",
  340. "? ^",
  341. "+ eg"],
  342. "efg",
  343. "eg",
  344. " ^",
  345. "")
  346. end
  347. def test_interesting_line
  348. from = ["class X",
  349. " def find(x=0)",
  350. " body",
  351. " end",
  352. "end"]
  353. to = ["def xxx",
  354. " raise 'not call me'",
  355. "end"]
  356. assert_interesting_line(" def find(x=0)",
  357. from, to,
  358. 2, 1)
  359. assert_interesting_line("def xxx",
  360. from, to,
  361. 2, 0)
  362. assert_interesting_line("class X",
  363. from, to,
  364. 0, 0)
  365. end
  366. private
  367. def assert_found_binary_search_ranges(numeric, ranges)
  368. assert_true(Test::Unit::Diff::UTF8Line.send(:binary_search_ranges,
  369. numeric,
  370. ranges))
  371. end
  372. def assert_not_found_binary_search_ranges(numeric, ranges)
  373. assert_false(Test::Unit::Diff::UTF8Line.send(:binary_search_ranges,
  374. numeric,
  375. ranges))
  376. end
  377. def assert_to_indexes(expected, to, &junk_predicate)
  378. matcher = Test::Unit::Diff::SequenceMatcher.new([""], to, &junk_predicate)
  379. assert_equal(expected, matcher.instance_variable_get("@to_indexes"))
  380. end
  381. def assert_find_best_match_position(expected, from, to,
  382. from_start, from_end,
  383. to_start, to_end, &junk_predicate)
  384. matcher = Test::Unit::Diff::SequenceMatcher.new(from, to, &junk_predicate)
  385. assert_equal(expected, matcher.send(:find_best_match_position,
  386. from_start, from_end,
  387. to_start, to_end))
  388. end
  389. def assert_longest_match(expected, from, to,
  390. from_start, from_end,
  391. to_start, to_end, &junk_predicate)
  392. matcher = Test::Unit::Diff::SequenceMatcher.new(from, to, &junk_predicate)
  393. assert_equal(expected, matcher.longest_match(from_start, from_end,
  394. to_start, to_end))
  395. end
  396. def assert_matches(expected, from, to, &junk_predicate)
  397. matcher = Test::Unit::Diff::SequenceMatcher.new(from, to, &junk_predicate)
  398. assert_equal(expected, matcher.send(:matches))
  399. end
  400. def assert_blocks(expected, from, to, &junk_predicate)
  401. matcher = Test::Unit::Diff::SequenceMatcher.new(from, to, &junk_predicate)
  402. assert_equal(expected, matcher.blocks)
  403. end
  404. def assert_operations(expected, from, to, &junk_predicate)
  405. matcher = Test::Unit::Diff::SequenceMatcher.new(from, to, &junk_predicate)
  406. assert_equal(expected, matcher.operations)
  407. end
  408. def assert_grouped_operations(expected, from, to)
  409. matcher = Test::Unit::Diff::SequenceMatcher.new(from, to)
  410. assert_equal(expected, matcher.grouped_operations)
  411. end
  412. def assert_ratio(expected, from, to)
  413. matcher = Test::Unit::Diff::SequenceMatcher.new(from, to)
  414. assert_in_delta(expected, 0.001, matcher.ratio)
  415. end
  416. def assert_readable_diff(expected, from, to)
  417. assert_equal(expected,
  418. Test::Unit::Diff.readable(from.join("\n"), to.join("\n")))
  419. end
  420. def assert_unified_diff(expected, from, to, from_label, to_label, options={})
  421. options = options.merge(:from_label => from_label,
  422. :to_label => to_label)
  423. assert_equal(expected,
  424. Test::Unit::Diff.unified(from.join("\n"), to.join("\n"),
  425. options))
  426. end
  427. def assert_diff_lines(expected, from, to,
  428. from_start, from_end,
  429. to_start, to_end)
  430. differ = Test::Unit::Diff::ReadableDiffer.new(from, to)
  431. result = []
  432. differ.instance_variable_set("@result", result)
  433. differ.send(:diff_lines,
  434. from_start, from_end,
  435. to_start, to_end)
  436. assert_equal(expected, result)
  437. end
  438. def assert_diff_line(expected, from_line, to_line)
  439. differ = Test::Unit::Diff::ReadableDiffer.new([""], [""])
  440. result = []
  441. differ.instance_variable_set("@result", result)
  442. differ.send(:diff_line, from_line, to_line)
  443. assert_equal(expected, result)
  444. end
  445. def assert_format_diff_point(expected, from_line, to_line, from_tags, to_tags)
  446. differ = Test::Unit::Diff::ReadableDiffer.new([""], [""])
  447. result = []
  448. differ.instance_variable_set("@result", result)
  449. differ.send(:format_diff_point,
  450. from_line, to_line,
  451. from_tags, to_tags)
  452. assert_equal(expected, result)
  453. end
  454. def assert_interesting_line(expected, from, to, from_start, to_start)
  455. differ = Test::Unit::Diff::UnifiedDiffer.new(from, to)
  456. assert_equal(expected, differ.send(:find_interesting_line,
  457. from_start, to_start,
  458. :define_line?))
  459. end
  460. end