MSTestExtensions/FileAssert.cs
/// <summary>
/// Asserts that a file can be read using a specific encoding.
/// </summary>
/// <param name="assert"><see cref="Assert.That"/></param>
/// <param name="path">The path to the file.</param>
/// <param name="encoding">The encoding to check the file against.</param>
Asserts that a file can be read using a specific encoding.
Asserts that a file can be read using a specific encoding .
public static void FileUsesEncoding(this Assert assert, string path, Encoding encoding)
        {
            FileExists(assert, path);
            Assert.IsNotNull(encoding, $"Attempted to call {nameof(FileAssert)}.{nameof(FileUsesEncoding)} with null encoding.");
            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                var throwingEncoding = Encoding.GetEncoding(encoding.CodePage,
                    EncoderFallback.ExceptionFallback,
                    DecoderFallback.ExceptionFallback
                );
                try
                {
                    using (var reader = new StreamReader(stream, throwingEncoding, detectEncodingFromByteOrderMarks: false))
                    {
                        string line = reader.ReadLine();
                        while (line != null)
                        {
                            line = reader.ReadLine();
                        }
                    }
                }
                catch (DecoderFallbackException ex)
                {
                    throw new AssertFailedException(
                        $"File is not in the encoding {encoding.EncodingName}",
                        ex
                    );
                }
            }
        }
public static void FileUsesEncoding ( this Assert assert , string path , Encoding encoding ) { FileExists ( assert , path ) ; Assert . IsNotNull ( encoding , $" Attempted to call  { nameof ( FileAssert ) } . { nameof ( FileUsesEncoding ) }  with null encoding. " ) ; using ( var stream = new FileStream ( path , FileMode . Open , FileAccess . Read ) ) { var throwingEncoding = Encoding . GetEncoding ( encoding . CodePage , EncoderFallback . ExceptionFallback , DecoderFallback . ExceptionFallback ) ; try { using ( var reader = new StreamReader ( stream , throwingEncoding , detectEncodingFromByteOrderMarks : false ) ) { string line = reader . ReadLine ( ) ; while ( line != null ) { line = reader . ReadLine ( ) ; } } } catch ( DecoderFallbackException ex ) { throw new AssertFailedException ( $" File is not in the encoding  { encoding . EncodingName } " , ex ) ; } } }
Asserts that a file can be read using a specific encoding.
Asserts that a file can be read using a specific encoding .
// get a copy of the encoding that throws on un-mappable characters. // just read the lines - if there's no exception, then we're good.
Assert
this
path
string
The path to the file.
The path to the file .
encoding
Encoding
The encoding to check the file against.
The encoding to check the file against .
assert
None