| 1 | // This file contains some unit tests copied from the `console` project: |
| 2 | // https://github.com/mitsuhiko/console |
| 3 | // |
| 4 | // The MIT License (MIT) |
| 5 | |
| 6 | // Copyright (c) 2017 Armin Ronacher <[email protected]> |
| 7 | |
| 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | // of this software and associated documentation files (the "Software"), to deal |
| 10 | // in the Software without restriction, including without limitation the rights |
| 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | // copies of the Software, and to permit persons to whom the Software is |
| 13 | // furnished to do so, subject to the following conditions: |
| 14 | |
| 15 | // The above copyright notice and this permission notice shall be included in all |
| 16 | // copies or substantial portions of the Software. |
| 17 | |
| 18 | #[cfg(test)] |
| 19 | mod testsrust-analyzer cargo git-delta 0.19.2 ansi/console_tests/tests/ { |
| 20 | use consolerust-analyzer cargo console 0.15.7 crate/::{selfrust-analyzer cargo console 0.15.7 crate/, stylerust-analyzer cargo console 0.15.7 utils/style().}; |
| 21 | |
| 22 | use craterust-analyzer cargo git-delta 0.19.2 crate/::ansirust-analyzer cargo git-delta 0.19.2 ansi/::{measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width()., truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().}; |
| 23 | |
| 24 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 25 | fn test_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/console_tests/tests/test_text_width().() { |
| 26 | let s = stylerust-analyzer cargo console 0.15.7 utils/style().("foo") |
| 27 | .redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().() |
| 28 | .on_blackrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]on_black().() |
| 29 | .boldrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]bold().() |
| 30 | .force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true) |
| 31 | .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(); |
| 32 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(measure_text_widthrust-analyzer cargo git-delta 0.19.2 ansi/measure_text_width().(&s), 3); |
| 33 | } |
| 34 | |
| 35 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 36 | fn test_truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/console_tests/tests/test_truncate_str().() { |
| 37 | let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {}", stylerust-analyzer cargo console 0.15.7 utils/style().("bar").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)); |
| 38 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 39 | &truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(&s, 5, ""), |
| 40 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {}", stylerust-analyzer cargo console 0.15.7 utils/style().("b").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)) |
| 41 | ); |
| 42 | let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {}", stylerust-analyzer cargo console 0.15.7 utils/style().("bar").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)); |
| 43 | // DED: I'm changing this test assertion: delta does not move `!` inside the styled region. |
| 44 | // assert_eq!( |
| 45 | // &truncate_str(&s, 5, "!"), |
| 46 | // &format!("foo {}", style("!").red().force_styling(true)) |
| 47 | // ); |
| 48 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 49 | &truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(&s, 5, "!"), |
| 50 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {}!", stylerust-analyzer cargo console 0.15.7 utils/style().("").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)) |
| 51 | ); |
| 52 | let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {} baz", stylerust-analyzer cargo console 0.15.7 utils/style().("bar").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)); |
| 53 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 54 | &truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(&s, 10, "..."), |
| 55 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {}...", stylerust-analyzer cargo console 0.15.7 utils/style().("bar").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)) |
| 56 | ); |
| 57 | // `バ`(width = 2) will be truncate to 1, we use space to fill |
| 58 | let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {}", stylerust-analyzer cargo console 0.15.7 utils/style().("バー").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)); |
| 59 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 60 | &truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(&s, 5, ""), |
| 61 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {}", stylerust-analyzer cargo console 0.15.7 utils/style().(" ").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)) |
| 62 | ); |
| 63 | let s = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {}", stylerust-analyzer cargo console 0.15.7 utils/style().("バー").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)); |
| 64 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 65 | &truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().(&s, 6, ""), |
| 66 | &formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("foo {}", stylerust-analyzer cargo console 0.15.7 utils/style().("バ").redrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]red().().force_stylingrust-analyzer cargo console 0.15.7 utils/impl#[`StyledObject<D>`]force_styling().(true)) |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 71 | fn test_truncate_str_no_ansirust-analyzer cargo git-delta 0.19.2 ansi/console_tests/tests/test_truncate_str_no_ansi().() { |
| 72 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().("foo bar", 5, ""), "foo b"); |
| 73 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().("foo bar", 5, "!"), "foo !"); |
| 74 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&truncate_strrust-analyzer cargo git-delta 0.19.2 ansi/truncate_str().("foo bar baz", 10, "..."), "foo bar..."); |
| 75 | } |
| 76 | } |