Skip to content

Commit 3706f4b

Browse files
committed
Kata Wrapper
1 parent 6b6a568 commit 3706f4b

File tree

6 files changed

+304
-0
lines changed

6 files changed

+304
-0
lines changed

Wrapper/Wrapper.sln

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wrapper", "Wrapper\Wrapper.csproj", "{07C3ABAA-357B-4812-B14A-E3A5BEB60E57}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|x86 = Debug|x86
9+
Release|x86 = Release|x86
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{07C3ABAA-357B-4812-B14A-E3A5BEB60E57}.Debug|x86.ActiveCfg = Debug|x86
13+
{07C3ABAA-357B-4812-B14A-E3A5BEB60E57}.Debug|x86.Build.0 = Debug|x86
14+
{07C3ABAA-357B-4812-B14A-E3A5BEB60E57}.Release|x86.ActiveCfg = Release|x86
15+
{07C3ABAA-357B-4812-B14A-E3A5BEB60E57}.Release|x86.Build.0 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

Wrapper/Wrapper/Program.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Wrapper
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
}
13+
}
14+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Wrapper")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("http://der-albert.com")]
12+
[assembly: AssemblyProduct("Wrapper")]
13+
[assembly: AssemblyCopyright("Copyright © http://der-albert.com 2011")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("75f621b1-dbd6-4b5d-a3c4-0be98487756c")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Wrapper/Wrapper/Text.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Wrapper
2+
{
3+
public static class Text
4+
{
5+
public static string LoremIpsum =
6+
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
7+
}
8+
}

Wrapper/Wrapper/Wrapper.csproj

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
6+
<ProductVersion>8.0.30703</ProductVersion>
7+
<SchemaVersion>2.0</SchemaVersion>
8+
<ProjectGuid>{07C3ABAA-357B-4812-B14A-E3A5BEB60E57}</ProjectGuid>
9+
<OutputType>Exe</OutputType>
10+
<AppDesignerFolder>Properties</AppDesignerFolder>
11+
<RootNamespace>Wrapper</RootNamespace>
12+
<AssemblyName>Wrapper</AssemblyName>
13+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
14+
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
15+
<FileAlignment>512</FileAlignment>
16+
</PropertyGroup>
17+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
18+
<PlatformTarget>x86</PlatformTarget>
19+
<DebugSymbols>true</DebugSymbols>
20+
<DebugType>full</DebugType>
21+
<Optimize>false</Optimize>
22+
<OutputPath>bin\Debug\</OutputPath>
23+
<DefineConstants>DEBUG;TRACE</DefineConstants>
24+
<ErrorReport>prompt</ErrorReport>
25+
<WarningLevel>4</WarningLevel>
26+
</PropertyGroup>
27+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
28+
<PlatformTarget>x86</PlatformTarget>
29+
<DebugType>pdbonly</DebugType>
30+
<Optimize>true</Optimize>
31+
<OutputPath>bin\Release\</OutputPath>
32+
<DefineConstants>TRACE</DefineConstants>
33+
<ErrorReport>prompt</ErrorReport>
34+
<WarningLevel>4</WarningLevel>
35+
</PropertyGroup>
36+
<ItemGroup>
37+
<Reference Include="System" />
38+
<Reference Include="System.Core" />
39+
<Reference Include="System.Xml.Linq" />
40+
<Reference Include="System.Data.DataSetExtensions" />
41+
<Reference Include="Microsoft.CSharp" />
42+
<Reference Include="System.Data" />
43+
<Reference Include="System.Xml" />
44+
<Reference Include="xunit">
45+
<HintPath>..\..\external\xUnit\xunit.dll</HintPath>
46+
</Reference>
47+
<Reference Include="xunit.extensions">
48+
<HintPath>..\..\external\xUnit\xunit.extensions.dll</HintPath>
49+
</Reference>
50+
</ItemGroup>
51+
<ItemGroup>
52+
<Compile Include="Program.cs" />
53+
<Compile Include="Properties\AssemblyInfo.cs" />
54+
<Compile Include="Text.cs" />
55+
<Compile Include="WrapperTests.cs" />
56+
</ItemGroup>
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
59+
Other similar extension points exist, see Microsoft.Common.targets.
60+
<Target Name="BeforeBuild">
61+
</Target>
62+
<Target Name="AfterBuild">
63+
</Target>
64+
-->
65+
</Project>

Wrapper/Wrapper/WrapperTests.cs

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Xunit;
6+
7+
namespace Wrapper
8+
{
9+
public class WrapperTests : IDisposable
10+
{
11+
private string original;
12+
private string result;
13+
14+
[Fact]
15+
public void Test_Column_72()
16+
{
17+
string expected =
18+
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy\n" +
19+
"eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam\n" +
20+
"voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet\n" +
21+
"clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit\n" +
22+
"amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam\n" +
23+
"nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,\n" +
24+
"sed diam voluptua. At vero eos et accusam et justo duo dolores et ea\n" +
25+
"rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem\n" +
26+
"ipsum dolor sit amet.";
27+
original = Text.LoremIpsum;
28+
result = LineWrapper.Wrap(Text.LoremIpsum, 72);
29+
Assert.Equal(expected, result);
30+
}
31+
32+
[Fact]
33+
public void Can_Break_Two_Words_with_Column_2()
34+
{
35+
original = "aa aa";
36+
result = LineWrapper.Wrap(original, 2);
37+
Assert.Equal("aa\naa", result);
38+
}
39+
40+
[Fact]
41+
public void Dont_Break_One_Word_with_Column_2()
42+
{
43+
original = "aaaa";
44+
result = LineWrapper.Wrap(original, 2);
45+
Assert.Equal("aaaa", result);
46+
}
47+
48+
[Fact]
49+
public void Can_Break_Three_Words_with_Column_2()
50+
{
51+
original = "aaa bb ccc";
52+
result = LineWrapper.Wrap(original, 2);
53+
Assert.Equal("aaa\nbb\nccc", result);
54+
}
55+
56+
[Fact]
57+
public void Can_Break_Five_Words_with_Column_2()
58+
{
59+
original = "aaa b c d eee";
60+
result = LineWrapper.Wrap(original, 2);
61+
Assert.Equal("aaa\nb\nc\nd\neee", result);
62+
}
63+
64+
[Fact]
65+
public void Can_Break_Three_Small_Words_with_Column_3()
66+
{
67+
original = "a b c";
68+
result = LineWrapper.Wrap(original, 3);
69+
Assert.Equal("a b\nc", result);
70+
}
71+
72+
[Fact]
73+
public void Dont_Break_DoubleSpaces_in_Line()
74+
{
75+
original = "aa bb cc c";
76+
result = LineWrapper.Wrap(original, 6);
77+
Assert.Equal("aa bb\ncc c", result);
78+
}
79+
80+
[Fact]
81+
public void Empy_String_result_in_Empty_String()
82+
{
83+
original = "";
84+
result = LineWrapper.Wrap(original, 6);
85+
Assert.Equal("", result);
86+
}
87+
88+
[Fact]
89+
public void Lots_of_Spaces_at_EndOf_Line_should_removed()
90+
{
91+
original = "a ";
92+
result = LineWrapper.Wrap(original, 4);
93+
Assert.Equal("a", result);
94+
}
95+
96+
[Fact]
97+
public void Multiple_Lines_with_Lots_of_Spaces__at_EndOf_Line_should_removed()
98+
{
99+
original = "a b c d e ";
100+
result = LineWrapper.Wrap(original, 4);
101+
Assert.Equal("a\nb\nc\nd\ne", result);
102+
}
103+
104+
[Fact]
105+
public void Lots_of_Spaces_at_Start_of_Line_should_removed()
106+
{
107+
original = " e";
108+
result = LineWrapper.Wrap(original, 4);
109+
Assert.Equal("e", result);
110+
}
111+
public void Dispose()
112+
{
113+
DebugWrite(original);
114+
DebugWrite(result);
115+
}
116+
117+
private void DebugWrite(string text)
118+
{
119+
Console.WriteLine("!" + text.Replace("\n", "\\n") + "!");
120+
}
121+
}
122+
123+
public class LineWrapper
124+
{
125+
public static string Wrap(string text, int columns)
126+
{
127+
var words = SplitWords(text);
128+
return string.Join("\n", WrapWords(words, columns));
129+
}
130+
131+
private static IEnumerable<string> WrapWords(IEnumerable<string> words, int columns)
132+
{
133+
var sb = new StringBuilder();
134+
foreach (var word in words) {
135+
if (WordExceedsLine(sb, word, columns)) {
136+
yield return sb.ToString().Trim();
137+
sb.Clear();
138+
}
139+
if (sb.Length != 0)
140+
sb.Append(' ');
141+
sb.Append(word);
142+
}
143+
if (sb.Length == 0)
144+
yield break;
145+
yield return sb.ToString().Trim();
146+
}
147+
148+
private static bool WordExceedsLine(StringBuilder sb, string word, int columns)
149+
{
150+
if (sb.Length == 0) {
151+
return false;
152+
}
153+
return (sb.Length + word.Length + 1) > columns;
154+
}
155+
156+
private static IEnumerable<string> SplitWords(string text)
157+
{
158+
return text.Split(new[] {' '});
159+
}
160+
}
161+
}

0 commit comments

Comments
 (0)