Skip to content

Commit

Permalink
Rename StructureAsString to toTestString (#656)
Browse files Browse the repository at this point in the history
We originally used StructureAsString to represent a method for
generating a string representation, primarily for debugging purposes.
However, we've now decided to use `struct` to represent the structure
for serialization. As a result, this commit updates the method name to
align with our new naming convention, which is toTestString.
  • Loading branch information
hackerwins authored Nov 1, 2023
1 parent 107498e commit acc6703
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 103 deletions.
2 changes: 1 addition & 1 deletion api/converter/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestConverter(t *testing.T) {
pbNodes := converter.ToTreeNodes(root)
clone, err := converter.FromTreeNodes(pbNodes)
assert.NoError(t, err)
assert.Equal(t, crdt.ToStructure(root), crdt.ToStructure(clone))
assert.Equal(t, crdt.ToTreeNodeForTest(root), crdt.ToTreeNodeForTest(clone))
assert.Equal(t, crdt.ToXML(root), crdt.ToXML(clone))
})

Expand Down
6 changes: 3 additions & 3 deletions pkg/document/crdt/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ func (a *Array) Marshal() string {
return a.elements.Marshal()
}

// StructureAsString returns a String containing the metadata of the elements
// ToTestString returns a String containing the metadata of the elements
// for debugging purpose.
func (a *Array) StructureAsString() string {
return a.elements.StructureAsString()
func (a *Array) ToTestString() string {
return a.elements.ToTestString()
}

// DeepCopy copies itself deeply.
Expand Down
6 changes: 3 additions & 3 deletions pkg/document/crdt/rga_tree_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,10 @@ func (a *RGATreeList) Len() int {
return a.nodeMapByIndex.Len()
}

// StructureAsString returns a String containing the metadata of the node id
// ToTestString returns a String containing the metadata of the node id
// for debugging purpose.
func (a *RGATreeList) StructureAsString() string {
return a.nodeMapByIndex.StructureAsString()
func (a *RGATreeList) ToTestString() string {
return a.nodeMapByIndex.ToTestString()
}

// Delete deletes the node of the given index.
Expand Down
32 changes: 16 additions & 16 deletions pkg/document/crdt/rga_tree_split.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type RGATreeSplitValue interface {
DeepCopy() RGATreeSplitValue
String() string
Marshal() string
structureAsString() string
toTestString() string
}

// RGATreeSplitNodeID is an ID of RGATreeSplitNode.
Expand Down Expand Up @@ -85,10 +85,10 @@ func (id *RGATreeSplitNodeID) Split(offset int) *RGATreeSplitNodeID {
return NewRGATreeSplitNodeID(id.createdAt, id.offset+offset)
}

// StructureAsString returns a String containing the metadata of the node id
// ToTestString returns a String containing the metadata of the node id
// for debugging purpose.
func (id *RGATreeSplitNodeID) StructureAsString() string {
return fmt.Sprintf("%s:%d", id.createdAt.StructureAsString(), id.offset)
func (id *RGATreeSplitNodeID) ToTestString() string {
return fmt.Sprintf("%s:%d", id.createdAt.ToTestString(), id.offset)
}

func (id *RGATreeSplitNodeID) hasSameCreatedAt(other *RGATreeSplitNodeID) bool {
Expand Down Expand Up @@ -120,10 +120,10 @@ func (pos *RGATreeSplitNodePos) getAbsoluteID() *RGATreeSplitNodeID {
return NewRGATreeSplitNodeID(pos.id.createdAt, pos.id.offset+pos.relativeOffset)
}

// StructureAsString returns a String containing the metadata of the position
// ToTestString returns a String containing the metadata of the position
// for debugging purpose.
func (pos *RGATreeSplitNodePos) StructureAsString() string {
return fmt.Sprintf("%s:%d", pos.id.StructureAsString(), pos.relativeOffset)
func (pos *RGATreeSplitNodePos) ToTestString() string {
return fmt.Sprintf("%s:%d", pos.id.ToTestString(), pos.relativeOffset)
}

// ID returns the ID of this RGATreeSplitNodePos.
Expand Down Expand Up @@ -245,10 +245,10 @@ func (s *RGATreeSplitNode[V]) createdAt() *time.Ticket {
return s.id.createdAt
}

// structureAsString returns a String containing the metadata of the node
// toTestString returns a String containing the metadata of the node
// for debugging purpose.
func (s *RGATreeSplitNode[V]) structureAsString() string {
return fmt.Sprintf("%s %s", s.id.StructureAsString(), s.value.structureAsString())
func (s *RGATreeSplitNode[V]) toTestString() string {
return fmt.Sprintf("%s %s", s.id.ToTestString(), s.value.toTestString())
}

// Remove removes this node if it created before the time of deletion are
Expand Down Expand Up @@ -357,7 +357,7 @@ func (s *RGATreeSplit[V]) findNodeWithSplit(
func (s *RGATreeSplit[V]) findFloorNodePreferToLeft(id *RGATreeSplitNodeID) (*RGATreeSplitNode[V], error) {
node := s.findFloorNode(id)
if node == nil {
return nil, fmt.Errorf("the node of the given id should be found: " + s.StructureAsString())
return nil, fmt.Errorf("the node of the given id should be found: " + s.ToTestString())
}

if id.offset > 0 && node.id.offset == id.offset {
Expand All @@ -373,7 +373,7 @@ func (s *RGATreeSplit[V]) findFloorNodePreferToLeft(id *RGATreeSplitNodeID) (*RG

func (s *RGATreeSplit[V]) splitNode(node *RGATreeSplitNode[V], offset int) (*RGATreeSplitNode[V], error) {
if offset > node.contentLen() {
return nil, fmt.Errorf("offset should be less than or equal to length: " + s.StructureAsString())
return nil, fmt.Errorf("offset should be less than or equal to length: " + s.ToTestString())
}

if offset == 0 {
Expand Down Expand Up @@ -599,17 +599,17 @@ func (s *RGATreeSplit[V]) nodes() []*RGATreeSplitNode[V] {
return nodes
}

// StructureAsString returns a String containing the metadata of the nodes
// ToTestString returns a String containing the metadata of the nodes
// for debugging purpose.
func (s *RGATreeSplit[V]) StructureAsString() string {
func (s *RGATreeSplit[V]) ToTestString() string {
builder := strings.Builder{}

node := s.initialHead
for node != nil {
if node.removedAt != nil {
builder.WriteString(fmt.Sprintf("{%s}", node.structureAsString()))
builder.WriteString(fmt.Sprintf("{%s}", node.toTestString()))
} else {
builder.WriteString(fmt.Sprintf("[%s]", node.structureAsString()))
builder.WriteString(fmt.Sprintf("[%s]", node.toTestString()))
}
node = node.next
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/document/crdt/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func (t *TextValue) Marshal() string {
)
}

// structureAsString returns a String containing the metadata of this value
// toTestString returns a String containing the metadata of this value
// for debugging purpose.
func (t *TextValue) structureAsString() string {
func (t *TextValue) toTestString() string {
return fmt.Sprintf(
`%s "%s"`,
t.attrs.Marshal(),
Expand Down Expand Up @@ -309,10 +309,10 @@ func (t *Text) Nodes() []*RGATreeSplitNode[*TextValue] {
return t.rgaTreeSplit.nodes()
}

// StructureAsString returns a String containing the metadata of the text
// ToTestString returns a String containing the metadata of the text
// for debugging purpose.
func (t *Text) StructureAsString() string {
return t.rgaTreeSplit.StructureAsString()
func (t *Text) ToTestString() string {
return t.rgaTreeSplit.ToTestString()
}

// CheckWeight returns false when there is an incorrect weight node.
Expand Down
14 changes: 7 additions & 7 deletions pkg/document/crdt/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func NewTreeNode(id *TreeNodeID, nodeType string, attributes *RHT, value ...stri

// toIDString returns a string that can be used as an ID for this TreeNodeID.
func (t *TreeNodeID) toIDString() string {
return t.CreatedAt.StructureAsString() + ":" + strconv.Itoa(t.Offset)
return t.CreatedAt.ToTestString() + ":" + strconv.Itoa(t.Offset)
}

// Compare compares the given two CRDTTreePos.
Expand Down Expand Up @@ -895,9 +895,9 @@ func (t *Tree) toTreeNodes(pos *TreePos) (*TreeNode, *TreeNode) {
return parentNode, leftSiblingNode
}

// Structure returns the structure of this tree.
func (t *Tree) Structure() TreeNodeForTest {
return ToStructure(t.Root())
// ToTreeNodeForTest returns the JSON of this tree for debugging.
func (t *Tree) ToTreeNodeForTest() TreeNodeForTest {
return ToTreeNodeForTest(t.Root())
}

// PathToPos returns the position of the given path
Expand All @@ -915,8 +915,8 @@ func (t *Tree) PathToPos(path []int) (*TreePos, error) {
return pos, nil
}

// ToStructure returns the JSON of this tree for debugging.
func ToStructure(node *TreeNode) TreeNodeForTest {
// ToTreeNodeForTest returns the JSON of this tree for debugging.
func ToTreeNodeForTest(node *TreeNode) TreeNodeForTest {
if node.IsText() {
currentNode := node
return TreeNodeForTest{
Expand All @@ -929,7 +929,7 @@ func ToStructure(node *TreeNode) TreeNodeForTest {

var children []TreeNodeForTest
for _, child := range node.IndexTreeNode.Children() {
children = append(children, ToStructure(child.Value))
children = append(children, ToTreeNodeForTest(child.Value))
}

return TreeNodeForTest{
Expand Down
28 changes: 14 additions & 14 deletions pkg/document/crdt/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func TestTree(t *testing.T) {
},
Size: 15,
IsRemoved: false,
}, tree.Structure())
}, tree.ToTreeNodeForTest())

// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// <root> <p> h e l l o ~ ! </p> <p> w o r l d </p> </root>
Expand Down Expand Up @@ -205,10 +205,10 @@ func TestTree(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "<root><p>ab</p><p>cd</p></root>", tree.ToXML())

structure := tree.Structure()
assert.Equal(t, 8, structure.Size)
assert.Equal(t, 2, structure.Children[0].Size)
assert.Equal(t, 2, structure.Children[0].Children[0].Size)
node := tree.ToTreeNodeForTest()
assert.Equal(t, 8, node.Size)
assert.Equal(t, 2, node.Children[0].Size)
assert.Equal(t, 2, node.Children[0].Children[0].Size)

// 02. Delete b from the first paragraph.
// 0 1 2 3 4 5 6 7
Expand All @@ -217,10 +217,10 @@ func TestTree(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "<root><p>a</p><p>cd</p></root>", tree.ToXML())

structure = tree.Structure()
assert.Equal(t, 7, structure.Size)
assert.Equal(t, 1, structure.Children[0].Size)
assert.Equal(t, 1, structure.Children[0].Children[0].Size)
node = tree.ToTreeNodeForTest()
assert.Equal(t, 7, node.Size)
assert.Equal(t, 1, node.Children[0].Size)
assert.Equal(t, 1, node.Children[0].Children[0].Size)
})

t.Run("delete nodes between element nodes test", func(t *testing.T) {
Expand Down Expand Up @@ -254,11 +254,11 @@ func TestTree(t *testing.T) {
// TODO(sejongk): Use the below assertions after implementing Tree.Move.
// assert.Equal(t, "<root><p>ad</p></root>", tree.ToXML())

// structure := tree.Structure()
// assert.Equal(t, 4, structure.Size)
// assert.Equal(t, 2, structure.Children[0].Size)
// assert.Equal(t, 1, structure.Children[0].Children[0].Size)
// assert.Equal(t, 1, structure.Children[0].Children[1].Size)
// node := tree.ToTreeNodeForTest()
// assert.Equal(t, 4, node.Size)
// assert.Equal(t, 2, node.Children[0].Size)
// assert.Equal(t, 1, node.Children[0].Children[0].Size)
// assert.Equal(t, 1, node.Children[0].Children[1].Size)

// // 03. insert a new text node at the start of the first paragraph.
// _, err = tree.EditByIndex(1, 1, nil, []*crdt.TreeNode{crdt.NewTreeNode(helper.IssuePos(ctx),
Expand Down
38 changes: 19 additions & 19 deletions pkg/document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,22 @@ func TestDocument(t *testing.T) {
root.SetNewArray("k1").AddInteger(1).AddInteger(2).AddInteger(3)
assert.Equal(t, 3, root.GetArray("k1").Len())
assert.Equal(t, `{"k1":[1,2,3]}`, root.Marshal())
assert.Equal(t, "[0,0]0[1,1]1[2,1]2[3,1]3", root.GetArray("k1").StructureAsString())
assert.Equal(t, "[0,0]0[1,1]1[2,1]2[3,1]3", root.GetArray("k1").ToTestString())

root.GetArray("k1").Delete(1)
assert.Equal(t, `{"k1":[1,3]}`, root.Marshal())
assert.Equal(t, 2, root.GetArray("k1").Len())
assert.Equal(t, "[0,0]0[1,1]1[2,0]2[1,1]3", root.GetArray("k1").StructureAsString())
assert.Equal(t, "[0,0]0[1,1]1[2,0]2[1,1]3", root.GetArray("k1").ToTestString())

root.GetArray("k1").InsertIntegerAfter(0, 2)
assert.Equal(t, `{"k1":[1,2,3]}`, root.Marshal())
assert.Equal(t, 3, root.GetArray("k1").Len())
assert.Equal(t, "[0,0]0[1,1]1[3,1]2[1,0]2[1,1]3", root.GetArray("k1").StructureAsString())
assert.Equal(t, "[0,0]0[1,1]1[3,1]2[1,0]2[1,1]3", root.GetArray("k1").ToTestString())

root.GetArray("k1").InsertIntegerAfter(2, 4)
assert.Equal(t, `{"k1":[1,2,3,4]}`, root.Marshal())
assert.Equal(t, 4, root.GetArray("k1").Len())
assert.Equal(t, "[0,0]0[1,1]1[2,1]2[2,0]2[3,1]3[4,1]4", root.GetArray("k1").StructureAsString())
assert.Equal(t, "[0,0]0[1,1]1[2,1]2[2,0]2[3,1]3[4,1]4", root.GetArray("k1").ToTestString())

for i := 0; i < root.GetArray("k1").Len(); i++ {
assert.Equal(
Expand Down Expand Up @@ -216,23 +216,23 @@ func TestDocument(t *testing.T) {
text := root.GetText("k1")
assert.Equal(t,
`[0:0:00:0 {} ""][1:2:00:0 {} "A"][1:3:00:0 {} "12"]{1:2:00:1 {} "BC"}[1:2:00:3 {} "D"]`,
text.StructureAsString(),
text.ToTestString(),
)

from, _ := text.CreateRange(0, 0)
assert.Equal(t, "0:0:00:0:0", from.StructureAsString())
assert.Equal(t, "0:0:00:0:0", from.ToTestString())

from, _ = text.CreateRange(1, 1)
assert.Equal(t, "1:2:00:0:1", from.StructureAsString())
assert.Equal(t, "1:2:00:0:1", from.ToTestString())

from, _ = text.CreateRange(2, 2)
assert.Equal(t, "1:3:00:0:1", from.StructureAsString())
assert.Equal(t, "1:3:00:0:1", from.ToTestString())

from, _ = text.CreateRange(3, 3)
assert.Equal(t, "1:3:00:0:2", from.StructureAsString())
assert.Equal(t, "1:3:00:0:2", from.ToTestString())

from, _ = text.CreateRange(4, 4)
assert.Equal(t, "1:2:00:3:1", from.StructureAsString())
assert.Equal(t, "1:2:00:3:1", from.ToTestString())
return nil
})
assert.NoError(t, err)
Expand Down Expand Up @@ -265,7 +265,7 @@ func TestDocument(t *testing.T) {
assert.Equal(
t,
`[0:0:00:0 {} ""][1:2:00:0 {} "Hello world"]`,
text.StructureAsString(),
text.ToTestString(),
)
return nil
})
Expand All @@ -277,7 +277,7 @@ func TestDocument(t *testing.T) {
text.Style(0, 5, map[string]string{"b": "1"})
assert.Equal(t,
`[0:0:00:0 {} ""][1:2:00:0 {"b":"1"} "Hello"][1:2:00:5 {} " world"]`,
text.StructureAsString(),
text.ToTestString(),
)
return nil
})
Expand All @@ -294,14 +294,14 @@ func TestDocument(t *testing.T) {
assert.Equal(
t,
`[0:0:00:0 {} ""][1:2:00:0 {"b":"1"} "Hello"][1:2:00:5 {} " world"]`,
text.StructureAsString(),
text.ToTestString(),
)

text.Style(3, 5, map[string]string{"i": "1"})
assert.Equal(
t,
`[0:0:00:0 {} ""][1:2:00:0 {"b":"1"} "Hel"][1:2:00:3 {"b":"1","i":"1"} "lo"][1:2:00:5 {} " world"]`,
text.StructureAsString(),
text.ToTestString(),
)
return nil
})
Expand All @@ -319,7 +319,7 @@ func TestDocument(t *testing.T) {
t,
`[0:0:00:0 {} ""][1:2:00:0 {"b":"1"} "Hel"][1:2:00:3 {"b":"1","i":"1"} "lo"]`+
`[4:1:00:0 {} " Yorkie"]{1:2:00:5 {} " world"}`,
text.StructureAsString(),
text.ToTestString(),
)
return nil
})
Expand All @@ -337,7 +337,7 @@ func TestDocument(t *testing.T) {
t,
`[0:0:00:0 {} ""][1:2:00:0 {"b":"1"} "Hel"][1:2:00:3 {"b":"1","i":"1"} "lo"]`+
`[5:1:00:0 {"list":"true"} "\n"][4:1:00:0 {} " Yorkie"]{1:2:00:5 {} " world"}`,
text.StructureAsString(),
text.ToTestString(),
)
return nil
})
Expand Down Expand Up @@ -483,7 +483,7 @@ func TestDocument(t *testing.T) {
assert.Equal(
t,
`[0:0:00:0 {} ""][1:3:00:0 {} "12"]{1:2:00:0 {} "AB"}[1:2:00:2 {} "CD"]`,
doc.Root().GetText("text").StructureAsString(),
doc.Root().GetText("text").ToTestString(),
)

assert.Equal(t, 1, doc.GarbageLen())
Expand All @@ -492,7 +492,7 @@ func TestDocument(t *testing.T) {
assert.Equal(
t,
`[0:0:00:0 {} ""][1:3:00:0 {} "12"][1:2:00:2 {} "CD"]`,
doc.Root().GetText("text").StructureAsString(),
doc.Root().GetText("text").ToTestString(),
)

err = doc.Update(func(root *json.Object, p *presence.Presence) error {
Expand All @@ -503,7 +503,7 @@ func TestDocument(t *testing.T) {
assert.Equal(
t,
`[0:0:00:0 {} ""][1:3:00:0 {} "12"]{1:2:00:2 {} "CD"}`,
doc.Root().GetText("text").StructureAsString(),
doc.Root().GetText("text").ToTestString(),
)
})

Expand Down
Loading

0 comments on commit acc6703

Please sign in to comment.