blob: 9cc6d390bac2f05b55e702cc15910b3df4952ca4 [file] [log] [blame]
Brian Silvermanf3ec38b2022-07-06 20:43:36 -07001// 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
11use autocxx_integration_tests::{directives_from_lists, do_run_test};
12use indoc::indoc;
13use proc_macro2::TokenStream;
14use quote::quote;
15
Austin Schuh6ea9bfa2023-08-06 19:05:10 -070016const fn arbitrary_self_types_supported() -> bool {
17 rustversion::cfg!(nightly)
18}
19
Brian Silvermanf3ec38b2022-07-06 20:43:36 -070020/// A positive test, we expect to pass.
21fn run_cpprefs_test(
22 cxx_code: &str,
23 header_code: &str,
24 rust_code: TokenStream,
25 generate: &[&str],
26 generate_pods: &[&str],
27) {
Austin Schuh6ea9bfa2023-08-06 19:05:10 -070028 if !arbitrary_self_types_supported() {
29 // "unsafe_references_wrapped" requires arbitrary_self_types, which requires nightly.
30 return;
31 }
Brian Silvermanf3ec38b2022-07-06 20:43:36 -070032 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 Schuh6ea9bfa2023-08-06 19:05:10 -070041 Some(quote! {
42 #![feature(arbitrary_self_types)]
43 }),
Brian Silvermanf3ec38b2022-07-06 20:43:36 -070044 )
45 .unwrap()
46}
47
48#[test]
49fn 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 Schuh6ea9bfa2023-08-06 19:05:10 -070068 let mut goat = autocxx::CppUniquePtrPin::new(goat);
Brian Silvermanf3ec38b2022-07-06 20:43:36 -070069 goat.as_cpp_mut_ref().add_a_horn();
70 },
71 &["Goat"],
72 &[],
73 )
74}
75
76#[test]
77fn 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 Schuh6ea9bfa2023-08-06 19:05:10 -0700101 let goat = autocxx::CppUniquePtrPin::new(goat);
Brian Silvermanf3ec38b2022-07-06 20:43:36 -0700102 goat.as_cpp_ref().describe();
103 },
104 &["Goat"],
105 &[],
106 )
107}