tdd whee
parent
ed75a59ca0
commit
459bc54760
|
|
@ -1,6 +1,8 @@
|
|||
package ledger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
|
@ -36,6 +38,12 @@ func (file File) Deltas(like ...Like) ([]Delta, error) {
|
|||
}
|
||||
}
|
||||
for currency, value := range sums {
|
||||
if value == 0 {
|
||||
continue
|
||||
}
|
||||
if transaction.payee == "" {
|
||||
return nil, fmt.Errorf("didnt find net zero and no dumping ground payee set: %+v", transaction)
|
||||
}
|
||||
delta := newDelta(
|
||||
transaction.date,
|
||||
transaction.description,
|
||||
|
|
@ -64,6 +72,10 @@ type transactionRecipient struct {
|
|||
currency string
|
||||
}
|
||||
|
||||
func (t transaction) empty() bool {
|
||||
return fmt.Sprint(t) == fmt.Sprint(transaction{})
|
||||
}
|
||||
|
||||
func (file File) transactions() ([]transaction, error) {
|
||||
f, err := os.Open(string(file))
|
||||
if err != nil {
|
||||
|
|
@ -74,8 +86,10 @@ func (file File) transactions() ([]transaction, error) {
|
|||
result := make([]transaction, 0)
|
||||
for {
|
||||
one, err := readTransaction(f)
|
||||
if !one.empty() {
|
||||
result = append(result, one)
|
||||
if err == io.EOF {
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
return result, nil
|
||||
}
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue