зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-08 00:15:50 +03:00
support generating random CursorMove with arbitrary
Этот коммит содержится в:
@@ -10,7 +10,7 @@ cargo-fuzz = true
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
libfuzzer-sys = "0.4"
|
libfuzzer-sys = "0.4"
|
||||||
arbitrary = "1"
|
arbitrary = { version = "1", features = ["derive"] }
|
||||||
tui-textarea = { path = "..", features = ["search", "arbitrary"] }
|
tui-textarea = { path = "..", features = ["search", "arbitrary"] }
|
||||||
tui-textarea-bench = { path = "../bench" }
|
tui-textarea-bench = { path = "../bench" }
|
||||||
|
|
||||||
|
|||||||
@@ -2,21 +2,35 @@
|
|||||||
|
|
||||||
use arbitrary::{Arbitrary, Result, Unstructured};
|
use arbitrary::{Arbitrary, Result, Unstructured};
|
||||||
use libfuzzer_sys::fuzz_target;
|
use libfuzzer_sys::fuzz_target;
|
||||||
use std::iter;
|
|
||||||
use std::str;
|
use std::str;
|
||||||
use tui_textarea::{Input, TextArea};
|
use tui_textarea::{CursorMove, Input, TextArea};
|
||||||
use tui_textarea_bench::{dummy_terminal, TerminalExt};
|
use tui_textarea_bench::{dummy_terminal, TerminalExt};
|
||||||
|
|
||||||
|
#[derive(Arbitrary)]
|
||||||
|
enum RandomInput {
|
||||||
|
Input(Input),
|
||||||
|
Cursor(CursorMove),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RandomInput {
|
||||||
|
fn apply(self, t: &mut TextArea<'_>) {
|
||||||
|
match self {
|
||||||
|
Self::Input(input) => {
|
||||||
|
t.input(input);
|
||||||
|
}
|
||||||
|
Self::Cursor(m) => t.move_cursor(m),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn fuzz(data: &[u8]) -> Result<()> {
|
fn fuzz(data: &[u8]) -> Result<()> {
|
||||||
let mut term = dummy_terminal();
|
let mut term = dummy_terminal();
|
||||||
let mut data = Unstructured::new(data);
|
let mut data = Unstructured::new(data);
|
||||||
let inputs: Vec<_> = iter::repeat_with(|| Input::arbitrary(&mut data))
|
|
||||||
.take(100)
|
|
||||||
.collect::<Result<_>>()?;
|
|
||||||
let text = <&str>::arbitrary(&mut data)?;
|
let text = <&str>::arbitrary(&mut data)?;
|
||||||
let mut textarea = TextArea::from(text.lines());
|
let mut textarea = TextArea::from(text.lines());
|
||||||
for input in inputs {
|
for _ in 0..100 {
|
||||||
textarea.input(input);
|
let input = RandomInput::arbitrary(&mut data)?;
|
||||||
|
input.apply(&mut textarea);
|
||||||
term.draw_textarea(&mut textarea);
|
term.draw_textarea(&mut textarea);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
use crate::word::{find_word_start_backward, find_word_start_forward};
|
use crate::word::{find_word_start_backward, find_word_start_forward};
|
||||||
|
#[cfg(feature = "arbitrary")]
|
||||||
|
use arbitrary::Arbitrary;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
|
|
||||||
/// Specify how to move the cursor.
|
/// Specify how to move the cursor.
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
|
||||||
pub enum CursorMove {
|
pub enum CursorMove {
|
||||||
/// Move cursor forward by one character. When the cursor is at the end of line, it moves to the head of next line.
|
/// Move cursor forward by one character. When the cursor is at the end of line, it moves to the head of next line.
|
||||||
/// ```
|
/// ```
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user