Exemplo n.º 1
0
        public void Configure(IApplicationBuilder app)
        {
            app.Run(async context =>
            {
                if (context.Request.Method.Equals("GET", StringComparison.OrdinalIgnoreCase))
                {
                    using (var writer = new StreamWriter(context.Response.Body))
                    {
                        await writer.WriteAsync("Welcome to Pig Latin text converter. Send the text to convert in a POST request.".AsMemory(), context.RequestAborted);
                    }
                }

                if (context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
                {
                    context.Response.ContentType = "text/plain";
                    using (var outStream = context.Response.Body)
                    {
                        using (var converter = new StreamWordConverter(context.Request.Body, PigLatinWordConverter.Convert, CharExtensions.IsWordSeparator, CharExtensions.IsNotLatinLetter))
                        {
                            await converter.ConvertTo(outStream, context.RequestAborted);
                        }
                    }
                }
            });
        }
Exemplo n.º 2
0
 private static void Run(PigLatinCliArgs args)
 {
     using (var outStream = File.OpenWrite(args.OutputFilePath))
     {
         using (var converter = new StreamWordConverter(File.OpenRead(args.InputFilePath), PigLatinWordConverter.Convert, CharExtensions.IsWordSeparator, CharExtensions.IsNotLatinLetter))
         {
             converter.ConvertTo(outStream, CancellationToken.None).GetAwaiter().GetResult();
         }
     }
 }
Exemplo n.º 3
0
        public async Task StreamWordConverter_WithDegenerateConverter(string fileName)
        {
            string path            = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"Resources\\{fileName}");
            string text            = File.ReadAllText(path);
            var    converter       = new StreamWordConverter(new MemoryStream(Encoding.UTF8.GetBytes(text)), s => s, CharExtensions.IsWordSeparator, CharExtensions.IsNotLatinLetter);
            var    convertedStream = new MemoryStream();
            await converter.ConvertTo(convertedStream, CancellationToken.None);

            convertedStream.Position = 0;
            var convertedText = await new StreamReader(convertedStream).ReadToEndAsync();

            File.WriteAllText(path + ".pig", convertedText);
            Assert.AreEqual(text, convertedText);
        }