1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
|
#region Copyright
//
// Nini Configuration Project.
// Copyright (C) 2006 Brent R. Matzelle. All rights reserved.
//
// This software is published under the terms of the MIT X11 license, a copy of
// which has been included with this distribution in the LICENSE.txt file.
//
#endregion
using System;
using System.IO;
using Nini.Ini;
using NUnit.Framework;
namespace Nini.Test.Ini
{
[TestFixture]
public class IniDocumentTests
{
[Test]
public void GetSection ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("; Test");
writer.WriteLine ("[Nini Thing]");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
Assert.AreEqual (1, doc.Sections.Count);
Assert.AreEqual ("Nini Thing", doc.Sections["Nini Thing"].Name);
Assert.AreEqual ("Nini Thing", doc.Sections[0].Name);
Assert.IsNull (doc.Sections["Non Existant"]);
}
[Test]
public void GetKey ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("[Nini]");
writer.WriteLine (" my key = something");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
IniSection section = doc.Sections["Nini"];
Assert.IsTrue (section.Contains ("my key"));
Assert.AreEqual ("something", section.GetValue ("my key"));
Assert.IsFalse (section.Contains ("not here"));
}
[Test]
public void SetSection ()
{
IniDocument doc = new IniDocument ();
IniSection section = new IniSection ("new section");
doc.Sections.Add (section);
Assert.AreEqual ("new section", doc.Sections[0].Name);
Assert.AreEqual ("new section", doc.Sections["new section"].Name);
section = new IniSection ("a section", "a comment");
doc.Sections.Add (section);
Assert.AreEqual ("a comment", doc.Sections[1].Comment);
}
[Test]
public void SetKey ()
{
IniDocument doc = new IniDocument ();
IniSection section = new IniSection ("new section");
doc.Sections.Add (section);
section.Set ("new key", "some value");
Assert.IsTrue (section.Contains ("new key"));
Assert.AreEqual ("some value", section.GetValue ("new key"));
}
[Test]
[ExpectedException (typeof (IniException))]
public void ParserError ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("[Nini Thing");
writer.WriteLine (" my key = something");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
}
[Test]
public void RemoveSection ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("[Nini Thing]");
writer.WriteLine (" my key = something");
writer.WriteLine ("[Parser]");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
Assert.IsNotNull (doc.Sections["Nini Thing"]);
doc.Sections.Remove ("Nini Thing");
Assert.IsNull (doc.Sections["Nini Thing"]);
}
[Test]
public void RemoveKey ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("[Nini]");
writer.WriteLine (" my key = something");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
Assert.IsTrue (doc.Sections["Nini"].Contains ("my key"));
doc.Sections["Nini"].Remove ("my key");
Assert.IsFalse (doc.Sections["Nini"].Contains ("my key"));
}
[Test]
public void GetAllKeys ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("[Nini]");
writer.WriteLine (" ; a comment");
writer.WriteLine (" my key = something");
writer.WriteLine (" dog = rover");
writer.WriteLine (" cat = muffy");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
IniSection section = doc.Sections["Nini"];
Assert.AreEqual (4, section.ItemCount);
Assert.AreEqual (3, section.GetKeys ().Length);
Assert.AreEqual ("my key", section.GetKeys ()[0]);
Assert.AreEqual ("dog", section.GetKeys ()[1]);
Assert.AreEqual ("cat", section.GetKeys ()[2]);
}
[Test]
public void SaveDocumentWithComments ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("; some comment");
writer.WriteLine (""); // empty line
writer.WriteLine ("[new section]");
writer.WriteLine (" dog = rover");
writer.WriteLine (""); // Empty line
writer.WriteLine ("; a comment");
writer.WriteLine (" cat = muffy");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
StringWriter newWriter = new StringWriter ();
doc.Save (newWriter);
StringReader reader = new StringReader (newWriter.ToString ());
Assert.AreEqual ("; some comment", reader.ReadLine ());
Assert.AreEqual ("", reader.ReadLine ());
Assert.AreEqual ("[new section]", reader.ReadLine ());
Assert.AreEqual ("dog = rover", reader.ReadLine ());
Assert.AreEqual ("", reader.ReadLine ());
Assert.AreEqual ("; a comment", reader.ReadLine ());
Assert.AreEqual ("cat = muffy", reader.ReadLine ());
writer.Close ();
}
[Test]
public void SaveToStream ()
{
string filePath = "SaveToStream.ini";
FileStream stream = new FileStream (filePath, FileMode.Create);
// Create a new document and save to stream
IniDocument doc = new IniDocument ();
IniSection section = new IniSection ("Pets");
section.Set ("dog", "rover");
section.Set ("cat", "muffy");
doc.Sections.Add (section);
doc.Save (stream);
stream.Close ();
IniDocument newDoc = new IniDocument (new FileStream (filePath,
FileMode.Open));
section = newDoc.Sections["Pets"];
Assert.IsNotNull (section);
Assert.AreEqual (2, section.GetKeys ().Length);
Assert.AreEqual ("rover", section.GetValue ("dog"));
Assert.AreEqual ("muffy", section.GetValue ("cat"));
stream.Close ();
File.Delete (filePath);
}
[Test]
public void SambaStyleDocument ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("; some comment");
writer.WriteLine ("# another comment"); // empty line
writer.WriteLine ("[test]");
writer.WriteLine (" cat = cats are not tall\\ ");
writer.WriteLine (" animals ");
writer.WriteLine (" dog = dogs \\ ");
writer.WriteLine (" do not eat cats ");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()),
IniFileType.SambaStyle);
Assert.AreEqual ("cats are not tall animals",
doc.Sections["test"].GetValue ("cat"));
Assert.AreEqual ("dogs do not eat cats",
doc.Sections["test"].GetValue ("dog"));
}
[Test]
public void PythonStyleDocument ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("; some comment");
writer.WriteLine ("# another comment"); // empty line
writer.WriteLine ("[test]");
writer.WriteLine (" cat: cats are not tall animals ");
writer.WriteLine (" dog : dogs bark");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()),
IniFileType.PythonStyle);
Assert.AreEqual ("cats are not tall animals",
doc.Sections["test"].GetValue ("cat"));
Assert.AreEqual ("dogs bark", doc.Sections["test"].GetValue ("dog"));
}
[Test]
public void DuplicateSections ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("[Test]");
writer.WriteLine (" my key = something");
writer.WriteLine ("[Test]");
writer.WriteLine (" another key = something else");
writer.WriteLine ("[Test]");
writer.WriteLine (" value 0 = something 0");
writer.WriteLine (" value 1 = something 1");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
Assert.IsNotNull (doc.Sections["Test"]);
Assert.AreEqual (1, doc.Sections.Count);
Assert.AreEqual (2, doc.Sections["Test"].ItemCount);
Assert.IsNull (doc.Sections["Test"].GetValue ("my key"));
Assert.IsNotNull (doc.Sections["Test"].GetValue ("value 0"));
Assert.IsNotNull (doc.Sections["Test"].GetValue ("value 1"));
}
[Test]
public void DuplicateKeys ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("[Test]");
writer.WriteLine (" a value = something 0");
writer.WriteLine (" a value = something 1");
writer.WriteLine (" a value = something 2");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()));
Assert.IsNotNull (doc.Sections["Test"]);
Assert.AreEqual (1, doc.Sections.Count);
Assert.AreEqual (1, doc.Sections["Test"].ItemCount);
Assert.IsNotNull (doc.Sections["Test"].GetValue ("a value"));
Assert.AreEqual ("something 0", doc.Sections["Test"].GetValue ("a value"));
}
[Test]
public void MysqlStyleDocument ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("# another comment"); // empty line
writer.WriteLine ("[test]");
writer.WriteLine (" quick ");
writer.WriteLine (" cat = cats are not tall animals ");
writer.WriteLine (" dog : dogs bark");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()),
IniFileType.MysqlStyle);
Assert.IsTrue (doc.Sections["test"].Contains ("quick"));
Assert.AreEqual ("", doc.Sections["test"].GetValue ("quick"));
Assert.AreEqual ("cats are not tall animals",
doc.Sections["test"].GetValue ("cat"));
Assert.AreEqual ("dogs bark", doc.Sections["test"].GetValue ("dog"));
}
[Test]
public void WindowsStyleDocument ()
{
StringWriter writer = new StringWriter ();
writer.WriteLine ("; another comment"); // empty line
writer.WriteLine ("[test]");
writer.WriteLine (" cat = cats are not ; tall ");
writer.WriteLine (" dog = dogs \"bark\"");
IniDocument doc = new IniDocument (new StringReader (writer.ToString ()),
IniFileType.WindowsStyle);
IniSection section = doc.Sections["test"];
Assert.AreEqual ("cats are not ; tall", section.GetValue ("cat"));
Assert.AreEqual ("dogs \"bark\"", section.GetValue ("dog"));
}
[Test]
public void SaveAsPythonStyle ()
{
string filePath = "Save.ini";
FileStream stream = new FileStream (filePath, FileMode.Create);
// Create a new document and save to stream
IniDocument doc = new IniDocument ();
doc.FileType = IniFileType.PythonStyle;
IniSection section = new IniSection ("Pets");
section.Set ("my comment");
section.Set ("dog", "rover");
doc.Sections.Add (section);
doc.Save (stream);
stream.Close ();
StringWriter writer = new StringWriter ();
writer.WriteLine ("[Pets]");
writer.WriteLine ("# my comment");
writer.WriteLine ("dog : rover");
StreamReader reader = new StreamReader (filePath);
Assert.AreEqual (writer.ToString (), reader.ReadToEnd ());
reader.Close ();
File.Delete (filePath);
}
[Test]
public void SaveAsMysqlStyle ()
{
string filePath = "Save.ini";
FileStream stream = new FileStream (filePath, FileMode.Create);
// Create a new document and save to stream
IniDocument doc = new IniDocument ();
doc.FileType = IniFileType.MysqlStyle;
IniSection section = new IniSection ("Pets");
section.Set ("my comment");
section.Set ("dog", "rover");
doc.Sections.Add (section);
doc.Save (stream);
stream.Close ();
StringWriter writer = new StringWriter ();
writer.WriteLine ("[Pets]");
writer.WriteLine ("# my comment");
writer.WriteLine ("dog = rover");
StreamReader reader = new StreamReader (filePath);
Assert.AreEqual (writer.ToString (), reader.ReadToEnd ());
reader.Close ();
IniDocument iniDoc = new IniDocument ();
iniDoc.FileType = IniFileType.MysqlStyle;
iniDoc.Load (filePath);
File.Delete (filePath);
}
[Test]
[ExpectedException (typeof (IniException))]
public void SambaLoadAsStandard ()
{
string filePath = "Save.ini";
FileStream stream = new FileStream (filePath, FileMode.Create);
// Create a new document and save to stream
IniDocument doc = new IniDocument ();
doc.FileType = IniFileType.SambaStyle;
IniSection section = new IniSection ("Pets");
section.Set ("my comment");
section.Set ("dog", "rover");
doc.Sections.Add (section);
doc.Save (stream);
stream.Close ();
IniDocument iniDoc = new IniDocument ();
iniDoc.FileType = IniFileType.Standard;
iniDoc.Load (filePath);
File.Delete (filePath);
}
}
}
|