this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix EOF and line number handling in parser

The parser now returns an EOF on the first call to Next() if the input
is empty and increments the line number before returning EOF for the
first time.

+33 -4
+5 -4
gitdiff/parser.go
··· 146 146 // reading the line, including io.EOF when the end of stream is reached. 147 147 func (p *parser) Next() error { 148 148 if p.eof { 149 - p.lines[0] = "" 150 149 return io.EOF 151 150 } 152 151 ··· 160 159 } 161 160 162 161 err := p.shiftLines() 163 - if err == io.EOF { 164 - p.eof = p.lines[1] == "" 165 - } else if err != nil { 162 + if err != nil && err != io.EOF { 166 163 return err 167 164 } 168 165 169 166 p.lineno++ 167 + if p.lines[0] == "" { 168 + p.eof = true 169 + return io.EOF 170 + } 170 171 return nil 171 172 } 172 173
+28
gitdiff/parser_test.go
··· 21 21 if err := p.Next(); err != nil { 22 22 t.Fatalf("error advancing parser: %v", err) 23 23 } 24 + if p.lineno != 1 { 25 + t.Fatalf("incorrect line number: expected %d, actual: %d", 1, p.lineno) 26 + } 24 27 25 28 line := p.Line(0) 26 29 if line != "the first line\n" { ··· 30 33 if err := p.Next(); err != nil { 31 34 t.Fatalf("error advancing parser: %v", err) 32 35 } 36 + if p.lineno != 2 { 37 + t.Fatalf("incorrect line number: expected %d, actual: %d", 2, p.lineno) 38 + } 33 39 34 40 line = p.Line(0) 35 41 if line != "the second line\n" { ··· 39 45 if err := p.Next(); err != nil { 40 46 t.Fatalf("error advancing parser: %v", err) 41 47 } 48 + if p.lineno != 3 { 49 + t.Fatalf("incorrect line number: expected %d, actual: %d", 3, p.lineno) 50 + } 42 51 43 52 line = p.Line(0) 44 53 if line != "the third line\n" { 45 54 t.Fatalf("incorrect third line: %s", line) 46 55 } 47 56 57 + // reading after the last line should return EOF 48 58 if err := p.Next(); err != io.EOF { 49 59 t.Fatalf("expected EOF, but got: %v", err) 50 60 } 61 + if p.lineno != 4 { 62 + t.Fatalf("incorrect line number: expected %d, actual: %d", 4, p.lineno) 63 + } 64 + 65 + // reading again returns EOF again and does not advance the line 66 + if err := p.Next(); err != io.EOF { 67 + t.Fatalf("expected EOF, but got: %v", err) 68 + } 69 + if p.lineno != 4 { 70 + t.Fatalf("incorrect line number: expected %d, actual: %d", 4, p.lineno) 71 + } 51 72 }) 52 73 53 74 t.Run("peek", func(t *testing.T) { ··· 69 90 line = p.Line(0) 70 91 if line != "the second line\n" { 71 92 t.Fatalf("incorrect line: %s", line) 93 + } 94 + }) 95 + 96 + t.Run("emptyInput", func(t *testing.T) { 97 + p := &parser{r: bufio.NewReader(strings.NewReader(""))} 98 + if err := p.Next(); err != io.EOF { 99 + t.Fatalf("expected EOF, but got: %v", err) 72 100 } 73 101 }) 74 102 }