From 57e4c2728696fe02c22ab2dc6f7e3742c80b082f Mon Sep 17 00:00:00 2001 From: qwerty2501 <939468+qwerty2501@users.noreply.github.com> Date: Sun, 15 May 2022 22:11:03 +0900 Subject: [PATCH] =?UTF-8?q?[Rust]=E7=B0=A1=E5=8D=98=E3=81=AA=E3=83=86?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=82=B3=E3=83=BC=E3=83=89=E3=81=A8testing?= =?UTF-8?q?=E3=83=A9=E3=82=A4=E3=83=96=E3=83=A9=E3=83=AA=E3=81=AE=E5=B0=8E?= =?UTF-8?q?=E5=85=A5=20(#134)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 簡単なテストコードとtestingライブラリの導入 参考になりそうな簡単なテストコードと独断ですがtestingライブラリを導入しました * pull_request時にtestが実行されるようにした --- .github/workflows/test.yml | 2 +- crates/voicevox_core/Cargo.toml | 4 ++++ crates/voicevox_core/src/c_export.rs | 18 ++++++++++++++++++ crates/voicevox_core/src/lib.rs | 3 +++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 76f6a3725..9b78c11dc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,5 +1,5 @@ name: test workflow -on: [push] +on: [push, pull_request] jobs: rust-lint: runs-on: ubuntu-latest diff --git a/crates/voicevox_core/Cargo.toml b/crates/voicevox_core/Cargo.toml index 2b8a6f181..7f63d504a 100644 --- a/crates/voicevox_core/Cargo.toml +++ b/crates/voicevox_core/Cargo.toml @@ -13,5 +13,9 @@ derive-new = "0.5.9" once_cell = "1.10.0" thiserror = "1.0.31" +[dev-dependencies] +rstest = "0.12.0" +pretty_assertions = "1.2.1" + [build-dependencies] cbindgen = "0.23.0" diff --git a/crates/voicevox_core/src/c_export.rs b/crates/voicevox_core/src/c_export.rs index 58a96ef7d..dd213da57 100644 --- a/crates/voicevox_core/src/c_export.rs +++ b/crates/voicevox_core/src/c_export.rs @@ -11,6 +11,7 @@ use std::sync::Mutex; */ #[repr(C)] +#[derive(Debug, PartialEq)] #[allow(non_camel_case_types)] pub enum VoicevoxResultCode { // C でのenum定義に合わせて大文字で定義している @@ -214,3 +215,20 @@ pub extern "C" fn voicevox_error_result_to_message( ) -> *const c_char { internal::voicevox_error_result_to_message(result_code).as_ptr() as *const c_char } + +#[cfg(test)] +mod tests { + use super::*; + use pretty_assertions::assert_eq; + + #[rstest] + #[case(Ok(()), VoicevoxResultCode::VOICEVOX_RESULT_SUCCEED)] + #[case( + Err(Error::NotLoadedOpenjtalkDict), + VoicevoxResultCode::VOICEVOX_RESULT_NOT_LOADED_OPENJTALK_DICT + )] + fn convert_result_works(#[case] result: Result<()>, #[case] expected: VoicevoxResultCode) { + let (_, actual) = convert_result(result); + assert_eq!(expected, actual); + } +} diff --git a/crates/voicevox_core/src/lib.rs b/crates/voicevox_core/src/lib.rs index 748ff9e01..7ec79b138 100644 --- a/crates/voicevox_core/src/lib.rs +++ b/crates/voicevox_core/src/lib.rs @@ -5,3 +5,6 @@ mod result; use error::*; use result::*; + +#[cfg(test)] +use rstest::*;