Created
August 21, 2017 13:33
-
-
Save alx9r/fec92c03387d694ad2b9dee48cd7d4be to your computer and use it in GitHub Desktop.
experimental string type to overcome https://github.com/PowerShell/PowerShell/issues/4616
This file contains hidden or 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
Add-Type @" | |
public class NullsafeString : System.IEquatable<string>,System.IConvertible | |
{ | |
public string Value = null; | |
public NullsafeString(string v = null) | |
{ | |
Value = v; | |
} | |
public override bool Equals(System.Object obj) | |
{ | |
return Value.Equals(obj); | |
} | |
public override int GetHashCode() | |
{ | |
return Value.GetHashCode(); | |
} | |
public bool Equals(string other) | |
{ | |
return Value.Equals(other); | |
} | |
public System.TypeCode GetTypeCode() | |
{ | |
return Value.GetTypeCode(); | |
} | |
public bool ToBoolean(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToBoolean(provider); | |
} | |
public byte ToByte(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToByte(provider); | |
} | |
public char ToChar(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToChar(provider); | |
} | |
public System.DateTime ToDateTime(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToDateTime(provider); | |
} | |
public decimal ToDecimal(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToDecimal(provider); | |
} | |
public double ToDouble(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToDouble(provider); | |
} | |
public short ToInt16(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToInt16(provider); | |
} | |
public int ToInt32(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToInt32(provider); | |
} | |
public long ToInt64(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToInt64(provider); | |
} | |
public sbyte ToSByte(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToSByte(provider); | |
} | |
public float ToSingle(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToSingle(provider); | |
} | |
override public System.String ToString() | |
{ | |
return Value; | |
} | |
public string ToString(System.IFormatProvider provider) | |
{ | |
return Value.ToString(provider); | |
} | |
public object ToType(System.Type conversionType, System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToType(conversionType, provider); | |
} | |
public ushort ToUInt16(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToUInt16(provider); | |
} | |
public uint ToUInt32(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToUInt32(provider); | |
} | |
public ulong ToUInt64(System.IFormatProvider provider) | |
{ | |
return ((System.IConvertible)Value).ToUInt64(provider); | |
} | |
}; | |
"@ | |
Describe '[NullsafeString]' { | |
It 'assignment' { | |
[NullsafeString]$r = 'some string' | |
$r.GetType() | Should be 'NullSafeString' | |
$r.Value | Should be 'some string' | |
} | |
Context 'as parameter static type' { | |
function f { param([NullsafeString]$x) $x } | |
It 'null remains null' { | |
$r = f -x $null | |
$null -eq $r | Should be $true | |
} | |
It '[string]::empty remains [string]::empty' { | |
$r = f -x ([string]::Empty) | |
[string]::Empty -eq $r.Value | Should be $true | |
} | |
It 'value remains value' { | |
$r = f -x 'some string' | |
$r.Value | Should be 'some string' | |
} | |
} | |
Context 'as argument to [string] parameter' { | |
function f { param([string]$x) $x } | |
It 'null becomes [string]::empty' { | |
$r = f -x $null | |
[string]::Empty -eq $r | Should be $true | |
} | |
It '[string]::empty remains [string]::empty' { | |
$r = f -x ([string]::Empty) | |
[string]::Empty -eq $r | Should be $true | |
} | |
It 'value remains value' { | |
[NullsafeString]$v = 'some value' | |
$r = f -x $v | |
$r | Should be 'some value' | |
} | |
} | |
Context 'equation of equals' { | |
It 'null with null' { | |
[NullsafeString]$r = $null | |
$null -eq $r | Should be $true | |
$r -eq $null | Should be $true | |
} | |
It '[string]::empty with [NullsafeString] containing [string]::empty' { | |
[string]$s = [string]::Empty | |
[NullsafeString]$ns = [string]::Empty | |
$s -eq $ns | Should be $true | |
$ns -eq $s | Should be $true | |
} | |
It 'value with [NullsafeString] containing value' { | |
[string]$s = 'some value' | |
[NullsafeString]$ns = 'some value' | |
$s -eq $ns | Should be $true | |
$ns -eq $s | Should be $true | |
} | |
} | |
Context 'equation of unequals' { | |
It 'null with [NullsafeString] containing value' { | |
[NullsafeString]$r = $null | |
'value' -eq $r | Should be $false | |
$r -eq 'value' | Should be $false | |
} | |
It 'null with [NullsafeString] containing [string]::empty' { | |
[NullsafeString]$ns = [string]::Empty | |
$null -eq $ns | Should be $false | |
$ns -eq $null | Should be $false | |
} | |
It 'value with [NullsafeString] containing [string]::empty' { | |
[NullsafeString]$ns = [string]::Empty | |
'value' -eq $ns | Should be $false | |
$ns -eq 'value' | Should be $false | |
} | |
It '[string]::empty with [NullsafeString] containing value' { | |
[NullsafeString]$ns = 'value' | |
[string]::Empty -eq $ns | Should be $false | |
$ns -eq [string]::Empty | Should be $false | |
} | |
It 'value with [NullsafeString] containing value' { | |
[string]$s = 'some value' | |
[NullsafeString]$ns = 'some value' | |
$s -eq $ns | Should be $true | |
$ns -eq $s | Should be $true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment