Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
679 changes: 679 additions & 0 deletions src/System.ComponentModel.TypeConverter/tests/BindingListTests.cs

Large diffs are not rendered by default.

742 changes: 742 additions & 0 deletions src/System.ComponentModel.TypeConverter/tests/ContainerTests.cs

Large diffs are not rendered by default.

186 changes: 186 additions & 0 deletions src/System.ComponentModel.TypeConverter/tests/ContextStackTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Licensed to the .NET Foundation under one or more agreements.
// See the LICENSE file in the project root for more information.

//
// ContextStackTest.cs - Unit tests for
// System.ComponentModel.Design.Serialization.ContextStack
//
// Author:
// Ivan N. Zlatev <contact@i-nz.net>
//
// Copyright (C) 2007 Ivan N. Zlatev <contact@i-nz.net>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//


using System;
using System.ComponentModel.Design.Serialization;
using Xunit;

namespace System.ComponentModel.Tests
{
public class ContextStackTest
{
[Fact]
public void IntegrityTest()
{
ContextStack stack = new ContextStack();

string one = "one";
string two = "two";
stack.Push(two);
stack.Push(one);
Assert.Same(one, stack[typeof(string)]);
Assert.Same(one, stack[0]);
Assert.Same(one, stack.Current);

Assert.Same(one, stack.Pop());

Assert.Same(two, stack[typeof(string)]);
Assert.Same(two, stack[0]);
Assert.Same(two, stack.Current);

string three = "three";
stack.Append(three);

Assert.Same(two, stack[typeof(string)]);
Assert.Same(two, stack[0]);
Assert.Same(two, stack.Current);

Assert.Same(two, stack.Pop());

Assert.Same(three, stack[typeof(string)]);
Assert.Same(three, stack[0]);
Assert.Same(three, stack.Current);
Assert.Same(three, stack.Pop());

Assert.Null(stack.Pop());
Assert.Null(stack.Current);
}

[Fact]
public void Append_Context_Null()
{
ContextStack stack = new ContextStack();
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => stack.Append(null));
Assert.Equal(typeof(ArgumentNullException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.Equal("context", ex.ParamName);
}

[Fact] // Item (Int32)
public void Indexer1()
{
ContextStack stack = new ContextStack();
string one = "one";
string two = "two";

stack.Push(one);
stack.Push(two);

Assert.Same(two, stack[0]);
Assert.Same(one, stack[1]);
Assert.Null(stack[2]);
Assert.Same(two, stack.Pop());
Assert.Same(one, stack[0]);
Assert.Null(stack[1]);
Assert.Same(one, stack.Pop());
Assert.Null(stack[0]);
Assert.Null(stack[1]);
}

[Fact] // Item (Int32)
public void Indexer1_Level_Negative()
{
ContextStack stack = new ContextStack();
stack.Push(new Foo());
ArgumentOutOfRangeException ex;

ex = Assert.Throws<ArgumentOutOfRangeException>(() => stack[-1]);
Assert.Equal(typeof(ArgumentOutOfRangeException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.Equal(new ArgumentOutOfRangeException("level").Message, ex.Message);
Assert.Equal("level", ex.ParamName);


ex = Assert.Throws<ArgumentOutOfRangeException>(() => stack[-5]);
Assert.Equal(typeof(ArgumentOutOfRangeException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.Equal(new ArgumentOutOfRangeException("level").Message, ex.Message);
Assert.Equal("level", ex.ParamName);
}

[Fact] // Item (Type)
public void Indexer2()
{
ContextStack stack = new ContextStack();

Foo foo = new Foo();
FooBar foobar = new FooBar();

stack.Push(foobar);
stack.Push(foo);
Assert.Same(foo, stack[typeof(Foo)]);
Assert.Same(foo, stack[typeof(IFoo)]);
Assert.Same(foo, stack.Pop());
Assert.Same(foobar, stack[typeof(Foo)]);
Assert.Same(foobar, stack[typeof(FooBar)]);
Assert.Same(foobar, stack[typeof(IFoo)]);
Assert.Null(stack[typeof(string)]);
}

[Fact] // Item (Type)
public void Indexer2_Type_Null()
{
ContextStack stack = new ContextStack();
stack.Push(new Foo());
ArgumentNullException ex = Assert.Throws<ArgumentNullException>(() => stack[(Type)null]);
Assert.Equal(typeof(ArgumentNullException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.Equal("type", ex.ParamName);
}

[Fact]
public void Push_Context_Null()
{
ContextStack stack = new ContextStack();
ArgumentNullException ex= Assert.Throws<ArgumentNullException>(()=> stack.Push(null));
Assert.Equal(typeof(ArgumentNullException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.Equal("context", ex.ParamName);
}

public interface IFoo
{
}

public class Foo : IFoo
{
}

public class FooBar : Foo
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
// Licensed to the .NET Foundation under one or more agreements.
// See the LICENSE file in the project root for more information.

//
// System.ComponentModel.CultureInfoConverter test cases
//
// Authors:
// Gert Driesen (drieseng@users.sourceforge.net)
//
// (c) 2008 Gert Driesen
//

using System.ComponentModel.Design.Serialization;
using System.Globalization;
using Xunit;

namespace System.ComponentModel.Tests
{
public class CultureInfoConverterTest
{
private CultureInfoConverter converter;

public CultureInfoConverterTest()
{
converter = new CultureInfoConverter();
}

[Fact]
public void CanConvertFrom()
{
Assert.True(converter.CanConvertFrom(typeof(string)));
Assert.False(converter.CanConvertFrom(typeof(CultureInfo)));
Assert.False(converter.CanConvertFrom(typeof(object)));
Assert.False(converter.CanConvertFrom(typeof(int)));
}

[Fact]
public void CanConvertTo()
{
Assert.True(converter.CanConvertTo(typeof(string)));
Assert.False(converter.CanConvertTo(typeof(object)));
Assert.False(converter.CanConvertTo(typeof(CultureInfo)));
Assert.False(converter.CanConvertTo(typeof(int)));
Assert.True(converter.CanConvertTo(typeof(InstanceDescriptor)));
}

[ActiveIssue(11611, TestPlatforms.AnyUnix)]
[Fact]
public void ConvertFrom_String()
{
CultureInfo c;

c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture,
String.Empty);
Assert.Equal(CultureInfo.InvariantCulture, c);

c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture,
"nl-BE");
Assert.Equal(new CultureInfo("nl-BE"), c);

Assert.Throws<ArgumentException>(() => c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "Dutch (Bel"));
Assert.Throws<ArgumentException>(() => c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "duTcH (Bel"));

c = (CultureInfo)converter.ConvertFrom(null, CultureInfo.InvariantCulture, "(Default)");
Assert.Equal(CultureInfo.InvariantCulture, c);
}

[ActiveIssue(11611, TestPlatforms.AnyUnix)]
[Fact]
public void ConvertFrom_String_IncompleteName()
{
converter.ConvertFrom(null, CultureInfo.InvariantCulture,
"nl-B");
}

[ActiveIssue(11611, TestPlatforms.AnyUnix)]
[Fact]
public void ConvertFrom_String_InvalidCulture()
{
ArgumentException ex;

ex = Assert.Throws<ArgumentException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, "(default)"));
// The (default) culture cannot be converted to
// a CultureInfo object on this computer
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf(typeof(CultureInfo).Name) != -1);
Assert.True(ex.Message.IndexOf("(default)") != -1);
Assert.Null(ex.ParamName);

ex = Assert.Throws<ArgumentException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, " "));
// The culture cannot be converted to
// a CultureInfo object on this computer
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf(typeof(CultureInfo).Name) != -1);
Assert.True(ex.Message.IndexOf(" ") != -1);
Assert.Null(ex.ParamName);

ex = Assert.Throws<ArgumentException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, "\r\n"));
// The \r\n culture cannot be converted to
// a CultureInfo object on this computer
Assert.Equal(typeof(ArgumentException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf(typeof(CultureInfo).Name) != -1);
Assert.True(ex.Message.IndexOf("\r\n") != -1);
Assert.Null(ex.ParamName);
}

[Fact]
public void ConvertFrom_Value_Null()
{
NotSupportedException ex = Assert.Throws<NotSupportedException>(() => converter.ConvertFrom(null, CultureInfo.InvariantCulture, (string)null));
// CultureInfoConverter cannot convert from (null)
Assert.Equal(typeof(NotSupportedException), ex.GetType());
Assert.Null(ex.InnerException);
Assert.NotNull(ex.Message);
Assert.True(ex.Message.IndexOf(typeof(CultureInfoConverter).Name) != -1);
Assert.True(ex.Message.IndexOf("(null)") != -1);
}

[Fact]
public void ConvertToString()
{
Assert.Equal("nl-BE", converter.ConvertToString(null, CultureInfo.InvariantCulture, new MyCultureInfo()));
Assert.Equal("(Default)", converter.ConvertToString(null, CultureInfo.InvariantCulture, null));
Assert.Equal("(Default)", converter.ConvertToString(null, CultureInfo.InvariantCulture, CultureInfo.InvariantCulture));
Assert.Equal("nl-BE", converter.ConvertToString(null, CultureInfo.InvariantCulture, new CultureInfo("nl-BE")));
}

[Serializable]
private sealed class MyCultureInfo : CultureInfo
{
internal MyCultureInfo() : base("nl-BE")
{
}

public override string DisplayName
{
get { return "display"; }
}

public override string EnglishName
{
get { return "english"; }
}
}

[Fact]
public void GetCultureName()
{
CustomCultureInfoConverter custom_converter = new CustomCultureInfoConverter();

CultureInfo fr_culture = CultureInfo.GetCultureInfo("fr-FR");
Assert.Equal(fr_culture.Name, custom_converter.GetCultureName(fr_culture));

CultureInfo es_culture = CultureInfo.GetCultureInfo("es-MX");
Assert.Equal(es_culture.Name, custom_converter.GetCultureName(es_culture));
}

private class CustomCultureInfoConverter : CultureInfoConverter
{
public new string GetCultureName(CultureInfo culture)
{
return base.GetCultureName(culture);
}
}
}
}
Loading