pub struct Mocksmith { /* private fields */ }Expand description
Mocksmith is a struct for generating Google Mock mocks for C++ classes.
Implementations§
Source§impl Mocksmith
impl Mocksmith
Sourcepub fn new(log_write: Option<Box<dyn Write>>, verbose: bool) -> Result<Self>
pub fn new(log_write: Option<Box<dyn Write>>, verbose: bool) -> Result<Self>
Creates a new Mocksmith instance.
The function fails if another thread already holds an instance, since Clang can only be used from one thread.
Sourcepub fn new_when_available() -> Result<Self>
pub fn new_when_available() -> Result<Self>
Creates a new Mocksmith instance.
The function waits for any other thread holding an instance to release its instance before returning since Clang can only be used from one thread. If a thread using Mocksmith panics, the poisoning is cleared.
Sourcepub fn include_path<P>(self, include_path: P) -> Self
pub fn include_path<P>(self, include_path: P) -> Self
Adds an include path to the list of paths to search for headers. If no include paths are set, the current directory is used.
Sourcepub fn include_paths(self, include_paths: &[PathBuf]) -> Self
pub fn include_paths(self, include_paths: &[PathBuf]) -> Self
Adds include paths to the list of paths to search for headers. If no include paths are set, the current directory is used.
Sourcepub fn methods_to_mock(self, methods: MethodsToMockStrategy) -> Self
pub fn methods_to_mock(self, methods: MethodsToMockStrategy) -> Self
Sets which methods to mock in the classes. Default is AllVirtual, which mocks
all virtual methods.
Sourcepub fn class_filter_fun(self, filter: impl Fn(&str) -> bool + 'static) -> Self
pub fn class_filter_fun(self, filter: impl Fn(&str) -> bool + 'static) -> Self
Sets a function to filter which classes to mock. The function takes the name of
a class and should return true if the class should be mocked.
Sourcepub fn ignore_errors(self, value: bool) -> Self
pub fn ignore_errors(self, value: bool) -> Self
Errors detected by Clang during parsing normally causes mock generation to fail.
Setting this option disables which may be useful, e.g., when not able to provide
all the include paths. Beware that this may lead to unknown types in arguments
being referred to as int in generated mocks, and entire methods and classes
being ignored (when return value of method is unknown).
Sourcepub fn cpp_standard(self, standard: Option<String>) -> Self
pub fn cpp_standard(self, standard: Option<String>) -> Self
Sets the C++ standard to use when parsing the source header files. Default is “c++17”.
Sourcepub fn additional_clang_args(self, args: Vec<String>) -> Self
pub fn additional_clang_args(self, args: Vec<String>) -> Self
Sets additional arguments to the clang C++ parser.
Sourcepub fn parse_function_bodies(self, value: bool) -> Self
pub fn parse_function_bodies(self, value: bool) -> Self
For easy testability of parser warnings.
Sourcepub fn msvc_allow_overriding_deprecated_methods(self, value: bool) -> Self
pub fn msvc_allow_overriding_deprecated_methods(self, value: bool) -> Self
Sets whether to add MSVC pragma to allow overriding methods marked as deprecated. If it is not added mocked methods marked as deprecated will cause compilation warnings. The pragma is only added when generating headers. Default is false.
Sourcepub fn simplified_nested_namespaces(self, value: bool) -> Self
pub fn simplified_nested_namespaces(self, value: bool) -> Self
Controls whether to use C++17 style nested namespace declarations with colon separation or older style. Default is true.
Sourcepub fn indent_str(self, indent: String) -> Self
pub fn indent_str(self, indent: String) -> Self
Sets the string to use for indentation for the generated code. Default is 2 spaces.
Sourcepub fn mock_name_fun(self, name_mock: impl Fn(&str) -> String + 'static) -> Self
pub fn mock_name_fun(self, name_mock: impl Fn(&str) -> String + 'static) -> Self
Sets a custom function to generate mock names based on class names.
Sourcepub fn create_mocks_for_file<P>(&self, file: P) -> Result<Vec<Mock>>
pub fn create_mocks_for_file<P>(&self, file: P) -> Result<Vec<Mock>>
Generates mocks for classes in the given file. If no appropriate classes to mock are found, an empty vector is returned.
Sourcepub fn create_mocks_from_string(&self, content: &str) -> Result<Vec<Mock>>
pub fn create_mocks_from_string(&self, content: &str) -> Result<Vec<Mock>>
Generates mocks for classes in the given string. If no appropriate classes to mock are found, an empty vector is returned.
Sourcepub fn create_mock_header_for_files<P>(&self, files: &[P]) -> Result<MockHeader>
pub fn create_mock_header_for_files<P>(&self, files: &[P]) -> Result<MockHeader>
Generate the contents for a header file with mocks for classes in the give file. If no appropriate classes to mock are found, an error is returned.