|
39 | 39 | /// Convert the `CowStr` into an owned `String<N, LenT>`.
|
40 | 40 | ///
|
41 | 41 | /// This uses `String::try_from(&str)` and will `panic!` on capacity overflow.
|
42 |
| - /// If you prefer a fallible API, we can add `try_into_owned()` that returns `Result`. |
43 | 42 | pub fn to_owned(&self) -> String<N, LenT> {
|
44 | 43 | match self {
|
45 | 44 | CowStr::Borrowed(sv) => {
|
@@ -113,3 +112,83 @@ where
|
113 | 112 | self.as_str()
|
114 | 113 | }
|
115 | 114 | }
|
| 115 | + |
| 116 | +// ---------------------- UNIT TESTS ---------------------- |
| 117 | +#[cfg(test)] |
| 118 | +mod tests { |
| 119 | + use super::*; |
| 120 | + |
| 121 | + // Helper function to create a StringView from a static str |
| 122 | + fn sv_from_str(s: &'static str) -> StringView<usize> { |
| 123 | + StringView::new(s) |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn test_borrowed_variant() { |
| 128 | + let view = sv_from_str("hello"); |
| 129 | + let cow: CowStr<16> = CowStr::Borrowed(&view); |
| 130 | + |
| 131 | + assert!(cow.is_borrowed()); |
| 132 | + assert!(!cow.is_static()); |
| 133 | + assert!(!cow.is_owned()); |
| 134 | + assert_eq!(cow.as_str(), "hello"); |
| 135 | + |
| 136 | + let owned = cow.to_owned(); |
| 137 | + assert_eq!(owned.as_str(), "hello"); |
| 138 | + } |
| 139 | + |
| 140 | + #[test] |
| 141 | + fn test_static_variant() { |
| 142 | + static VIEW: StringView<usize> = StringView::new("world"); |
| 143 | + let cow: CowStr<16> = CowStr::from_static(&VIEW); |
| 144 | + |
| 145 | + assert!(!cow.is_borrowed()); |
| 146 | + assert!(cow.is_static()); |
| 147 | + assert!(!cow.is_owned()); |
| 148 | + assert_eq!(cow.as_str(), "world"); |
| 149 | + |
| 150 | + let owned = cow.to_owned(); |
| 151 | + assert_eq!(owned.as_str(), "world"); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + fn test_owned_variant() { |
| 156 | + let s: String<16> = String::try_from("heapless").unwrap(); |
| 157 | + let cow: CowStr<16> = CowStr::Owned(s.clone()); |
| 158 | + |
| 159 | + assert!(!cow.is_borrowed()); |
| 160 | + assert!(!cow.is_static()); |
| 161 | + assert!(cow.is_owned()); |
| 162 | + assert_eq!(cow.as_str(), "heapless"); |
| 163 | + |
| 164 | + let owned = cow.to_owned(); |
| 165 | + assert_eq!(owned.as_str(), "heapless"); |
| 166 | + } |
| 167 | + |
| 168 | + #[test] |
| 169 | + fn test_from_stringview() { |
| 170 | + let view = sv_from_str("from_borrowed"); |
| 171 | + let cow: CowStr<16> = CowStr::from(&view); |
| 172 | + |
| 173 | + assert!(cow.is_borrowed()); |
| 174 | + assert_eq!(cow.as_str(), "from_borrowed"); |
| 175 | + } |
| 176 | + |
| 177 | + #[test] |
| 178 | + fn test_from_string() { |
| 179 | + let s: String<16> = String::try_from("from_owned").unwrap(); |
| 180 | + let cow: CowStr<16> = CowStr::from(s.clone()); |
| 181 | + |
| 182 | + assert!(cow.is_owned()); |
| 183 | + assert_eq!(cow.as_str(), "from_owned"); |
| 184 | + } |
| 185 | + |
| 186 | + #[test] |
| 187 | + fn test_borrow_trait() { |
| 188 | + let s: String<16> = String::try_from("borrow_trait").unwrap(); |
| 189 | + let cow: CowStr<16> = CowStr::Owned(s); |
| 190 | + |
| 191 | + let b: &str = cow.borrow(); |
| 192 | + assert_eq!(b, "borrow_trait"); |
| 193 | + } |
| 194 | +} |
0 commit comments