public void InvalidInstanceParameter_ThrowsException() { var organization = default(Organization); var remute = new Remute(); remute.With(organization, x => x.Name, "organization"); }
public void InvalidExpressionParameter_ThrowsException() { var organization = new Organization(null, null); var remute = new Remute(); remute.With(organization, null, "organization"); }
public void EmitEventFiredWithProps_Success() { var expectedSource = default(Organization); var expectedTarget = default(Organization); var expectedValue = default(string); var expectedAffectedProperties = default(string[]); var remute = new Remute(); remute.OnEmit += (source, target, value, affectedProperties) => { expectedSource = (Organization)source; expectedTarget = (Organization)target; expectedValue = (string)value; expectedAffectedProperties = affectedProperties; }; var organization = new Organization("organization 1", new Department("department 1", new Employee(Guid.NewGuid(), "developer", "manager"), null)); var actual = remute.With(organization, x => x.DevelopmentDepartment.Manager.FirstName, "Foo"); Assert.AreSame(expectedSource, organization); Assert.AreSame(expectedTarget, actual); Assert.AreEqual("Foo", expectedValue); Assert.IsTrue(expectedAffectedProperties.Length == 3); Assert.AreEqual(expectedAffectedProperties[0], "DevelopmentDepartment"); Assert.AreEqual(expectedAffectedProperties[1], "Manager"); Assert.AreEqual(expectedAffectedProperties[2], "FirstName"); }
public void EmitEventFiredWithoutProps_Success() { var expectedSource = default(Employee); var expectedTarget = default(Employee); var expectedValue = default(string); var expectedAffectedProperties = default(string[]); var remute = new Remute(); remute.OnEmit += (source, target, value, affectedProperties) => { expectedSource = (Employee)source; expectedTarget = (Employee)target; expectedValue = (string)value; expectedAffectedProperties = affectedProperties; }; var employee = new Employee(Guid.NewGuid(), "Joe", "Doe"); var actual = remute.With <Employee>(employee); Assert.AreSame(expectedSource, employee); Assert.AreSame(expectedTarget, actual); Assert.IsNull(expectedValue); Assert.IsNull(expectedAffectedProperties); }
/// <summary> /// Constructs immutable object from any other object. /// Helpful cloning immutable object or converting POCO, DTO, anonymous type, dynamic ect. /// </summary> /// <typeparam name="TInstance">Immutable object type.</typeparam> /// <param name="source">Original object.</param> /// <param name="remute"></param> /// <returns>Configuration to use. Default if not specified.</returns> public static TInstance Remute <TInstance>(this object source, Remute remute = null) { if (remute is null) { remute = Remutable.Remute.Default; } return(remute.With <TInstance>(source)); }
public void StaticConstructor_Success() { var original = new StaticConstructor("original"); var remute = new Remute(); var actual = remute.With(original, x => x.Property1, "updated value"); Assert.AreEqual("updated value", actual.Property1); Assert.AreEqual("original", original.Property1); Assert.AreNotSame(original, actual); }
public void SetSameObject_NoUpdate() { var organization = new Organization("organization 1", new Department("department 1", null, null)); var remute = new Remute(); var actual = remute.With(organization, x => x.Name, "organization 1"); Assert.AreSame(organization.Name, actual.Name); Assert.AreSame(organization, actual); Assert.AreSame(organization.DevelopmentDepartment, actual.DevelopmentDepartment); }
public void SetFirstLevelPropery_Success() { var organization = new Organization("organization 1", new Department("department 1", null, null)); var remute = new Remute(); var actual = remute.With(organization, x => x.Name, "organization 2"); Assert.AreEqual("organization 2", actual.Name); Assert.AreNotSame(organization, actual); Assert.AreSame(organization.DevelopmentDepartment, actual.DevelopmentDepartment); }
public void StaticFieldInitialization_Success() { var original = StaticFieldInit.Default; var remute = new Remute(); var actual = remute.With(original, x => x.Property1, "updated value"); Assert.AreEqual("updated value", actual.Property1); Assert.AreEqual("Default", original.Property1); Assert.AreNotSame(original, actual); }
public void FromDynamic_Success() { var remute = new Remute(); dynamic expected = new { Id = Guid.NewGuid(), FirstName = "Joe", LastName = "Doe" }; var actual = remute.With <Employee>(expected); Assert.IsInstanceOfType(actual, typeof(Employee)); Assert.AreNotSame(expected, actual); Assert.AreEqual(expected.Id, actual.Id); Assert.AreEqual(expected.FirstName, actual.FirstName); Assert.AreEqual(expected.LastName, actual.LastName); }
public void Clone_Success() { var remute = new Remute(); var expected = new Employee(Guid.NewGuid(), "Joe", "Doe"); var actual = remute.With <Employee>(expected); Assert.IsInstanceOfType(actual, typeof(Employee)); Assert.AreNotSame(expected, actual); Assert.AreEqual(expected.Id, actual.Id); Assert.AreEqual(expected.FirstName, actual.FirstName); Assert.AreEqual(expected.LastName, actual.LastName); }
public void SetSameNull_NoUpdate() { var organization = new Organization("organization 1", new Department(null, new Employee(Guid.NewGuid(), "Joe", "Doe"), null)); var remute = new Remute(); var actual = remute.With(organization, x => x.DevelopmentDepartment.Title, null); Assert.IsNull(actual.DevelopmentDepartment.Title); Assert.AreSame(organization, actual); Assert.AreSame(organization.DevelopmentDepartment, actual.DevelopmentDepartment); Assert.AreSame(organization.DevelopmentDepartment.Title, actual.DevelopmentDepartment.Title); Assert.AreSame(organization.DevelopmentDepartment.Manager, actual.DevelopmentDepartment.Manager); }
public void SetSameValue_NoUpdate() { var managerId = Guid.NewGuid(); var organization = new Organization("organization 1", new Department("department 1", new Employee(managerId, "Joe", "Doe"), null)); var remute = new Remute(); var actual = remute.With(organization, x => x.DevelopmentDepartment.Manager.Id, managerId); Assert.AreSame(organization, actual); Assert.AreSame(organization.DevelopmentDepartment, actual.DevelopmentDepartment); Assert.AreSame(organization.DevelopmentDepartment.Manager, actual.DevelopmentDepartment.Manager); Assert.AreEqual(organization.DevelopmentDepartment.Manager.Id, actual.DevelopmentDepartment.Manager.Id); Assert.AreEqual(managerId, actual.DevelopmentDepartment.Manager.Id); }
public void TestConfig_Success() { var config = new ActivationConfiguration() .Configure <InheritedType4>(x => new InheritedType4(x.Prop1, x.Prop2, x.Prop3, x.Prop4)); var remute = new Remute(config); var instance = new InheritedType4("prop1", "prop2", "prop3", "prop4"); var actual = remute.With(instance, x => x.Prop1, "update"); Assert.AreEqual("update", actual.Prop1); Assert.AreEqual(instance.Prop2, actual.Prop2); Assert.AreEqual(instance.Prop3, actual.Prop3); Assert.AreEqual(instance.Prop4, actual.Prop4); }
public void SetValueTypePropery_Success() { var employeeId = Guid.NewGuid(); var employee = new Employee(Guid.NewGuid(), "Joe", "Doe"); var remute = new Remute(); var actual = remute.With(employee, x => x.Id, employeeId); Assert.AreEqual(employeeId, actual.Id); Assert.AreEqual("Joe", actual.FirstName); Assert.AreEqual("Doe", actual.LastName); Assert.AreSame(employee.FirstName, actual.FirstName); Assert.AreSame(employee.LastName, actual.LastName); }
public void SetNestedPropery_Success() { var organization = new Organization("organization 1", new Department("department 1", new Employee(Guid.NewGuid(), "developer", "manager"), null)); var remute = new Remute(); var actual = remute.With(organization, x => x.DevelopmentDepartment.Manager.FirstName, "name"); Assert.AreEqual("name", actual.DevelopmentDepartment.Manager.FirstName); Assert.AreEqual(organization.DevelopmentDepartment.Manager.Id, actual.DevelopmentDepartment.Manager.Id); Assert.AreNotSame(organization, actual); Assert.AreNotSame(organization.DevelopmentDepartment, actual.DevelopmentDepartment); Assert.AreNotSame(organization.DevelopmentDepartment.Manager, actual.DevelopmentDepartment.Manager); Assert.AreSame(organization.Name, actual.Name); Assert.AreSame(organization.DevelopmentDepartment.Title, organization.DevelopmentDepartment.Title); }
public void ConfigureByExpression_Success() { var user = new User(1, "Joe", "Doe"); var config = new ActivationConfiguration() .Configure <User>(x => new User(x.FirstName, x.LastName)); var remute = new Remute(config); var actual = remute.With(user, x => x.FirstName, "Foo"); Assert.AreEqual("Foo", actual.FirstName); Assert.AreEqual(user.LastName, actual.LastName); Assert.AreEqual(default(int), actual.Id); }
public void UnableToFindProperty_ThrowsException() { var invalid = new InvalidProperty("property"); var remute = new Remute(); try { invalid = remute.With(invalid, x => x.Property1, "test"); } catch (Exception ex) when(ex.Message == $"Unable to find appropriate property to use as a constructor parameter 'property'. Type '{nameof(InvalidProperty)}'. Consider to use {nameof(ActivationConfiguration)} parameter.") { return; } Assert.Fail(); }
public void FieldSpecified_ThrowsException() { var invalid = new InvalidProperty("property"); var remute = new Remute(); try { invalid = remute.With(invalid, x => x.Field1, "test"); } catch (NotSupportedException ex) when(ex.Message == $"Unable to process expression. Expression: 'x.Field1'.") { return; } Assert.Fail(); }
public void ConfigureByConstructor_Success() { var user = new User(1, "Joe", "Doe"); var type = user.GetType(); var constructor = type.GetConstructor(new[] { typeof(int), typeof(string), typeof(string) }); var config = new ActivationConfiguration() .Configure(constructor); var remute = new Remute(config); var actual = remute.With(user, x => x.FirstName, "Foo"); Assert.AreEqual("Foo", actual.FirstName); Assert.AreEqual(user.LastName, actual.LastName); Assert.AreEqual(user.Id, actual.Id); }
public void SetNestedProperyTest() { // ~19 ms var remute = new Remute(); var stopwatch = new Stopwatch(); stopwatch.Start(); for (var i = 0; i < 1000; i++) { var organization = new Organization("organization 1", new Department("department 1", new Employee(Guid.NewGuid(), "developer", "manager"), null)); var actual = remute.With(organization, x => x.DevelopmentDepartment.Manager.FirstName, "name"); } stopwatch.Stop(); var time = stopwatch.ElapsedMilliseconds; Console.WriteLine(time); }
public void ConfigureByConstructorAndParameters_Success() { var user = new User(1, "Joe", "Doe"); var type = user.GetType(); var constructor = type.GetConstructor(new[] { typeof(string), typeof(string) }); var firstNameParameter = constructor.GetParameters().Single(x => x.Name == "firstNameNotMatchingPropertyName"); var firstNameProperty = type.GetProperty("FirstName"); var config = new ActivationConfiguration() .Configure(constructor, new Dictionary <ParameterInfo, PropertyInfo>() { [firstNameParameter] = firstNameProperty }); var remute = new Remute(config); var actual = remute.With(user, x => x.FirstName, "Foo"); Assert.AreEqual("Foo", actual.FirstName); Assert.AreEqual(user.LastName, actual.LastName); Assert.AreEqual(default(int), actual.Id); }
/// <summary> /// Contructs immutable object from existing one with changed property specified by lambda expression. /// </summary> /// <typeparam name="TInstance">Immutable object type.</typeparam> /// <typeparam name="TValue">Value to set type.</typeparam> /// <param name="instance">Original immutable object.</param> /// <param name="expression">Navigation property specifying what to change.</param> /// <param name="value">Value to set in the resulting object.</param> /// <param name="remute">Configuration to use. Default if not specified.</param> /// <returns></returns> public static TInstance Remute <TInstance, TValue>(this TInstance instance, Expression <Func <TInstance, TValue> > expression, TValue value, Remute remute = null) { if (remute is null) { remute = Remutable.Remute.Default; } return(remute.With(instance, expression, value)); }