Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Rust]簡単なテストコードとtestingライブラリの導入 #134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: test workflow
on: [push]
on: [push, pull_request]
jobs:
rust-lint:
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions crates/voicevox_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rstest は parameterized testや fixtureを提供できるらしいtesting libraryで今回は parameterized testを行うために導入
pretty_assertions は標準ライブラリにある assert_eq!, assert_ne! をリッチにしたもので、差分の表示が色付きでわかりやすく表示される rustのバージョンがあたらしくなってからは各test moduleごとにuse する必要が出たが、他に有力そうなものがなさそうだったので採用

手探りなのでこっちのがいいよ!ってあったら教えてください


[build-dependencies]
cbindgen = "0.23.0"
18 changes: 18 additions & 0 deletions crates/voicevox_core/src/c_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::sync::Mutex;
*/

#[repr(C)]
#[derive(Debug, PartialEq)]
#[allow(non_camel_case_types)]
pub enum VoicevoxResultCode {
// C でのenum定義に合わせて大文字で定義している
Expand Down Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pretty_assertionsを使う場合はこんな感じで各test module内で useする必要があるらしい


#[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);
}
}
3 changes: 3 additions & 0 deletions crates/voicevox_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ mod result;

use error::*;
use result::*;

#[cfg(test)]
use rstest::*;