mirror of
https://github.com/antonmedv/fx.git
synced 2024-09-11 18:27:10 +03:00
Add more tests
This commit is contained in:
parent
2c634b27d1
commit
1034e6ee34
67
main_test.go
67
main_test.go
@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/bubbles/textinput"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/x/exp/teatest"
|
||||
@ -15,10 +16,10 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
lipgloss.SetColorProfile(termenv.Ascii)
|
||||
lipgloss.SetColorProfile(termenv.ANSI)
|
||||
}
|
||||
|
||||
func TestOutput(t *testing.T) {
|
||||
func prepare(t *testing.T) *teatest.TestModel {
|
||||
file, err := os.Open("testdata/example.json")
|
||||
require.NoError(t, err)
|
||||
|
||||
@ -29,25 +30,73 @@ func TestOutput(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
m := &model{
|
||||
top: head,
|
||||
head: head,
|
||||
wrap: true,
|
||||
showCursor: true,
|
||||
search: newSearch(),
|
||||
top: head,
|
||||
head: head,
|
||||
wrap: true,
|
||||
showCursor: true,
|
||||
digInput: textinput.New(),
|
||||
searchInput: textinput.New(),
|
||||
search: newSearch(),
|
||||
}
|
||||
tm := teatest.NewTestModel(
|
||||
t, m,
|
||||
teatest.WithInitialTermSize(300, 100),
|
||||
teatest.WithInitialTermSize(80, 40),
|
||||
)
|
||||
return tm
|
||||
}
|
||||
|
||||
func read(t *testing.T, tm *teatest.TestModel) []byte {
|
||||
var out []byte
|
||||
teatest.WaitFor(t,
|
||||
tm.Output(),
|
||||
func(b []byte) bool {
|
||||
return bytes.Contains(b, []byte("Fox and Dog"))
|
||||
out = b
|
||||
return bytes.Contains(b, []byte("{"))
|
||||
},
|
||||
teatest.WithCheckInterval(time.Millisecond*100),
|
||||
teatest.WithDuration(time.Second),
|
||||
)
|
||||
return out
|
||||
}
|
||||
|
||||
func TestOutput(t *testing.T) {
|
||||
tm := prepare(t)
|
||||
|
||||
teatest.RequireEqualOutput(t, read(t, tm))
|
||||
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
|
||||
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
||||
}
|
||||
|
||||
func TestNavigation(t *testing.T) {
|
||||
tm := prepare(t)
|
||||
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyDown})
|
||||
teatest.RequireEqualOutput(t, read(t, tm))
|
||||
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
|
||||
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
||||
}
|
||||
|
||||
func TestDig(t *testing.T) {
|
||||
tm := prepare(t)
|
||||
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(".")})
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(".year")})
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyEnter})
|
||||
teatest.RequireEqualOutput(t, read(t, tm))
|
||||
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
|
||||
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
||||
}
|
||||
|
||||
func TestCollapseRecursive(t *testing.T) {
|
||||
tm := prepare(t)
|
||||
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyShiftLeft})
|
||||
teatest.RequireEqualOutput(t, read(t, tm))
|
||||
|
||||
tm.Send(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("q")})
|
||||
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
|
||||
|
25
node.go
25
node.go
@ -133,18 +133,23 @@ func (n *node) expandRecursively() {
|
||||
}
|
||||
|
||||
func (n *node) findChildByKey(key string) *node {
|
||||
for at := n.next; at != nil && at != n.end; {
|
||||
k, err := strconv.Unquote(string(at.key))
|
||||
if err != nil {
|
||||
return nil
|
||||
it := n.next
|
||||
for it != nil && it != n.end {
|
||||
if it.key != nil {
|
||||
k, err := strconv.Unquote(string(it.key))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
if k == key {
|
||||
return it
|
||||
}
|
||||
}
|
||||
if k == key {
|
||||
return at
|
||||
}
|
||||
if at.end != nil {
|
||||
at = at.end.next
|
||||
if it.chunkEnd != nil {
|
||||
it = it.chunkEnd.next
|
||||
} else if it.end != nil {
|
||||
it = it.end.next
|
||||
} else {
|
||||
at = at.next
|
||||
it = it.next
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
40
testdata/TestCollapseRecursive.golden
vendored
Normal file
40
testdata/TestCollapseRecursive.golden
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
[?25l[7m{[0m
|
||||
"title": "Lorem ipsum",
|
||||
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusm
|
||||
od tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam
|
||||
, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo cons
|
||||
equat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum d
|
||||
olore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident
|
||||
, sunt in culpa qui officia deserunt mollit anim id est laborum.",
|
||||
"tags": […],
|
||||
"year": 3000,
|
||||
"funny": true,
|
||||
"author": {"name":…}
|
||||
}
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
[80D
|
40
testdata/TestDig.golden
vendored
Normal file
40
testdata/TestDig.golden
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
[?25l{
|
||||
"title": "Lorem ipsum",
|
||||
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusm
|
||||
od tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam
|
||||
, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo cons
|
||||
equat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum d
|
||||
olore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident
|
||||
, sunt in culpa qui officia deserunt mollit anim id est laborum.",
|
||||
"tags": [
|
||||
"lorem",
|
||||
"ipsum",
|
||||
null
|
||||
],
|
||||
[7m"year"[0m: 3000,
|
||||
"funny": true,
|
||||
"author": {
|
||||
"name": "John Doe",
|
||||
"email": "john@doe.com"
|
||||
}
|
||||
}
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
.year [80D
|
40
testdata/TestNavigation.golden
vendored
Normal file
40
testdata/TestNavigation.golden
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
[?25l{
|
||||
"title": "Lorem ipsum",
|
||||
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusm
|
||||
[7mod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam[0m
|
||||
, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo cons
|
||||
equat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum d
|
||||
olore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident
|
||||
, sunt in culpa qui officia deserunt mollit anim id est laborum.",
|
||||
"tags": [
|
||||
"lorem",
|
||||
"ipsum",
|
||||
null
|
||||
],
|
||||
"year": 3000,
|
||||
"funny": true,
|
||||
"author": {
|
||||
"name": "John Doe",
|
||||
"email": "john@doe.com"
|
||||
}
|
||||
}
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
.text [80D
|
40
testdata/TestOutput.golden
vendored
Normal file
40
testdata/TestOutput.golden
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
[?25l[7m{[0m
|
||||
"title": "Lorem ipsum",
|
||||
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusm
|
||||
od tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam
|
||||
, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo cons
|
||||
equat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum d
|
||||
olore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident
|
||||
, sunt in culpa qui officia deserunt mollit anim id est laborum.",
|
||||
"tags": [
|
||||
"lorem",
|
||||
"ipsum",
|
||||
null
|
||||
],
|
||||
"year": 3000,
|
||||
"funny": true,
|
||||
"author": {
|
||||
"name": "John Doe",
|
||||
"email": "john@doe.com"
|
||||
}
|
||||
}
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
~
|
||||
[80D
|
9
testdata/example.json
vendored
9
testdata/example.json
vendored
@ -1,10 +1,9 @@
|
||||
{
|
||||
"title": "Fox and Dog",
|
||||
"body": "Brown fox jumps over a lazy dog",
|
||||
"title": "Lorem ipsum",
|
||||
"text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
|
||||
"tags": [
|
||||
"fox",
|
||||
"dog",
|
||||
"lazy",
|
||||
"lorem",
|
||||
"ipsum",
|
||||
null
|
||||
],
|
||||
"year": 3000,
|
||||
|
Loading…
Reference in New Issue
Block a user