103 lines
2.1 KiB
Go
103 lines
2.1 KiB
Go
package terrace
|
|
|
|
import (
|
|
"io"
|
|
"testing"
|
|
)
|
|
|
|
// MockReader is a mock implementation of the Reader interface for testing.
|
|
type MockReader struct {
|
|
lines []string
|
|
index int
|
|
}
|
|
|
|
// Read returns the next line from the mock reader.
|
|
func (r *MockReader) Read() (string, error) {
|
|
if r.index >= len(r.lines) {
|
|
return "", io.EOF
|
|
}
|
|
line := r.lines[r.index]
|
|
r.index++
|
|
return line, nil
|
|
}
|
|
|
|
func TestTerraceDocument(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
lines []string
|
|
}{
|
|
{
|
|
name: "simple document",
|
|
lines: []string{"hello world", " child1", " child2", "another top-level"},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
reader := &MockReader{lines: tt.lines}
|
|
doc := NewTerraceDocument(reader, ' ')
|
|
|
|
// Read all nodes
|
|
var nodes []*TerraceNode
|
|
for {
|
|
node, err := doc.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
}
|
|
nodes = append(nodes, node)
|
|
}
|
|
|
|
if len(nodes) != len(tt.lines) {
|
|
t.Errorf("Expected %d nodes, but got %d", len(tt.lines), len(nodes))
|
|
}
|
|
|
|
// Push back a node
|
|
if len(nodes) > 0 {
|
|
lastNode := nodes[len(nodes)-1]
|
|
doc.PushBack(lastNode)
|
|
|
|
node, err := doc.Next()
|
|
if err != nil {
|
|
t.Fatalf("Unexpected error: %v", err)
|
|
}
|
|
|
|
if node != lastNode {
|
|
t.Errorf("Expected to read the pushed back node")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestTerraceNode(t *testing.T) {
|
|
lineData := &LineData{Indent: ' ', Level: 2, OffsetHead: 2, OffsetTail: 7}
|
|
node := &TerraceNode{
|
|
lineData: lineData,
|
|
content: " hello world",
|
|
lineNumber: 1,
|
|
}
|
|
|
|
if node.Head() != "hello" {
|
|
t.Errorf("Expected head to be 'hello', but got '%s'", node.Head())
|
|
}
|
|
|
|
if node.Tail() != "world" {
|
|
t.Errorf("Expected tail to be 'world', but got '%s'", node.Tail())
|
|
}
|
|
|
|
if node.Content() != "hello world" {
|
|
t.Errorf("Expected content to be 'hello world', but got '%s'", node.Content())
|
|
}
|
|
|
|
if node.Level() != 2 {
|
|
t.Errorf("Expected level to be 2, but got %d", node.Level())
|
|
}
|
|
|
|
if node.LineNumber() != 1 {
|
|
t.Errorf("Expected line number to be 1, but got %d", node.LineNumber())
|
|
}
|
|
}
|