about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNick Cameron <nrc@ncameron.org>2022-04-18 12:37:54 +0100
committerNick Cameron <nrc@ncameron.org>2022-06-06 12:10:14 +0100
commit843f90cbb72758a091db25653ac515098f18399d (patch)
tree548faa45ebbace0fa04f71f01d583611e90ed0ec
parent2e0ca2537f0f8549e5b24ff7d0b849b61aba7414 (diff)
downloadrust-843f90cbb72758a091db25653ac515098f18399d.tar.gz
rust-843f90cbb72758a091db25653ac515098f18399d.zip
Add some more tests
Signed-off-by: Nick Cameron <nrc@ncameron.org>
-rw-r--r--library/core/tests/any.rs44
1 files changed, 42 insertions, 2 deletions
diff --git a/library/core/tests/any.rs b/library/core/tests/any.rs
index 14a2b09c95e..cdc6fadbab7 100644
--- a/library/core/tests/any.rs
+++ b/library/core/tests/any.rs
@@ -138,12 +138,15 @@ struct SomeConcreteType {
 }
 
 impl Provider for SomeConcreteType {
-    fn provide<'a>(&'a self, req: &mut Demand<'a>) {
-        req.provide_ref::<String>(&self.some_string)
+    fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
+        demand
+            .provide_ref::<String>(&self.some_string)
+            .provide_ref::<str>(&self.some_string)
             .provide_value::<String, _>(|| "bye".to_owned());
     }
 }
 
+// Test the provide and request mechanisms with a by-reference trait object.
 #[test]
 fn test_provider() {
     let obj: &dyn Provider = &SomeConcreteType { some_string: "hello".to_owned() };
@@ -152,3 +155,40 @@ fn test_provider() {
     assert_eq!(&*request_value::<String, _>(obj).unwrap(), "bye");
     assert_eq!(request_value::<u8, _>(obj), None);
 }
+
+// Test the provide and request mechanisms with a boxed trait object.
+#[test]
+fn test_provider_boxed() {
+    let obj: Box<dyn Provider> = Box::new(SomeConcreteType { some_string: "hello".to_owned() });
+
+    assert_eq!(&**request_ref::<String, _>(&*obj).unwrap(), "hello");
+    assert_eq!(&*request_value::<String, _>(&*obj).unwrap(), "bye");
+    assert_eq!(request_value::<u8, _>(&*obj), None);
+}
+
+// Test the provide and request mechanisms with a concrete object.
+#[test]
+fn test_provider_concrete() {
+    let obj = SomeConcreteType { some_string: "hello".to_owned() };
+
+    assert_eq!(&**request_ref::<String, _>(&obj).unwrap(), "hello");
+    assert_eq!(&*request_value::<String, _>(&obj).unwrap(), "bye");
+    assert_eq!(request_value::<u8, _>(&obj), None);
+}
+
+trait OtherTrait: Provider {}
+
+impl OtherTrait for SomeConcreteType {}
+
+impl dyn OtherTrait {
+    fn get_ref<T: 'static + ?Sized>(&self) -> Option<&T> {
+        request_ref::<T, _>(self)
+    }
+}
+
+// Test the provide and request mechanisms via an intermediate trait.
+#[test]
+fn test_provider_intermediate() {
+    let obj: &dyn OtherTrait = &SomeConcreteType { some_string: "hello".to_owned() };
+    assert_eq!(obj.get_ref::<str>().unwrap(), "hello");
+}