From 673bdcb84b52c3041ebebe15b7451ad2001b74a4 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 12 Jul 2024 23:36:45 +0900 Subject: [PATCH] add tests for serde support --- .github/workflows/ci.yml | 9 ++++---- Cargo.toml | 5 ++++- tests/serde.rs | 47 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 tests/serde.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4cd17e5..ff32a09 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,12 +18,12 @@ jobs: uses: taiki-e/install-action@cargo-llvm-cov - name: Run tests on Linux or macOS run: | - cargo llvm-cov --color always --lcov --output-path lcov.info --features=search,termwiz,termion,arbitrary + cargo llvm-cov --color always --lcov --output-path lcov.info --features=search,termwiz,termion,serde,arbitrary cargo llvm-cov --color always --no-run if: ${{ matrix.os != 'windows-latest' }} - name: Run tests on Windows run: | - cargo llvm-cov --color always --lcov --output-path lcov.info --features=search,termwiz,arbitrary + cargo llvm-cov --color always --lcov --output-path lcov.info --features=search,termwiz,serde,arbitrary cargo llvm-cov --color always --no-run if: ${{ matrix.os == 'windows-latest' }} - run: cargo test --no-default-features --features=tuirs-crossterm,search -- --skip .rs @@ -44,8 +44,7 @@ jobs: - uses: Swatinem/rust-cache@v2 - run: cargo fmt -- --check - run: cargo clippy --examples --tests -- -D warnings - - run: cargo clippy --examples --tests --features search -- -D warnings - - run: cargo clippy --examples --tests --features serde -- -D warnings + - run: cargo clippy --examples --tests --features search,serde -- -D warnings - run: cargo clippy --examples --tests --no-default-features --features termion -- -D warnings - run: cargo clippy --examples --tests --no-default-features --features termion,search -- -D warnings - run: cargo clippy --examples --tests --no-default-features --features termwiz -- -D warnings @@ -58,7 +57,7 @@ jobs: - run: cargo clippy --examples --tests --no-default-features --features tuirs-termion,search -- -D warnings - run: cargo clippy --examples --tests --no-default-features --features tuirs-no-backend -- -D warnings - run: cargo clippy --examples --tests --no-default-features --features tuirs-no-backend,search -- -D warnings - - run: cargo rustdoc --features=search,termwiz,termion -p tui-textarea -- -D warnings + - run: cargo rustdoc --features=search,termwiz,termion,serde -p tui-textarea -- -D warnings cargo-doc: runs-on: ubuntu-latest steps: diff --git a/Cargo.toml b/Cargo.toml index e946b00..669a4bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -103,7 +103,10 @@ members = ["bench"] [profile.bench] lto = "thin" +[dev-dependencies] +serde_json = "1.0.120" + [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] -features = ["search", "crossterm", "termwiz", "termion"] +features = ["search", "crossterm", "termwiz", "termion", "serde"] rustdoc-args = ["--cfg", "docsrs"] diff --git a/tests/serde.rs b/tests/serde.rs new file mode 100644 index 0000000..9eccb72 --- /dev/null +++ b/tests/serde.rs @@ -0,0 +1,47 @@ +#![cfg(feature = "serde")] + +use tui_textarea::{CursorMove, Input, Key, Scrolling}; + +#[test] +fn test_serde_key() { + let k = Key::Char('a'); + let s = serde_json::to_string(&k).unwrap(); + assert_eq!(s, r#"{"Char":"a"}"#); + let d: Key = serde_json::from_str(&s).unwrap(); + assert_eq!(d, k); +} + +#[test] +fn test_serde_input() { + let i = Input { + key: Key::Char('a'), + ctrl: true, + alt: false, + shift: true, + }; + let s = serde_json::to_string(&i).unwrap(); + assert_eq!( + s, + r#"{"key":{"Char":"a"},"ctrl":true,"alt":false,"shift":true}"#, + ); + let d: Input = serde_json::from_str(&s).unwrap(); + assert_eq!(d, i); +} + +#[test] +fn test_serde_scrolling() { + let scroll = Scrolling::Delta { rows: 1, cols: 2 }; + let s = serde_json::to_string(&scroll).unwrap(); + assert_eq!(s, r#"{"Delta":{"rows":1,"cols":2}}"#); + let d: Scrolling = serde_json::from_str(&s).unwrap(); + assert_eq!(d, scroll); +} + +#[test] +fn test_serde_cursor_move() { + let c = CursorMove::Forward; + let s = serde_json::to_string(&c).unwrap(); + assert_eq!(s, r#""Forward""#); + let d: CursorMove = serde_json::from_str(&s).unwrap(); + assert_eq!(d, c); +}