Skip to content

Commit a3bdcd1

Browse files
committed
Fix root module, fix type names, powershell version may be string
1 parent b310902 commit a3bdcd1

File tree

5 files changed

+34
-99
lines changed

5 files changed

+34
-99
lines changed

CrossCompatibility/CrossCompatibility.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@{
1010

1111
# Script module or binary module file associated with this manifest.
12-
RootModule = '.\CrossCompatibility.psm1'
12+
RootModule = 'CrossCompatibility.psm1'
1313

1414
# Version number of this module.
1515
ModuleVersion = '0.0.1'

CrossCompatibility/CrossCompatibility/Data/Platform/PowerShellData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class PowerShellData : ICloneable
1616
/// From $PSVersionTable.PSVersion.
1717
/// </summary>
1818
[DataMember]
19-
public Version Version { get; set; }
19+
public string Version { get; set; }
2020

2121
/// <summary>
2222
/// The edition of PowerShell, from

CrossCompatibility/CrossCompatibility/Query/Platform/PowerShellData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public PowerShellData(PowerShellDataMut powerShellData)
1313
_powerShellData = powerShellData;
1414
}
1515

16-
public Version Version => _powerShellData.Version;
16+
public string Version => _powerShellData.Version;
1717

1818
public string Edition => _powerShellData.Edition;
1919

CrossCompatibility/CrossCompatibility/Utility/CompatibilityProfileConversion.cs

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -486,102 +486,7 @@ private static bool HasSpecialMethodPrefix(MemberInfo member)
486486

487487
public static string GetFullTypeName(Type type)
488488
{
489-
if (TryGetGenericParameterTypeName(type, out string genericTypeParamName))
490-
{
491-
return genericTypeParamName;
492-
}
493-
494-
if (!type.IsGenericType)
495-
{
496-
return type.FullName;
497-
}
498-
499-
var sb = new StringBuilder(type.Namespace).Append('.');
500-
501-
if (!type.IsNested)
502-
{
503-
int backtickIdx = type.Name.IndexOf('`');
504-
sb.Append(type.Name.Substring(0, backtickIdx));
505-
}
506-
else
507-
{
508-
// Run up to the outermost type
509-
Type currType = type;
510-
var typePath = new Stack<Type>();
511-
do
512-
{
513-
typePath.Push(currType);
514-
currType = currType.DeclaringType;
515-
}
516-
while (currType != null);
517-
518-
// Now unspool back down to the base type
519-
while (typePath.Any())
520-
{
521-
Type innerType = typePath.Pop();
522-
523-
int backtickIdx = innerType.Name.IndexOf('`');
524-
if (backtickIdx > 0)
525-
{
526-
sb.Append(innerType.Name.Substring(0, backtickIdx));
527-
}
528-
else
529-
{
530-
sb.Append(innerType.Name);
531-
}
532-
533-
if (typePath.Any())
534-
{
535-
sb.Append('+');
536-
}
537-
}
538-
}
539-
540-
sb.Append('[');
541-
542-
Type[] typeParameters = type.GetGenericArguments();
543-
for (int i = 0; i < typeParameters.Length; i++)
544-
{
545-
sb.Append(GetFullTypeName(typeParameters[i]));
546-
547-
if (i < typeParameters.Length - 1)
548-
{
549-
sb.Append(',');
550-
}
551-
}
552-
553-
sb.Append(']');
554-
555-
return sb.ToString();
556-
}
557-
558-
private static bool TryGetGenericParameterTypeName(Type t, out string name)
559-
{
560-
if (t.IsGenericParameter)
561-
{
562-
string typeParamName = t.Name;
563-
name = new StringBuilder(typeParamName.Length + 2)
564-
.Append('<')
565-
.Append(typeParamName)
566-
.Append('>')
567-
.ToString();
568-
return true;
569-
}
570-
571-
if (t.IsByRef && TryGetGenericParameterTypeName(t.GetElementType(), out string refElementName))
572-
{
573-
name = refElementName + '&';
574-
return true;
575-
}
576-
577-
if (t.IsArray && TryGetGenericParameterTypeName(t.GetElementType(), out string arrElementName))
578-
{
579-
name = arrElementName + "[]";
580-
return true;
581-
}
582-
583-
name = null;
584-
return false;
489+
return type?.ToString();
585490
}
586491
}
587492
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Import-Module "$PSScriptRoot/../out/CrossCompatibility" -Force -ErrorAction Stop
2+
3+
Describe "Type name transformation" {
4+
BeforeAll {
5+
$typeNameTestCases = @(
6+
@{ InputType = [System.Reflection.Assembly]; ExpectedName = "System.Reflection.Assembly" }
7+
@{ InputType = [string]; ExpectedName = "System.String" }
8+
@{ InputType = [datetime]; ExpectedName = "System.DateTime" }
9+
@{ InputType = [string[]]; ExpectedName = "System.String[]" }
10+
@{ InputType = [System.Collections.Generic.List[object]]; ExpectedName = "System.Collections.Generic.List``1[System.Object]" }
11+
@{ InputType = [System.Collections.Generic.Dictionary[string, object]]; ExpectedName = "System.Collections.Generic.Dictionary``2[System.String,System.Object]" }
12+
@{ InputType = [System.Func`1]; ExpectedName = "System.Func``1[TResult]" }
13+
@{ InputType = [System.Collections.Generic.Dictionary`2]; ExpectedName = "System.Collections.Generic.Dictionary``2[TKey,TValue]" }
14+
@{ InputType = [System.Collections.Generic.Dictionary`2+Enumerator]; ExpectedName = "System.Collections.Generic.Dictionary``2+Enumerator[TKey,TValue]" }
15+
@{ InputType = [System.Collections.Generic.Dictionary[string,object]].GetNestedType('Enumerator'); ExpectedName = "System.Collections.Generic.Dictionary``2+Enumerator[TKey,TValue]" }
16+
@{ InputType = [System.Collections.Concurrent.ConcurrentDictionary`2].GetMethod('ToArray').ReturnType; ExpectedName = "System.Collections.Generic.KeyValuePair``2[TKey,TValue][]"}
17+
)
18+
}
19+
20+
It "Serializes the name of type <InputType> to <ExpectedName>" -TestCases $typeNameTestCases {
21+
param([type]$InputType, [string]$ExpectedName)
22+
23+
$name = [Microsoft.PowerShell.CrossCompatibility.Utility.TypeDataConversion]::GetFullTypeName($InputType)
24+
$name | Should -BeExactly $ExpectedName
25+
}
26+
27+
It "Null type gives null type name" {
28+
[Microsoft.PowerShell.CrossCompatibility.Utility.TypeDataConversion]::GetFullTypeName($null) | Should -Be $null
29+
}
30+
}

0 commit comments

Comments
 (0)