-
Notifications
You must be signed in to change notification settings - Fork 11
Implementing mocking of host functions for tests #102
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2bdc829
Implementing mocking of host functions, for nft only interim
tekvyy b923729
Added Drop Trait handling
tekvyy c9f138d
cleanup
tekvyy 26506d4
added test_mock_cleanup test
tekvyy a2b25ad
fixed Conditional Compilation Attributes
tekvyy 6676b3e
added a new test
tekvyy d78ed93
fixed error
tekvyy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| //! Simple macro-based mocking for host functions. | ||
| //! | ||
| //! # Usage | ||
| //! | ||
| //! ```rust | ||
| //! use xrpl_wasm_stdlib::mock_host; | ||
| //! use xrpl_wasm_stdlib::core::types::nft::NFToken; | ||
| //! | ||
| //! #[test] | ||
| //! fn test_nft_transfer_fee() { | ||
| //! mock_host! { | ||
| //! get_nft_transfer_fee(_ptr, _len) => 5000 | ||
| //! }; | ||
| //! | ||
| //! let nft = NFToken::new([0u8; 32]); | ||
| //! assert_eq!(nft.transfer_fee().unwrap(), 5000); | ||
| //! } | ||
| //! ``` | ||
|
|
||
| extern crate std; | ||
|
|
||
| use std::cell::RefCell; | ||
| use std::collections::HashMap; | ||
| use std::rc::Rc; | ||
| use std::thread_local; | ||
|
|
||
| type MockFn = Rc<dyn Fn(&[*const u8]) -> i32>; | ||
|
|
||
| thread_local! { | ||
| static MOCKS: RefCell<HashMap<&'static str, MockFn>> = RefCell::new(HashMap::new()); | ||
| } | ||
|
|
||
| pub fn set_mock<F>(name: &'static str, f: F) | ||
| where | ||
| F: Fn(&[*const u8]) -> i32 + 'static, | ||
| { | ||
| MOCKS.with(|m| { | ||
| m.borrow_mut().insert(name, Rc::new(f)); | ||
| }); | ||
| } | ||
|
|
||
| /// Clear a specific mock. | ||
| pub fn clear_mock(name: &'static str) { | ||
| MOCKS.with(|m| { | ||
| m.borrow_mut().remove(name); | ||
| }); | ||
| } | ||
|
|
||
| /// Clear all mocks. | ||
| pub fn clear_all_mocks() { | ||
| MOCKS.with(|m| { | ||
| m.borrow_mut().clear(); | ||
| }); | ||
| } | ||
|
|
||
| /// Get a mock function if it exists. | ||
| pub(crate) fn get_mock(name: &'static str) -> Option<MockFn> { | ||
| MOCKS.with(|m| m.borrow().get(name).cloned()) | ||
| } | ||
|
|
||
| /// Guard that clears mocks when dropped. | ||
| pub struct MockGuard; | ||
|
|
||
| impl Drop for MockGuard { | ||
| fn drop(&mut self) { | ||
| clear_all_mocks(); | ||
| } | ||
| } | ||
|
|
||
| /// Macro for easily setting up mocks in tests. | ||
| /// | ||
| /// This macro sets up the specified mocks and keeps them active until the end of the current scope. | ||
| /// It expands to a `let` binding internally, so you don't need to assign the result to a variable. | ||
| /// | ||
| /// # Usage | ||
| /// | ||
| /// ```rust | ||
| /// mock_host! { | ||
| /// get_nft(...) => 42 | ||
| /// }; | ||
| /// ``` | ||
| #[macro_export] | ||
| macro_rules! mock_host { | ||
| // Simple value return | ||
| ($($name:ident($($arg:ident),*) => $ret:expr),+ $(,)?) => { | ||
| let _mock_guard = { | ||
| $({ | ||
| $crate::host::mock::set_mock(stringify!($name), move |_args| $ret); | ||
| })+ | ||
| $crate::host::mock::MockGuard | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
||
| use core::ptr; | ||
|
|
||
| #[test] | ||
| fn test_simple_mock() { | ||
| mock_host! { | ||
| get_nft_transfer_fee(_ptr, _len) => 42 | ||
| }; | ||
|
|
||
| let result = unsafe { super::super::get_nft_transfer_fee(ptr::null(), 0) }; | ||
| assert_eq!(result, 42); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_multiple_mocks() { | ||
| mock_host! { | ||
| get_nft_transfer_fee(_ptr, _len) => 100, | ||
| get_nft_flags(_ptr, _len) => 200 | ||
| }; | ||
|
|
||
| assert_eq!( | ||
| unsafe { super::super::get_nft_transfer_fee(ptr::null(), 0) }, | ||
| 100 | ||
| ); | ||
| assert_eq!(unsafe { super::super::get_nft_flags(ptr::null(), 0) }, 200); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_mock_cleanup() { | ||
| { | ||
| mock_host! { | ||
| get_nft_flags(_ptr, _len) => 200 | ||
| }; | ||
| assert_eq!(unsafe { super::super::get_nft_flags(ptr::null(), 0) }, 200); | ||
| } | ||
| // Should return 0 (the length passed) when not mocked | ||
| assert_eq!(unsafe { super::super::get_nft_flags(ptr::null(), 0) }, 0); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_mock_with_verification() { | ||
| use core::slice; | ||
|
|
||
| let expected_data = [1u8, 2, 3, 4]; | ||
|
|
||
| let _guard = { | ||
| super::set_mock("get_nft_transfer_fee", move |args| { | ||
| // args[0] is pointer, args[1] is length | ||
| let ptr = args[0]; | ||
| let len = args[1] as usize; | ||
| let actual_data = unsafe { slice::from_raw_parts(ptr, len) }; | ||
| assert_eq!(actual_data, &[1, 2, 3, 4]); | ||
|
|
||
| 42 | ||
| }); | ||
| super::MockGuard | ||
| }; | ||
|
|
||
| let result = unsafe { | ||
| super::super::get_nft_transfer_fee(expected_data.as_ptr(), expected_data.len()) | ||
| }; | ||
| assert_eq!(result, 42); | ||
| } | ||
| } | ||
tekvyy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding a test to check actually passing some data into the pointer (the way the host functions actually work)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bump @tekvyy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mvadari Pushed the test, please review
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mvadari let me know if anything else is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mvadari Let me know if we can merge this, i will create a followup PR with tests for other modules :)