add numXnum suport to process and summary. add summary test

This commit is contained in:
Dan Ballard 2018-05-08 10:07:20 -07:00
parent 2a69d61aa3
commit 6f79ecbd28
4 changed files with 121 additions and 9 deletions

View File

@ -0,0 +1,28 @@
# Work
- [ ] Write tests
- [x] summary
- [ ] migrate
# Test Data
- note
-
- [ ] nesting1
- [ ] nesting 2
- [ ] nesting 3
- [ ] nesting 4
- [x] nesting 5
- [x] not nested
-
asdasd
# Nothing done
- [ ] not done
- note
# Repetition
- [x] 5x5 things
- [ ] 0x2 other things

View File

@ -32,7 +32,7 @@ func (ph *processHandler) Eof() {
ph.Writeln(strconv.Itoa(ph.doneCount) + " / " + strconv.Itoa(ph.totalCount)) ph.Writeln(strconv.Itoa(ph.doneCount) + " / " + strconv.Itoa(ph.totalCount))
} }
func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, done bool) { func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, done bool, repTask process.RepTask) {
if strings.Trim(line, " \t\n\r") == "" { if strings.Trim(line, " \t\n\r") == "" {
return return
} }
@ -48,12 +48,16 @@ func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, do
if done { if done {
if !ph.headerPrinted { if !ph.headerPrinted {
ph.Writeln(" # " + ph.header) ph.Writeln("\t# " + ph.header)
ph.headerPrinted = true ph.headerPrinted = true
} }
ph.doneCount += 1 ph.doneCount += 1
// TODO: Math for [x] numXnum // TODO: Math for [x] numXnum
ph.Writeln(" " + strings.Join(stack, " / ")) repStr := ""
if repTask.Is {
repStr = strconv.Itoa( repTask.A * repTask.B)
}
ph.Writeln("\t\t" + repStr + strings.Join(stack, " / "))
} }
} }
@ -77,4 +81,6 @@ func main() {
ph.Writeln(file) ph.Writeln(file)
process.ProcessFile(ph, file) process.ProcessFile(ph, file)
} }
// If windows open summary.md
} }

View File

@ -0,0 +1,60 @@
package main
import (
"testing"
"os"
"github.com/dballard/markdown-bullet-journal/process"
"strings"
"math"
"bytes"
)
const EXPECTED = `
2018-05-07-TEST.md
# Work
Write tests / summary
# Test Data
nesting1 / nesting 2 / nesting 3 / nesting 4 / nesting 5
not nested
# Repetition
25 things
4 / 12
`
func TestSummary(t *testing.T) {
ph := new(processHandler)
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
ph.File = w
files := process.GetFiles()
for _, file := range files {
ph.Writeln("")
ph.Writeln(file)
process.ProcessFile(ph, file)
}
w.Close()
var result = make([]byte, 1000)
n, _ := r.Read(result)
//fmt.Printf("n:%v len(res):%v len(EXP):%v\n", n, len(result), len(EXPECTED))
if ! bytes.Equal(result[:n], []byte(EXPECTED)) {
var diffLoc = 0
for i, ch := range EXPECTED {
//fmt.Printf("%v/%v: %v %v\n", i, n, ch, result[i])
if i > n-1 || result[i] != byte(ch) {
diffLoc = i
break
}
}
//fmt.Println(diffLoc)
line := strings.Count(string(result[:diffLoc]), "\n")
errorStr := string(result[int(math.Max(0, float64(diffLoc - 10))) : int(math.Min(float64(len(result)), float64(diffLoc + 10))) ])
t.Errorf("Summary results do not match expected:\nfirst difference at line %v: '%v'\n%v<---->\n%v\n", line, errorStr, string(result), EXPECTED)
}
}

View File

@ -6,11 +6,17 @@ import (
"bufio" "bufio"
"regexp" "regexp"
"io/ioutil" "io/ioutil"
"strconv"
) )
type RepTask struct {
Is bool
A, B int
}
type ProcessHandler interface { type ProcessHandler interface {
Writeln(line string) Writeln(line string)
ProcessLine(line string, stack []string, todo bool, done bool) ProcessLine(line string, stack []string, todo bool, done bool, repTask RepTask)
Eof() Eof()
NewFile() NewFile()
} }
@ -58,31 +64,33 @@ func ProcessFile(ph ProcessHandler, fileName string) {
indentLevel := len(startSpaces.Find([]byte(line)))/4 indentLevel := len(startSpaces.Find([]byte(line)))/4
todo := false todo := false
done := false done := false
var repTask RepTask
if indentLevel < len(stack)-1 { if indentLevel < len(stack)-1 {
stack = stack[: indentLevel+1] stack = stack[: indentLevel+1]
} }
if indentLevel == len(stack)-1 { if indentLevel == len(stack)-1 {
stack[len(stack)-1], todo, done = getText(line, indentLevel) stack[len(stack)-1], todo, done, repTask = getText(line, indentLevel)
} }
if indentLevel >= len(stack) { if indentLevel >= len(stack) {
row := "" row := ""
row, todo, done = getText(line, indentLevel) row, todo, done, repTask = getText(line, indentLevel)
stack = append(stack, row) stack = append(stack, row)
} }
ph.ProcessLine(line, stack, todo, done) ph.ProcessLine(line, stack, todo, done, repTask)
} }
ph.Eof() ph.Eof()
} }
func getText(str string, indentLevel int) (text string, todo bool, done bool) { func getText(str string, indentLevel int) (text string, todo bool, done bool, repTask RepTask) {
//fmt.Printf("indentLevel: %v str: '%s'\n", indentLevel, str ) //fmt.Printf("indentLevel: %v str: '%s'\n", indentLevel, str )
if len(str) < (indentLevel*4 +2) { if len(str) < (indentLevel*4 +2) {
return "", false, false return "", false, false, RepTask{false, 0, 0}
} }
text = str[indentLevel*4 +2:] text = str[indentLevel*4 +2:]
done = false done = false
todo = false todo = false
repTask.Is = false
if text[0] == '[' { if text[0] == '[' {
todo = true todo = true
if text[1] == 'x' || text[1] == 'X' { if text[1] == 'x' || text[1] == 'X' {
@ -91,6 +99,16 @@ func getText(str string, indentLevel int) (text string, todo bool, done bool) {
if len(text) > 4 { if len(text) > 4 {
text = text[4:] text = text[4:]
} }
repTaskRegExp := regexp.MustCompile("^([0-9]*)[xX]([0-9]*)")
if repTaskRegExp.MatchString(text) {
repTask.Is = true
matches := repTaskRegExp.FindStringSubmatch(text)
repTask.A, _ = strconv.Atoi(matches[1])
repTask.B, _ = strconv.Atoi(matches[2])
loc := repTaskRegExp.FindIndex([]byte(text))
text = text[loc[1]:]
}
} }
return return
} }