@@ -199,4 +199,104 @@ Describe -Name $function -Fixture {
199199 $assertion.Exception.InnerException.Message | Should - BeIn @ (' Cannot access a closed Stream.' , ' Stream was not readable.' )
200200 }
201201 }
202+
203+ Context - Name ' Encoding' - Fixture {
204+ It - Name ' Uses UTF8 encoding by default' - Test {
205+ $string = ' Hello World'
206+ $bytes = [System.Text.Encoding ]::UTF8.GetBytes($string )
207+ $stream = [System.IO.MemoryStream ]::new($bytes )
208+
209+ $assertion = ConvertFrom-MemoryStreamToString - Stream $stream
210+
211+ $assertion | Should - BeExactly $string
212+ $stream.Dispose ()
213+ }
214+
215+ It - Name ' Converts ASCII encoded stream' - Test {
216+ $string = ' Hello World'
217+ $bytes = [System.Text.Encoding ]::ASCII.GetBytes($string )
218+ $stream = [System.IO.MemoryStream ]::new($bytes )
219+
220+ $assertion = ConvertFrom-MemoryStreamToString - Stream $stream - Encoding ASCII
221+
222+ $assertion | Should - BeExactly $string
223+ $stream.Dispose ()
224+ }
225+
226+ It - Name ' Converts Unicode (UTF-16 LE) encoded stream' - Test {
227+ $string = ' Hello World'
228+ $bytes = [System.Text.Encoding ]::Unicode.GetBytes($string )
229+ $stream = [System.IO.MemoryStream ]::new($bytes )
230+
231+ $assertion = ConvertFrom-MemoryStreamToString - Stream $stream - Encoding Unicode
232+
233+ $assertion | Should - BeExactly $string
234+ $stream.Dispose ()
235+ }
236+
237+ It - Name ' Converts BigEndianUnicode (UTF-16 BE) encoded stream' - Test {
238+ $string = ' Hello World'
239+ $bytes = [System.Text.Encoding ]::BigEndianUnicode.GetBytes($string )
240+ $stream = [System.IO.MemoryStream ]::new($bytes )
241+
242+ $assertion = ConvertFrom-MemoryStreamToString - Stream $stream - Encoding BigEndianUnicode
243+
244+ $assertion | Should - BeExactly $string
245+ $stream.Dispose ()
246+ }
247+
248+ It - Name ' Converts UTF32 encoded stream' - Test {
249+ $string = ' Hello World'
250+ $bytes = [System.Text.Encoding ]::UTF32.GetBytes($string )
251+ $stream = [System.IO.MemoryStream ]::new($bytes )
252+
253+ $assertion = ConvertFrom-MemoryStreamToString - Stream $stream - Encoding UTF32
254+
255+ $assertion | Should - BeExactly $string
256+ $stream.Dispose ()
257+ }
258+
259+ It - Name ' Converts UTF8 encoded stream explicitly' - Test {
260+ $string = ' Hello World'
261+ $bytes = [System.Text.Encoding ]::UTF8.GetBytes($string )
262+ $stream = [System.IO.MemoryStream ]::new($bytes )
263+
264+ $assertion = ConvertFrom-MemoryStreamToString - Stream $stream - Encoding UTF8
265+
266+ $assertion | Should - BeExactly $string
267+ $stream.Dispose ()
268+ }
269+
270+ It - Name ' Supports -Encoding with pipeline input' - Test {
271+ $string = ' Hello World'
272+ $bytes = [System.Text.Encoding ]::Unicode.GetBytes($string )
273+ $stream = [System.IO.MemoryStream ]::new($bytes )
274+
275+ $assertion = $stream | ConvertFrom-MemoryStreamToString - Encoding Unicode
276+
277+ $assertion | Should - BeExactly $string
278+ $stream.Dispose ()
279+ }
280+
281+ It - Name ' Handles special characters with Unicode encoding' - Test {
282+ $string = ' Hello 世界 🌍'
283+ $bytes = [System.Text.Encoding ]::Unicode.GetBytes($string )
284+ $stream = [System.IO.MemoryStream ]::new($bytes )
285+
286+ $assertion = ConvertFrom-MemoryStreamToString - Stream $stream - Encoding Unicode
287+
288+ $assertion | Should - BeExactly $string
289+ $stream.Dispose ()
290+ }
291+
292+ It - Name ' Rejects invalid encoding name' - Test {
293+ $string = ' Hello World'
294+ $bytes = [System.Text.Encoding ]::UTF8.GetBytes($string )
295+ $stream = [System.IO.MemoryStream ]::new($bytes )
296+
297+ { ConvertFrom-MemoryStreamToString - Stream $stream - Encoding InvalidEncoding } | Should - Throw
298+
299+ $stream.Dispose ()
300+ }
301+ }
202302}
0 commit comments