~ubuntu-branches/ubuntu/saucy/monodevelop/saucy

« back to all changes in this revision

Viewing changes to external/ngit/NGit.Test/NGit.Diff/DiffFormatterReflowTest.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (1.8.5) (1.5.8 sid)
  • Revision ID: package-import@ubuntu.com-20120527180820-f1ub6lhg0s50wci1
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
5
This program and the accompanying materials are made available
 
6
under the terms of the Eclipse Distribution License v1.0 which
 
7
accompanies this distribution, is reproduced below, and is
 
8
available at http://www.eclipse.org/org/documents/edl-v10.php
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
19
- Redistributions in binary form must reproduce the above
 
20
  copyright notice, this list of conditions and the following
 
21
  disclaimer in the documentation and/or other materials provided
 
22
  with the distribution.
 
23
 
 
24
- Neither the name of the Eclipse Foundation, Inc. nor the
 
25
  names of its contributors may be used to endorse or promote
 
26
  products derived from this software without specific prior
 
27
  written permission.
 
28
 
 
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
42
*/
 
43
 
 
44
using System;
 
45
using NGit.Diff;
 
46
using NGit.Junit;
 
47
using NGit.Patch;
 
48
using NGit.Util;
 
49
using Sharpen;
 
50
 
 
51
namespace NGit.Diff
 
52
{
 
53
        [NUnit.Framework.TestFixture]
 
54
        public class DiffFormatterReflowTest
 
55
        {
 
56
                private RawText a;
 
57
 
 
58
                private RawText b;
 
59
 
 
60
                private FileHeader file;
 
61
 
 
62
                private ByteArrayOutputStream @out;
 
63
 
 
64
                private DiffFormatter fmt;
 
65
 
 
66
                /// <exception cref="System.Exception"></exception>
 
67
                [NUnit.Framework.SetUp]
 
68
                public virtual void SetUp()
 
69
                {
 
70
                        @out = new ByteArrayOutputStream();
 
71
                        fmt = new DiffFormatter(@out);
 
72
                }
 
73
 
 
74
                /// <exception cref="System.IO.IOException"></exception>
 
75
                [NUnit.Framework.Test]
 
76
                public virtual void TestNegativeContextFails()
 
77
                {
 
78
                        Init("X");
 
79
                        try
 
80
                        {
 
81
                                fmt.SetContext(-1);
 
82
                                NUnit.Framework.Assert.Fail("accepted negative context");
 
83
                        }
 
84
                        catch (ArgumentException)
 
85
                        {
 
86
                        }
 
87
                }
 
88
 
 
89
                // pass
 
90
                /// <exception cref="System.IO.IOException"></exception>
 
91
                [NUnit.Framework.Test]
 
92
                public virtual void TestContext0()
 
93
                {
 
94
                        Init("X");
 
95
                        fmt.SetContext(0);
 
96
                        AssertFormatted();
 
97
                }
 
98
 
 
99
                /// <exception cref="System.IO.IOException"></exception>
 
100
                [NUnit.Framework.Test]
 
101
                public virtual void TestContext1()
 
102
                {
 
103
                        Init("X");
 
104
                        fmt.SetContext(1);
 
105
                        AssertFormatted();
 
106
                }
 
107
 
 
108
                /// <exception cref="System.IO.IOException"></exception>
 
109
                [NUnit.Framework.Test]
 
110
                public virtual void TestContext3()
 
111
                {
 
112
                        Init("X");
 
113
                        fmt.SetContext(3);
 
114
                        AssertFormatted();
 
115
                }
 
116
 
 
117
                /// <exception cref="System.IO.IOException"></exception>
 
118
                [NUnit.Framework.Test]
 
119
                public virtual void TestContext5()
 
120
                {
 
121
                        Init("X");
 
122
                        fmt.SetContext(5);
 
123
                        AssertFormatted();
 
124
                }
 
125
 
 
126
                /// <exception cref="System.IO.IOException"></exception>
 
127
                [NUnit.Framework.Test]
 
128
                public virtual void TestContext10()
 
129
                {
 
130
                        Init("X");
 
131
                        fmt.SetContext(10);
 
132
                        AssertFormatted();
 
133
                }
 
134
 
 
135
                /// <exception cref="System.IO.IOException"></exception>
 
136
                [NUnit.Framework.Test]
 
137
                public virtual void TestContext100()
 
138
                {
 
139
                        Init("X");
 
140
                        fmt.SetContext(100);
 
141
                        AssertFormatted();
 
142
                }
 
143
 
 
144
                /// <exception cref="System.IO.IOException"></exception>
 
145
                [NUnit.Framework.Test]
 
146
                public virtual void TestEmpty1()
 
147
                {
 
148
                        Init("E");
 
149
                        AssertFormatted("E.patch");
 
150
                }
 
151
 
 
152
                /// <exception cref="System.IO.IOException"></exception>
 
153
                [NUnit.Framework.Test]
 
154
                public virtual void TestNoNewLine1()
 
155
                {
 
156
                        Init("Y");
 
157
                        AssertFormatted("Y.patch");
 
158
                }
 
159
 
 
160
                /// <exception cref="System.IO.IOException"></exception>
 
161
                [NUnit.Framework.Test]
 
162
                public virtual void TestNoNewLine2()
 
163
                {
 
164
                        Init("Z");
 
165
                        AssertFormatted("Z.patch");
 
166
                }
 
167
 
 
168
                /// <exception cref="System.IO.IOException"></exception>
 
169
                private void Init(string name)
 
170
                {
 
171
                        a = new RawText(ReadFile(name + "_PreImage"));
 
172
                        b = new RawText(ReadFile(name + "_PostImage"));
 
173
                        file = ParseTestPatchFile(name + ".patch").GetFiles()[0];
 
174
                }
 
175
 
 
176
                /// <exception cref="System.IO.IOException"></exception>
 
177
                private void AssertFormatted()
 
178
                {
 
179
                        AssertFormatted(Sharpen.Extensions.GetTestName() + ".out");
 
180
                }
 
181
 
 
182
                /// <exception cref="System.IO.IOException"></exception>
 
183
                private void AssertFormatted(string name)
 
184
                {
 
185
                        fmt.Format(file, a, b);
 
186
                        string exp = RawParseUtils.Decode(ReadFile(name));
 
187
                        NUnit.Framework.Assert.AreEqual(exp, RawParseUtils.Decode(@out.ToByteArray()));
 
188
                }
 
189
 
 
190
                /// <exception cref="System.IO.IOException"></exception>
 
191
                private byte[] ReadFile(string patchFile)
 
192
                {
 
193
                        InputStream @in = GetType().GetResourceAsStream(patchFile);
 
194
                        if (@in == null)
 
195
                        {
 
196
                                NUnit.Framework.Assert.Fail("No " + patchFile + " test vector");
 
197
                                return null;
 
198
                        }
 
199
                        // Never happens
 
200
                        try
 
201
                        {
 
202
                                byte[] buf = new byte[1024];
 
203
                                ByteArrayOutputStream temp = new ByteArrayOutputStream();
 
204
                                int n;
 
205
                                while ((n = @in.Read(buf)) > 0)
 
206
                                {
 
207
                                        temp.Write(buf, 0, n);
 
208
                                }
 
209
                                return temp.ToByteArray();
 
210
                        }
 
211
                        finally
 
212
                        {
 
213
                                @in.Close();
 
214
                        }
 
215
                }
 
216
 
 
217
                /// <exception cref="System.IO.IOException"></exception>
 
218
                private NGit.Patch.Patch ParseTestPatchFile(string patchFile)
 
219
                {
 
220
                        InputStream @in = GetType().GetResourceAsStream(patchFile);
 
221
                        if (@in == null)
 
222
                        {
 
223
                                NUnit.Framework.Assert.Fail("No " + patchFile + " test vector");
 
224
                                return null;
 
225
                        }
 
226
                        // Never happens
 
227
                        try
 
228
                        {
 
229
                                NGit.Patch.Patch p = new NGit.Patch.Patch();
 
230
                                p.Parse(@in);
 
231
                                return p;
 
232
                        }
 
233
                        finally
 
234
                        {
 
235
                                @in.Close();
 
236
                        }
 
237
                }
 
238
        }
 
239
}