Skip to content

Commit

Permalink
fix cargo test --example select_test
Browse files Browse the repository at this point in the history
... to work with unicode-width 0.1.12 as well.

before this, because it was expecting only 0.1.13,
it was failing with the following output:

     Running unittests examples/select_test.rs (target/x86_64-unknown-linux-gnu/debug/examples/select_test-7b09a2b35e8eb3ba)

running 4 tests
captured piece:
x01234567890123456789012345678901234567890123456789012345678901234567890123456789x
0                                                                                |
1                                                                                |
2                           ┌┤ Where are you from? ├┐                            |
3                           │      Abidjan       ▒  │                            |
4                           │     Abu Dhabi      |  │                            |
5                           │       Abuja        |  │                            |
6                           │       Accra        |  │                            |
7                           │     Adamstown      |  │                            |
8                           │    Addis Ababa     |  │                            |
9                           │      Algiers       |  │                            |
0                           │       Alofi        |  │                            |
1                           │       Amman        |  │                            |
2                           │     Amsterdam      |  │                            |
3                           └───────────────────────┘                            |
4                                                                                |
5                                                                                |
x--------------------------------------------------------------------------------x

captured piece:
x01234567890123456789012345678901234567890123456789012345678901234567890123456789x
0................................................................................|
1................................................................................|
2................................................................................|
3.............................  short nul 1str  .|...............................|
4.................................  1thru8 ......................................|
5...............................tabandnewline....................................|
6..................................bthru15.......................................|
7................................. 16thru1F......................................|
8................................   7Fonly  .....................................|
9..................................80thru89......................................|
0..................................8.thru93......................................|
1..................................94thru9D......................................|
2.............................     9Ethru9F     .▒...............................|
3................................................................................|
4................................................................................|
5................................................................................|
x--------------------------------------------------------------------------------x

captured piece:
x01234567890123456789012345678901234567890123456789012345678901234567890123456789x
0................................................................................|
1................................................................................|
2................................................................................|
3.............................  short nul 1str  .|...............................|
4.................................  1thru8 ......................................|
5...............................tabandnewline....................................|
6..................................bthru15.......................................|
7................................. 16thru1F......................................|
8................................   7Fonly  .....................................|
9..................................80thru89......................................|
0..................................8.thru93......................................|
1..................................94thru9D......................................|
2.............................     9Ethru9F     .▒...............................|
3................................................................................|
4................................................................................|
5................................................................................|
x--------------------------------------------------------------------------------x

test tests::displays ... ok
thread 'tests::control_chars_become_replacement_char' panicked at /home/user/1tmp/ncurses_things/cursive/cursive/examples/select_test.rs:148:9:
assertion `left == right` failed: tabs and newline should've been replaced with replacement char � aka \u{FFFD}
  left: 0
 right: 1
stack backtrace:
thread 'tests::nuls_become_replacement_char' panicked at /home/user/1tmp/ncurses_things/cursive/cursive/examples/select_test.rs:133:9:
assertion `left == right` failed: nuls aka \0 in strings are supposed to become the replacement char '�'
  left: 0
 right: 1
captured piece:
x01234567890123456789012345678901234567890123456789012345678901234567890123456789x
0................................................................................|
1................................................................................|
2...........................                         ............................|
3...........................                          ...........................|
4...........................                          ...........................|
5.........................┌────────────────────────────┐.........................|
6.........................│ Abu.Dhabi is a.great.city! │ ........................|
7.........................│  .....         ...... ..   │ ........................|
8.........................│  ....     .     ....<Quit> │ ........................|
9.........................└────────────────────────────┘ ........................|
0..........................                          .   ........................|
1...........................                          ...........................|
2...........................                          ...........................|
3...........................                          ...........................|
4............................                         ...........................|
5................................................................................|
x--------------------------------------------------------------------------------x

test tests::interacts ... ok
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::panicking::assert_failed_inner
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:337:23
   3: core::panicking::assert_failed
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:297:5
   4: select_test::tests::control_chars_become_replacement_char
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/macros/mod.rs:58:21
   5: select_test::tests::control_chars_become_replacement_char::{{closure}}
             at ./examples/select_test.rs:142:47
   6: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::panicking::assert_failed_inner
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:337:23
   3: core::panicking::assert_failed
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:297:5
   4: select_test::tests::nuls_become_replacement_char
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/macros/mod.rs:58:21
   5: select_test::tests::nuls_become_replacement_char::{{closure}}
             at ./examples/select_test.rs:128:38
   6: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5test tests::control_chars_become_replacement_char ...
FAILED
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
test tests::nuls_become_replacement_char ... FAILED

failures:

failures:
    tests::control_chars_become_replacement_char
    tests::nuls_become_replacement_char

test result: FAILED. 2 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s

error: test failed, to rerun pass `-p cursive --example select_test`
  • Loading branch information
correabuscar committed Jun 7, 2024
1 parent 1f6598a commit aea0fe2
Showing 1 changed file with 110 additions and 58 deletions.
168 changes: 110 additions & 58 deletions cursive/examples/select_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ pub mod tests {
select.add_item_str("16\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1Fthru1F");
select.add_item_str("7F\x7Fonly");
select.add_item_str("80\u{0080}\u{0081}\u{0082}\u{0083}\u{0084}\u{0085}\u{0086}\u{0087}\u{0088}\u{0089}thru89");
select.add_item_str("8A\u{008A}\u{008B}\u{008C}\u{008D}\u{008E}\u{008F}\u{0090}\u{0091}\u{0092}\u{0093}thru93");
select.add_item_str("8a\u{008A}\u{008B}\u{008C}\u{008D}\u{008E}\u{008F}\u{0090}\u{0091}\u{0092}\u{0093}thru93");
select.add_item_str("94\u{0094}\u{0095}\u{0096}\u{0097}\u{0098}\u{0099}\u{009A}\u{009B}\u{009C}\u{009D}thru9D");
select.add_item_str("9E\u{009E}\u{009F}thru9F");
//XXX: can't add more lines here, it would cause them to go off view thus fail the is-it-on-screen tests, unless the dialog is made bigger below!

// Sets the callback for when "Enter" is pressed.
select.set_on_submit(show_next_window);
Expand Down Expand Up @@ -125,68 +126,119 @@ pub mod tests {
}

#[test]
fn nuls_become_replacement_char() {
let mut s = BasicSetup::new();
s.hit_keystroke(Key::End);
let screen = s.last_screen().unwrap();
s.dump_debug();
assert_eq!(
screen
.find_occurences("short \u{fffd}nul\u{FFFD} 1str")
.len(),
1,
"nuls aka \\0 in strings are supposed to become the replacement char '\u{fffd}'"
);
}
#[test]
fn control_chars_become_replacement_char() {
fn control_chars_including_nul_when_on_screen() {
let mut s = BasicSetup::new();
s.hit_keystroke(Key::End);
let screen = s.last_screen().unwrap();
s.dump_debug();
let replacement_char = "\u{FFFD} aka \\u{FFFD}";
assert_eq!(
screen.find_occurences("tab�and�newline").len(),
1,
"tabs and newline should've been replaced with replacement char {}",
replacement_char
);
assert_eq!(
screen.find_occurences("b�����������thru15").len(),
1,
"control chars \\x0B thru \\x15 should've been replaced with the replacement char {}",
replacement_char
);
assert_eq!(
screen.find_occurences("16����������thru1F").len(),
1,
"control chars \\x16 thru \\x1F should've been replaced with the replacement char {}",
replacement_char
);
assert_eq!(
screen.find_occurences("7F�only").len(),
1,
"control char \\x7F should've been replaced with the replacement char {}",
replacement_char
);
assert_eq!(
screen.find_occurences("80����������thru89").len(),
1,
"control chars \\x80 thru \\x89 should've been replaced with the replacement char {}",
replacement_char
);
assert_eq!(
screen.find_occurences("8A����������thru93").len(),
1,
"control chars \\x8A thru \\x93 should've been replaced with the replacement char {}",
replacement_char
);
assert_eq!(
screen.find_occurences("9E��thru9F").len(),
1,
"control chars \\x9E thru \\x9F should've been replaced with the replacement char {}",
replacement_char
);
use unicode_width::UnicodeWidthStr;
let width_of_nul = "\0".width();
if !(0..=1).contains(&width_of_nul) {
panic!(
"nul aka \\0 has a width of '{width_of_nul}' instead of the expected one of 0 or 1"
);
}
//we assume that all other control chars have same width as nul for chosing
//which test to perform on them which depends on which unicode-width crate
//version was used: the <=0.1.12 (width==0) or >=0.1.13 (width==1)
//for width==0 we expect control chars to have been deleted from on-screen output
//for width==1 we expect they were replaced with the width 1 replacement char: �
if width_of_nul == 1 {
// unicode-width version =1.1.13 or maybe later too
assert_eq!(
screen
.find_occurences("short \u{fffd}nul\u{FFFD} 1str")
.len(),
1,
"nuls aka \\0 in strings are supposed to become the replacement char '\u{fffd}'"
);
assert_eq!(
screen.find_occurences("tab�and�newline").len(),
1,
"tabs and newline should've been replaced with replacement char {replacement_char}"
);
assert_eq!(
screen.find_occurences("b�����������thru15").len(),
1,
"control chars \\x0B thru \\x15 should've been replaced with the replacement char {replacement_char}",
);
assert_eq!(
screen.find_occurences("16����������thru1F").len(),
1,
"control chars \\x16 thru \\x1F should've been replaced with the replacement char {replacement_char}",
);
assert_eq!(
screen.find_occurences("7F�only").len(),
1,
"control char \\x7F should've been replaced with the replacement char {replacement_char}",
);
assert_eq!(
screen.find_occurences("80����������thru89").len(),
1,
"control chars \\x80 thru \\x89 should've been replaced with the replacement char {replacement_char}",
);
assert_eq!(
screen.find_occurences("8a����������thru93").len(),
1,
"control chars \\x8A thru \\x93 should've been replaced with the replacement char {replacement_char}",
);
assert_eq!(
screen.find_occurences("9E��thru9F").len(),
1,
"control chars \\x9E thru \\x9F should've been replaced with the replacement char {replacement_char}",
);
} else if width_of_nul == 0 {
// unicode-width version <=1.1.12
assert_eq!(
screen.find_occurences("short nul 1str").len(),
1,
"nuls aka \\0 in strings are supposed to deleted from output"
);
assert_eq!(
screen.find_occurences("tabandnewline").len(),
1,
"tabs and newline should've been deleted from output"
);
assert_eq!(
screen.find_occurences("bthru15").len(),
1,
"control chars \\x0B thru \\x15 should've been deleted from output"
);
assert_eq!(
screen.find_occurences("16thru1F").len(),
1,
"control chars \\x16 thru \\x1F should've been deleted from output"
);
assert_eq!(
screen.find_occurences("7Fonly").len(),
1,
"control char \\x7F should've been deleted from output"
);
assert_eq!(
screen.find_occurences("80thru89").len(),
1,
"control chars \\x80 thru \\x89 should've been deleted from output"
);
// XXX: the "8A" here on stdout is "8." due to something(? buffer.rs ?)
// that sees it was an "A" there, in the same spot, before the End key
// was pressed(it was A from Alofi city) and it got replaced with "."(None?)
// as if to say it's unchanged now, thus neither "A" nor "." can actually
// be found via find_occurrences() below. Workaround: used "a", however:
// FIXME: these tests could fail if the city names in cities.txt are ever changed.
assert_eq!(
screen.find_occurences("8athru93").len(),
1,
"control chars \\x8A thru \\x93 should've been deleted from output"
);
assert_eq!(
screen.find_occurences("9Ethru9F").len(),
1,
"control chars \\x9E thru \\x9F should've been deleted from output"
);
} else {
unreachable!();
}
}

#[test]
Expand Down

0 comments on commit aea0fe2

Please sign in to comment.