diff --git a/mdbj-migrate/2018-05-07-TEST.md b/mdbj-migrate/2018-05-07-TEST.md new file mode 100644 index 0000000..007581d --- /dev/null +++ b/mdbj-migrate/2018-05-07-TEST.md @@ -0,0 +1,31 @@ +# 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 +- [ ] Group + - [ ] 0x3 nesting rep + - [x] 2x6 done nested rep \ No newline at end of file diff --git a/mdbj-migrate/migrate.go b/mdbj-migrate/migrate.go index db2683f..233ded6 100644 --- a/mdbj-migrate/migrate.go +++ b/mdbj-migrate/migrate.go @@ -6,6 +6,8 @@ import ( "log" "github.com/dballard/markdown-bullet-journal/process" "fmt" + "strconv" + "strings" ) const template = `# Work @@ -38,10 +40,14 @@ func (ph *processHandler) Writeln(line string) { func (ph *processHandler) Eof() {} func (ph *processHandler) NewFile() {} -func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, done bool) { +func (ph *processHandler) ProcessLine(line string, indentLevel int, stack []string, todo bool, done bool, repTask process.RepTask) { // TODO: handle [x] numXnum - if !done { - ph.Writeln(line) + if !done || repTask.Is { + if repTask.Is { + ph.Writeln(strings.Repeat("\t", indentLevel) + "- [ ] 0x" + strconv.Itoa(repTask.B) + stack[len(stack)-1] ) + } else { + ph.Writeln(line) + } } } diff --git a/mdbj-migrate/migrate_test.go b/mdbj-migrate/migrate_test.go new file mode 100644 index 0000000..7dd6adc --- /dev/null +++ b/mdbj-migrate/migrate_test.go @@ -0,0 +1,75 @@ +package main + +import ( + "testing" + "os" + "bytes" + "strings" + "math" + "github.com/dballard/markdown-bullet-journal/process" +) + +const EXPECTED = `# Work + +- [ ] Write tests + - [ ] migrate + +# Test Data + +- note +- +- [ ] nesting1 + - [ ] nesting 2 + - [ ] nesting 3 + - [ ] nesting 4 + - + asdasd + +# Nothing done + +- [ ] not done +- note + +# Repetition + +- [ ] 0x5 things +- [ ] 0x2 other things +- [ ] Group + - [ ] 0x3 nesting rep + - [ ] 0x6 done nested rep +` + +func TestMigrate(t *testing.T) { + ph := new(processHandler) + r, w, err := os.Pipe() + if err != nil { + t.Fatal(err) + } + ph.File = w + + files := process.GetFiles() + + lastFile := files[len(files)-1] + process.ProcessFile(ph, lastFile) + + w.Close() + var result= make([]byte, 1000) + n, _ := r.Read(result) + + 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) + + } +} \ No newline at end of file diff --git a/mdbj-summary/summary.go b/mdbj-summary/summary.go index 4e3dd3f..3f0f1f4 100644 --- a/mdbj-summary/summary.go +++ b/mdbj-summary/summary.go @@ -32,7 +32,7 @@ func (ph *processHandler) Eof() { ph.Writeln(strconv.Itoa(ph.doneCount) + " / " + strconv.Itoa(ph.totalCount)) } -func (ph *processHandler) ProcessLine(line string, stack []string, todo bool, done bool, repTask process.RepTask) { +func (ph *processHandler) ProcessLine(line string, indentLevel int, stack []string, todo bool, done bool, repTask process.RepTask) { if strings.Trim(line, " \t\n\r") == "" { return } diff --git a/process/process.go b/process/process.go index 71a070a..314ed06 100644 --- a/process/process.go +++ b/process/process.go @@ -16,7 +16,7 @@ type RepTask struct { type ProcessHandler interface { Writeln(line string) - ProcessLine(line string, stack []string, todo bool, done bool, repTask RepTask) + ProcessLine(line string, indentLevel int, stack []string, todo bool, done bool, repTask RepTask) Eof() NewFile() } @@ -77,7 +77,7 @@ func ProcessFile(ph ProcessHandler, fileName string) { stack = append(stack, row) } - ph.ProcessLine(line, stack, todo, done, repTask) + ph.ProcessLine(line, indentLevel, stack, todo, done, repTask) } ph.Eof() }