Ooui-tws-port/Tests/JsonTests.cs

53 lines
1.7 KiB
C#
Raw Normal View History

2018-03-03 07:44:28 +00:00
using System;
#if NUNIT
using NUnit.Framework;
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
using TestMethodAttribute = NUnit.Framework.TestCaseAttribute;
#else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
using Ooui;
using System.IO;
using System.Text.RegularExpressions;
namespace Tests
{
[TestClass]
public class JsonTests
{
static readonly Regex noid = new Regex ("⦙\\d+");
static string NoId (string s)
{
return noid.Replace (s, "⦙");
}
[TestMethod]
public void ButtonIndividualMessages ()
{
var b = new Button ();
b.Text = "Hello";
b.Click += (sender, e) => { };
Assert.AreEqual ("{\"m\":\"create\",\"id\":\"⦙\",\"k\":\"button\"}", NoId (b.StateMessages[0].ToJson ()));
Assert.AreEqual ("{\"m\":\"call\",\"id\":\"⦙\",\"k\":\"insertBefore\",\"v\":[\"⦙\",null]}", NoId (b.StateMessages[1].ToJson ()));
Assert.AreEqual ("{\"m\":\"listen\",\"id\":\"⦙\",\"k\":\"click\"}", NoId (b.StateMessages[2].ToJson ()));
}
[TestMethod]
public void ButtonWriteMessages ()
{
var b = new Button ();
b.Text = "Hello";
b.Click += (sender, e) => { };
var sw = new StringWriter ();
foreach (var m in b.StateMessages) {
m.WriteJson (sw);
}
Assert.AreEqual ("{\"m\":\"create\",\"id\":\"⦙\",\"k\":\"button\"}" +
"{\"m\":\"call\",\"id\":\"⦙\",\"k\":\"insertBefore\",\"v\":[\"⦙\",null]}" +
"{\"m\":\"listen\",\"id\":\"⦙\",\"k\":\"click\"}", NoId (sw.ToString ()));
}
}
}