-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Omondi
committed
Jan 26, 2024
1 parent
f7e9dd3
commit 092be98
Showing
6 changed files
with
173 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,24 @@ | ||
using Kiota.Builder.CodeDOM; | ||
using System; | ||
using System.IO; | ||
using Kiota.Builder.CodeDOM; | ||
using Kiota.Builder.Extensions; | ||
|
||
namespace Kiota.Builder.PathSegmenters; | ||
public class CSharpPathSegmenter : CommonPathSegmenter | ||
{ | ||
public CSharpPathSegmenter(string rootPath, string clientNamespaceName) : base(rootPath, clientNamespaceName) { } | ||
public override string FileSuffix => ".cs"; | ||
public override string NormalizeNamespaceSegment(string segmentName) => segmentName.ToFirstCharacterUpperCase(); | ||
public override string NormalizeFileName(CodeElement currentElement) => GetLastFileNameSegment(currentElement).ToFirstCharacterUpperCase(); | ||
public override string NormalizeNamespaceSegment(string segmentName) => segmentName.ToFirstCharacterUpperCase().ShortenFileName(); | ||
public override string NormalizeFileName(CodeElement currentElement) => GetLastFileNameSegment(currentElement).ToFirstCharacterUpperCase().ShortenFileName(); | ||
public override string NormalizePath(string fullPath) | ||
{ | ||
if (ExceedsMaxPathLength(fullPath) && Path.GetDirectoryName(fullPath) is string directoryName) | ||
{ | ||
var availableLength = MaxFilePathLength - (directoryName.Length + FileSuffix.Length + 2); // one for the folder separator and another to ensure its below limit | ||
return Path.Combine(directoryName, Path.GetFileName(fullPath).ShortenFileName(availableLength)[..Math.Min(64, availableLength)]) + FileSuffix; | ||
} | ||
return fullPath; | ||
} | ||
internal const int MaxFilePathLength = 32767; | ||
private static bool ExceedsMaxPathLength(string fullPath) => !string.IsNullOrEmpty(fullPath) && fullPath.Length > MaxFilePathLength; | ||
} |
51 changes: 51 additions & 0 deletions
51
tests/Kiota.Builder.Tests/PathSegmenters/CSharpPathSegmenterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Kiota.Builder.CodeDOM; | ||
using Kiota.Builder.PathSegmenters; | ||
using Xunit; | ||
|
||
namespace Kiota.Builder.Tests.PathSegmenters | ||
{ | ||
public class CSharpPathSegmenterTests | ||
{ | ||
private readonly CSharpPathSegmenter segmenter; | ||
public CSharpPathSegmenterTests() | ||
{ | ||
segmenter = new CSharpPathSegmenter("D:\\source\\repos\\kiota-sample", "client"); | ||
} | ||
|
||
[Fact] | ||
public void CSharpPathSegmenterGeneratesCorrectFileName() | ||
{ | ||
var fileName = segmenter.NormalizeFileName(new CodeClass | ||
{ | ||
Name = "testClass" | ||
}); | ||
Assert.Equal("TestClass", fileName);// the file name should be PascalCase | ||
} | ||
|
||
[Fact] | ||
public void CSharpPathSegmenterDoesNotGenerateNamespaceFoldersThatAreTooLong() | ||
{ | ||
var longNamespace = "ThisIsAVeryLongNamespaceNameThatShouldBeTruncatedAsItIsLongerThanTwoHundredAndFiftySixCharactersAccordingToWindows"; | ||
longNamespace = longNamespace + longNamespace + longNamespace; //make it more than 256 | ||
var normalizedNamespace = segmenter.NormalizeNamespaceSegment(longNamespace); | ||
Assert.NotEqual(longNamespace, normalizedNamespace); | ||
Assert.Equal(64, normalizedNamespace.Length);// shortened to sha256 length | ||
} | ||
|
||
[Fact] | ||
public void CSharpPathSegmenterDoesNotGeneratePathsThatAreTooLong() | ||
{ | ||
var rootDir = "D:\\source\\repos\\kiota-sample\\Item\\ErpBOLaborSvc\\"; | ||
var longNamespace = "ThisIsAVeryLongNamespaceNameThatIsNotLongerThanTwoHundredAndFiftySixCharactersAccordingToWindows"; | ||
while (rootDir.Length < CSharpPathSegmenter.MaxFilePathLength - longNamespace.Length) | ||
{ | ||
rootDir = $"{rootDir}\\{longNamespace}"; | ||
} | ||
var longPathName = $"{rootDir}\\TimeWeeklyViewsWithCompanyWithEmployeeNumWithWeekBeginDateWithWeekEndDateWithQuickEntryCodeWithProjectIDWithPhaseIDWithTimeTypCdWithJobNumWithAssemblySeqWithOprSeqWithRoleCdWithIndirectCodeWithExpenseCodeWithResourceGrpIDWithResourceIDWithLaborTypePseudoWithShiftWithNewRowTypeWithTimeStatusRequestBuilder.cs"; | ||
var normalizedPath = segmenter.NormalizePath(longPathName); | ||
Assert.NotEqual(longPathName, normalizedPath); | ||
Assert.True(normalizedPath.Length < longPathName.Length);//new path is shorter than the original | ||
Assert.True(normalizedPath.Length < CSharpPathSegmenter.MaxFilePathLength); // new path is shorter than the max path length | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/Kiota.Builder.Tests/PathSegmenters/JavaPathSegmenterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Kiota.Builder.CodeDOM; | ||
using Kiota.Builder.PathSegmenters; | ||
using Xunit; | ||
|
||
namespace Kiota.Builder.Tests.PathSegmenters | ||
{ | ||
public class JavaPathSegmenterTests | ||
{ | ||
private readonly JavaPathSegmenter segmenter; | ||
public JavaPathSegmenterTests() | ||
{ | ||
segmenter = new JavaPathSegmenter("D:\\source\\repos\\kiota-sample", "client"); | ||
} | ||
|
||
[Fact] | ||
public void JavaPathSegmenterGeneratesCorrectFileName() | ||
{ | ||
var fileName = segmenter.NormalizeFileName(new CodeClass | ||
{ | ||
Name = "testClass" | ||
}); | ||
Assert.Equal("TestClass", fileName);// the file name should be PascalCase | ||
} | ||
|
||
[Fact] | ||
public void JavaPathSegmenterGeneratesNamespaceFolderName() | ||
{ | ||
var namespaceName = "Microsoft.Graph"; | ||
var normalizedNamespace = segmenter.NormalizeNamespaceSegment(namespaceName); | ||
Assert.Equal("microsoft.graph", normalizedNamespace);// the file name should be lower case | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/Kiota.Builder.Tests/PathSegmenters/PythonPathSegmenterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Linq; | ||
using Kiota.Builder.CodeDOM; | ||
using Kiota.Builder.PathSegmenters; | ||
using Xunit; | ||
|
||
namespace Kiota.Builder.Tests.PathSegmenters | ||
{ | ||
public class PythonPathSegmenterTests | ||
{ | ||
private readonly PythonPathSegmenter segmenter; | ||
public PythonPathSegmenterTests() | ||
{ | ||
segmenter = new PythonPathSegmenter("D:\\source\\repos\\kiota-sample", "client"); | ||
} | ||
|
||
[Fact] | ||
public void PythonPathSegmenterGeneratesCorrectFileName() | ||
{ | ||
var rootNamespace = CodeNamespace.InitRootNamespace(); | ||
var classExample = rootNamespace.AddClass(new CodeClass | ||
{ | ||
Name = "testClass" | ||
}).First(); | ||
var fileName = segmenter.NormalizeFileName(classExample); | ||
Assert.Equal("test_class", fileName);// the file name should be snake case | ||
} | ||
|
||
[Fact] | ||
public void PythonPathSegmenterGeneratesNamespaceFolderName() | ||
{ | ||
var namespaceName = "MicrosoftGraph"; | ||
var normalizedNamespace = segmenter.NormalizeNamespaceSegment(namespaceName); | ||
Assert.Equal("microsoft_graph", normalizedNamespace);// the namespace name should be snake case | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/Kiota.Builder.Tests/PathSegmenters/TypeScriptPathSegmenterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Linq; | ||
using Kiota.Builder.CodeDOM; | ||
using Kiota.Builder.PathSegmenters; | ||
using Xunit; | ||
|
||
namespace Kiota.Builder.Tests.PathSegmenters | ||
{ | ||
public class TypeScriptPathSegmenterTests | ||
{ | ||
private readonly TypeScriptPathSegmenter segmenter; | ||
public TypeScriptPathSegmenterTests() | ||
{ | ||
segmenter = new TypeScriptPathSegmenter("D:\\source\\repos\\kiota-sample", "client"); | ||
} | ||
|
||
[Fact] | ||
public void TypeScriptPathSegmenterGeneratesCorrectFileName() | ||
{ | ||
var rootNamespace = CodeNamespace.InitRootNamespace(); | ||
var classExample = rootNamespace.AddClass(new CodeClass | ||
{ | ||
Name = "testClass" | ||
}).First(); | ||
var fileName = segmenter.NormalizeFileName(classExample); | ||
Assert.Equal("testClass", fileName);// the file name should be camelCase | ||
} | ||
|
||
[Fact] | ||
public void TypeScriptPathSegmenterGeneratesNamespaceFolderName() | ||
{ | ||
var namespaceName = "MicrosoftGraph"; | ||
var normalizedNamespace = segmenter.NormalizeNamespaceSegment(namespaceName); | ||
Assert.Equal("microsoftGraph", normalizedNamespace);// the namespace name should be camelCase | ||
} | ||
} | ||
} |