Permalink
Browse files

Added a fluent interface for chaining together steams

  • Loading branch information...
1 parent e47c99d commit db60479d0f22205b634b4037d68919f90c93163f @jamesfoster committed Feb 2, 2012
View
@@ -0,0 +1,52 @@
+namespace Streams
+{
+ using System;
+ using System.Collections.Generic;
+ using System.IO;
+ using System.Text;
+ using System.Text.RegularExpressions;
+
+ public static class Fluent
+ {
+ public static Base64DecoderStream Base64Decoder(this Stream stream,
+ Base64DecodeMode whitespaces = Base64DecodeMode.IgnoreWhiteSpaces)
+ {
+ return new Base64DecoderStream(stream, whitespaces);
+ }
+
+ public static Base64EncoderStream Base64Encoder(this Stream stream)
+ {
+ return new Base64EncoderStream(stream);
+ }
+
+ public static RegexFindReplaceStream RegexFindReplace(this Stream stream,
+ IDictionary<string, string> replacements,
+ int maxMatchLength = 4096,
+ Encoding encoding = null)
+ {
+ if (encoding == null) encoding = Encoding.UTF8;
+
+ return new RegexFindReplaceStream(stream, replacements, maxMatchLength, encoding);
+ }
+
+ public static RegexFindReplaceStream RegexFindReplace(this Stream stream,
+ IDictionary<Regex, string> replacements,
+ int maxMatchLength = 4096,
+ Encoding encoding = null)
+ {
+ if (encoding == null) encoding = Encoding.UTF8;
+
+ return new RegexFindReplaceStream(stream, replacements, maxMatchLength, encoding);
+ }
+
+ public static RegexFindReplaceStream RegexFindReplace(this Stream stream,
+ IDictionary<Regex, Func<Match, string>> replacements,
+ int maxMatchLength = 4096,
+ Encoding encoding = null)
+ {
+ if (encoding == null) encoding = Encoding.UTF8;
+
+ return new RegexFindReplaceStream(stream, replacements, maxMatchLength, encoding);
+ }
+ }
+}
View
@@ -42,6 +42,7 @@
<ItemGroup>
<Compile Include="Base64DecoderStream.cs" />
<Compile Include="Base64EncoderStream.cs" />
+ <Compile Include="Fluent.cs" />
<Compile Include="RegexFindReplaceStream.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
@@ -0,0 +1,23 @@
+namespace StreamsTest.FluentTests
+{
+ using System.IO;
+ using Machine.Specifications;
+ using Streams;
+
+ public class When_creating_a_Base64Decoder
+ {
+ static MemoryStream innerStream;
+ static Base64DecoderStream base64Decoder;
+
+ Establish context = () =>
+ {
+ innerStream = new MemoryStream();
+ };
+
+ Because of = () => { base64Decoder = innerStream.Base64Decoder(); };
+
+ It should_not_be_null = () => base64Decoder.ShouldNotBeNull();
+ It should_contain_the_inner_stream = () => base64Decoder.InnerStream.ShouldBeTheSameAs(innerStream);
+ It should_ignore_whitespaces = () => base64Decoder.Whitespaces.ShouldEqual(Base64DecodeMode.IgnoreWhiteSpaces);
+ }
+}
@@ -0,0 +1,23 @@
+namespace StreamsTest.FluentTests
+{
+ using System.IO;
+ using Machine.Specifications;
+ using Streams;
+
+ public class When_creating_a_Base64Decoder_not_ignoring_whitespace
+ {
+ static MemoryStream innerStream;
+ static Base64DecoderStream base64Decoder;
+
+ Establish context = () =>
+ {
+ innerStream = new MemoryStream();
+ };
+
+ Because of = () => { base64Decoder = innerStream.Base64Decoder(Base64DecodeMode.DoNotIgnoreWhiteSpaces); };
+
+ It should_not_be_null = () => base64Decoder.ShouldNotBeNull();
+ It should_contain_the_inner_stream = () => base64Decoder.InnerStream.ShouldBeTheSameAs(innerStream);
+ It should_not_ignore_whitespaces = () => base64Decoder.Whitespaces.ShouldEqual(Base64DecodeMode.DoNotIgnoreWhiteSpaces);
+ }
+}
@@ -0,0 +1,22 @@
+namespace StreamsTest.FluentTests
+{
+ using System.IO;
+ using Machine.Specifications;
+ using Streams;
+
+ public class When_creating_a_Base64Encoder
+ {
+ static MemoryStream innerStream;
+ static Base64EncoderStream base64Encoder;
+
+ Establish context = () =>
+ {
+ innerStream = new MemoryStream();
+ };
+
+ Because of = () => { base64Encoder = innerStream.Base64Encoder(); };
+
+ It should_not_be_null = () => base64Encoder.ShouldNotBeNull();
+ It should_contain_the_inner_stream = () => base64Encoder.InnerStream.ShouldBeTheSameAs(innerStream);
+ }
+}
@@ -0,0 +1,39 @@
+namespace StreamsTest.FluentTests
+{
+ using System;
+ using System.Collections.Generic;
+ using System.IO;
+ using System.Text;
+ using System.Text.RegularExpressions;
+ using Machine.Specifications;
+ using Streams;
+
+ public class When_creating_a_RegexFindReplace
+ {
+ static MemoryStream innerStream;
+ static RegexFindReplaceStream regexFindReplace;
+ static Dictionary<Regex, Func<Match, string>> replacements;
+ static int maxMatchLength;
+ static Encoding encoding;
+
+ Establish context = () =>
+ {
+ innerStream = new MemoryStream();
+ replacements = new Dictionary<Regex, Func<Match, string>>
+ {
+ {new Regex("abc"), m => "xyz"},
+ {new Regex("def"), m => "uvw"}
+ };
+ maxMatchLength = 1234;
+ encoding = Encoding.UTF8;
+ };
+
+ Because of = () => { regexFindReplace = innerStream.RegexFindReplace(replacements, maxMatchLength, encoding); };
+
+ It should_not_be_null = () => regexFindReplace.ShouldNotBeNull();
+ It should_contain_the_inner_stream = () => regexFindReplace.InnerStream.ShouldBeTheSameAs(innerStream);
+ It should_contain_the_specified_replacements = () => regexFindReplace.Replacements.ShouldBeTheSameAs(replacements);
+ It should_have_correct_maxMatchLength = () => regexFindReplace.MaxMatchLength.ShouldEqual(maxMatchLength);
+ It should_have_correct_Encoding = () => regexFindReplace.Encoding.ShouldEqual(encoding);
+ }
+}
@@ -0,0 +1,47 @@
+namespace StreamsTest.FluentTests
+{
+ using System.Collections.Generic;
+ using System.IO;
+ using System.Linq;
+ using System.Text;
+ using System.Text.RegularExpressions;
+ using Machine.Specifications;
+ using Streams;
+
+ public class When_creating_a_RegexFindReplace2
+ {
+ static MemoryStream innerStream;
+ static RegexFindReplaceStream regexFindReplace;
+ static Dictionary<Regex, string> replacements;
+ static int maxMatchLength;
+ static Encoding encoding;
+
+ Establish context = () =>
+ {
+ innerStream = new MemoryStream();
+ replacements = new Dictionary<Regex, string>
+ {
+ {new Regex("abc"), "xyz"},
+ {new Regex("def"), "uvw"}
+ };
+ maxMatchLength = 1234;
+ encoding = Encoding.UTF8;
+ };
+
+ Because of = () => { regexFindReplace = innerStream.RegexFindReplace(replacements, maxMatchLength, encoding); };
+
+ It should_not_be_null = () => regexFindReplace.ShouldNotBeNull();
+ It should_contain_the_inner_stream = () => regexFindReplace.InnerStream.ShouldBeTheSameAs(innerStream);
+ It should_have_correct_maxMatchLength = () => regexFindReplace.MaxMatchLength.ShouldEqual(maxMatchLength);
+ It should_have_correct_Encoding = () => regexFindReplace.Encoding.ShouldEqual(encoding);
+
+ It should_contain_1_replacement = () =>
+ regexFindReplace.Replacements.Count.ShouldEqual(2);
+
+ It replacement_1_should_have_correct_Regex = () =>
+ regexFindReplace.Replacements.ElementAt(0).Key.ToString().ShouldEqual("abc");
+
+ It replacement_2_should_have_correct_Regex = () =>
+ regexFindReplace.Replacements.ElementAt(1).Key.ToString().ShouldEqual("def");
+ }
+}
@@ -0,0 +1,46 @@
+namespace StreamsTest.FluentTests
+{
+ using System.Collections.Generic;
+ using System.IO;
+ using System.Linq;
+ using System.Text;
+ using Machine.Specifications;
+ using Streams;
+
+ public class When_creating_a_RegexFindReplace3
+ {
+ static MemoryStream innerStream;
+ static RegexFindReplaceStream regexFindReplace;
+ static Dictionary<string, string> replacements;
+ static int maxMatchLength;
+ static Encoding encoding;
+
+ Establish context = () =>
+ {
+ innerStream = new MemoryStream();
+ replacements = new Dictionary<string, string>
+ {
+ {"abc", "xyz"},
+ {"def", "uvw"}
+ };
+ maxMatchLength = 1234;
+ encoding = Encoding.UTF8;
+ };
+
+ Because of = () => { regexFindReplace = innerStream.RegexFindReplace(replacements, maxMatchLength, encoding); };
+
+ It should_not_be_null = () => regexFindReplace.ShouldNotBeNull();
+ It should_contain_the_inner_stream = () => regexFindReplace.InnerStream.ShouldBeTheSameAs(innerStream);
+ It should_have_correct_maxMatchLength = () => regexFindReplace.MaxMatchLength.ShouldEqual(maxMatchLength);
+ It should_have_correct_Encoding = () => regexFindReplace.Encoding.ShouldEqual(encoding);
+
+ It should_contain_1_replacement = () =>
+ regexFindReplace.Replacements.Count.ShouldEqual(2);
+
+ It replacement_1_should_have_correct_Regex = () =>
+ regexFindReplace.Replacements.ElementAt(0).Key.ToString().ShouldEqual("abc");
+
+ It replacement_2_should_have_correct_Regex = () =>
+ regexFindReplace.Replacements.ElementAt(1).Key.ToString().ShouldEqual("def");
+ }
+}
@@ -61,6 +61,12 @@
<Compile Include="FindReplaceStreamTests\When_reading_1_byte_at_a_time.cs" />
<Compile Include="FindReplaceStreamTests\When_reading_3_bytes_at_a_time.cs" />
<Compile Include="FindReplaceStreamTests\When_reading_5_bytes_at_a_time.cs" />
+ <Compile Include="FluentTests\When_creating_a_Base64Decoder.cs" />
+ <Compile Include="FluentTests\When_creating_a_Base64Decoder_with_whitespace_mode.cs" />
+ <Compile Include="FluentTests\When_creating_a_Base64Encoder.cs" />
+ <Compile Include="FluentTests\When_creating_a_RegexFindReplace.cs" />
+ <Compile Include="FluentTests\When_creating_a_RegexFindReplace2.cs" />
+ <Compile Include="FluentTests\When_creating_a_RegexFindReplace3.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
@@ -72,6 +78,7 @@
<Name>Streams</Name>
</ProjectReference>
</ItemGroup>
+ <ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

0 comments on commit db60479

Please sign in to comment.