Skip to content

Commit

Permalink
Expand/collapse sections, filling out some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Banks committed Jun 12, 2018
1 parent b509071 commit beba17d
Show file tree
Hide file tree
Showing 15 changed files with 445 additions and 48 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ install:
.PHONY: install

test:
@go vet ./...
@go test -cover ./...
.PHONY: test

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

## Features

- **TODO** Expand/collapse sections
- **TODO** Copy selected text to your clipboard
- **TODO** Load remote or local files
- **TODO** Cache remote files for offline access
- **TODO** Automatically discover README of remote Git repositories on GitHub, BitBucket and GitLab
- **TODO** Syntax highlighting for code snippets
- Expand/collapse content sections.
- **TODO** Copy selected text to your clipboard.
- **TODO** Load remote or local files.
- **TODO** Cache remote files for offline access.
- **TODO** Automatically discover README of remote Git repositories on GitHub, BitBucket and GitLab.
- **TODO** Syntax highlighting for code snippets.

## Installation

Expand Down
2 changes: 1 addition & 1 deletion cmd/kurz/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func log(msg string, a ...interface{}) {
if !strings.HasSuffix(msg, "\n") {
msg = msg + "\n"
msg += "\n"
}
fmt.Printf(msg, a...)
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/kurz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/KyleBanks/kurz/pkg/debug"
"github.com/KyleBanks/kurz/pkg/doc"
"github.com/KyleBanks/kurz/pkg/doc/parser"
"github.com/KyleBanks/kurz/pkg/doc/resolver"
Expand All @@ -27,8 +28,9 @@ func init() {

default:
path = arg

}

debug.Enabled = os.Getenv("KURZ_DEBUG") == "true"
}

func main() {
Expand Down
23 changes: 21 additions & 2 deletions pkg/debug/debug.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
package debug

import "os"
import (
"fmt"
"io"
"os"
"strings"
)

var Enabled = os.Getenv("KURZ_DEBUG") == "true"
var Enabled = false
var Out io.Writer = os.Stdout

// Log outputs a debug log message (with formatting) if
// the Enabled flag is set to true.
func Log(msg string, a ...interface{}) {
if !Enabled {
return
}

if !strings.HasSuffix(msg, "\n") {
msg += "\n"
}
fmt.Fprintf(Out, msg, a...)
}
27 changes: 27 additions & 0 deletions pkg/debug/debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package debug

import (
"bytes"
"testing"
)

func TestLog(t *testing.T) {

// Disabled
output := bytes.Buffer{}
Out = &output
Enabled = false
Log("Message")
if len(output.String()) > 0 {
t.Errorf("Unexpected logging when disabled, expected='', got=%v", output.String())
}

// Enabled
output = bytes.Buffer{}
Out = &output
Enabled = true
Log("Message")
if output.String() != "Message\n" {
t.Errorf("Unexpected log, expected=Message\n, got=%v", output.String())
}
}
4 changes: 2 additions & 2 deletions pkg/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ type Parser interface {
}

type Document struct {
Headings []Heading
Headers []Header
}

type Heading struct {
type Header struct {
Title string
Level int
Content []Section
Expand Down
113 changes: 113 additions & 0 deletions pkg/doc/doc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package doc

import (
"bytes"
"errors"
"io"
"io/ioutil"
"reflect"
"testing"
)

type mockResolver struct {
resolveFn func(string) (io.ReadCloser, error)
}

func (m mockResolver) Resolve(path string) (io.ReadCloser, error) {
return m.resolveFn(path)
}

type mockParser struct {
parseFn func(io.Reader) (Document, error)
}

func (m mockParser) Parse(r io.Reader) (Document, error) {
return m.parseFn(r)
}

func TestNewDocument(t *testing.T) {
expectPath := "/path/to/file"
expectContent := "CONTENT"
var expectDoc Document

r := mockResolver{
resolveFn: func(path string) (io.ReadCloser, error) {
if path != expectPath {
t.Fatalf("Unexpected path, expected=%v, got=%v", expectPath, path)
}

b := bytes.NewBufferString(expectContent)
return ioutil.NopCloser(b), nil
},
}
p := mockParser{
parseFn: func(r io.Reader) (Document, error) {
got, err := ioutil.ReadAll(r)
if err != nil {
t.Fatal(err)
}

if string(got) != expectContent {
t.Fatalf("Unexpected content, expected=%v, got=%s", expectContent, got)
}

return expectDoc, nil
},
}

d, err := NewDocument(expectPath, r, p)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(d, expectDoc) {
t.Fatalf("Unexpected document, expected=%v, got=%v", expectDoc, d)
}
}

func TestNewDocument_errors(t *testing.T) {
// Resolver Error
{
expectErr := errors.New("resolver error")
r := mockResolver{
resolveFn: func(path string) (io.ReadCloser, error) {
return nil, expectErr
},
}

if _, err := NewDocument("", r, nil); err != expectErr {
t.Fatalf("Unexpected error, expected=%v, got=%v", expectErr, err)
}
}

// Parser Error
{
expectErr := errors.New("parser error")
r := mockResolver{
resolveFn: func(path string) (io.ReadCloser, error) {
return ioutil.NopCloser(&bytes.Buffer{}), nil
},
}
p := mockParser{
parseFn: func(r io.Reader) (Document, error) {
return Document{}, expectErr
},
}

if _, err := NewDocument("", r, p); err != expectErr {
t.Fatalf("Unexpected error, expected=%v, got=%v", expectErr, err)
}
}
}

func TestNopStyler_Style(t *testing.T) {
var ns NopStyler
styles := []Style{Bold, Italic, Underline, Code}
for _, s := range styles {
got := ns.Style("string", s)
if got != "string" {
t.Errorf("Unexpected output for style %v, expected=string, got=%v", s, got)
}
}

}
6 changes: 2 additions & 4 deletions pkg/doc/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m Markdown) Parse(r io.Reader) (doc.Document, error) {
return blackfriday.GoToNext
}

d.Headings = append(d.Headings, doc.Heading{
d.Headers = append(d.Headers, doc.Header{
Title: m.nodeContents(node),
Level: node.HeadingData.Level,
Content: m.sectionContents(node),
Expand Down Expand Up @@ -140,9 +140,7 @@ func (Markdown) skipNext(n *blackfriday.Node) bool {
}

func (m Markdown) nodeContents(n *blackfriday.Node) string {
if debug.Enabled {
fmt.Printf("Type=%v, Literal=%s\n", n.Type, n.Literal)
}
debug.Log("Type=%v, Literal=%s\n", n.Type, n.Literal)

switch n.Type {

Expand Down
26 changes: 13 additions & 13 deletions pkg/doc/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ func TestMarkdown_Parse_Basic(t *testing.T) {
m := NewMarkdown(doc.NopStyler{})

d, err := m.Parse(bytes.NewBufferString(`
# Heading 1
# Header 1
Text goes here.
Another paragraph.
## Heading 2
## Header 2
And here's some ` + "`" + `code` + "`" + `.
`))
Expand All @@ -26,26 +26,26 @@ And here's some ` + "`" + `code` + "`" + `.
}

assertDocsEqual(t, d, doc.Document{
Headings: []doc.Heading{
{Title: "Heading 1", Level: 1, Content: []doc.Section{
{"Text goes here.\n"},
{"Another paragraph.\n"},
Headers: []doc.Header{
{Title: "Header 1", Level: 1, Content: []doc.Section{
{Text: "Text goes here.\n"},
{Text: "Another paragraph.\n"},
}},
{Title: "Heading 2", Level: 2, Content: []doc.Section{
{"And here's some code.\n"},
{Title: "Header 2", Level: 2, Content: []doc.Section{
{Text: "And here's some code.\n"},
}},
},
})
}

func assertDocsEqual(t *testing.T, got, exp doc.Document) {
if len(got.Headings) != len(exp.Headings) {
t.Fatalf("Heading count mismatch, expected=%v, got=%v", len(exp.Headings), len(got.Headings))
if len(got.Headers) != len(exp.Headers) {
t.Fatalf("Header count mismatch, expected=%v, got=%v", len(exp.Headers), len(got.Headers))
}

for h := 0; h < len(got.Headings); h++ {
h1 := got.Headings[h]
h2 := exp.Headings[h]
for h := 0; h < len(got.Headers); h++ {
h1 := got.Headers[h]
h2 := exp.Headers[h]
if h1.Title != h2.Title {
t.Errorf("[h=%v] Unexpected title, expected=%v, got=%v", h, h2.Title, h1.Title)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/ui/console/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var (
{" ⬅ / ESC ", "Go Back"},
{"⬆ ", "Up"},
{"⬇ ", "Down"},
{" SPACE ", "Collapse"},
}
)

Expand Down

0 comments on commit beba17d

Please sign in to comment.