Skip to content

Commit d1fd61e

Browse files
Merge pull request #190 from microsoftgraph/fixCRLFdoc
Handle CRLF in doc annotations as code comments.
2 parents f958816 + 092ef1f commit d1fd61e

File tree

6 files changed

+144
-3
lines changed

6 files changed

+144
-3
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using Microsoft.Graph.ODataTemplateWriter.CodeHelpers.CSharp;
2+
using Microsoft.Graph.ODataTemplateWriter.CodeHelpers.TypeScript;
3+
using Microsoft.Graph.ODataTemplateWriter.CodeHelpers.PHP;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Vipr.Core.CodeModel;
6+
7+
8+
namespace GraphODataTemplateWriter.Test
9+
{
10+
[TestClass]
11+
public class TypeHelperTests
12+
{
13+
OdcmProperty testProperty;
14+
string actualInputString = "\r\n Test string";
15+
string expectedOutputString = "\r\n/// Test string";
16+
17+
[TestInitialize]
18+
public void Initialize()
19+
{
20+
testProperty = new OdcmProperty("testPropertyName");
21+
}
22+
23+
/// <summary>
24+
/// Test that GetSanitizedLongDescription provides long descriptions without uncommented lines of text.
25+
/// </summary>
26+
[TestMethod]
27+
public void Long_Description_Doesnt_Contain_Uncommented_NewLine_For_CSharp()
28+
{
29+
testProperty.LongDescription = actualInputString;
30+
string sanitizedString = TypeHelperCSharp.GetSanitizedLongDescription(testProperty); // Explicitly bind to extension method.
31+
32+
Assert.AreEqual(expectedOutputString, sanitizedString, "GetSanitizedLongDescription is not handling escaped CRLF.");
33+
}
34+
35+
/// <summary>
36+
/// Test that GetSanitizedLongDescription provides descriptions without uncommented lines of text.
37+
/// </summary>
38+
[TestMethod]
39+
public void Description_Doesnt_Contain_Uncommented_NewLine_For_CSharp()
40+
{
41+
testProperty.Description = actualInputString;
42+
string sanitizedString = TypeHelperCSharp.GetSanitizedLongDescription(testProperty);
43+
44+
Assert.AreEqual(expectedOutputString, sanitizedString, "GetSanitizedLongDescription is not handling escaped CRLF.");
45+
}
46+
47+
/// <summary>
48+
/// Test that GetSanitizedLongDescription provides long descriptions without uncommented lines of text for Typescript.
49+
/// </summary>
50+
[TestMethod]
51+
public void Long_Description_Doesnt_Contain_Uncommented_NewLine_For_Typescript()
52+
{
53+
testProperty.LongDescription = actualInputString;
54+
string sanitizedString = TypeHelperTypeScript.GetSanitizedLongDescription(testProperty); // Explicitly bind to extension method.
55+
56+
Assert.AreEqual(expectedOutputString, sanitizedString, "GetSanitizedLongDescription is not handling escaped CRLF.");
57+
}
58+
59+
/// <summary>
60+
/// Test that GetSanitizedLongDescription provides descriptions without uncommented lines of text for Typescript.
61+
/// </summary>
62+
[TestMethod]
63+
public void Description_Doesnt_Contain_Uncommented_NewLine_For_Typescript()
64+
{
65+
testProperty.Description = actualInputString;
66+
string sanitizedString = TypeHelperTypeScript.GetSanitizedLongDescription(testProperty);
67+
68+
Assert.AreEqual(expectedOutputString, sanitizedString, "GetSanitizedLongDescription is not handling escaped CRLF.");
69+
}
70+
71+
/// <summary>
72+
/// Test that GetSanitizedLongDescription provides long descriptions without uncommented lines of text for PHP.
73+
/// </summary>
74+
[TestMethod]
75+
public void Long_Description_Doesnt_Contain_Uncommented_NewLine_For_PHP()
76+
{
77+
testProperty.LongDescription = actualInputString;
78+
string sanitizedString = TypeHelperPHP.GetSanitizedLongDescription(testProperty); // Explicitly bind to extension method.
79+
80+
Assert.AreEqual(expectedOutputString, sanitizedString, "GetSanitizedLongDescription is not handling escaped CRLF.");
81+
}
82+
83+
/// <summary>
84+
/// Test that GetSanitizedLongDescription provides descriptions without uncommented lines of text for PHP.
85+
/// </summary>
86+
[TestMethod]
87+
public void Description_Doesnt_Contain_Uncommented_NewLine_For_PHP()
88+
{
89+
testProperty.Description = actualInputString;
90+
string sanitizedString = TypeHelperPHP.GetSanitizedLongDescription(testProperty);
91+
92+
Assert.AreEqual(expectedOutputString, sanitizedString, "GetSanitizedLongDescription is not handling escaped CRLF.");
93+
}
94+
}
95+
}

src/GraphODataTemplateWriter/CodeHelpers/CSharp/TypeHelperCSharp.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,10 @@ public static string GetSanitizedLongDescription(this OdcmProperty property)
217217

218218
if (description != null)
219219
{
220-
return description.Replace("<", "&lt;").Replace(">", "&gt;").Replace("&", "&amp;");
220+
return description.Replace("<", "&lt;")
221+
.Replace(">", "&gt;")
222+
.Replace("&", "&amp;")
223+
.Replace("\r\n", "\r\n///"); // &#xD;&#xA; The HTML encoded has already been converted to escaped chars.
221224
}
222225
return null;
223226
}

src/GraphODataTemplateWriter/CodeHelpers/PHP/TypeHelperPHP.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public static string GetSanitizedLongDescription(this OdcmProperty property)
4343

4444
if (description != null)
4545
{
46-
return description.Replace("<", "&lt;").Replace(">", "&gt;").Replace("&", "&amp;");
46+
return description.Replace("<", "&lt;")
47+
.Replace(">", "&gt;")
48+
.Replace("&", "&amp;")
49+
.Replace("\r\n", "\r\n///"); // &#xD;&#xA; The HTML encoded has already been converted to escaped chars.
4750
}
4851
return null;
4952
}

src/GraphODataTemplateWriter/CodeHelpers/TypeScript/TypeHelperTypeScript.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ public static string GetSanitizedLongDescription(this OdcmProperty property)
6666
var description = property.LongDescription ?? property.Description;
6767
if (description != null)
6868
{
69-
return description.Replace("<", "&lt;").Replace(">", "&gt;").Replace("&", "&amp;");
69+
return description.Replace("<", "&lt;")
70+
.Replace(">", "&gt;")
71+
.Replace("&", "&amp;")
72+
.Replace("\r\n", "\r\n///"); // &#xD;&#xA; The HTML encoded has already been converted to escaped chars.
7073
}
7174
return null;
7275
}

test/Typewriter.Test/Given_a_valid_metadata_file_to_Typewriter.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,39 @@ public void It_generates_dotNet_client_with_default_beta_baseUrl()
106106
}
107107
Assert.IsTrue(hasFoundBetaUrl, $"The expected default base URL, {betaUrl}, was not set in the generated test file.");
108108
}
109+
110+
111+
[TestMethod]
112+
public void It_generates_dotNet_client_with_commented_out_code_comments()
113+
{
114+
const string outputDirectory = "output";
115+
116+
Options options = new Options()
117+
{
118+
Output = outputDirectory,
119+
Language = "CSharp",
120+
GenerationMode = GenerationMode.Files
121+
};
122+
123+
Generator.GenerateFiles(testMetadata, options);
124+
125+
FileInfo fileInfo = new FileInfo(outputDirectory + generatedOutputUrl + @"\Model\OnenotePage.cs");
126+
Assert.IsTrue(fileInfo.Exists, $"Expected: {fileInfo.FullName}. File was not found.");
127+
128+
// Check that the test string is found in the output file. Converting "&#xD;&#xA; Test token string" to "/// Test token string" is what we are testing.
129+
// We are making sure that the documentation annotation is handled correctly for this scenario.
130+
IEnumerable<string> lines = File.ReadLines(fileInfo.FullName);
131+
bool hasTestString = false;
132+
string testString = @"/// Test token string";
133+
foreach (var line in lines)
134+
{
135+
if (line.Contains(testString))
136+
{
137+
hasTestString = true;
138+
break;
139+
}
140+
}
141+
Assert.IsTrue(hasTestString, $"The expected test token string, '{testString}', was not set in the generated test file. We are not correctly handling the \r\n coming from the annotations.");
142+
}
109143
}
110144
}

test/Typewriter.Test/Resources/dirtyMetadata.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
<Singleton Name="testSingleton2" Type="microsoft.graph.testSingleton2"/>
4242
<EntitySet Name="testTypes" EntityType="microsoft.graph.testType3"/>
4343
</EntityContainer>
44+
<Annotations Target="microsoft.graph.onenotePage/content">
45+
<Annotation Term="Org.OData.Core.V1.Description" String="The OneNotePage content.&#xD;&#xA;&#xD;&#xA; Test token string" />
46+
</Annotations>
4447
<Annotations Target="microsoft.graph.directoryObject">
4548
<Annotation Term="Org.OData.Capabilities.V1.ExpandRestrictions">
4649
<Record>

0 commit comments

Comments
 (0)