1+ // Copyright (c) 2013-2025 Cem Dervis, MIT License.
2+ // https://sharpconfig.org
3+
4+ using NUnit . Framework ;
5+ using SharpConfig ;
6+
7+ namespace Tests
8+ {
9+ internal sealed class DummyClass
10+ {
11+ public int Value ;
12+ }
13+
14+ internal sealed class DummyTypeStringConverter : ITypeStringConverter
15+ {
16+ public Type ConvertibleType => typeof ( DummyClass ) ;
17+
18+ public string ConvertToString ( object value )
19+ {
20+ return ( value as DummyClass ) ! . Value . ToString ( ) ;
21+ }
22+
23+ public object ? TryConvertFromString ( string value , Type hint )
24+ {
25+ return int . TryParse ( value , out int result ) ? new DummyClass { Value = result } : null ;
26+ }
27+ }
28+
29+ [ TestFixture ]
30+ public sealed class InvalidInputTest
31+ {
32+ [ Test ]
33+ public void ConfigLoading ( )
34+ {
35+ var cfg = new Configuration ( ) ;
36+
37+ Assert . Throws < ArgumentNullException > ( ( ) => Configuration . LoadFromFile ( null ) ) ;
38+ Assert . Throws < FileNotFoundException > ( ( ) => Configuration . LoadFromFile ( "doesnotexist.ini" ) ) ;
39+ Assert . Throws < ArgumentNullException > ( ( ) => Configuration . LoadFromStream ( null ) ) ;
40+ Assert . Throws < ArgumentNullException > ( ( ) => Configuration . LoadFromString ( null ) ) ;
41+ Assert . Throws < ArgumentNullException > ( ( ) => Configuration . LoadFromBinaryFile ( null ) ) ;
42+ Assert . Throws < ArgumentNullException > ( ( ) => Configuration . LoadFromBinaryStream ( null ) ) ;
43+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . SaveToFile ( null ) ) ;
44+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . SaveToStream ( null ) ) ;
45+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . SaveToBinaryFile ( null ) ) ;
46+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . SaveToBinaryStream ( null ) ) ;
47+ }
48+
49+ [ Test ]
50+ public void EmptyObjects ( )
51+ {
52+ Assert . Throws < ArgumentNullException > ( ( ) => new Setting ( null ) ) ;
53+ Assert . Throws < ArgumentNullException > ( ( ) => new Section ( null ) ) ;
54+ }
55+
56+ [ Test ]
57+ public void Options ( )
58+ {
59+ Assert . Throws < ArgumentNullException > ( ( ) => Configuration . CultureInfo = null ) ;
60+ Assert . Throws < ArgumentException > ( ( ) => Configuration . PreferredCommentChar = 'a' ) ;
61+ Assert . Throws < ArgumentException > ( ( ) => Configuration . ArrayElementSeparator = '\0 ' ) ;
62+ }
63+
64+ [ Test ]
65+ public void ConfigSectionOperations ( )
66+ {
67+ var cfg = new Configuration ( ) ;
68+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . Add ( ( Section ) null ) ) ;
69+
70+ var section = new Section ( "SomeSection" ) ;
71+ Assert . DoesNotThrow ( ( ) => cfg . Add ( section ) ) ;
72+
73+ Assert . Throws < ArgumentException > ( ( ) => cfg . Add ( section ) ) ;
74+
75+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . Remove ( ( string ) null ) ) ;
76+ Assert . DoesNotThrow ( ( ) => cfg . Remove ( "SomeSection" ) ) ;
77+
78+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . RemoveAllNamed ( ( string ) null ) ) ;
79+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . Contains ( ( string ) null ) ) ;
80+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . Contains ( "A" , null ) ) ;
81+ Assert . Throws < ArgumentNullException > ( ( ) => cfg . Contains ( null , "B" ) ) ;
82+ Assert . DoesNotThrow ( ( ) => cfg . Contains ( "A" , "B" ) ) ;
83+
84+ Assert . DoesNotThrow ( ( ) => cfg . Add ( "SomeSection" ) ) ;
85+
86+ Assert . DoesNotThrow ( ( ) => cfg [ 0 ] . ToString ( ) ) ;
87+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => cfg [ - 1 ] . ToString ( ) ) ;
88+ }
89+
90+ [ Test ]
91+ public void Sections ( )
92+ {
93+ var section = new Section ( "SomeSection" ) ;
94+ section . Add ( "S" ) ;
95+
96+ Assert . Throws < ArgumentException > ( ( ) => Section . FromObject ( null , 1 ) ) ;
97+ Assert . Throws < ArgumentNullException > ( ( ) => Section . FromObject ( "A" , null ) ) ;
98+ Assert . Throws < ArgumentNullException > ( ( ) => section . ToObject ( null ) ) ;
99+ Assert . Throws < ArgumentNullException > ( ( ) => section . GetValuesFrom ( null ) ) ;
100+ Assert . Throws < ArgumentNullException > ( ( ) => section . SetValuesTo ( null ) ) ;
101+ Assert . Throws < ArgumentNullException > ( ( ) => section . Remove ( ( string ) null ) ) ;
102+ Assert . Throws < ArgumentNullException > ( ( ) => section . Remove ( ( Setting ) null ) ) ;
103+ Assert . Throws < ArgumentNullException > ( ( ) => section . Remove ( "" ) ) ;
104+ Assert . Throws < ArgumentNullException > ( ( ) => section . RemoveAllNamed ( null ) ) ;
105+ Assert . Throws < ArgumentNullException > ( ( ) => section . Contains ( ( string ) null ) ) ;
106+ Assert . DoesNotThrow ( ( ) => section [ 0 ] . ToString ( ) ) ;
107+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => section [ - 1 ] . ToString ( ) ) ;
108+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => section [ 1 ] . ToString ( ) ) ;
109+ }
110+
111+ [ Test ]
112+ public void TypeStringConverterRegistration ( )
113+ {
114+ Assert . Throws < ArgumentNullException > ( ( ) => Configuration . RegisterTypeStringConverter ( null ) ) ;
115+
116+ var converter = new DummyTypeStringConverter ( ) ;
117+ Assert . DoesNotThrow ( ( ) => Configuration . RegisterTypeStringConverter ( converter ) ) ;
118+
119+ Assert . Throws < InvalidOperationException > ( ( ) => Configuration . RegisterTypeStringConverter ( converter ) ) ;
120+
121+ Assert . Throws < ArgumentNullException > ( ( ) => Configuration . DeregisterTypeStringConverter ( null ) ) ;
122+ Assert . DoesNotThrow ( ( ) => Configuration . DeregisterTypeStringConverter ( typeof ( DummyClass ) ) ) ;
123+ Assert . Throws < InvalidOperationException > ( ( ) => Configuration . DeregisterTypeStringConverter ( typeof ( DummyClass ) ) ) ;
124+
125+ Assert . Throws < ArgumentNullException > ( ( ) => Configuration . FindTypeStringConverter ( null ) ) ;
126+ }
127+ }
128+ }
0 commit comments