Brian Silverman | f3ec38b | 2022-07-06 20:43:36 -0700 | [diff] [blame] | 1 | // Copyright 2022 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | // option. This file may not be copied, modified, or distributed |
| 7 | // except according to those terms. |
| 8 | |
| 9 | //! Tests specific to reference wrappers. |
| 10 | |
| 11 | use autocxx_integration_tests::{directives_from_lists, do_run_test}; |
| 12 | use indoc::indoc; |
| 13 | use proc_macro2::TokenStream; |
| 14 | use quote::quote; |
| 15 | |
Austin Schuh | 6ea9bfa | 2023-08-06 19:05:10 -0700 | [diff] [blame^] | 16 | const fn arbitrary_self_types_supported() -> bool { |
| 17 | rustversion::cfg!(nightly) |
| 18 | } |
| 19 | |
Brian Silverman | f3ec38b | 2022-07-06 20:43:36 -0700 | [diff] [blame] | 20 | /// A positive test, we expect to pass. |
| 21 | fn run_cpprefs_test( |
| 22 | cxx_code: &str, |
| 23 | header_code: &str, |
| 24 | rust_code: TokenStream, |
| 25 | generate: &[&str], |
| 26 | generate_pods: &[&str], |
| 27 | ) { |
Austin Schuh | 6ea9bfa | 2023-08-06 19:05:10 -0700 | [diff] [blame^] | 28 | if !arbitrary_self_types_supported() { |
| 29 | // "unsafe_references_wrapped" requires arbitrary_self_types, which requires nightly. |
| 30 | return; |
| 31 | } |
Brian Silverman | f3ec38b | 2022-07-06 20:43:36 -0700 | [diff] [blame] | 32 | do_run_test( |
| 33 | cxx_code, |
| 34 | header_code, |
| 35 | rust_code, |
| 36 | directives_from_lists(generate, generate_pods, None), |
| 37 | None, |
| 38 | None, |
| 39 | None, |
| 40 | "unsafe_references_wrapped", |
Austin Schuh | 6ea9bfa | 2023-08-06 19:05:10 -0700 | [diff] [blame^] | 41 | Some(quote! { |
| 42 | #![feature(arbitrary_self_types)] |
| 43 | }), |
Brian Silverman | f3ec38b | 2022-07-06 20:43:36 -0700 | [diff] [blame] | 44 | ) |
| 45 | .unwrap() |
| 46 | } |
| 47 | |
| 48 | #[test] |
| 49 | fn test_method_call_mut() { |
| 50 | run_cpprefs_test( |
| 51 | "", |
| 52 | indoc! {" |
| 53 | #include <string> |
| 54 | #include <sstream> |
| 55 | |
| 56 | class Goat { |
| 57 | public: |
| 58 | Goat() : horns(0) {} |
| 59 | void add_a_horn(); |
| 60 | private: |
| 61 | uint32_t horns; |
| 62 | }; |
| 63 | |
| 64 | inline void Goat::add_a_horn() { horns++; } |
| 65 | "}, |
| 66 | quote! { |
| 67 | let goat = ffi::Goat::new().within_unique_ptr(); |
Austin Schuh | 6ea9bfa | 2023-08-06 19:05:10 -0700 | [diff] [blame^] | 68 | let mut goat = autocxx::CppUniquePtrPin::new(goat); |
Brian Silverman | f3ec38b | 2022-07-06 20:43:36 -0700 | [diff] [blame] | 69 | goat.as_cpp_mut_ref().add_a_horn(); |
| 70 | }, |
| 71 | &["Goat"], |
| 72 | &[], |
| 73 | ) |
| 74 | } |
| 75 | |
| 76 | #[test] |
| 77 | fn test_method_call_const() { |
| 78 | run_cpprefs_test( |
| 79 | "", |
| 80 | indoc! {" |
| 81 | #include <string> |
| 82 | #include <sstream> |
| 83 | |
| 84 | class Goat { |
| 85 | public: |
| 86 | Goat() : horns(0) {} |
| 87 | std::string describe() const; |
| 88 | private: |
| 89 | uint32_t horns; |
| 90 | }; |
| 91 | |
| 92 | inline std::string Goat::describe() const { |
| 93 | std::ostringstream oss; |
| 94 | std::string plural = horns == 1 ? \"\" : \"s\"; |
| 95 | oss << \"This goat has \" << horns << \" horn\" << plural << \".\"; |
| 96 | return oss.str(); |
| 97 | } |
| 98 | "}, |
| 99 | quote! { |
| 100 | let goat = ffi::Goat::new().within_unique_ptr(); |
Austin Schuh | 6ea9bfa | 2023-08-06 19:05:10 -0700 | [diff] [blame^] | 101 | let goat = autocxx::CppUniquePtrPin::new(goat); |
Brian Silverman | f3ec38b | 2022-07-06 20:43:36 -0700 | [diff] [blame] | 102 | goat.as_cpp_ref().describe(); |
| 103 | }, |
| 104 | &["Goat"], |
| 105 | &[], |
| 106 | ) |
| 107 | } |