public void TestDetectNoPreamble() { using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream, Encoding.ASCII)) { writer.Write("Controversial opinion: White House Down is a better movie than Olympus has Fallen, ikr"); writer.Flush(); stream.Position = 0; var detector = new EncodingDetector(); detector.TryFindEncoding(stream).Should().Be(null, "because there's no preamble to detect ASCII and the detector shall not make any guesses and give up"); } }
public void TestDetect([ValueSource(nameof(EncodingsWithPreamble))] Encoding encoding) { var preamble = encoding.GetPreamble(); preamble.Length.Should().BeGreaterOrEqualTo(1, "because this test doesn't make much sense if the encoding has no preamble"); using (var stream = new MemoryStream()) using (var writer = new StreamWriter(stream, encoding)) { writer.Write("Controversial opinion: White House Down is a better movie than Olympus has Fallen, ikr"); writer.Flush(); stream.Position = 0; var detector = new EncodingDetector(); detector.TryFindEncoding(stream).Should().Be(encoding); } }
private Encoding DetectEncoding(string fileName) { var detector = new EncodingDetector(); return(detector.TryFindEncoding(fileName) ?? Encoding.UTF8); }