This commit is contained in:
Bel LaPointe
2023-10-24 06:09:53 -06:00
parent ed75a59ca0
commit 459bc54760
2 changed files with 81 additions and 2 deletions

View File

@@ -1,6 +1,9 @@
package ledger
import (
"fmt"
"io"
"strings"
"testing"
"time"
)
@@ -70,3 +73,65 @@ func TestFileDeltas(t *testing.T) {
})
}
}
func TestReadTransaction(t *testing.T) {
d := func(s string) time.Time {
v, _ := time.Parse("2006-01-02", s)
return v
}
cases := map[string]struct {
input string
want transaction
err error
}{
"empty": {
input: "",
want: transaction{},
err: io.EOF,
},
"white space": {
input: " ",
want: transaction{},
err: io.EOF,
},
"verbose": {
input: `
2003-04-05 Reasoning here
A:B $1.00
C:D $-1.00
`,
want: transaction{
date: d("2003-04-05"),
description: "Reasoning here",
payee: "",
recipients: []transactionRecipient{
{
name: "A:B",
value: 1.0,
currency: "$",
},
{
name: "C:D",
value: -1.0,
currency: "$",
},
},
},
err: io.EOF,
},
}
for name, d := range cases {
c := d
t.Run(name, func(t *testing.T) {
got, err := readTransaction(strings.NewReader(c.input))
if err != c.err {
t.Error(err)
}
if fmt.Sprintf("%+v", got) != fmt.Sprintf("%+v", c.want) {
t.Errorf("want\n\t%+v, got\n\t%+v", c.want, got)
}
})
}
}