summaryrefslogtreecommitdiff
path: root/tests/rustdoc/deref
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2023-04-28 12:54:26 -0700
committerMichael Howell <michael@notriddle.com>2023-04-28 12:54:26 -0700
commit10c77b1cd0027926a966620c988a3af8643314f2 (patch)
tree4786c5d2860b4c53a0d37fde850a14d5dc866a22 /tests/rustdoc/deref
parent43a78029b4f4d92978b8fde0a677ea300b113c41 (diff)
downloadrust-10c77b1cd0027926a966620c988a3af8643314f2.tar.gz
rustdoc: move deref tests into a directory
Diffstat (limited to 'tests/rustdoc/deref')
-rw-r--r--tests/rustdoc/deref/deref-const-fn.rs38
-rw-r--r--tests/rustdoc/deref/deref-mut-methods.rs29
-rw-r--r--tests/rustdoc/deref/deref-recursive-pathbuf.rs25
-rw-r--r--tests/rustdoc/deref/deref-recursive.rs41
-rw-r--r--tests/rustdoc/deref/deref-slice-core.rs22
-rw-r--r--tests/rustdoc/deref/deref-to-primitive.rs15
-rw-r--r--tests/rustdoc/deref/deref-typedef.rs46
-rw-r--r--tests/rustdoc/deref/escape-deref-methods.rs35
-rw-r--r--tests/rustdoc/deref/issue-100679-sidebar-links-deref.rs30
-rw-r--r--tests/rustdoc/deref/recursive-deref-sidebar.rs22
-rw-r--r--tests/rustdoc/deref/recursive-deref.rs120
11 files changed, 423 insertions, 0 deletions
diff --git a/tests/rustdoc/deref/deref-const-fn.rs b/tests/rustdoc/deref/deref-const-fn.rs
new file mode 100644
index 00000000000..8ecca6d12d2
--- /dev/null
+++ b/tests/rustdoc/deref/deref-const-fn.rs
@@ -0,0 +1,38 @@
+// This test ensures that the const methods from Deref aren't shown as const.
+// For more information, see https://github.com/rust-lang/rust/issues/90855.
+
+#![crate_name = "foo"]
+
+#![feature(staged_api)]
+
+#![stable(feature = "rust1", since = "1.0.0")]
+
+// @has 'foo/struct.Bar.html'
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Bar;
+
+impl Bar {
+ // @has - '//*[@id="method.len"]' 'pub const fn len(&self) -> usize'
+ // @has - '//*[@id="method.len"]//span[@class="since"]' 'const: 1.0.0'
+ #[stable(feature = "rust1", since = "1.0.0")]
+ #[rustc_const_stable(feature = "rust1", since = "1.0.0")]
+ pub const fn len(&self) -> usize { 0 }
+}
+
+#[stable(feature = "rust1", since = "1.0.0")]
+pub struct Foo {
+ value: Bar,
+}
+
+// @has 'foo/struct.Foo.html'
+// @has - '//*[@id="method.len"]' 'pub fn len(&self) -> usize'
+// @!has - '//*[@id="method.len"]//span[@class="since"]' '1.0.0'
+// @!has - '//*[@id="method.len"]//span[@class="since"]' '(const: 1.0.0)'
+#[stable(feature = "rust1", since = "1.0.0")]
+impl std::ops::Deref for Foo {
+ type Target = Bar;
+
+ fn deref(&self) -> &Self::Target {
+ &self.value
+ }
+}
diff --git a/tests/rustdoc/deref/deref-mut-methods.rs b/tests/rustdoc/deref/deref-mut-methods.rs
new file mode 100644
index 00000000000..fdf8434224f
--- /dev/null
+++ b/tests/rustdoc/deref/deref-mut-methods.rs
@@ -0,0 +1,29 @@
+#![crate_name = "foo"]
+
+use std::ops;
+
+pub struct Foo;
+
+impl Foo {
+ pub fn foo(&mut self) {}
+}
+
+// @has foo/struct.Bar.html
+// @has - '//*[@class="sidebar-elems"]//*[@class="block"]//a[@href="#method.foo"]' 'foo'
+pub struct Bar {
+ foo: Foo,
+}
+
+impl ops::Deref for Bar {
+ type Target = Foo;
+
+ fn deref(&self) -> &Foo {
+ &self.foo
+ }
+}
+
+impl ops::DerefMut for Bar {
+ fn deref_mut(&mut self) -> &mut Foo {
+ &mut self.foo
+ }
+}
diff --git a/tests/rustdoc/deref/deref-recursive-pathbuf.rs b/tests/rustdoc/deref/deref-recursive-pathbuf.rs
new file mode 100644
index 00000000000..be2b42b5ac6
--- /dev/null
+++ b/tests/rustdoc/deref/deref-recursive-pathbuf.rs
@@ -0,0 +1,25 @@
+// #26207: Show all methods reachable via Deref impls, recursing through multiple dereferencing
+// levels and across multiple crates.
+// For `Deref` on non-foreign types, look at `deref-recursive.rs`.
+
+// @has 'foo/struct.Foo.html'
+// @has '-' '//*[@id="deref-methods-PathBuf"]' 'Methods from Deref<Target = PathBuf>'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.as_path"]' 'pub fn as_path(&self)'
+// @has '-' '//*[@id="deref-methods-Path"]' 'Methods from Deref<Target = Path>'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.exists"]' 'pub fn exists(&self)'
+// @has '-' '//div[@class="sidebar-elems"]//h3/a[@href="#deref-methods-PathBuf"]' 'Methods from Deref<Target=PathBuf>'
+// @has '-' '//*[@class="sidebar-elems"]//*[@class="block"]//a[@href="#method.as_path"]' 'as_path'
+// @has '-' '//div[@class="sidebar-elems"]//h3/a[@href="#deref-methods-Path"]' 'Methods from Deref<Target=Path>'
+// @has '-' '//*[@class="sidebar-elems"]//*[@class="block"]//a[@href="#method.exists"]' 'exists'
+
+#![crate_name = "foo"]
+
+use std::ops::Deref;
+use std::path::PathBuf;
+
+pub struct Foo(PathBuf);
+
+impl Deref for Foo {
+ type Target = PathBuf;
+ fn deref(&self) -> &PathBuf { &self.0 }
+}
diff --git a/tests/rustdoc/deref/deref-recursive.rs b/tests/rustdoc/deref/deref-recursive.rs
new file mode 100644
index 00000000000..0436f2f86f5
--- /dev/null
+++ b/tests/rustdoc/deref/deref-recursive.rs
@@ -0,0 +1,41 @@
+// #26207: Show all methods reachable via Deref impls, recursing through multiple dereferencing
+// levels if needed.
+// For `Deref` on foreign types, look at `deref-recursive-pathbuf.rs`.
+
+// @has 'foo/struct.Foo.html'
+// @has '-' '//*[@id="deref-methods-Bar"]' 'Methods from Deref<Target = Bar>'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.bar"]' 'pub fn bar(&self)'
+// @has '-' '//*[@id="deref-methods-Baz"]' 'Methods from Deref<Target = Baz>'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.baz"]' 'pub fn baz(&self)'
+// @has '-' '//div[@class="sidebar-elems"]//h3/a[@href="#deref-methods-Bar"]' 'Methods from Deref<Target=Bar>'
+// @has '-' '//*[@class="sidebar-elems"]//section//a[@href="#method.bar"]' 'bar'
+// @has '-' '//div[@class="sidebar-elems"]//h3/a[@href="#deref-methods-Baz"]' 'Methods from Deref<Target=Baz>'
+// @has '-' '//*[@class="sidebar-elems"]//section//a[@href="#method.baz"]' 'baz'
+
+#![crate_name = "foo"]
+
+use std::ops::Deref;
+
+pub struct Foo(Bar);
+pub struct Bar(Baz);
+pub struct Baz;
+
+impl Deref for Foo {
+ type Target = Bar;
+ fn deref(&self) -> &Bar { &self.0 }
+}
+
+impl Deref for Bar {
+ type Target = Baz;
+ fn deref(&self) -> &Baz { &self.0 }
+}
+
+impl Bar {
+ /// This appears under `Foo` methods
+ pub fn bar(&self) {}
+}
+
+impl Baz {
+ /// This should also appear in `Foo` methods when recursing
+ pub fn baz(&self) {}
+}
diff --git a/tests/rustdoc/deref/deref-slice-core.rs b/tests/rustdoc/deref/deref-slice-core.rs
new file mode 100644
index 00000000000..cccf273a820
--- /dev/null
+++ b/tests/rustdoc/deref/deref-slice-core.rs
@@ -0,0 +1,22 @@
+// https://github.com/rust-lang/rust/issues/95325
+//
+// Show methods reachable from Deref of primitive.
+#![no_std]
+
+use core::ops::Deref;
+
+// @has 'deref_slice_core/struct.MyArray.html'
+// @has '-' '//*[@id="deref-methods-%5BT%5D"]' 'Methods from Deref<Target = [T]>'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.len"]' 'pub fn len(&self)'
+
+pub struct MyArray<T> {
+ array: [T; 10],
+}
+
+impl<T> Deref for MyArray<T> {
+ type Target = [T];
+
+ fn deref(&self) -> &Self::Target {
+ &self.array
+ }
+}
diff --git a/tests/rustdoc/deref/deref-to-primitive.rs b/tests/rustdoc/deref/deref-to-primitive.rs
new file mode 100644
index 00000000000..527de780d48
--- /dev/null
+++ b/tests/rustdoc/deref/deref-to-primitive.rs
@@ -0,0 +1,15 @@
+#![crate_name = "foo"]
+
+// @has 'foo/struct.Foo.html'
+// @has - '//*[@id="deref-methods-i32"]' 'Methods from Deref<Target = i32>'
+// @has - '//*[@id="deref-methods-i32-1"]//*[@id="associatedconstant.BITS"]/h4' \
+// 'pub const BITS: u32 = 32u32'
+pub struct Foo(i32);
+
+impl std::ops::Deref for Foo {
+ type Target = i32;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
diff --git a/tests/rustdoc/deref/deref-typedef.rs b/tests/rustdoc/deref/deref-typedef.rs
new file mode 100644
index 00000000000..32424d13eb8
--- /dev/null
+++ b/tests/rustdoc/deref/deref-typedef.rs
@@ -0,0 +1,46 @@
+#![crate_name = "foo"]
+
+// @has 'foo/struct.Bar.html'
+// @has '-' '//*[@id="deref-methods-FooJ"]' 'Methods from Deref<Target = FooJ>'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.foo_a"]' 'pub fn foo_a(&self)'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.foo_b"]' 'pub fn foo_b(&self)'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.foo_c"]' 'pub fn foo_c(&self)'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.foo_j"]' 'pub fn foo_j(&self)'
+// @has '-' '//div[@class="sidebar-elems"]//h3/a[@href="#deref-methods-FooJ"]' 'Methods from Deref<Target=FooJ>'
+// @has '-' '//*[@class="sidebar-elems"]//section//a[@href="#method.foo_a"]' 'foo_a'
+// @has '-' '//*[@class="sidebar-elems"]//section//a[@href="#method.foo_b"]' 'foo_b'
+// @has '-' '//*[@class="sidebar-elems"]//section//a[@href="#method.foo_c"]' 'foo_c'
+// @has '-' '//*[@class="sidebar-elems"]//section//a[@href="#method.foo_j"]' 'foo_j'
+
+pub struct FooA;
+pub type FooB = FooA;
+pub type FooC = FooB;
+pub type FooD = FooC;
+pub type FooE = FooD;
+pub type FooF = FooE;
+pub type FooG = FooF;
+pub type FooH = FooG;
+pub type FooI = FooH;
+pub type FooJ = FooI;
+
+impl FooA {
+ pub fn foo_a(&self) {}
+}
+
+impl FooB {
+ pub fn foo_b(&self) {}
+}
+
+impl FooC {
+ pub fn foo_c(&self) {}
+}
+
+impl FooJ {
+ pub fn foo_j(&self) {}
+}
+
+pub struct Bar;
+impl std::ops::Deref for Bar {
+ type Target = FooJ;
+ fn deref(&self) -> &Self::Target { unimplemented!() }
+}
diff --git a/tests/rustdoc/deref/escape-deref-methods.rs b/tests/rustdoc/deref/escape-deref-methods.rs
new file mode 100644
index 00000000000..66919d73eeb
--- /dev/null
+++ b/tests/rustdoc/deref/escape-deref-methods.rs
@@ -0,0 +1,35 @@
+#![crate_name = "foo"]
+
+use std::ops::{Deref, DerefMut};
+
+#[derive(Debug, Clone)]
+pub struct Title {
+ name: String,
+}
+
+#[derive(Debug, Clone)]
+pub struct TitleList {
+ pub members: Vec<Title>,
+}
+
+impl TitleList {
+ pub fn new() -> Self {
+ TitleList { members: Vec::new() }
+ }
+}
+
+impl Deref for TitleList {
+ type Target = Vec<Title>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.members
+ }
+}
+
+// @has foo/struct.TitleList.html
+// @has - '//div[@class="sidebar-elems"]//h3' 'Methods from Deref<Target=Vec<Title>>'
+impl DerefMut for TitleList {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.members
+ }
+}
diff --git a/tests/rustdoc/deref/issue-100679-sidebar-links-deref.rs b/tests/rustdoc/deref/issue-100679-sidebar-links-deref.rs
new file mode 100644
index 00000000000..f09d2320609
--- /dev/null
+++ b/tests/rustdoc/deref/issue-100679-sidebar-links-deref.rs
@@ -0,0 +1,30 @@
+#![crate_name="foo"]
+
+pub struct Vec;
+
+pub struct Slice;
+
+impl std::ops::Deref for Vec {
+ type Target = Slice;
+ fn deref(&self) -> &Slice {
+ &Slice
+ }
+}
+
+// @has foo/struct.Vec.html '//*[@class="sidebar-elems"]//section//li/a[@href="#method.is_empty"]' \
+// "is_empty"
+impl Vec {
+ pub fn is_empty(&self) -> bool {
+ true
+ }
+}
+
+// @has foo/struct.Vec.html '//*[@class="sidebar-elems"]//section//li/a[@href="#method.is_empty-1"]' \
+// "is_empty"
+// @has foo/struct.Slice.html '//*[@class="sidebar-elems"]//section//li/a[@href="#method.is_empty"]' \
+// "is_empty"
+impl Slice {
+ pub fn is_empty(&self) -> bool {
+ true
+ }
+}
diff --git a/tests/rustdoc/deref/recursive-deref-sidebar.rs b/tests/rustdoc/deref/recursive-deref-sidebar.rs
new file mode 100644
index 00000000000..619f40eff89
--- /dev/null
+++ b/tests/rustdoc/deref/recursive-deref-sidebar.rs
@@ -0,0 +1,22 @@
+use std::ops::Deref;
+
+pub struct A {}
+impl A { pub fn foo_a(&self) {} }
+
+pub struct B {}
+impl B { pub fn foo_b(&self) {} }
+
+pub struct C {}
+impl C { pub fn foo_c(&self) {} }
+
+// @has recursive_deref_sidebar/struct.A.html '//*[@class="sidebar-elems"]//section' 'foo_b'
+impl Deref for A {
+ type Target = B;
+ fn deref(&self) -> &B { todo!() }
+}
+
+// @has recursive_deref_sidebar/struct.A.html '//*[@class="sidebar-elems"]//section' 'foo_c'
+impl Deref for B {
+ type Target = C;
+ fn deref(&self) -> &C { todo!() }
+}
diff --git a/tests/rustdoc/deref/recursive-deref.rs b/tests/rustdoc/deref/recursive-deref.rs
new file mode 100644
index 00000000000..aa38485c445
--- /dev/null
+++ b/tests/rustdoc/deref/recursive-deref.rs
@@ -0,0 +1,120 @@
+use std::ops::Deref;
+
+// Cyclic deref with the parent (which is not the top parent).
+pub struct A;
+pub struct B;
+pub struct C;
+
+impl C {
+ pub fn c(&self) {}
+}
+
+// @has recursive_deref/struct.A.html '//h3[@class="code-header"]' 'impl Deref for A'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.c"]' 'pub fn c(&self)'
+impl Deref for A {
+ type Target = B;
+
+ fn deref(&self) -> &Self::Target {
+ panic!()
+ }
+}
+
+// @has recursive_deref/struct.B.html '//h3[@class="code-header"]' 'impl Deref for B'
+// @has '-' '//*[@class="impl-items"]//*[@id="method.c"]' 'pub fn c(&self)'
+impl Deref for B {
+ type Target = C;
+
+ fn deref(&self) -> &Self::Target {
+ panic!()
+ }
+}
+
+// @has recursive_deref/struct.C.html '//h3[@class="code-header"]' 'impl Deref for C'
+impl Deref for C {
+ type Target = B;
+
+ fn deref(&self) -> &Self::Target {
+ panic!()
+ }
+}
+
+// Cyclic deref with the grand-parent (which is not the top parent).
+pub struct D;
+pub struct E;
+pub struct F;
+pub struct G;
+
+impl G {
+ // There is no "self" parameter so it shouldn't be listed!
+ pub fn g() {}
+}
+
+// @has recursive_deref/struct.D.html '//h3[@class="code-header"]' 'impl Deref for D'
+// We also check that `G::g` method isn't rendered because there is no `self` argument.
+// @!has '-' '//*[@id="deref-methods-G"]' ''
+impl Deref for D {
+ type Target = E;
+
+ fn deref(&self) -> &Self::Target {
+ panic!()
+ }
+}
+
+// @has recursive_deref/struct.E.html '//h3[@class="code-header"]' 'impl Deref for E'
+// We also check that `G::g` method isn't rendered because there is no `self` argument.
+// @!has '-' '//*[@id="deref-methods-G"]' ''
+impl Deref for E {
+ type Target = F;
+
+ fn deref(&self) -> &Self::Target {
+ panic!()
+ }
+}
+
+// @has recursive_deref/struct.F.html '//h3[@class="code-header"]' 'impl Deref for F'
+// We also check that `G::g` method isn't rendered because there is no `self` argument.
+// @!has '-' '//*[@id="deref-methods-G"]' ''
+impl Deref for F {
+ type Target = G;
+
+ fn deref(&self) -> &Self::Target {
+ panic!()
+ }
+}
+
+// @has recursive_deref/struct.G.html '//h3[@class="code-header"]' 'impl Deref for G'
+impl Deref for G {
+ type Target = E;
+
+ fn deref(&self) -> &Self::Target {
+ panic!()
+ }
+}
+
+// Cyclic deref with top parent.
+pub struct H;
+pub struct I;
+
+impl I {
+ // There is no "self" parameter so it shouldn't be listed!
+ pub fn i() {}
+}
+
+// @has recursive_deref/struct.H.html '//h3[@class="code-header"]' 'impl Deref for H'
+// @!has '-' '//*[@id="deref-methods-I"]' ''
+impl Deref for H {
+ type Target = I;
+
+ fn deref(&self) -> &Self::Target {
+ panic!()
+ }
+}
+
+// @has recursive_deref/struct.I.html '//h3[@class="code-header"]' 'impl Deref for I'
+impl Deref for I {
+ type Target = H;
+
+ fn deref(&self) -> &Self::Target {
+ panic!()
+ }
+}