Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make printed structures slightly more readable #2910

Draft
wants to merge 15 commits into
base: develop
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/kOS.Safe.Test/Collections/CollectionValueTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using kOS.Safe.Encapsulation;
using kOS.Safe.Execution;
using NUnit.Framework;
Expand All @@ -18,7 +18,7 @@ public void Setup()
cpu = new FakeCpu();
}

protected object InvokeDelegate(SerializableStructure stack, string suffixName, params object[] parameters)
protected object InvokeDelegate(kOS.Safe.Encapsulation.Structure stack, string suffixName, params object[] parameters)
{
var lengthObj = stack.GetSuffix(suffixName) as DelegateSuffixResult;
Assert.IsNotNull(lengthObj);
Expand Down
22 changes: 1 addition & 21 deletions src/kOS.Safe.Test/Collections/LexiconTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using kOS.Safe.Encapsulation;
Expand Down Expand Up @@ -229,26 +229,6 @@ public void CopyIsDifferentObject()

}

[Test]
public void CanFormatNumericKeys()
{
var map = MakeNestedExample();

var hasKeyInner = (BooleanValue)InvokeDelegate(map, "HASKEY" , new StringValue("inner"));
Assert.IsTrue(hasKeyInner);

var inner = (Lexicon) ((Lexicon)map)[new StringValue("inner")];
Assert.IsNotNull(inner);

var hasNumericKey = (BooleanValue)InvokeDelegate(inner, "HASKEY" , new ScalarIntValue(3));
Assert.IsTrue(hasNumericKey);

var innerString = inner.ToString();

Assert.IsTrue(innerString.Contains("[\"2\"]"));
Assert.IsTrue(innerString.Contains("[3]"));
}

[Test]
public void CanClearOnCaseChange()
{
Expand Down
194 changes: 177 additions & 17 deletions src/kOS.Safe.Test/Collections/MixedCollectionPrintingTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using kOS.Safe.Encapsulation;
using kOS.Safe.Serialization;
using NUnit.Framework;
Expand All @@ -10,28 +10,188 @@ public class MixedCollectionPrintingTest : CollectionValueTest
{

[Test]
public void CanPrintListInLexicon()
public void CanSerialize()
{
var list = new ListValue
{
new StringValue("First In List"),
new StringValue("Second In List"),
new StringValue("Last In List")
};

var lexicon = new Lexicon
{
{new StringValue("list"), list},
{new StringValue("not list"), new ScalarIntValue(2)}
};
var runner = new KSRunner(@"
local l to lex(
""Boolean"", true,
""List"", list(1, false, ""foo""),
""Pid"", PidLoop(1, 2, 3, 4, 5)
// ""Queue"", queue(1, false, ""foo""),
// ""Range"", range(1, 2),
// ""Double"", 1.1,
// ""Int"", 42,
// ""Stack"", stack(1, 2),
// ""String"", ""foo"",
// ""Set"", uniqueset(1, 2)
).
writejson(l, ""serialization.json"").
return l.
");
Assert.AreEqual("", runner.Output);

var result = (StringValue)InvokeDelegate(lexicon, "DUMP");
Lexicon l = runner.Result as Lexicon;
Assert.NotNull(l);

var file = runner.Volume.RootHarddiskDirectory.GetFileContent("serialization.json");
Assert.NotNull(file);

string reference = @"{
""entries"": [
{
""value"": ""Boolean"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""value"": true,
""$type"": ""kOS.Safe.Encapsulation.BooleanValue""
},
{
""value"": ""List"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""items"": [
{
""value"": 1,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
},
{
""value"": false,
""$type"": ""kOS.Safe.Encapsulation.BooleanValue""
},
{
""value"": ""foo"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
}
],
""$type"": ""kOS.Safe.Encapsulation.ListValue""
},
{
""value"": ""Pid"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""Kp"": 1,
""Ki"": 2,
""Kd"": 3,
""Setpoint"": 0,
""MaxOutput"": 5,
""MinOutput"": 4,
""ExtraUnwind"": false,
""$type"": ""kOS.Safe.Encapsulation.PIDLoop""
},
{
""value"": ""Queue"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""items"": [
{
""value"": 1,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
},
{
""value"": false,
""$type"": ""kOS.Safe.Encapsulation.BooleanValue""
},
{
""value"": ""foo"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
}
],
""$type"": ""kOS.Safe.Encapsulation.QueueValue""
},
{
""value"": ""Range"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""stop"": {
""value"": 2,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
},
""start"": {
""value"": 1,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
},
""step"": {
""value"": 1,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
},
""$type"": ""kOS.Safe.RangeValue""
},
{
""value"": ""Double"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""value"": 1.1,
""$type"": ""kOS.Safe.Encapsulation.ScalarDoubleValue""
},
{
""value"": ""Int"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""value"": 42,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
},
{
""value"": ""Stack"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""items"": [
{
""value"": 2,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
},
{
""value"": 1,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
}
],
""$type"": ""kOS.Safe.Encapsulation.StackValue""
},
{
""value"": ""String"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""value"": ""foo"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""value"": ""Set"",
""$type"": ""kOS.Safe.Encapsulation.StringValue""
},
{
""items"": [
{
""value"": 1,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
},
{
""value"": 2,
""$type"": ""kOS.Safe.Encapsulation.ScalarIntValue""
}
],
""$type"": ""kOS.Safe.Encapsulation.UniqueSetValue""
}
],
""$type"": ""kOS.Safe.Encapsulation.Lexicon""
}".Replace("\r\n", "\n");

string serializationResult = file.String.Replace("\r\n", "\n");
System.Diagnostics.Debug.WriteLine(serializationResult);
Assert.AreEqual(reference, serializationResult);

Assert.IsTrue(result.Contains("LEXICON of 2 items"));
Assert.IsTrue(result.Contains("[\"list\"] = LIST of 3 items"));
Assert.IsTrue(result.Contains("Last In List"));
}



[Test]
public void DoesNotContainInvalidToString()
{
Expand Down
6 changes: 3 additions & 3 deletions src/kOS.Safe.Test/Communication/MessageQueueTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ public void CanHandleSerializableStructures()
{
Lexicon lex = new Lexicon();
lex.Add(new StringValue("key1"), new StringValue("value1"));
queue.Push(new BaseMessage(new SafeSerializationMgr(null).Dump(lex), 0, 0));
//queue.Push(new BaseMessage(new SafeSerializationMgr(null).Dump(lex), 0, 0));

Lexicon read = new SafeSerializationMgr(null).CreateFromDump(queue.Pop().Content as Dump) as Lexicon;
Lexicon read = null;// new SafeSerializationMgr(null).CreateFromDump(queue.Pop().Content as Dump) as Lexicon;
Assert.AreEqual(new StringValue("value1"), read[new StringValue("key1")]);
}

Expand All @@ -156,7 +156,7 @@ public void CanDump()

GenericMessageQueue<BaseMessage,FakeCurrentTimeProvider> newQueue = new GenericMessageQueue<BaseMessage,FakeCurrentTimeProvider>();

newQueue.LoadDump(queue.Dump());
newQueue.LoadDump(queue.Dump(new DumperState()) as DumpList);

newQueue.TimeProvider.SetTime(11);

Expand Down
8 changes: 4 additions & 4 deletions src/kOS.Safe.Test/Execution/Noop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using kOS.Safe.Callback;
using kOS.Safe.Callback;
using kOS.Safe.Execution;
using kOS.Safe.Module;
using kOS.Safe.Persistence;
Expand All @@ -8,7 +8,7 @@ namespace kOS.Safe.Test.Execution
{
internal class NoopLogger : ILogger
{
public void Log(Exception e)
public virtual void Log(Exception e)
{
Console.WriteLine(e);
}
Expand All @@ -17,12 +17,12 @@ public void Log(string text)
{
}

public void LogError(string s)
public virtual void LogError(string s)
{
Console.WriteLine(s);
}

public void LogException(Exception exception)
public virtual void LogException(Exception exception)
{
Console.WriteLine(exception);
}
Expand Down
8 changes: 7 additions & 1 deletion src/kOS.Safe.Test/Execution/Screen.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using kOS.Safe.Screen;
using kOS.Safe.Screen;
using System;
using System.Collections.Generic;
using System.Linq;

namespace kOS.Safe.Test
{
internal class Screen : IScreenBuffer
{
private List<String> output = new List<string>();

public override string ToString()
{
return string.Join(Environment.NewLine, output);
}

public void ClearOutput()
{
output.Clear();
Expand Down
Loading