Skip to content

Commit 0ea5faf

Browse files
author
drewmiller
committed
added C++ language support
1 parent 613c547 commit 0ea5faf

File tree

7 files changed

+268
-0
lines changed

7 files changed

+268
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace ColorCode
2+
{
3+
public static class AcceptanceHelper
4+
{
5+
const string baseExpectedFormat = @"<div style=""color:Black;background-color:White;""><pre>
6+
{0}
7+
</pre></div>";
8+
9+
public static string BuildExpected(string format, params object[] args)
10+
{
11+
var expectedFormat = string.Format(baseExpectedFormat, format);
12+
return string.Format(expectedFormat, args);
13+
}
14+
}
15+
}

ColorCode.AcceptanceTests/ColorCode.AcceptanceTests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050
</Reference>
5151
</ItemGroup>
5252
<ItemGroup>
53+
<Compile Include="AcceptanceHelper.cs" />
5354
<Compile Include="ColorizeData.cs" />
5455
<Compile Include="ColorizeTests.cs" />
56+
<Compile Include="CppAcceptanceTests.cs" />
5557
<Compile Include="Data\csharp\abstract-keyword.source.cs">
5658
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5759
</Compile>
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using Xunit;
5+
using Xunit.Extensions;
6+
7+
namespace ColorCode
8+
{
9+
public class CppAcceptanceTests
10+
{
11+
public class CommentTests
12+
{
13+
[Fact]
14+
public void WillColorizeACommentOnMultipleLines()
15+
{
16+
const string source = @"/*
17+
comment line
18+
comment line 2
19+
*/";
20+
var expected = AcceptanceHelper.BuildExpected(@"<span style=""color:Green;"">/*
21+
comment line
22+
comment line 2
23+
*/</span>");
24+
25+
var actual = new CodeColorizer().Colorize(source, Languages.Cpp);
26+
27+
Assert.Equal(expected, actual);
28+
}
29+
30+
[Fact]
31+
public void WillColorizeAMultieLineStyleCommentOnOneLine()
32+
{
33+
const string source = @"/*comment*/";
34+
var expected = AcceptanceHelper.BuildExpected(@"<span style=""color:Green;"">/*comment*/</span>");
35+
36+
var actual = new CodeColorizer().Colorize(source, Languages.Cpp);
37+
38+
Assert.Equal(expected, actual);
39+
}
40+
41+
[Fact]
42+
public void WillColorizeASingleLineStyleComment()
43+
{
44+
const string source = @"//comment";
45+
var expected = AcceptanceHelper.BuildExpected(@"<span style=""color:Green;"">//comment</span>");
46+
47+
var actual = new CodeColorizer().Colorize(source, Languages.Cpp);
48+
49+
Assert.Equal(expected, actual);
50+
}
51+
}
52+
53+
public class StringTests
54+
{
55+
[Fact]
56+
public void WillColorizeStrings()
57+
{
58+
const string source = @"string aString = ""aString"";";
59+
var expected = AcceptanceHelper.BuildExpected(@"string aString = <span style=""color:#A31515;"">&quot;aString&quot;</span>;");
60+
61+
var actual = new CodeColorizer().Colorize(source, Languages.Cpp);
62+
63+
Assert.Equal(expected, actual);
64+
}
65+
}
66+
67+
public class KeywordTests
68+
{
69+
[Theory]
70+
[KeywordData]
71+
public void WillColorizeAKeywordWithNoSurroundingText(string keyword)
72+
{
73+
var source = keyword;
74+
var exepected = AcceptanceHelper.BuildExpected(@"<span style=""color:Blue;"">{0}</span>", keyword);
75+
76+
var actual = new CodeColorizer().Colorize(source, Languages.Cpp);
77+
78+
Assert.Equal(exepected, actual);
79+
}
80+
81+
[Theory]
82+
[KeywordData]
83+
public void WillColorizeAKeywordWithPrecedingAndSucceedingText(string keyword)
84+
{
85+
var source = string.Format("fnord {0} fnord", keyword);
86+
var exepected = AcceptanceHelper.BuildExpected(@"fnord <span style=""color:Blue;"">{0}</span> fnord", keyword);
87+
88+
var actual = new CodeColorizer().Colorize(source, Languages.Cpp);
89+
90+
Assert.Equal(exepected, actual);
91+
}
92+
93+
[Theory]
94+
[KeywordData]
95+
public void WillNotColorizeAKeywordInsideAWord(string keyword)
96+
{
97+
var source = string.Format("fnord{0}fnord", keyword);
98+
var exepected = AcceptanceHelper.BuildExpected(@"fnord{0}fnord", keyword);
99+
100+
var actual = new CodeColorizer().Colorize(source, Languages.Cpp);
101+
102+
Assert.Equal(exepected, actual);
103+
}
104+
}
105+
}
106+
107+
#region Theory Data
108+
public class KeywordData : DataAttribute
109+
{
110+
public override IEnumerable<object[]> GetData(MethodInfo methodUnderTest, Type[] parameterTypes)
111+
{
112+
var keywordData = new List<object[]>
113+
{
114+
new object[] {"auto"},
115+
new object[] {"bool"},
116+
new object[] {"break"},
117+
new object[] {"case"},
118+
new object[] {"catch"},
119+
new object[] {"char"},
120+
new object[] {"class"},
121+
new object[] {"const"},
122+
new object[] {"const_cast"},
123+
new object[] {"continue"},
124+
new object[] {"default"},
125+
new object[] {"delete"},
126+
new object[] {"do"},
127+
new object[] {"double"},
128+
new object[] {"dynamic_cast"},
129+
new object[] {"else"},
130+
new object[] {"enum"},
131+
new object[] {"explicit"},
132+
new object[] {"export"},
133+
new object[] {"extern"},
134+
new object[] {"false"},
135+
new object[] {"float"},
136+
new object[] {"for"},
137+
new object[] {"friend"},
138+
new object[] {"goto"},
139+
new object[] {"if"},
140+
new object[] {"inline"},
141+
new object[] {"int"},
142+
new object[] {"long"},
143+
new object[] {"mutable"},
144+
new object[] {"namespace"},
145+
new object[] {"new"},
146+
new object[] {"operator"},
147+
new object[] {"private"},
148+
new object[] {"protected"},
149+
new object[] {"public"},
150+
new object[] {"register"},
151+
new object[] {"reinterpret_cast"},
152+
new object[] {"return"},
153+
new object[] {"short"},
154+
new object[] {"signed"},
155+
new object[] {"sizeof"},
156+
new object[] {"static"},
157+
new object[] {"static_cast"},
158+
new object[] {"struct"},
159+
new object[] {"switch"},
160+
new object[] {"template"},
161+
new object[] {"this"},
162+
new object[] {"throw"},
163+
new object[] {"true"},
164+
new object[] {"try"},
165+
new object[] {"typedef"},
166+
new object[] {"typeid"},
167+
new object[] {"typename"},
168+
new object[] {"union"},
169+
new object[] {"unsigned"},
170+
new object[] {"using"},
171+
new object[] {"virtual"},
172+
new object[] {"void"},
173+
new object[] {"volatile"},
174+
new object[] {"wchar_t"},
175+
new object[] {"while"},
176+
};
177+
return keywordData;
178+
}
179+
}
180+
#endregion
181+
}

ColorCode/ColorCode.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<Compile Include="Compilation\Languages\AspxVb.cs">
5656
<SubType>Code</SubType>
5757
</Compile>
58+
<Compile Include="Compilation\Languages\Cpp.cs" />
5859
<Compile Include="Compilation\Languages\Css.cs" />
5960
<Compile Include="Compilation\Languages\Html.cs">
6061
<SubType>Code</SubType>

ColorCode/Common/LanguageId.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ public static class LanguageId
1717
public const string JavaScript = "javascript";
1818
public const string Sql = "sql";
1919
public const string VbDotNet = "vb.net";
20+
public const string Cpp = "cpp";
2021
}
2122
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
3+
using System.Collections.Generic;
4+
using ColorCode.Common;
5+
6+
namespace ColorCode.Compilation.Languages
7+
{
8+
public class Cpp : ILanguage
9+
{
10+
public string Id
11+
{
12+
get { return LanguageId.Cpp; }
13+
}
14+
15+
public string Name
16+
{
17+
get { return "C++"; }
18+
}
19+
20+
public string FirstLinePattern
21+
{
22+
get
23+
{
24+
return null;
25+
}
26+
}
27+
28+
public IList<LanguageRule> Rules
29+
{
30+
get
31+
{
32+
return new List<LanguageRule>
33+
{
34+
new LanguageRule(
35+
@"/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/",
36+
new Dictionary<int, string>
37+
{
38+
{ 0, ScopeName.Comment },
39+
}),
40+
new LanguageRule(
41+
@"(//.*?)\r?$",
42+
new Dictionary<int, string>
43+
{
44+
{ 1, ScopeName.Comment }
45+
}),
46+
new LanguageRule(
47+
@"(?s)(""[^\n]*?(?<!\\)"")",
48+
new Dictionary<int, string>
49+
{
50+
{ 0, ScopeName.String }
51+
}),
52+
new LanguageRule(
53+
@"\b(auto|bool|break|case|catch|char|class|const|const_cast|continue|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|false|float|for|friend|goto|if|inline|int|long|mutable|namespace|new|operator|private|protected|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|wchar_t|while)\b",
54+
new Dictionary<int, string>
55+
{
56+
{0, ScopeName.Keyword},
57+
}),
58+
};
59+
}
60+
}
61+
}
62+
}

ColorCode/Languages.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static Languages()
3232
Load<Xml>();
3333
Load<Php>();
3434
Load<Css>();
35+
Load<Cpp>();
3536
}
3637

3738
public static ILanguage Ashx
@@ -99,6 +100,11 @@ public static ILanguage Css
99100
get { return LanguageRepository.FindById(LanguageId.Css); }
100101
}
101102

103+
public static ILanguage Cpp
104+
{
105+
get { return LanguageRepository.FindById(LanguageId.Cpp); }
106+
}
107+
102108
public static ILanguage FindById(string id)
103109
{
104110
return LanguageRepository.FindById(id);

0 commit comments

Comments
 (0)