forked from dsuryd/dotNetify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializerVMTest.cs
More file actions
75 lines (62 loc) · 2.49 KB
/
Copy pathSerializerVMTest.cs
File metadata and controls
75 lines (62 loc) · 2.49 KB
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
using DotNetify;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
namespace UnitTests
{
[TestClass]
public class SerializerVMTest
{
private class SerializerVM : INotifyPropertyChanged, ISerializer, IDeserializer
{
private string _firstName = "Hello";
private string _lastName = "World";
public string FullName => $"{_firstName} {_lastName}";
[DotNetify.Ignore]
public string IgnoreMe { get; set; }
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public string Serialize(object instance, List<string> ignoredPropertyNames)
{
if (ignoredPropertyNames != null && ignoredPropertyNames.Contains("IgnoreMe") == false)
throw new Exception();
if (instance == this)
return "{'FirstName': '" + _firstName + "', 'LastName': '" + _lastName + "', 'FullName': '" + FullName + "'}";
else
return JsonConvert.SerializeObject(instance);
}
public bool Deserialize(object instance, string propertyPath, string newValue)
{
if (instance == this)
{
if (propertyPath == "FirstName")
_firstName = newValue;
else if (propertyPath == "LastName")
_lastName = newValue;
this.Changed(nameof(FullName));
}
return true;
}
}
[TestMethod]
public void SerializerVM_Serialize()
{
var vmController = new MockVMController<SerializerVM>();
var response = vmController.RequestVM();
Assert.AreEqual("Hello", response.GetVMProperty<string>("FirstName"));
Assert.AreEqual("World", response.GetVMProperty<string>("LastName"));
Assert.AreEqual("Hello World", response.GetVMProperty<string>("FullName"));
}
[TestMethod]
public void SerializerVM_Deserialize()
{
var vmController = new MockVMController<SerializerVM>();
vmController.RequestVM();
var response = vmController.UpdateVM(new Dictionary<string, object>() { { "FirstName", "John" } });
Assert.AreEqual("John World", response["FullName"]);
response = vmController.UpdateVM(new Dictionary<string, object>() { { "LastName", "Hancock" } });
Assert.AreEqual("John Hancock", response["FullName"]);
}
}
}