| 1 | // Copyright 2018 The xi-editor Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | extern crate xi_unicode; |
| 15 | |
| 16 | use std::fs::File; |
| 17 | use std::io::BufReader; |
| 18 | use std::io::Read; |
| 19 | |
| 20 | use xi_unicode::LineBreakIterator; |
| 21 | |
| 22 | const TEST_FILE: &'static str = "tests/LineBreakTest.txt"; |
| 23 | |
| 24 | #[test] |
| 25 | fn line_break_test() { |
| 26 | let file = File::open(TEST_FILE).expect("unable to open test file."); |
| 27 | |
| 28 | let mut reader = BufReader::new(file); |
| 29 | let mut buffer = String::new(); |
| 30 | |
| 31 | reader.read_to_string(&mut buffer).expect("failed to read test file."); |
| 32 | |
| 33 | let mut failed_tests = Vec::new(); |
| 34 | |
| 35 | for full_test in buffer.lines().filter(|s| !s.starts_with('#')) { |
| 36 | let test = full_test.split('#').next().unwrap().trim(); |
| 37 | |
| 38 | let (string, breaks) = parse_test(test); |
| 39 | let xi_lb = LineBreakIterator::new(&string).map(|(idx, _)| idx).collect::<Vec<_>>(); |
| 40 | |
| 41 | if xi_lb != breaks { |
| 42 | failed_tests.push((full_test.to_string(), breaks, xi_lb)); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if !failed_tests.is_empty() { |
| 47 | println!("\nFailed {} line break tests.", failed_tests.len()); |
| 48 | |
| 49 | for fail in failed_tests { |
| 50 | println!("Failed Test: {}", fail.0); |
| 51 | println!("Unicode Breaks: {:?}", fail.1); |
| 52 | println!("Xi Breaks: {:?}\n", fail.2); |
| 53 | } |
| 54 | |
| 55 | panic!("failed line break test."); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // A typical test looks like: "× 0023 × 0308 × 0020 ÷ 0023 ÷" |
| 60 | fn parse_test(test: &str) -> (String, Vec<usize>) { |
| 61 | use std::char; |
| 62 | |
| 63 | let mut parts = test.split(' '); |
| 64 | let mut idx = 0usize; |
| 65 | |
| 66 | let mut string = String::new(); |
| 67 | let mut breaks = Vec::new(); |
| 68 | |
| 69 | loop { |
| 70 | let next = parts.next(); |
| 71 | if next.is_none() { |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | if next != Some("×") && next != Some("÷") { |
| 76 | panic!("syntax error"); |
| 77 | } |
| 78 | |
| 79 | if next == Some("÷") { |
| 80 | breaks.push(idx); |
| 81 | } |
| 82 | |
| 83 | if let Some(hex) = parts.next() { |
| 84 | let num = u32::from_str_radix(hex, 16).expect("syntax error"); |
| 85 | let ch = char::from_u32(num).expect("invalid codepoint"); |
| 86 | string.push(ch); |
| 87 | idx += ch.len_utf8(); |
| 88 | } else { |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | (string, breaks) |
| 94 | } |