File: AliasTextTests.cs

package info (click to toggle)
nini 1.1.0%2Bdfsg.3-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 880 kB
  • sloc: cs: 7,649; xml: 2,945; makefile: 28; ansic: 7
file content (143 lines) | stat: -rw-r--r-- 3,932 bytes parent folder | download | duplicates (7)
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
#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.Config;
using NUnit.Framework;

namespace Nini.Test.Config
{
	[TestFixture]
	public class AliasTextTests
	{
		[Test]
		public void GetBoolean ()
		{
			AliasText alias = new AliasText ();
			
			Assert.IsFalse (alias.ContainsBoolean ("on"));
			Assert.IsFalse (alias.ContainsBoolean ("off"));
			alias.AddAlias ("oN", true);
			alias.AddAlias ("oFF", false);
			
			Assert.IsTrue (alias.ContainsBoolean ("oN"));
			Assert.IsTrue (alias.ContainsBoolean ("off"));
			
			Assert.IsTrue (alias.GetBoolean ("oN"));
			Assert.IsFalse (alias.GetBoolean ("OfF"));
		}

		[Test]
		public void GetDefaultAliases ()
		{
			AliasText alias = new AliasText ();
			
			Assert.IsTrue (alias.ContainsBoolean ("true"));
			Assert.IsTrue (alias.ContainsBoolean ("false"));
		
			Assert.IsTrue (alias.GetBoolean ("tRUe"));
			Assert.IsFalse (alias.GetBoolean ("FaLse"));
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void NonExistantBooleanText ()
		{
			AliasText alias = new AliasText ();
			alias.AddAlias ("true", true);
			alias.AddAlias ("faLSe", false);
			
			Assert.IsTrue (alias.GetBoolean ("Not present"));
		}
		
		[Test]
		public void GetInt ()
		{
			AliasText alias = new AliasText ();
			
			Assert.IsFalse (alias.ContainsInt ("error code", "warn"));
			Assert.IsFalse (alias.ContainsInt ("error code", "error"));
			alias.AddAlias ("error code", "WaRn", 100);
			alias.AddAlias ("error code", "ErroR", 200);
			
			Assert.IsTrue (alias.ContainsInt ("error code", "warn"));
			Assert.IsTrue (alias.ContainsInt ("error code", "error"));
		
			Assert.AreEqual (100, alias.GetInt ("error code", "warn"));
			Assert.AreEqual (200, alias.GetInt ("error code", "ErroR"));
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void GetIntNonExistantText ()
		{
			AliasText alias = new AliasText ();
			alias.AddAlias ("error code", "WaRn", 100);
			
			Assert.AreEqual (100, alias.GetInt ("error code", "not here"));
		}
		
		[Test]
		[ExpectedException (typeof (ArgumentException))]
		public void GetIntNonExistantKey ()
		{
			AliasText alias = new AliasText ();
			alias.AddAlias ("error code", "WaRn", 100);
			
			Assert.AreEqual (100, alias.GetInt ("not exist", "warn"));
		}
		
		[Test]
		public void GetIntEnum ()
		{
			AliasText alias = new AliasText ();
			alias.AddAlias ("node type", new System.Xml.XmlNodeType ());
			
			Assert.AreEqual ((int)System.Xml.XmlNodeType.Text, 
							 alias.GetInt ("node type", "teXt"));
			Assert.AreEqual ((int)System.Xml.XmlNodeType.Attribute, 
							 alias.GetInt ("node type", "aTTribute"));
			
			try
			{
				alias.GetInt ("node type", "not here");
			}
			catch
			{
			}
		}
		
		[Test]
		public void GlobalAlias ()
		{
			StringWriter writer = new StringWriter ();
			writer.WriteLine ("[Test]");
			writer.WriteLine (" TurnOff = true");
			writer.WriteLine (" ErrorCode = WARN");
			IniConfigSource source = 
					new IniConfigSource (new StringReader (writer.ToString ()));
			
			source.Alias.AddAlias ("true", true);
			source.Alias.AddAlias ("ErrorCode", "warn", 35);
			
			IConfig config = source.Configs["Test"];
			
			Assert.AreEqual (35, config.GetInt ("ErrorCode", true));
			Assert.IsTrue (config.GetBoolean ("TurnOff"));
			
			config.Alias.AddAlias ("true", false);
			config.Alias.AddAlias ("ErrorCode", "warn", 45);
			
			Assert.AreEqual (45, config.GetInt ("ErrorCode", true));
			Assert.IsFalse (config.GetBoolean ("TurnOff"));
		}
	}
}