/spec/diff_matcher/difference_spec.rb

https://github.com/playup/diff_matcher · Ruby · 792 lines · 711 code · 80 blank · 1 comment · 5 complexity · 719bad2b5ee4d58cb695170d432cc6af MD5 · raw file

  1. require "spec_helper"
  2. def opts_to_s(opts)
  3. opts_strs = opts.map { |k,v| ":#{k}=>#{v}" if v }.compact
  4. opts_strs.size > 0 ? ", " + opts_strs * ", " : ""
  5. end
  6. def fix_EOF_problem(s)
  7. # <<-EOF isn't working like its meant to :(
  8. whitespace = s.split("\n")[-1][/^[ ]+/]
  9. indentation = whitespace ? whitespace.size : 0
  10. s.gsub("\n#{" " * indentation}", "\n").tap { |result|
  11. result.strip! if whitespace
  12. }
  13. end
  14. shared_examples_for "an or-ed matcher" do |expected, expected2, same, different, difference, opts|
  15. opts ||= {}
  16. context "where expected=#{expected.inspect}, expected2=#{expected2.inspect}" do
  17. describe "diff(#{same.inspect}#{opts_to_s(opts)})" do
  18. let(:expected ) { expected }
  19. let(:expected2) { expected }
  20. let(:actual ) { same }
  21. let(:opts ) { opts }
  22. it { should be_nil }
  23. end
  24. describe "diff(#{different.inspect}#{opts_to_s(opts)})" do
  25. let(:expected ) { expected }
  26. let(:expected2) { expected }
  27. let(:actual ) { different }
  28. let(:opts ) { opts }
  29. it { should_not be_nil } unless RUBY_1_9
  30. it { should == fix_EOF_problem(difference) } if RUBY_1_9
  31. end
  32. end
  33. end
  34. describe DiffMatcher::Matcher do
  35. expected, expected2, same, different, difference =
  36. {:nombre => String , :edad => Integer },
  37. {:name => String , :age => Integer },
  38. {:name => "Peter" , :age => 21 },
  39. {:name => 21 , :age => 21 },
  40. "{\n :name=>\e[31m- \e[1mString\e[0m\e[33m+ \e[1m21\e[0m,\n :age=>\e[34m: \e[1m21\e[0m\n}\n"
  41. describe "DiffMatcher::Matcher[expected, expected2]," do
  42. subject { DiffMatcher::Matcher[expected, expected2].diff(actual) }
  43. it_behaves_like "an or-ed matcher", expected, expected2, same, different, difference
  44. context "when Matchers are or-ed it works the same" do
  45. subject { (DiffMatcher::Matcher[expected] | DiffMatcher::Matcher[expected2]).diff(actual) }
  46. it_behaves_like "an or-ed matcher", expected, expected2, same, different, difference
  47. end
  48. context "expecteds are in different order it still uses the closest dif" do
  49. subject { DiffMatcher::Matcher[expected2, expected].diff(actual) }
  50. it_behaves_like "an or-ed matcher", expected2, expected, same, different, difference
  51. end
  52. end
  53. end
  54. describe "DiffMatcher::AllMatcher[expected]" do
  55. let(:all_matcher) { DiffMatcher::AllMatcher[expected] }
  56. let(:expected ) { 1 }
  57. describe "#diff(actual)" do
  58. subject { all_matcher.diff(actual) }
  59. context "when all match" do
  60. let(:actual) { [1, 1, 1] }
  61. it { should eql nil }
  62. end
  63. context "when not all match" do
  64. let(:actual) { [1, 2, 1] }
  65. it { should eql "[\n 1,\n \e[31m- \e[1m1\e[0m\e[33m+ \e[1m2\e[0m,\n 1\n]\n" }
  66. end
  67. context "when actual is not an array" do
  68. let(:actual) { 'a' }
  69. it { should eql "\e[31m- \e[1m[...]\e[0m\e[33m+ \e[1m\"a\"\e[0m" }
  70. end
  71. end
  72. end
  73. shared_examples_for "a diff matcher" do |expected, same, different, difference, opts|
  74. opts ||= {}
  75. context "with #{opts.size > 0 ? opts_to_s(opts) : "no opts"}" do
  76. describe "difference(#{expected.inspect}, #{same.inspect}#{opts_to_s(opts)})" do
  77. let(:expected) { expected }
  78. let(:actual ) { same }
  79. let(:opts ) { opts }
  80. it { should be_nil }
  81. end
  82. describe "difference(#{expected.inspect}, #{different.inspect}#{opts_to_s(opts)})" do
  83. let(:expected) { expected }
  84. let(:actual ) { different }
  85. let(:opts ) { opts }
  86. it { should_not be_nil } unless RUBY_1_9
  87. it {
  88. difference.is_a?(Regexp) ?
  89. should =~ difference :
  90. should == fix_EOF_problem(difference)
  91. } if RUBY_1_9
  92. end
  93. end
  94. end
  95. describe "DiffMatcher::Matcher[expected].diff(actual, opts)" do
  96. subject { DiffMatcher::Matcher[expected].diff(actual, opts) }
  97. describe "when expected is an instance," do
  98. context "of Fixnum," do
  99. expected, same, different =
  100. 1,
  101. 1,
  102. 2
  103. it_behaves_like "a diff matcher", expected, same, different,
  104. "\e[31m- \e[1m1\e[0m\e[33m+ \e[1m2\e[0m"
  105. end
  106. end
  107. describe "when expected is an instance," do
  108. context "of Hash, with optional keys" do
  109. expected, same, different =
  110. {:a=>1, :b=>Fixnum},
  111. {:a=>1},
  112. {:a=>2}
  113. it_behaves_like "a diff matcher", expected, same, different,
  114. "{\n :a=>\e[31m- \e[1m1\e[0m\e[33m+ \e[1m2\e[0m\n}\n",
  115. {:optional_keys=>[:b]}
  116. end
  117. end
  118. end
  119. describe "DiffMatcher::difference(expected, actual, opts)" do
  120. subject { DiffMatcher::difference(expected, actual, opts) }
  121. describe "when expected is an instance," do
  122. context "of Fixnum," do
  123. expected, same, different =
  124. 1,
  125. 1,
  126. 2
  127. it_behaves_like "a diff matcher", expected, same, different,
  128. <<-EOF
  129. - 1+ 2
  130. Where, - 1 missing, + 1 additional
  131. EOF
  132. end
  133. context "of String," do
  134. expected, same, different =
  135. "a",
  136. "a",
  137. "b"
  138. it_behaves_like "a diff matcher", expected, same, different,
  139. <<-EOF
  140. - "a"+ "b"
  141. Where, - 1 missing, + 1 additional
  142. EOF
  143. context "when actual is of a different class" do
  144. different = 0
  145. it_behaves_like "a diff matcher", expected, same, different,
  146. <<-EOF
  147. - "a"+ 0
  148. Where, - 1 missing, + 1 additional
  149. EOF
  150. end
  151. context "when actual is nil" do
  152. different = nil
  153. it_behaves_like "a diff matcher", expected, same, different,
  154. <<-EOF
  155. - "a"+ nil
  156. Where, - 1 missing, + 1 additional
  157. EOF
  158. end
  159. end
  160. context "of nil," do
  161. expected, same, different =
  162. nil,
  163. nil,
  164. false
  165. it_behaves_like "a diff matcher", expected, same, different,
  166. <<-EOF
  167. - nil+ false
  168. Where, - 1 missing, + 1 additional
  169. EOF
  170. end
  171. context "of Array," do
  172. expected, same, different =
  173. [ 1 ],
  174. [ 1 ],
  175. [ 2 ]
  176. it_behaves_like "a diff matcher", expected, same, different,
  177. <<-EOF
  178. [
  179. - 1+ 2
  180. ]
  181. Where, - 1 missing, + 1 additional
  182. EOF
  183. context "where actual has additional items" do
  184. expected, same, different =
  185. [ 1, 2 ],
  186. [ 1, 2, 3 ],
  187. [ 0, 2, 3 ]
  188. it_behaves_like "a diff matcher", expected, same, different,
  189. <<-EOF, :ignore_additional=>true
  190. [
  191. - 1+ 0,
  192. 2,
  193. + 3
  194. ]
  195. Where, - 1 missing, + 2 additional
  196. EOF
  197. it_behaves_like "a diff matcher", expected, same, different,
  198. <<-EOF, :ignore_additional=>true, :quiet=>true
  199. [
  200. - 1+ 0
  201. ]
  202. Where, - 1 missing, + 1 additional
  203. EOF
  204. context "where actual has additional items, it summarizes the Array item with ... and" do
  205. expected, same, different =
  206. [ 1, 2 ],
  207. [ 1, 2, [ 3 ] ],
  208. [ 0, 2, [ 3 ] ]
  209. it_behaves_like "a diff matcher", expected, same, different,
  210. <<-EOF, :ignore_additional=>true
  211. [
  212. - 1+ 0,
  213. 2,
  214. + [...]
  215. ]
  216. Where, - 1 missing, + 2 additional
  217. EOF
  218. end
  219. context "where actual has additional items, it summarizes the Hash item with ... and" do
  220. expected, same, different =
  221. [ 1, 2 ],
  222. [ 1, 2, { :a=> [ 3 ] } ],
  223. [ 0, 2, { :a=> [ 3 ] } ]
  224. it_behaves_like "a diff matcher", expected, same, different,
  225. <<-EOF, :ignore_additional=>true
  226. [
  227. - 1+ 0,
  228. 2,
  229. + {...}
  230. ]
  231. Where, - 1 missing, + 2 additional
  232. EOF
  233. end
  234. end
  235. context "where actual has missing items" do
  236. expected, same, different =
  237. [ 1, 2, 3 ],
  238. [ 1, 2, 3 ],
  239. [ 1, 2 ]
  240. it_behaves_like "a diff matcher", expected, same, different,
  241. <<-EOF, { :quiet => true }
  242. [
  243. - 3
  244. ]
  245. Where, - 1 missing
  246. EOF
  247. it_behaves_like "a diff matcher", expected, same, different,
  248. <<-EOF
  249. [
  250. 1,
  251. 2,
  252. - 3
  253. ]
  254. Where, - 1 missing
  255. EOF
  256. end
  257. context "where actual is not an array, it summarizes the diff with ... and" do
  258. expected, same, different =
  259. [ 1 ],
  260. [ 1 ],
  261. { 0 => 1 }
  262. it_behaves_like "a diff matcher", expected, same, different,
  263. <<-EOF
  264. - [...]+ {...}
  265. Where, - 1 missing, + 1 additional
  266. EOF
  267. end
  268. end
  269. context "of an Array derived class," do
  270. class ArrayChild < Array; end
  271. expected, same, different =
  272. ArrayChild[ 1 ],
  273. ArrayChild[ 1 ],
  274. ArrayChild[ 2 ]
  275. it_behaves_like "a diff matcher", expected, same, different,
  276. <<-EOF
  277. [
  278. - 1+ 2
  279. ]
  280. Where, - 1 missing, + 1 additional
  281. EOF
  282. end
  283. context "of Range," do
  284. expected, same, different =
  285. (1..3),
  286. 2,
  287. 4
  288. it_behaves_like "a diff matcher", expected, same, different,
  289. <<-EOF
  290. - 1..3+ 4
  291. Where, - 1 missing, + 1 additional
  292. EOF
  293. end
  294. context "of Hash," do
  295. expected, same, different =
  296. { "a"=>1 },
  297. { "a"=>1 },
  298. { "a"=>2 }
  299. it_behaves_like "a diff matcher", expected, same, different,
  300. <<-EOF
  301. {
  302. "a"=>- 1+ 2
  303. }
  304. Where, - 1 missing, + 1 additional
  305. EOF
  306. context "with values of differing classes" do
  307. expected, same, different =
  308. { "a"=>{ "b"=>1 } },
  309. { "a"=>{ "b"=>1 } },
  310. { "a"=>[ "b", 1 ] }
  311. it_behaves_like "a diff matcher", expected, same, different,
  312. <<-EOF
  313. {
  314. "a"=>- {...}+ [...]
  315. }
  316. Where, - 1 missing, + 1 additional
  317. EOF
  318. end
  319. context "with matching hash descendents" do
  320. expected, same, different =
  321. { "a"=>{ "b"=>{ "c"=>1 } } },
  322. { "a"=>{ "b"=>{ "c"=>1 } } },
  323. { "b"=>{ "c"=>1 } }
  324. describe "it won't match the descendents" do
  325. it_behaves_like "a diff matcher", expected, same, different,
  326. <<-EOF
  327. {
  328. - "a"=>{"b"=>{"c"=>1}},
  329. + "b"=>{"c"=>1}
  330. }
  331. Where, - 1 missing, + 1 additional
  332. EOF
  333. end
  334. end
  335. end
  336. context "of a Hash derived class," do
  337. class HashChild < Hash; end
  338. expected, same, different =
  339. HashChild["a",1],
  340. HashChild["a",1],
  341. HashChild["a",2]
  342. it_behaves_like "a diff matcher", expected, same, different,
  343. <<-EOF
  344. {
  345. "a"=>- 1+ 2
  346. }
  347. Where, - 1 missing, + 1 additional
  348. EOF
  349. end
  350. end
  351. describe "when expected is," do
  352. context "a class," do
  353. expected, same, different =
  354. String,
  355. "a",
  356. 1
  357. it_behaves_like "a diff matcher", expected, same, different,
  358. <<-EOF
  359. - String+ 1
  360. Where, - 1 missing, + 1 additional
  361. EOF
  362. end
  363. context "a Regex," do
  364. expected, same, different =
  365. /[a-z]/,
  366. "a",
  367. "A"
  368. it_behaves_like "a diff matcher", expected, same, different,
  369. <<-EOF
  370. - /[a-z]/+ "A"
  371. Where, - 1 missing, + 1 additional
  372. EOF
  373. context "and when actual is not a String," do
  374. different = :a
  375. it_behaves_like "a diff matcher", expected, same, different,
  376. <<-EOF
  377. - /[a-z]/+ :a
  378. Where, - 1 missing, + 1 additional
  379. EOF
  380. end
  381. end
  382. context "a proc," do
  383. expected, same, different =
  384. lambda { |x| [FalseClass, TrueClass].include? x.class },
  385. true,
  386. "true"
  387. it_behaves_like "a diff matcher", expected, same, different,
  388. /- #<Proc.*?>\+ \"true\"\nWhere, - 1 missing, \+ 1 additional/
  389. context "that defines another diff matcher" do
  390. expected, same, different =
  391. lambda { |array| array.all? { |item| DiffMatcher::Difference.new(String, item).matching? } },
  392. ["A", "B", "C"],
  393. ["A", "B", 0 ]
  394. it_behaves_like "a diff matcher", expected, same, different,
  395. /- #<Proc.*?>\+ \[\"A\", \"B\", 0\]\nWhere, - 1 missing, \+ 1 additional/
  396. end
  397. end
  398. context "a DiffMatcher::Matcher," do
  399. expected, same, different =
  400. DiffMatcher::Matcher[String],
  401. "a",
  402. 1
  403. it_behaves_like "a diff matcher", expected, same, different,
  404. <<-EOF
  405. - String+ 1
  406. Where, - 1 missing, + 1 additional
  407. EOF
  408. context "or-ed with another DiffMatcher::Matcher," do
  409. expected, same, different =
  410. DiffMatcher::Matcher[Fixnum] | DiffMatcher::Matcher[String],
  411. "a",
  412. 1.0
  413. it_behaves_like "a diff matcher", expected, same, different,
  414. <<-EOF
  415. - String+ 1.0
  416. Where, - 1 missing, + 1 additional
  417. EOF
  418. end
  419. end
  420. context "a DiffMatcher::AllMatcher," do
  421. expected, same, different =
  422. DiffMatcher::AllMatcher[String],
  423. %w(ay be ci),
  424. ["a", 2, "c"]
  425. it_behaves_like "a diff matcher", expected, same, different,
  426. <<-EOF
  427. [
  428. : "a",
  429. - String+ 2,
  430. : "c"
  431. ]
  432. Where, - 1 missing, + 1 additional, : 2 match_class
  433. EOF
  434. end
  435. context "with a min restriction" do
  436. expected, same, different =
  437. DiffMatcher::AllMatcher.new(String, :min=>3),
  438. %w(ay be ci),
  439. %w(ay be)
  440. it_behaves_like "a diff matcher", expected, same, different,
  441. <<-EOF
  442. [
  443. : "ay",
  444. : "be",
  445. - String
  446. ]
  447. Where, - 1 missing, : 2 match_class
  448. EOF
  449. end
  450. context "with a max restriction" do
  451. expected, same, different =
  452. DiffMatcher::AllMatcher.new(String, :max=>2),
  453. %w(ay be),
  454. %w(ay be ci)
  455. it_behaves_like "a diff matcher", expected, same, different,
  456. <<-EOF
  457. [
  458. : "ay",
  459. : "be",
  460. + "ci"
  461. ]
  462. Where, + 1 additional, : 2 match_class
  463. EOF
  464. end
  465. context "with a size restriction" do
  466. expected, same, different =
  467. DiffMatcher::AllMatcher.new(String, :size=>2),
  468. %w(ay be),
  469. %w(ay be ci)
  470. it_behaves_like "a diff matcher", expected, same, different,
  471. <<-EOF
  472. [
  473. : "ay",
  474. : "be",
  475. + "ci"
  476. ]
  477. Where, + 1 additional, : 2 match_class
  478. EOF
  479. end
  480. context "with a size restriction range" do
  481. expected, same, different =
  482. DiffMatcher::AllMatcher.new(String, :size=>0..2),
  483. %w(ay be),
  484. %w(ay be ci)
  485. it_behaves_like "a diff matcher", expected, same, different,
  486. <<-EOF
  487. [
  488. : "ay",
  489. : "be",
  490. + "ci"
  491. ]
  492. Where, + 1 additional, : 2 match_class
  493. EOF
  494. end
  495. end
  496. context "a DiffMatcher::AllMatcher using an or-ed DiffMatcher::Matcher," do
  497. expected, same, different =
  498. DiffMatcher::AllMatcher[ DiffMatcher::Matcher[Fixnum, Float] ],
  499. [1, 2.0, 3],
  500. [1, "2", 3]
  501. it_behaves_like "a diff matcher", expected, same, different,
  502. <<-EOF
  503. [
  504. | 1,
  505. - Float+ "2",
  506. | 3
  507. ]
  508. Where, - 1 missing, + 1 additional, | 2 match_matcher
  509. EOF
  510. context "more complex," do
  511. expected, same, different =
  512. DiffMatcher::AllMatcher[
  513. DiffMatcher::Matcher[
  514. {:nombre=>String, :edad=>Fixnum},
  515. {:name=>String, :age=>Fixnum}
  516. ]
  517. ],
  518. [
  519. {:name=>"Alice", :age=>10},
  520. {:name=>"Bob" , :age=>20},
  521. {:name=>"Con" , :age=>30}
  522. ],
  523. [
  524. {:name=>"Alice", :age=>10 },
  525. {:name=>"Bob" , :age=>nil},
  526. {:nombre=>"Con", :edad=>30}
  527. ]
  528. it_behaves_like "a diff matcher", expected, same, different,
  529. <<-EOF
  530. [
  531. | {:name=>"Alice", :age=>10},
  532. {
  533. :name=>: "Bob",
  534. :age=>- Fixnum+ nil
  535. },
  536. | {:nombre=>"Con", :edad=>30}
  537. ]
  538. Where, - 1 missing, + 1 additional, : 1 match_class, | 2 match_matcher
  539. EOF
  540. end
  541. end
  542. context "when expected has multiple items," do
  543. expected, same, different =
  544. [ 1, 2, /\d/, Fixnum, 4..6 , lambda { |x| x % 6 == 0 } ],
  545. [ 1, 2, "3" , 4 , 5 , 6 ],
  546. [ 0, 2, "3" , 4 , 5 , 6 ]
  547. describe "it shows regex, class, range, proc matches and matches" do
  548. it_behaves_like "a diff matcher", expected, same, different,
  549. <<-EOF
  550. [
  551. - 1+ 0,
  552. 2,
  553. ~ "(3)",
  554. : 4,
  555. . 5,
  556. { 6
  557. ]
  558. Where, - 1 missing, + 1 additional, ~ 1 match_regexp, : 1 match_class, . 1 match_range, { 1 match_proc
  559. EOF
  560. end
  561. describe "it doesn't show matches" do
  562. it_behaves_like "a diff matcher", expected, same, different,
  563. <<-EOF, :quiet=>true
  564. [
  565. - 1+ 0
  566. ]
  567. Where, - 1 missing, + 1 additional
  568. EOF
  569. end
  570. describe "it shows all matches" do
  571. it_behaves_like "a diff matcher", expected, same, different,
  572. <<-EOF
  573. [
  574. - 1+ 0,
  575. 2,
  576. ~ "(3)",
  577. : 4,
  578. . 5,
  579. { 6
  580. ]
  581. Where, - 1 missing, + 1 additional, ~ 1 match_regexp, : 1 match_class, . 1 match_range, { 1 match_proc
  582. EOF
  583. end
  584. describe "it shows matches in color" do
  585. it_behaves_like "a diff matcher", expected, same, different,
  586. <<-EOF , :color_scheme=>:default
  587. \e[0m[
  588. \e[0m \e[31m- \e[1m1\e[0m\e[33m+ \e[1m0\e[0m,
  589. \e[0m 2,
  590. \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
  591. \e[0m \e[34m: \e[1m4\e[0m,
  592. \e[0m \e[36m. \e[1m5\e[0m,
  593. \e[0m \e[36m{ \e[1m6\e[0m
  594. \e[0m]
  595. Where, \e[31m- \e[1m1 missing\e[0m, \e[33m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
  596. EOF
  597. context "on a white background" do
  598. it_behaves_like "a diff matcher", expected, same, different,
  599. <<-EOF , :color_scheme=>:white_background
  600. \e[0m[
  601. \e[0m \e[31m- \e[1m1\e[0m\e[35m+ \e[1m0\e[0m,
  602. \e[0m 2,
  603. \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
  604. \e[0m \e[34m: \e[1m4\e[0m,
  605. \e[0m \e[36m. \e[1m5\e[0m,
  606. \e[0m \e[36m{ \e[1m6\e[0m
  607. \e[0m]
  608. Where, \e[31m- \e[1m1 missing\e[0m, \e[35m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
  609. EOF
  610. end
  611. context "color_enabled can be set on the class" do
  612. before { DiffMatcher::Difference::color_enabled = true }
  613. it_behaves_like "a diff matcher", expected, same, different,
  614. <<-EOF
  615. \e[0m[
  616. \e[0m \e[31m- \e[1m1\e[0m\e[33m+ \e[1m0\e[0m,
  617. \e[0m 2,
  618. \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
  619. \e[0m \e[34m: \e[1m4\e[0m,
  620. \e[0m \e[36m. \e[1m5\e[0m,
  621. \e[0m \e[36m{ \e[1m6\e[0m
  622. \e[0m]
  623. Where, \e[31m- \e[1m1 missing\e[0m, \e[33m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
  624. EOF
  625. end
  626. context "color_scheme can be set on the class" do
  627. before { DiffMatcher::Difference::color_scheme = :white_background }
  628. it_behaves_like "a diff matcher", expected, same, different,
  629. <<-EOF
  630. \e[0m[
  631. \e[0m \e[31m- \e[1m1\e[0m\e[35m+ \e[1m0\e[0m,
  632. \e[0m 2,
  633. \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
  634. \e[0m \e[34m: \e[1m4\e[0m,
  635. \e[0m \e[36m. \e[1m5\e[0m,
  636. \e[0m \e[36m{ \e[1m6\e[0m
  637. \e[0m]
  638. Where, \e[31m- \e[1m1 missing\e[0m, \e[35m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
  639. EOF
  640. end
  641. end
  642. describe "it shows matches in color when color is configured in Difference" do
  643. before do
  644. DiffMatcher::Difference.configure do |config|
  645. config.color_enabled = true
  646. end
  647. end
  648. it_behaves_like "a diff matcher", expected, same, different,
  649. <<-EOF
  650. \e[0m[
  651. \e[0m \e[31m- \e[1m1\e[0m\e[35m+ \e[1m0\e[0m,
  652. \e[0m 2,
  653. \e[0m \e[32m~ \e[0m"\e[32m(\e[1m3\e[0m\e[32m)\e[0m"\e[0m,
  654. \e[0m \e[34m: \e[1m4\e[0m,
  655. \e[0m \e[36m. \e[1m5\e[0m,
  656. \e[0m \e[36m{ \e[1m6\e[0m
  657. \e[0m]
  658. Where, \e[31m- \e[1m1 missing\e[0m, \e[35m+ \e[1m1 additional\e[0m, \e[32m~ \e[1m1 match_regexp\e[0m, \e[34m: \e[1m1 match_class\e[0m, \e[36m. \e[1m1 match_range\e[0m, \e[36m{ \e[1m1 match_proc\e[0m
  659. EOF
  660. end
  661. describe "it shows matches in html" do
  662. it_behaves_like "a diff matcher", expected, same, different,
  663. <<-EOF , :color_scheme => :white_background, :html_output=>true
  664. <pre>
  665. [
  666. <span style=\"color:red\">- <b>1</b></span><span style=\"color:magenta\">+ <b>0</b></span>,
  667. 2,
  668. <span style=\"color:green\">~ </b></span>\"<span style=\"color:green\">(<b>3</b></span><span style=\"color:green\">)</b></span>\"</b></span>,
  669. <span style=\"color:blue\">: <b>4</b></span>,
  670. <span style=\"color:cyan\">. <b>5</b></span>,
  671. <span style=\"color:cyan\">{ <b>6</b></span>
  672. ]
  673. Where, <span style=\"color:red\">- <b>1 missing</b></span>, <span style=\"color:magenta\">+ <b>1 additional</b></span>, <span style=\"color:green\">~ <b>1 match_regexp</b></span>, <span style=\"color:blue\">: <b>1 match_class</b></span>, <span style=\"color:cyan\">. <b>1 match_range</b></span>, <span style=\"color:cyan\">{ <b>1 match_proc</b></span>
  674. </pre>
  675. EOF
  676. end
  677. end
  678. end