public void ImplicitArrayAssignmentWithSameTypes() { //Even though we don't specify types explicitly, the compiler //will pick one for us var names = new[] { "John", "Smith" }; Assert.AreEqual (typeof(string[]), names.GetType (), "Determine the type of the array elements to improve your Karma."); //but only if it can. So this doesn't work // (Try uncommenting the line below to see how the compiler reacts) //var array = new[] { "John", 1 }; }
public void Should_emit_all_nodes_in_block() { var model = new { Name = "World" }; var template = SyntaxTree.Block( SyntaxTree.WriteString("Hello "), SyntaxTree.WriteExpression(SyntaxTreeExpression.Property(model.GetType(), "Name")), SyntaxTree.WriteString("!") ); var result = ExecuteTemplate(template, model); Assert.That(result, Is.EqualTo("Hello World!")); }
public void Does_encode_unicode() { string test = "<\"I get this : "; var obj = new { test }; using (var mem = new System.IO.MemoryStream()) { ServiceStack.Text.JsonSerializer.SerializeToStream(obj, obj.GetType(), mem); var encoded = System.Text.Encoding.UTF8.GetString(mem.ToArray()); var copy1 = JsonObject.Parse(encoded); Assert.That(test, Is.EqualTo(copy1["test"])); System.Diagnostics.Debug.WriteLine(copy1["test"]); } }
public void Object_Transformation_Dynamic() { var a = "a"; var b = 1; var c = 12.0; var testTarget = new { a, b, c }; var prop = new Mock<IPropertyComponent>(); prop.Setup(s=>s.All(It.Is<object>(r=> r == testTarget))) .Returns(testTarget.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance)); var result = _.Object.ToDynamic(testTarget); Assert.AreEqual(a , result.a); Assert.AreEqual(b , result.b); Assert.AreEqual(c , result.c); }
public void should_return_the_nested_property_value() { var foo = new { Text = "Hello world" }; var getter = PropertyGetter.Create(foo.GetType(), "Text.Length"); Assert.That(getter.Get(foo), Is.EqualTo(foo.Text.Length)); }
public void Coalesce( ) { string middleName = "Henry"; var coalscing = new { MiddleName = middleName, FirstName = "ShouldNotBeHere", LastName = "ShouldBeHere" }; var coalscingType = coalscing.GetType( ); var mkprop = new Mock<IPropertyComponent>( ); mkprop.Setup( a => a.All( typeof( Person ) ) ) .Returns( typeof( Person ).GetProperties( BindingFlags.Instance | BindingFlags.Public ) ); mkprop.Setup( a => a.All( typeof( Employee ) ) ) .Returns( typeof( Employee ).GetProperties( BindingFlags.Instance | BindingFlags.Public ) ); mkprop.Setup( a => a.All( coalscingType ) ) .Returns( coalscingType.GetProperties( BindingFlags.Instance | BindingFlags.Public ) ); mkprop.Setup( a => a.All( It.IsAny<Person>( ) ) ) .Returns( typeof( Person ).GetProperties( BindingFlags.Instance | BindingFlags.Public ) ); mkprop.Setup( a => a.All( It.IsAny<Employee>( ) ) ) .Returns( typeof( Employee ).GetProperties( BindingFlags.Instance | BindingFlags.Public ) ); mkprop.Setup( a => a.All( coalscing ) ) .Returns( coalscingType.GetProperties( BindingFlags.Instance | BindingFlags.Public ) ); var target = new TransposeComponent( mkprop.Object ); string title = "Mr.", firstName = "Charles", suffix = "IV", nickName = "Chip"; int age = 24; var person = new Person { Title = title, FirstName = firstName, Suffix = suffix, NickName = nickName, Age = age, }; string shouldbehere = "ShouldBeHere"; decimal defaultedSalary = 60000m; var employeeWithExistingInfo = new Employee { FirstName = shouldbehere, LastName = shouldbehere, Salary = defaultedSalary }; var employeeWithNoInfo = new Employee { }; var employeeWithOnlySalary = new Employee { Salary = 60000m }; var result = target.Coalesce( employeeWithExistingInfo, person ); Assert.AreEqual( employeeWithExistingInfo, result ); Assert.AreEqual( defaultedSalary, result.Salary ); Assert.AreEqual( shouldbehere, result.FirstName ); Assert.AreEqual( shouldbehere, result.LastName ); var dm = default( decimal ); var ml = new Employee{}; var result3 = target.Coalesce( ml, new { Salary = 1000m } ); // the salary should not be replaced because Assert.AreEqual( result3, ml ); Assert.AreEqual( dm, ml.Salary ); var result2 = target.Coalesce( person, coalscing ); Assert.AreEqual ( person, result2 ); Assert.AreNotEqual ( "ShouldNotBeHere", result2.FirstName ); Assert.AreEqual( "ShouldBeHere", result2.LastName ); Assert.AreEqual( "Henry", result2.MiddleName ); Assert.AreEqual( "IV", result2.Suffix ); var result4 = target.Coalesce( person, coalscing, true ); Assert.AreNotEqual( person, result4 ); Assert.AreNotEqual( "ShouldNotBeHere", result2.FirstName ); Assert.AreEqual( "ShouldBeHere", result2.LastName ); Assert.AreEqual( "Henry", result2.MiddleName ); Assert.AreEqual( "IV", result2.Suffix ); }