public byte[] OptimizePng(byte[] bytes, int compressionLevel, bool imageQuantizationDisabled) { var optimizedBytes = bytes; if (!imageQuantizationDisabled) { using (var unQuantized = new Bitmap(new MemoryStream(bytes))) { using (var quantized = wuQuantizer.QuantizeImage(unQuantized, 10, 5)) { var memStream = new MemoryStream(); quantized.Save(memStream, ImageFormat.Png); optimizedBytes = memStream.GetBuffer(); } } } if (fileWrapper.FileExists(optiPngLocation)) { var scratchFile = Path.GetTempFileName(); fileWrapper.Save(optimizedBytes, scratchFile); var arg = String.Format(@"-fix -o{1} ""{0}""", scratchFile, compressionLevel); InvokeExecutable(arg, optiPngLocation); optimizedBytes = fileWrapper.GetFileBytes(scratchFile); } return(optimizedBytes); }
public byte[] OptimizePng(byte[] bytes, int compressionLevel, bool imageQuantizationDisabled) { var optimizedBytes = bytes; if (!imageQuantizationDisabled) { using (var unQuantized = new Bitmap(new MemoryStream(bytes))) { using (var quantized = wuQuantizer.QuantizeImage(unQuantized, 10, 5)) { var memStream = new MemoryStream(); quantized.Save(memStream, ImageFormat.Png); optimizedBytes = memStream.GetBuffer(); } } } if (fileWrapper.FileExists(optiPngLocation)) { var scratchFile = string.Format("{0}\\scratch-{1}.png", configuration.SpritePhysicalPath, Hasher.Hash(bytes)); try { fileWrapper.Save(optimizedBytes, scratchFile); var arg = String.Format(@"-fix -o{1} ""{0}""", scratchFile, compressionLevel); InvokeExecutable(arg, optiPngLocation); optimizedBytes = fileWrapper.GetFileBytes(scratchFile); } finally { fileWrapper.DeleteFile(scratchFile); } } return(optimizedBytes); }
public InputValidatorTests() { _fileWrapper = Substitute.For <IFileWrapper>(); _fileWrapper.ClearReceivedCalls(); _fileWrapper.FileExists(Arg.Is(_validWordDictionaryFilePath)).Returns(true); _fileWrapper.IsValidPath(Arg.Is(_validWordLadderFilePath)).Returns(true); _fileWrapper.HasTxtExtension(Arg.Is(_validWordLadderFilePath)).Returns(true); _sut = new InputValidator(_fileWrapper); }
public void Validate_WhenWordDictionaryFilePathDoesNotExists_CallsHandlesErrorsAction() { // Arrange var wordDictionaryFilePath = @"%TEMP%/teste.txt"; var startWord = "ABCD"; var endWord = "WXYZA"; var executing = false; var args = new Options(startWord, endWord, wordDictionaryFilePath, _validWordLadderFilePath); var expectedError = $"Unable to find the specified file {wordDictionaryFilePath}. Please provide an existing word dictionary file."; string actualError = null; _fileWrapper.FileExists(Arg.Is(wordDictionaryFilePath)).Returns(false); // act _sut.Validate(args, (args) => { executing = true; }).HandleErrors((str) => { actualError = str; }); // assert Assert.False(executing); actualError.Should().BeEquivalentTo(expectedError); }
public int CountLinesOfCode(string filePath) { if (string.IsNullOrEmpty(filePath)) { throw new InvalidOperationException("Cannot read from an empty file"); } if (!_wrapper.FileExists(filePath)) { throw new FileNotFoundException("File not found."); } var cleanedFileLines = GetCleanedFileLinesFrom(filePath); var validCodeFileLines = GetValidCodeLines(cleanedFileLines); return(validCodeFileLines.Count()); }
public virtual void Save(byte[] content, string url, string originalUrls) { var file = GetFileNameFromConfig(url); var sig = uriBuilder.ParseSignature(url); var guid = uriBuilder.ParseKey(url); FileWrapper.Save(content, file); if (!url.ToLower().EndsWith(".png") && ReductionRepository != null) { ReductionRepository.AddReduction(guid, url); } RRTracer.Trace("{0} saved to disk.", url); var expiredFile = file.Insert(file.IndexOf(sig, StringComparison.Ordinal), "Expired-"); if (FileWrapper.FileExists(expiredFile)) { FileWrapper.DeleteFile(expiredFile); } }
public void Initialise(Options options) { InitialiseDictionaries(options); _fileWrapper.FileExists(options.WordDictionaryFilePath); using (var sr = _fileWrapper.StreamReader(options.WordDictionaryFilePath)) { var line = ""; while ((line = sr.ReadLine()) != null) { if (line.Length != options.StartWord.Length) { continue; } _dictionaryPreprocessService.CreatePreprocessedDictionaries( new DictionaryPreprocessServiceParams(line, _listOfWords, _listOfPreprocessedWords)); } } }
public void GetPreprocessedWordsDictionary_ReturnsDictionaryOfStringAndCollectionOfStrings() { // Arrange _fileWrapper.FileExists(Arg.Any <string>()); var sb = new StringBuilder(); sb.AppendLine("A"); sb.AppendLine("B"); sb.AppendLine("C"); var wordDictionaryFile = Encoding.UTF8.GetBytes(sb.ToString()); var fakeMemoryStream = new MemoryStream(wordDictionaryFile); StreamReader stream = null; _fileWrapper.StreamReader(Arg.Any <string>()) .Returns(stream = new WordDictionaryStreamReader(fakeMemoryStream)); _dictionaryPreprocessService .When(service => service.CreatePreprocessedDictionaries(Arg.Is( new DictionaryPreprocessServiceParams("A", new Dictionary <string, bool>(), new Dictionary <string, ICollection <string> >())))) .Do(d => d.ArgAt <DictionaryPreprocessServiceParams>(0).ListOfPreprocessedWords .Add("*", new List <string>() { "A", "B", "C" })); // Act var result = _sut.GetPreprocessedWordsDictionary(); // Assert result.Keys.Should().ContainItemsAssignableTo <string>(); result.Values.Should().ContainItemsAssignableTo <IEnumerable <string> >(); stream.Dispose(); }
public LineCounterTestBuilder FileDoesNotExist(string filePath) { _fileWrapper.FileExists(filePath).Returns(false); return(this); }