From 696bb0e577e405f90b4f1e508cd0b55d9bcbb91b Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Mon, 6 Mar 2017 01:12:33 +0100 Subject: [PATCH] - JSON serialization for all lists (test for singly linked list) --- .../singlylinkedlist/singlylinkedlist_test.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index 0a45929..27daacc 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -345,6 +345,32 @@ func TestListIteratorFirst(t *testing.T) { } } +func TestListSerialization(t *testing.T) { + list := New() + list.Add("a", "b", "c") + + var err error + assert := func() { + if actualValue, expectedValue := fmt.Sprintf("%s%s%s", list.Values()...), "abc"; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if actualValue, expectedValue := list.Size(), 3; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + if err != nil { + t.Errorf("Got error %v", err) + } + } + + assert() + + json, err := list.ToJSON() + assert() + + err = list.FromJSON(json) + assert() +} + func benchmarkGet(b *testing.B, list *List, size int) { for i := 0; i < b.N; i++ { for n := 0; n < size; n++ {