summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2020-11-15 15:25:20 +0700
committerShulhan <m.shulhan@gmail.com>2020-11-15 15:25:20 +0700
commit3659f9e9e4b632ca4d759eadcbadc6d5e862f4db (patch)
tree89b1bab8cf693d43fde4e2eb2c1bbedb39d241d8
parent07a0d95ce49c3550850725f285d767b195b5efe8 (diff)
downloadpakakeh.go-3659f9e9e4b632ca4d759eadcbadc6d5e862f4db.tar.xz
parser: fix Line method that always return non-empty line
In case of content end without new line, for example "a\nb", the Line() method always return "b, 0" on the last line.
-rw-r--r--lib/parser/parser.go1
-rw-r--r--lib/parser/parser_test.go44
2 files changed, 45 insertions, 0 deletions
diff --git a/lib/parser/parser.go b/lib/parser/parser.go
index cebf033a..33897ba7 100644
--- a/lib/parser/parser.go
+++ b/lib/parser/parser.go
@@ -175,6 +175,7 @@ func (p *Parser) Line() (string, rune) {
}
p.token = append(p.token, r)
}
+ p.x = len(p.v)
return string(p.token), 0
}
diff --git a/lib/parser/parser_test.go b/lib/parser/parser_test.go
index cdb66b4f..35477717 100644
--- a/lib/parser/parser_test.go
+++ b/lib/parser/parser_test.go
@@ -34,6 +34,50 @@ func TestParser_AddDelimiters(t *testing.T) {
}
}
+type expLine struct {
+ line string
+ c rune
+}
+
+func TestParser_Line(t *testing.T) {
+ p := New("", "\n")
+
+ cases := []struct {
+ content string
+ exp []expLine
+ }{{
+ content: ``,
+ exp: []expLine{{}},
+ }, {
+ content: `a
+`,
+ exp: []expLine{
+ {"a", '\n'},
+ {"", 0},
+ },
+ }, {
+ content: `a
+
+b`,
+ exp: []expLine{
+ {"a", '\n'},
+ {"", '\n'},
+ {"b", 0},
+ {"", 0},
+ },
+ }}
+
+ for _, c := range cases {
+ p.Load(c.content, "\n")
+
+ for x := 0; x < len(c.exp); x++ {
+ gotLine, gotC := p.Line()
+ test.Assert(t, "", c.exp[x].line, gotLine, true)
+ test.Assert(t, "", c.exp[x].c, gotC, true)
+ }
+ }
+}
+
func TestParser_Lines(t *testing.T) {
cases := []struct {
desc string