| 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_unicoderust-analyzer cargo xi-unicode 0.3.0 xi_unicode/; |
| 15 | |
| 16 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#; |
| 17 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#; |
| 18 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#; |
| 19 | |
| 20 | use xi_unicoderust-analyzer cargo xi-unicode 0.3.0 crate/::LineBreakIteratorrust-analyzer cargo xi-unicode 0.3.0 LineBreakIterator#; |
| 21 | |
| 22 | const TEST_FILErust-analyzer cargo xi-unicode 0.3.0 TEST_FILE.: &'static str = "tests/LineBreakTest.txt"; |
| 23 | |
| 24 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 25 | fn line_break_testrust-analyzer cargo xi-unicode 0.3.0 line_break_test().() { |
| 26 | let file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(TEST_FILErust-analyzer cargo xi-unicode 0.3.0 TEST_FILE.).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("unable to open test file."); |
| 27 | |
| 28 | let mut reader = BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(file); |
| 29 | let mut buffer = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 30 | |
| 31 | reader.read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`][Read]read_to_string().(&mut buffer).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to read test file."); |
| 32 | |
| 33 | let mut failed_tests = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 34 | |
| 35 | for full_test in buffer.linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().().filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|s| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().s.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().('#')) { |
| 36 | let test = full_test.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('#').nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().trimrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim().(); |
| 37 | |
| 38 | let (string, breaks) = parse_testrust-analyzer cargo xi-unicode 0.3.0 parse_test().(test); |
| 39 | let xi_lb = LineBreakIteratorrust-analyzer cargo xi-unicode 0.3.0 LineBreakIterator#::newrust-analyzer cargo xi-unicode 0.3.0 impl#[`LineBreakIterator<'a>`]new().(&string).maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(idx, _)| idx).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>(); |
| 40 | |
| 41 | if xi_lb rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/partial_eq/impl#[`Vec<T, A1>`][`PartialEq<Vec<U, A2>>`]ne().!=rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/partial_eq/impl#[`Vec<T, A1>`][`PartialEq<Vec<U, A2>>`]ne(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/partial_eq/impl#[`Vec<T, A1>`][`PartialEq<Vec<U, A2>>`]ne().breaks { |
| 42 | failed_tests.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().((full_test.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), breaks, xi_lb)); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().failed_tests.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 47 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("\nFailed {} line break tests.", failed_tests.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 48 | |
| 49 | for fail in failed_tests { |
| 50 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Failed Test: {}", fail.0); |
| 51 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Unicode Breaks: {:?}", fail.1); |
| 52 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Xi Breaks: {:?}\n", fail.2); |
| 53 | } |
| 54 | |
| 55 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("failed line break test."); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // A typical test looks like: "× 0023 × 0308 × 0020 ÷ 0023 ÷" |
| 60 | fn parse_testrust-analyzer cargo xi-unicode 0.3.0 parse_test().(test: &str) -> (Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<usize>) { |
| 61 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/; |
| 62 | |
| 63 | let mut parts = test.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(' '); |
| 64 | let mut idx = 0usize; |
| 65 | |
| 66 | let mut string = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 67 | let mut breaks = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 68 | |
| 69 | loop { |
| 70 | let next = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().(); |
| 71 | if next.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() { |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | if next rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("×") && next rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("÷") { |
| 76 | panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("syntax error"); |
| 77 | } |
| 78 | |
| 79 | if next rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("÷") { |
| 80 | breaks.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(idx); |
| 81 | } |
| 82 | |
| 83 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(hex) = parts.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Split<'a, P>`][Iterator]next().() { |
| 84 | let num = u32::from_str_radixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u32]from_str_radix().(hex, 16).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("syntax error"); |
| 85 | let ch = charrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/::from_u32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/from_u32().(num).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("invalid codepoint"); |
| 86 | string.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push().(ch); |
| 87 | idx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().ch.len_utf8rust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]len_utf8().(); |
| 88 | } else { |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | (string, breaks) |
| 94 | } |