113 lines
2.0 KiB
Go
Executable File
113 lines
2.0 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestNewStubTransaction(t *testing.T) {
|
|
want := Transaction{
|
|
Date: "1",
|
|
Amount: 0,
|
|
Payer: UnknownAccount,
|
|
Payee: UnknownAccount,
|
|
Description: UnknownAccount,
|
|
}
|
|
got := newStubTransaction()
|
|
if got.Date == "" {
|
|
t.Fatal(got.Date)
|
|
}
|
|
got.Date = want.Date
|
|
if want != got {
|
|
t.Fatal(want, got)
|
|
}
|
|
}
|
|
|
|
func TestWords(t *testing.T) {
|
|
input := `
|
|
hello world
|
|
i have s ome things
|
|
`
|
|
got := words([]byte(input))
|
|
want := [][]byte{
|
|
[]byte("hello"),
|
|
[]byte("world"),
|
|
[]byte("i"),
|
|
[]byte("have"),
|
|
[]byte("s"),
|
|
[]byte("ome"),
|
|
[]byte("things"),
|
|
}
|
|
if fmt.Sprint(got) != fmt.Sprint(want) {
|
|
t.Fatal(want, got)
|
|
}
|
|
}
|
|
|
|
func TestLines(t *testing.T) {
|
|
s := `
|
|
|
|
hello world
|
|
`
|
|
scanner := strings.NewReader(s)
|
|
line, _ := nextLine(scanner)
|
|
if strings.TrimSpace(string(line)) != "hello world" {
|
|
t.Fatalf("%q", line)
|
|
}
|
|
if string(line) == strings.TrimSpace(string(line)) {
|
|
t.Fatalf("%q", line)
|
|
}
|
|
}
|
|
|
|
func TestTwoReadTransaction(t *testing.T) {
|
|
input := `
|
|
date1 desc1
|
|
payee1 $1.00
|
|
payer1
|
|
date2 desc2
|
|
payee2 $2.00
|
|
payer2
|
|
`
|
|
r := strings.NewReader(input)
|
|
got1, _ := readTransaction(r)
|
|
got2, _ := readTransaction(r)
|
|
t.Logf("%+v", got1)
|
|
t.Logf("%+v", got2)
|
|
}
|
|
|
|
func TestReadTransaction(t *testing.T) {
|
|
cases := map[string]struct {
|
|
input string
|
|
want Transaction
|
|
}{
|
|
"ez": {
|
|
input: `
|
|
2021-07-01 description
|
|
|
|
payee $1.00
|
|
payer
|
|
`,
|
|
want: Transaction{
|
|
Date: "2021-07-01",
|
|
Description: "description",
|
|
Payee: "payee",
|
|
Amount: 1.00,
|
|
Payer: "payer",
|
|
},
|
|
},
|
|
}
|
|
|
|
for name, d := range cases {
|
|
c := d
|
|
t.Run(name, func(t *testing.T) {
|
|
got, err := readTransaction(strings.NewReader(c.input))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != c.want {
|
|
t.Fatalf("\n%s =!> %+v, got %+v", c.input, c.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|