mirror of
https://github.com/documize/community.git
synced 2025-07-21 14:19:43 +02:00
Handle edge case for document numbering + tests
This commit is contained in:
parent
0da8ae9e4a
commit
2e9734e73e
3 changed files with 30 additions and 2 deletions
|
@ -18,7 +18,7 @@ import (
|
|||
|
||||
// LevelizeDocument generates level and sequence numbers for all document sections
|
||||
func (h *Handler) LevelizeDocument(ctx domain.RequestContext, documentID string) {
|
||||
method := "page.Levelize"
|
||||
method := "page.LevelizeDocument"
|
||||
var err error
|
||||
ctx.Transaction.Commit()
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ func Numberize(pages []Page) {
|
|||
|
||||
for i, p := range pages {
|
||||
// handle bad data
|
||||
if p.Level == 0 {
|
||||
if p.Level == 0 || (i == 0 && p.Level > 1) {
|
||||
p.Level = 1
|
||||
}
|
||||
|
||||
|
|
|
@ -145,6 +145,34 @@ func TestNumberize4(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// Tests that numbering does not crash because of bad data
|
||||
func TestNumberize5(t *testing.T) {
|
||||
pages := []Page{}
|
||||
|
||||
// corruption starts at the top with sequence 0
|
||||
pages = append(pages, Page{Level: 2, Sequence: 0})
|
||||
pages = append(pages, Page{Level: 1, Sequence: 1})
|
||||
pages = append(pages, Page{Level: 2, Sequence: 4})
|
||||
pages = append(pages, Page{Level: 2, Sequence: 8})
|
||||
pages = append(pages, Page{Level: 2, Sequence: 16})
|
||||
|
||||
Numberize(pages)
|
||||
|
||||
expecting := []string{
|
||||
"1",
|
||||
"2",
|
||||
"2.1",
|
||||
"2.2",
|
||||
"2.3",
|
||||
}
|
||||
|
||||
for i, p := range pages {
|
||||
if p.Numbering != expecting[i] {
|
||||
t.Errorf("(Test 4) Position %d: expecting %s got %s\n", i, expecting[i], p.Numbering)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tests that good level data is not messed with
|
||||
func TestLevelize1(t *testing.T) {
|
||||
pages := []Page{}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue