@@ -26,6 +26,9 @@ public static void NegativeTests()
26
26
27
27
// Passing a string which does NOT start with one of the valid base key names, that should throw ArgumentException.
28
28
Assert . Throws < ArgumentException > ( ( ) => Registry . GetValue ( "HHHH_MMMM" , null , null ) ) ;
29
+
30
+ // Should throw if passed string which only starts with one of the valid base key names but actually it isn't valid.
31
+ Assert . Throws < ArgumentException > ( ( ) => Registry . GetValue ( "HKEY_CURRENT_USER_FOOBAR" , null , null ) ) ;
29
32
}
30
33
31
34
[ Fact ]
@@ -55,19 +58,26 @@ public static IEnumerable<object[]> TestRegistryKeys
55
58
{
56
59
return new [ ]
57
60
{
58
- new object [ ] { Registry . CurrentUser , "Test1" } ,
59
- new object [ ] { Registry . LocalMachine , "Test2" } ,
60
- new object [ ] { Registry . ClassesRoot , "Test3" } ,
61
- new object [ ] { Registry . Users , "Test4" } ,
62
- new object [ ] { Registry . PerformanceData , "Test5" } ,
63
- new object [ ] { Registry . CurrentConfig , "Test6" } ,
61
+ new object [ ] { Registry . CurrentUser , "Test1" , true } ,
62
+ new object [ ] { Registry . LocalMachine , "Test2" , true } ,
63
+ new object [ ] { Registry . ClassesRoot , "Test3" , true } ,
64
+ new object [ ] { Registry . Users , "Test4" , true } ,
65
+ new object [ ] { Registry . PerformanceData , "Test5" , true } ,
66
+ new object [ ] { Registry . CurrentConfig , "Test6" , true } ,
67
+
68
+ new object [ ] { Registry . CurrentUser , "Test7" , false } ,
69
+ new object [ ] { Registry . LocalMachine , "Test8" , false } ,
70
+ new object [ ] { Registry . ClassesRoot , "Test9" , false } ,
71
+ new object [ ] { Registry . Users , "Test10" , false } ,
72
+ new object [ ] { Registry . PerformanceData , "Test11" , false } ,
73
+ new object [ ] { Registry . CurrentConfig , "Test12" , false } ,
64
74
} ;
65
75
}
66
76
}
67
77
68
78
[ Theory ]
69
79
[ MemberData ( "TestRegistryKeys" ) ]
70
- public static void GetValueFromDifferentKeys ( RegistryKey key , string valueName )
80
+ public static void GetValueFromDifferentKeys ( RegistryKey key , string valueName , bool useSeparator )
71
81
{
72
82
const int expectedValue = 11 ;
73
83
const int defaultValue = 42 ;
@@ -76,7 +86,9 @@ public static void GetValueFromDifferentKeys(RegistryKey key, string valueName)
76
86
key . SetValue ( valueName , expectedValue ) ;
77
87
try
78
88
{
79
- Assert . Equal ( expectedValue , ( int ) Registry . GetValue ( key . Name , valueName , defaultValue ) ) ;
89
+ // keyName should be case insensitive so we mix casing
90
+ string keyName = MixUpperAndLowerCase ( key . Name ) + ( useSeparator ? "\\ " : "" ) ;
91
+ Assert . Equal ( expectedValue , ( int ) Registry . GetValue ( keyName , valueName , defaultValue ) ) ;
80
92
}
81
93
finally
82
94
{
@@ -89,21 +101,43 @@ public static void GetValueFromDifferentKeys(RegistryKey key, string valueName)
89
101
90
102
[ Theory ]
91
103
[ MemberData ( "TestRegistryKeys" ) ]
92
- public static void GetDefaultValueFromDifferentKeys ( RegistryKey key , string valueName )
104
+ public static void GetDefaultValueFromDifferentKeys ( RegistryKey key , string valueName , bool useSeparator )
93
105
{
106
+ // We ignore valueName because we test against default value
94
107
valueName = null ;
95
108
try
96
109
{
110
+ // keyName should be case insensitive so we mix casing
111
+ string keyName = MixUpperAndLowerCase ( key . Name ) + ( useSeparator ? "\\ " : "" ) ;
97
112
if ( key . IsDefaultValueSet ( ) )
98
113
{
99
- Registry . GetValue ( key . Name , valueName , null ) ;
114
+ Registry . GetValue ( keyName , valueName , null ) ;
100
115
}
101
116
else
102
117
{
103
- Assert . Equal ( TestData . DefaultValue , Registry . GetValue ( key . Name , valueName , TestData . DefaultValue ) ) ;
118
+ Assert . Equal ( TestData . DefaultValue , Registry . GetValue ( keyName , valueName , TestData . DefaultValue ) ) ;
104
119
}
105
120
}
106
121
catch ( IOException ) { }
107
122
}
123
+
124
+ private static string MixUpperAndLowerCase ( string str )
125
+ {
126
+ var builder = new System . Text . StringBuilder ( str ) ;
127
+
128
+ for ( int i = 0 ; i < builder . Length ; ++ i )
129
+ {
130
+ if ( i % 2 == 0 )
131
+ {
132
+ builder [ i ] = char . ToLowerInvariant ( builder [ i ] ) ;
133
+ }
134
+ else
135
+ {
136
+ builder [ i ] = char . ToUpperInvariant ( builder [ i ] ) ;
137
+ }
138
+ }
139
+
140
+ return builder . ToString ( ) ;
141
+ }
108
142
}
109
143
}
0 commit comments