1 | extern crate difflib; |
2 | |
3 | use difflib::differ::Differ; |
4 | use difflib::sequencematcher::{Match, Opcode, SequenceMatcher}; |
5 | |
6 | #[test] |
7 | fn test_longest_match() { |
8 | let matcher = SequenceMatcher::new(" abcd" , "abcd abcd" ); |
9 | let m = matcher.find_longest_match(0, 5, 0, 9); |
10 | assert_eq!(m.first_start, 0); |
11 | assert_eq!(m.second_start, 4); |
12 | assert_eq!(m.size, 5); |
13 | } |
14 | |
15 | #[test] |
16 | fn test_all_matches() { |
17 | let mut matcher = SequenceMatcher::new("abxcd" , "abcd" ); |
18 | let result = matcher.get_matching_blocks(); |
19 | let mut expected_result = Vec::new(); |
20 | expected_result.push(Match { |
21 | first_start: 0, |
22 | second_start: 0, |
23 | size: 2, |
24 | }); |
25 | expected_result.push(Match { |
26 | first_start: 3, |
27 | second_start: 2, |
28 | size: 2, |
29 | }); |
30 | expected_result.push(Match { |
31 | first_start: 5, |
32 | second_start: 4, |
33 | size: 0, |
34 | }); |
35 | assert_eq!(result, expected_result); |
36 | } |
37 | |
38 | #[test] |
39 | fn test_get_opcodes() { |
40 | let mut matcher = SequenceMatcher::new("qabxcd" , "abycdf" ); |
41 | let result = matcher.get_opcodes(); |
42 | let mut expected_result = Vec::new(); |
43 | expected_result.push(Opcode { |
44 | tag: "delete" .to_string(), |
45 | first_start: 0, |
46 | first_end: 1, |
47 | second_start: 0, |
48 | second_end: 0, |
49 | }); |
50 | expected_result.push(Opcode { |
51 | tag: "equal" .to_string(), |
52 | first_start: 1, |
53 | first_end: 3, |
54 | second_start: 0, |
55 | second_end: 2, |
56 | }); |
57 | expected_result.push(Opcode { |
58 | tag: "replace" .to_string(), |
59 | first_start: 3, |
60 | first_end: 4, |
61 | second_start: 2, |
62 | second_end: 3, |
63 | }); |
64 | expected_result.push(Opcode { |
65 | tag: "equal" .to_string(), |
66 | first_start: 4, |
67 | first_end: 6, |
68 | second_start: 3, |
69 | second_end: 5, |
70 | }); |
71 | expected_result.push(Opcode { |
72 | tag: "insert" .to_string(), |
73 | first_start: 6, |
74 | first_end: 6, |
75 | second_start: 5, |
76 | second_end: 6, |
77 | }); |
78 | assert_eq!(result, expected_result); |
79 | } |
80 | |
81 | #[test] |
82 | fn test_ratio() { |
83 | let mut matcher = SequenceMatcher::new("abcd" , "bcde" ); |
84 | assert_eq!(matcher.ratio(), 0.75); |
85 | } |
86 | |
87 | #[test] |
88 | fn test_get_close_matches() { |
89 | let words = vec!["ape" , "apple" , "peach" , "puppy" ]; |
90 | let result = difflib::get_close_matches("appel" , words, 3, 0.6); |
91 | assert_eq!(result, vec!["apple" , "ape" ]); |
92 | } |
93 | |
94 | #[test] |
95 | fn test_differ_compare() { |
96 | let first_text = vec!["one \n" , "two \n" , "three \n" ]; |
97 | let second_text = vec!["ore \n" , "tree \n" , "emu \n" ]; |
98 | let differ = Differ::new(); |
99 | let result = differ.compare(&first_text, &second_text).join("" ); |
100 | assert_eq!( |
101 | result, |
102 | "- one \n? ^ \n+ ore \n? ^ \n- two \n- three \n? - \n+ tree \n+ emu \n" |
103 | ); |
104 | } |
105 | |
106 | fn is_junk_char(ch: &char) -> bool { |
107 | if *ch == ' ' || *ch == ' \t' { |
108 | return true; |
109 | } |
110 | false |
111 | } |
112 | |
113 | #[test] |
114 | fn test_differ_compare_with_func() { |
115 | let first_text = vec!["one \n" , "two \n" , "three \n" ]; |
116 | let second_text = vec!["ore \n" , "tree \n" , "emu \n" ]; |
117 | let mut differ = Differ::new(); |
118 | differ.char_junk = Some(is_junk_char); |
119 | let result = differ.compare(&first_text, &second_text).join("" ); |
120 | assert_eq!( |
121 | result, |
122 | "- one \n? ^ \n+ ore \n? ^ \n- two \n- three \n? - \n+ tree \n+ emu \n" |
123 | ); |
124 | } |
125 | |
126 | #[test] |
127 | fn test_differ_restore() { |
128 | let first_text = vec!["one \n" , " two \n" , "three \n" ]; |
129 | let second_text = vec!["ore \n" , "tree \n" , "emu \n" ]; |
130 | let differ = Differ::new(); |
131 | let diff = differ.compare(&first_text, &second_text); |
132 | assert_eq!(first_text, Differ::restore(&diff, 1)); |
133 | assert_eq!(second_text, Differ::restore(&diff, 2)); |
134 | } |
135 | |
136 | #[test] |
137 | fn test_unified_diff() { |
138 | let first_text = "one two three four" .split(" " ).collect::<Vec<&str>>(); |
139 | let second_text = "zero one tree four" .split(" " ).collect::<Vec<&str>>(); |
140 | let result = difflib::unified_diff( |
141 | &first_text, |
142 | &second_text, |
143 | "Original" , |
144 | "Current" , |
145 | "2005-01-26 23:30:50" , |
146 | "2010-04-02 10:20:52" , |
147 | 3, |
148 | ).join("" ); |
149 | assert_eq!( |
150 | result, |
151 | "--- Original \t2005-01-26 23:30:50 \n+++ Current \t2010-04-02 10:20:52 \n@@ -1,4 \ |
152 | +1,4 @@ \n+zero one-two-three+tree four" |
153 | ); |
154 | } |
155 | |
156 | #[test] |
157 | fn test_context_diff() { |
158 | let first_text = "one two three four" .split(" " ).collect::<Vec<&str>>(); |
159 | let second_text = "zero one tree four" .split(" " ).collect::<Vec<&str>>(); |
160 | let result = difflib::context_diff( |
161 | &first_text, |
162 | &second_text, |
163 | "Original" , |
164 | "Current" , |
165 | "2005-01-26 23:30:50" , |
166 | "2010-04-02 10:20:52" , |
167 | 3, |
168 | ).join("" ); |
169 | assert_eq!( |
170 | result, |
171 | "*** Original \t2005-01-26 23:30:50 \n--- Current \t2010-04-02 \ |
172 | 10:20:52 \n*************** \n*** 1,4 **** \n one! two! three four--- 1,4 ---- \n+ \ |
173 | zero one! tree four" |
174 | ); |
175 | } |
176 | |
177 | #[test] |
178 | fn test_integer_slice() { |
179 | let s1 = vec![1, 2, 3, 4, 5]; |
180 | let s2 = vec![5, 4, 3, 2, 1]; |
181 | let result = SequenceMatcher::new(&s1, &s2).get_matching_blocks(); |
182 | let mut expected_result = Vec::new(); |
183 | expected_result.push(Match { |
184 | first_start: 0, |
185 | second_start: 4, |
186 | size: 1, |
187 | }); |
188 | expected_result.push(Match { |
189 | first_start: 5, |
190 | second_start: 5, |
191 | size: 0, |
192 | }); |
193 | assert_eq!(result, expected_result); |
194 | } |
195 | |