blob: 4241decc214535269b33a8f21dd75a3c59ff61b7 [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
16/// A positive test, we expect to pass.
17fn run_cpprefs_test(
18 cxx_code: &str,
19 header_code: &str,
20 rust_code: TokenStream,
21 generate: &[&str],
22 generate_pods: &[&str],
23) {
24 do_run_test(
25 cxx_code,
26 header_code,
27 rust_code,
28 directives_from_lists(generate, generate_pods, None),
29 None,
30 None,
31 None,
32 "unsafe_references_wrapped",
33 )
34 .unwrap()
35}
36
37#[test]
38fn test_method_call_mut() {
39 run_cpprefs_test(
40 "",
41 indoc! {"
42 #include <string>
43 #include <sstream>
44
45 class Goat {
46 public:
47 Goat() : horns(0) {}
48 void add_a_horn();
49 private:
50 uint32_t horns;
51 };
52
53 inline void Goat::add_a_horn() { horns++; }
54 "},
55 quote! {
56 let goat = ffi::Goat::new().within_unique_ptr();
57 let mut goat = ffi::CppUniquePtrPin::new(goat);
58 goat.as_cpp_mut_ref().add_a_horn();
59 },
60 &["Goat"],
61 &[],
62 )
63}
64
65#[test]
66fn test_method_call_const() {
67 run_cpprefs_test(
68 "",
69 indoc! {"
70 #include <string>
71 #include <sstream>
72
73 class Goat {
74 public:
75 Goat() : horns(0) {}
76 std::string describe() const;
77 private:
78 uint32_t horns;
79 };
80
81 inline std::string Goat::describe() const {
82 std::ostringstream oss;
83 std::string plural = horns == 1 ? \"\" : \"s\";
84 oss << \"This goat has \" << horns << \" horn\" << plural << \".\";
85 return oss.str();
86 }
87 "},
88 quote! {
89 let goat = ffi::Goat::new().within_unique_ptr();
90 let goat = ffi::cpp_pin_uniqueptr(goat);
91 goat.as_cpp_ref().describe();
92 },
93 &["Goat"],
94 &[],
95 )
96}