Skip to content

Commit

Permalink
Fix #265: Index range bugs in YamlHeaderParser (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
qinezh committed May 6, 2016
1 parent ca190ef commit e4cbdb3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 17 deletions.
11 changes: 3 additions & 8 deletions src/Microsoft.DocAsCode.Build.Common/Coordinate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,11 @@ public static Coordinate GetCoordinate(string content)
return Default;
}
int index = content.Length - 1;
int line = content.Count(c => c == NewLineCharacter); // Assume there is no new line at the end of the file, count(line) = count(newline) + 1
int line = content.Count(c => c == NewLineCharacter);

// Remove last new line character if it is last character of the content
if (content[content.Length - 1] == NewLineCharacter)
{
line--;
index--;
}
int lineStart = content.LastIndexOf(NewLineCharacter, index);
int col = index - lineStart - 1;

int col = index - lineStart;
return new Coordinate { Line = line, Column = col };
}

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.DocAsCode.Build.Common/MarkdownReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ orderby location.StartLocation descending
{
Uid = item.Id,
Metadata = item.Detail.Properties,
Conceptual = content.Substring(start, end - start + 1),
Conceptual = content.Substring(start, end - start),
Documentation = new SourceDetail
{
Remote = repoInfo,
Expand Down
6 changes: 2 additions & 4 deletions src/Microsoft.DocAsCode.Build.Common/YamlHeaderParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.DocAsCode.Build.Common
/// 2. Followed by three dashes `---` as a line, spaces are allowed before
/// 3. Followed by and must followed by `uid: `
/// 4. Followed by other properties in YAML format
/// 5. Ended with three dashes `---` as a line, spaces are allowed before
/// 5. Ended with three dashes `---` and environment newline as a line, spaces are allowed before
/// </summary>
public static class YamlHeaderParser
{
Expand Down Expand Up @@ -50,9 +50,7 @@ private static MatchDetail SelectSingle(Match match, string input)

var overridenProperties = RemoveRequiredProperties(properties, RequiredProperties);

// Get one character larger then the actual match
var location = Location.GetLocation(input, wholeMatch.Index - 1, wholeMatch.Length + 2);

var location = Location.GetLocation(input, wholeMatch.Index, wholeMatch.Length);
return new MatchDetail
{
Id = properties[Constants.PropertyName.Uid].ToString(),
Expand Down
68 changes: 64 additions & 4 deletions test/Microsoft.DocAsCode.Build.Common.Tests/MarkdownReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace Microsoft.DocAsCode.Build.Common.Tests
{
using System;
using System.IO;
using System.Text.RegularExpressions;

using Xunit;

Expand All @@ -13,21 +14,80 @@ public class MarkdownReaderTest
[Fact]
public void TestReadMarkdownAsOverwrite()
{
const string Content = @"---
var content = @"---
uid: Test
remarks: Hello
---
This is unit test!";
content = Regex.Replace(content, "/r?/n", "/r/n");
const string FileName = "ut_ReadMarkdownAsOverwrite.md";
File.WriteAllText(FileName, Content);
File.WriteAllText(FileName, content);
var results = MarkdownReader.ReadMarkdownAsOverwrite(Environment.CurrentDirectory, FileName);
Assert.NotNull(results);
Assert.Equal(1, results.Count);
Assert.Equal("Test", results[0].Uid);
Assert.Equal("Hello", results[0].Metadata["remarks"]);
Assert.Equal(@"
This is unit test!", results[0].Conceptual);
Assert.Equal("\r\nThis is unit test!", results[0].Conceptual);
File.Delete(FileName);

// Test conceptual content between two yamlheader
content = @"---
uid: Test1
remarks: Hello
---
This is unit test!
---
uid: Test2
---
";
content = Regex.Replace(content, "/r?/n", "/r/n");
File.WriteAllText(FileName, content);
results = MarkdownReader.ReadMarkdownAsOverwrite(Environment.CurrentDirectory, FileName);
Assert.NotNull(results);
Assert.Equal(2, results.Count);
Assert.Equal("Test1", results[0].Uid);
Assert.Equal("Test2", results[1].Uid);
Assert.Equal("Hello", results[0].Metadata["remarks"]);
Assert.Equal("This is unit test!", results[0].Conceptual);
Assert.Equal(String.Empty, results[1].Conceptual);
File.Delete(FileName);

// Test conceptual content with extra empty line between two yamlheader
content = @"---
uid: Test1
remarks: Hello
---
This is unit test!
---
uid: Test2
---
";
content = Regex.Replace(content, "/r?/n", "/r/n");
File.WriteAllText(FileName, content);
results = MarkdownReader.ReadMarkdownAsOverwrite(Environment.CurrentDirectory, FileName);
Assert.NotNull(results);
Assert.Equal(2, results.Count);
Assert.Equal("Test1", results[0].Uid);
Assert.Equal("Test2", results[1].Uid);
Assert.Equal("Hello", results[0].Metadata["remarks"]);
Assert.Equal("\r\n\r\nThis is unit test!\r\n\r\n", results[0].Conceptual);
Assert.Equal(String.Empty, results[1].Conceptual);
File.Delete(FileName);

// Test different line ending
content = "---\nuid: Test\nremarks: Hello\n---\nThis is unit test!\n";
File.WriteAllText(FileName, content);
results = MarkdownReader.ReadMarkdownAsOverwrite(Environment.CurrentDirectory, FileName);
Assert.NotNull(results);
Assert.Equal(1, results.Count);
Assert.Equal("Test", results[0].Uid);
Assert.Equal("Hello", results[0].Metadata["remarks"]);
Assert.Equal("This is unit test!\n", results[0].Conceptual);
File.Delete(FileName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ public void TestYamlHeaderParser()
";
yamlHeaders = YamlHeaderParser.Select(input);
Assert.Equal(0, yamlHeaders.Count);

// Multi-YamlHeader Test
input = @"---
uid: abc
---
conceptual
---
uid: efg
---
";
yamlHeaders = YamlHeaderParser.Select(input);
Assert.Equal(2, yamlHeaders.Count);
Assert.Equal("abc", yamlHeaders[0].Id);
Assert.Equal("efg", yamlHeaders[1].Id);
}
}
}

0 comments on commit e4cbdb3

Please sign in to comment.