コード例 #1
0
        public void WriteFlushesToStreamButDoesntCloseItIfHadToCreateItsOwnWriter()
        {
            SparkCodec sparkCodec = new SparkCodecBuilder().Build();
            var httpEntity = MockRepository.GenerateStub<IHttpEntity>();
            var streamToWrite = MockRepository.GenerateStub<Stream>();
            streamToWrite.Stub(x => x.CanWrite).Return(true);
            httpEntity.Stub(x => x.Stream).Return(streamToWrite);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            streamToWrite.AssertWasCalled(x => x.Flush());
            streamToWrite.AssertWasNotCalled(x => x.Close());
        }
コード例 #2
0
        public void WriteFlushesToStreamButDoesntCloseItIfHadToCreateItsOwnWriter()
        {
            SparkCodec sparkCodec    = new SparkCodecBuilder().Build();
            var        httpEntity    = MockRepository.GenerateStub <IHttpEntity>();
            var        streamToWrite = MockRepository.GenerateStub <Stream>();

            streamToWrite.Stub(x => x.CanWrite).Return(true);
            httpEntity.Stub(x => x.Stream).Return(streamToWrite);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            streamToWrite.AssertWasCalled(x => x.Flush());
            streamToWrite.AssertWasNotCalled(x => x.Close());
        }
コード例 #3
0
        public void WriteToPassesViewDataToRender()
        {
            var renderer   = MockRepository.GenerateStub <ISparkRenderer>();
            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var viewData = new object();

            sparkCodec.WriteTo(viewData, MockRepository.GenerateStub <IHttpEntitySupportingTextWriter>(), new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                                         Arg <object> .Is.Equal(viewData),
                                         Arg <TextWriter> .Is.Anything,
                                         Arg <Dictionary <string, string> > .Is.Anything
                                         ));
        }
コード例 #4
0
        public void WriteCreatesDeterministicWriterWithCorrectEncoding()
        {
            const string funnyCharacters = "úĀąü";
            ISparkRenderer renderer = new StubSparkRendererThatWritesStuff(funnyCharacters);
            SparkCodec sparkCodec = new SparkCodecBuilder().With(renderer).Build();
            var httpEntity = MockRepository.GenerateStub<IHttpEntity>();
            var stream = new MemoryStream();
            Assert.That(stream.CanRead, Is.True);
            httpEntity.Stub(x => x.Stream).Return(stream);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream, new UTF8Encoding());
            Assert.That(reader.ReadToEnd(), Is.EqualTo(funnyCharacters));
        }
コード例 #5
0
        public void WriteWritesToStreamIfHttpEntityDoesNotSupportTextWriter()
        {
            const string expectedContents = "abcdefg";
            var          renderer         = new StubSparkRendererThatWritesStuff(expectedContents);
            SparkCodec   sparkCodec       = new SparkCodecBuilder().With(renderer).Build();

            var httpEntity = MockRepository.GenerateStub <IHttpEntity>();
            var stream     = new MemoryStream();

            httpEntity.Stub(x => x.Stream).Return(stream);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream);

            Assert.That(reader.ReadToEnd(), Is.EqualTo(expectedContents));
        }
コード例 #6
0
        public void WritePassesConfigObjectAsDictionaryToRender()
        {
            var renderer   = MockRepository.GenerateStub <ISparkRenderer>();
            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var configData = new{ foo = "bar", bat = 123 };
            IDictionary <string, string> expectedConfiguration = configData.ToCaseInvariantDictionary();

            sparkCodec.Configuration = configData;

            sparkCodec.WriteTo(new object(), MockRepository.GenerateStub <IHttpEntitySupportingTextWriter>(), new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                                         Arg <object> .Is.Anything,
                                         Arg <TextWriter> .Is.Anything,
                                         Arg <Dictionary <string, string> > .Is.Equal(expectedConfiguration)
                                         ));
        }
コード例 #7
0
        public void WriteCreatesDeterministicWriterWithCorrectEncoding()
        {
            const string   funnyCharacters = "úĀąü";
            ISparkRenderer renderer        = new StubSparkRendererThatWritesStuff(funnyCharacters);
            SparkCodec     sparkCodec      = new SparkCodecBuilder().With(renderer).Build();
            var            httpEntity      = MockRepository.GenerateStub <IHttpEntity>();
            var            stream          = new MemoryStream();

            Assert.That(stream.CanRead, Is.True);
            httpEntity.Stub(x => x.Stream).Return(stream);

            sparkCodec.WriteTo(null, httpEntity, new[] { "a", "b" });

            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream, new UTF8Encoding());

            Assert.That(reader.ReadToEnd(), Is.EqualTo(funnyCharacters));
        }
コード例 #8
0
        public void WriteToUsesExistingTextWriterIfHttpEntitySupportsTextWriter()
        {
            var renderer           = MockRepository.GenerateStub <ISparkRenderer>();
            var textWriter         = new StringWriter();
            var supportsTextWriter = MockRepository.GenerateStub <IHttpEntitySupportingTextWriter>();

            supportsTextWriter.Stub(x => x.TextWriter).Return(textWriter);

            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            sparkCodec.Configuration = new object();

            sparkCodec.WriteTo(new object(), supportsTextWriter, new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                                         Arg <object> .Is.Anything,
                                         Arg <TextWriter> .Is.Equal(textWriter),
                                         Arg <Dictionary <string, string> > .Is.Anything
                                         ));
        }
コード例 #9
0
        public void WritePassesConfigObjectAsDictionaryToRender()
        {
            var renderer = MockRepository.GenerateStub<ISparkRenderer>();
            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var configData = new{foo="bar",bat=123};
            IDictionary<string, string> expectedConfiguration = configData.ToCaseInvariantDictionary();
            sparkCodec.Configuration = configData;

            sparkCodec.WriteTo(new object(), MockRepository.GenerateStub<IHttpEntitySupportingTextWriter>(), new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                Arg<object>.Is.Anything,
                Arg<TextWriter>.Is.Anything,
                Arg<Dictionary<string, string>>.Is.Equal(expectedConfiguration)
                ));
        }
コード例 #10
0
        public void WriteWritesToStreamIfHttpEntityDoesNotSupportTextWriter()
        {
            const string expectedContents = "abcdefg";
            var renderer = new StubSparkRendererThatWritesStuff(expectedContents);
            SparkCodec sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var httpEntity = MockRepository.GenerateStub<IHttpEntity>();
            var stream = new MemoryStream();
            httpEntity.Stub(x => x.Stream).Return(stream);

            sparkCodec.WriteTo(null, httpEntity, new[]{"a", "b"});

            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream);
            Assert.That(reader.ReadToEnd(), Is.EqualTo(expectedContents));
        }
コード例 #11
0
        public void WriteToUsesExistingTextWriterIfHttpEntitySupportsTextWriter()
        {
            var renderer = MockRepository.GenerateStub<ISparkRenderer>();
            var textWriter = new StringWriter();
            var supportsTextWriter = MockRepository.GenerateStub<IHttpEntitySupportingTextWriter>();
            supportsTextWriter.Stub(x => x.TextWriter).Return(textWriter);

            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();
            sparkCodec.Configuration = new object();

            sparkCodec.WriteTo(new object(), supportsTextWriter, new[] {"A", "B"});

            renderer.AssertWasCalled(x => x.Render(
                Arg<object>.Is.Anything,
                Arg<TextWriter>.Is.Equal(textWriter),
                Arg<Dictionary<string,string>>.Is.Anything
                ));
        }
コード例 #12
0
        public void WriteToPassesViewDataToRender()
        {
            var renderer = MockRepository.GenerateStub<ISparkRenderer>();
            var sparkCodec = new SparkCodecBuilder().With(renderer).Build();

            var viewData = new object();

            sparkCodec.WriteTo(viewData, MockRepository.GenerateStub<IHttpEntitySupportingTextWriter>(), new[] { "A", "B" });

            renderer.AssertWasCalled(x => x.Render(
                Arg<object>.Is.Equal(viewData),
                Arg<TextWriter>.Is.Anything,
                Arg<Dictionary<string, string>>.Is.Anything
                ));
        }