Open the report on last month
The report answered for the whole index, which is the one window a spending report is least often asked for: last January's groceries sat in the same total as last week's, and nothing on screen said which was which. It now opens on the month that has just ended -- the last one a statement can be complete for -- and left/right step along an axis beside the totals, from all time down through the named windows to the oldest month the index holds. The period narrows the report and nothing else. reloadReport runs its own query rather than reusing the rows the transaction list is showing: the two share the account, the search and the untagged toggle, and differ only in the date bounds, so opening on last month must not hide the rest of the index from the list beside it. Nothing may put the period into m.filter, which is exactly what would make it leak. The windows are relative to today, never to the newest statement. "Last month" with nothing in it reports nothing and says so, because silently answering for a month nobody asked for is worse than an empty screen; an empty period and an empty index therefore give different messages, one asking for another period and the other for an import. The rolling windows run to the end of this month rather than to the last complete one -- "last 3 months" is asked in order to see what is happening now, and leaving out the days since the 1st answers a question nobody put. The axis is built over the whole index rather than the rows in view, or it would grow and shrink as the account or search filter changed and move under the cursor; a reload rebuilds it, since an import can reach further back, but keeps the window the user was on. s cycles how those rows are arranged: largest out first as before, then in, net lowest first so the biggest losses lead, count, and the tag A to Z. A letter rather than a chord because the report is not a form. The marked heading says which column the rows are read from and the help names what the key does next, as the rule builder's preview already does. The sort rearranges rows and never changes which rows there are, so the order stays out of the filter for the same reason the period does. Currency remains the outer key under every order -- there are no rates here, so two currencies interleaved by amount would invite a comparison that cannot be made -- and every order falls back to the tag, so ties keep a fixed position instead of reshuffling between reloads. money report takes the same choice as --sort out|in|net|count|tag, and a misspelt one is refused rather than silently reporting in the default order. It keeps --month and has no equivalent of the wider windows. store.Filter gains From and To, compared as strings because dates are stored ISO-8601 and a string comparison is therefore a date comparison. store.Months replaces report.Months, which nothing had ever called: the axis needs the months of the whole index, not of a slice already in hand. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Period is the window a report covers: an inclusive range of ISO dates, with
|
||||
// either side empty for unbounded. Dates are stored as YYYY-MM-DD, so comparing
|
||||
// them as strings compares them as dates and nothing has to be parsed to filter
|
||||
// on one.
|
||||
type Period struct {
|
||||
// Label is what to call the window: "last month", "all time", "2026-06".
|
||||
Label string
|
||||
// Span names the dates Label stands for, because "last month" says nothing
|
||||
// on its own about which month it was. Empty when the label already is the
|
||||
// span, as it is for a single month and for all time.
|
||||
Span string
|
||||
From string
|
||||
To string
|
||||
}
|
||||
|
||||
// String names the window, and what it covers when the label does not.
|
||||
func (p Period) String() string {
|
||||
if p.Span == "" {
|
||||
return p.Label
|
||||
}
|
||||
return p.Label + " · " + p.Span
|
||||
}
|
||||
|
||||
// LastMonth is the label of the window a report opens on. The month that has
|
||||
// just ended is the last one a statement can be complete for: this month is
|
||||
// still filling up, so its total is not comparable with any other.
|
||||
const LastMonth = "last month"
|
||||
|
||||
// Periods is the axis a report steps along, widest window first: the named
|
||||
// ranges, then every month the index holds that is older than last month, most
|
||||
// recent of those first. months is expected newest first, as store.Months
|
||||
// returns it; anything in it that a named range already covers is left out of
|
||||
// the tail rather than offered twice.
|
||||
//
|
||||
// The named ranges are relative to today, not to the newest statement. "Last
|
||||
// month" means the month that has just ended, so if no statement covers it yet
|
||||
// the report for it is empty — which is the honest answer, and one the user can
|
||||
// act on, where silently reporting an older month they did not ask for is not.
|
||||
func Periods(today time.Time, months []string) []Period {
|
||||
thisMonth := time.Date(today.Year(), today.Month(), 1, 0, 0, 0, 0, time.UTC)
|
||||
lastMonth := thisMonth.AddDate(0, -1, 0)
|
||||
|
||||
// A range ends with this month rather than with the last complete one:
|
||||
// "last 3 months" is asked in order to see what is happening now, and
|
||||
// leaving out the days since the 1st would answer a different question.
|
||||
rolling := func(back int, label string) Period {
|
||||
from := thisMonth.AddDate(0, -back, 0)
|
||||
return Period{
|
||||
Label: label,
|
||||
Span: from.Format("2006-01") + " … " + thisMonth.Format("2006-01"),
|
||||
From: isoDate(from),
|
||||
To: isoDate(endOfMonth(thisMonth)),
|
||||
}
|
||||
}
|
||||
month := func(start time.Time, label string) Period {
|
||||
p := Period{Label: label, From: isoDate(start), To: isoDate(endOfMonth(start))}
|
||||
if label != start.Format("2006-01") {
|
||||
p.Span = start.Format("2006-01")
|
||||
}
|
||||
return p
|
||||
}
|
||||
year := time.Date(today.Year(), time.January, 1, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
out := []Period{
|
||||
{Label: "all time"},
|
||||
{
|
||||
Label: "this year",
|
||||
Span: fmt.Sprintf("%d", today.Year()),
|
||||
From: isoDate(year),
|
||||
To: isoDate(year.AddDate(1, 0, -1)),
|
||||
},
|
||||
rolling(11, "last 12 months"),
|
||||
rolling(2, "last 3 months"),
|
||||
month(thisMonth, "this month"),
|
||||
month(lastMonth, LastMonth),
|
||||
}
|
||||
|
||||
cutoff := lastMonth.Format("2006-01")
|
||||
for _, ym := range months {
|
||||
// Months at or after last month are already on the axis above, and one
|
||||
// dated in the future is a parsing bug rather than a window to offer.
|
||||
if ym >= cutoff {
|
||||
continue
|
||||
}
|
||||
start, err := time.Parse("2006-01", ym)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
out = append(out, month(start, ym))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// DefaultIndex is where on the axis a report opens.
|
||||
func DefaultIndex(ps []Period) int {
|
||||
for i, p := range ps {
|
||||
if p.Label == LastMonth {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func isoDate(t time.Time) string { return t.Format("2006-01-02") }
|
||||
|
||||
// endOfMonth takes the first of a month to its last day, whatever its length.
|
||||
func endOfMonth(first time.Time) time.Time { return first.AddDate(0, 1, -1) }
|
||||
@@ -0,0 +1,102 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func on(s string) time.Time {
|
||||
t, err := time.Parse("2006-01-02", s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return t
|
||||
}
|
||||
|
||||
// The axis opens on the month that has just ended, whatever the index holds.
|
||||
func TestPeriodsDefaultsToLastMonth(t *testing.T) {
|
||||
ps := Periods(on("2026-08-23"), []string{"2026-08", "2026-07", "2026-06"})
|
||||
p := ps[DefaultIndex(ps)]
|
||||
if p.Label != LastMonth {
|
||||
t.Fatalf("default = %q, want %q", p.Label, LastMonth)
|
||||
}
|
||||
if p.From != "2026-07-01" || p.To != "2026-07-31" {
|
||||
t.Errorf("last month = %s…%s, want the whole of July", p.From, p.To)
|
||||
}
|
||||
if p.Span != "2026-07" {
|
||||
t.Errorf("span = %q, want the month it stands for", p.Span)
|
||||
}
|
||||
}
|
||||
|
||||
// An index that stops short of last month still opens there: an empty report
|
||||
// for the window asked for beats a full one for a window nobody chose.
|
||||
func TestPeriodsDefaultsToLastMonthWithNothingInIt(t *testing.T) {
|
||||
ps := Periods(on("2026-08-23"), []string{"2026-03", "2026-02"})
|
||||
if p := ps[DefaultIndex(ps)]; p.Label != LastMonth {
|
||||
t.Fatalf("default = %q, want %q", p.Label, LastMonth)
|
||||
}
|
||||
}
|
||||
|
||||
// The named windows come first, widest to narrowest, and the months follow in
|
||||
// reverse order, skipping the ones the named windows already name.
|
||||
func TestPeriodsOrder(t *testing.T) {
|
||||
ps := Periods(on("2026-08-23"), []string{"2026-09", "2026-08", "2026-07", "2026-06", "2026-01"})
|
||||
var got []string
|
||||
for _, p := range ps {
|
||||
got = append(got, p.Label)
|
||||
}
|
||||
want := []string{
|
||||
"all time", "this year", "last 12 months", "last 3 months",
|
||||
"this month", LastMonth, "2026-06", "2026-01",
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("periods = %v, want %v", got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("periods = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A month is bounded by its own length, not by a fixed 30 or 31 days.
|
||||
func TestPeriodsMonthEnds(t *testing.T) {
|
||||
ps := Periods(on("2026-03-15"), []string{"2026-02"})
|
||||
p := ps[DefaultIndex(ps)]
|
||||
if p.From != "2026-02-01" || p.To != "2026-02-28" {
|
||||
t.Errorf("February 2026 = %s…%s, want the 1st to the 28th", p.From, p.To)
|
||||
}
|
||||
}
|
||||
|
||||
// A rolling window runs to the end of this month rather than to the last
|
||||
// complete one: it is asked in order to see what is happening now.
|
||||
func TestPeriodsRollingWindows(t *testing.T) {
|
||||
ps := Periods(on("2026-08-23"), nil)
|
||||
for _, tc := range []struct{ label, from, to string }{
|
||||
{"last 3 months", "2026-06-01", "2026-08-31"},
|
||||
{"last 12 months", "2025-09-01", "2026-08-31"},
|
||||
{"this year", "2026-01-01", "2026-12-31"},
|
||||
} {
|
||||
var found bool
|
||||
for _, p := range ps {
|
||||
if p.Label != tc.label {
|
||||
continue
|
||||
}
|
||||
found = true
|
||||
if p.From != tc.from || p.To != tc.to {
|
||||
t.Errorf("%s = %s…%s, want %s…%s", tc.label, p.From, p.To, tc.from, tc.to)
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("no %q window", tc.label)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// All time is unbounded on both sides, so nothing has to special-case it.
|
||||
func TestPeriodsAllTimeIsUnbounded(t *testing.T) {
|
||||
ps := Periods(on("2026-08-23"), []string{"2026-01"})
|
||||
if ps[0].Label != "all time" || ps[0].From != "" || ps[0].To != "" {
|
||||
t.Errorf("first window = %+v, want an unbounded all time", ps[0])
|
||||
}
|
||||
}
|
||||
+127
-23
@@ -2,7 +2,9 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"git.petrovv.com/nikola/money/internal/model"
|
||||
)
|
||||
@@ -24,7 +26,105 @@ func (t TagTotal) Net() int64 { return t.In - t.Out }
|
||||
// collected under "(untagged)" so they stay visible instead of vanishing.
|
||||
const Untagged = "(untagged)"
|
||||
|
||||
// ByTag returns totals sorted by currency, then by largest outflow first.
|
||||
// Order is how the rows of a breakdown are arranged within a currency.
|
||||
//
|
||||
// Currency is always the outer key and no order can change that: the tool holds
|
||||
// no exchange rates, so two currencies interleaved by amount would invite a
|
||||
// comparison between numbers that cannot be compared.
|
||||
type Order int
|
||||
|
||||
const (
|
||||
// OrderOut is the default, and the question a spending report is usually
|
||||
// opened to answer: where did the money go.
|
||||
OrderOut Order = iota // largest outflow first
|
||||
OrderIn // largest inflow first
|
||||
OrderNet // lowest net first, so the biggest losses lead
|
||||
OrderCount // most transactions first
|
||||
OrderTag // tag name, A→Z
|
||||
|
||||
numOrders // how many there are, for cycling; not an order itself
|
||||
)
|
||||
|
||||
// Next is the order after o, wrapping round. There are too many for a toggle,
|
||||
// so a view offering the choice cycles through them.
|
||||
func (o Order) Next() Order { return (o + 1) % numOrders }
|
||||
|
||||
// String is the short name the `--sort` flag takes, and ParseOrder accepts.
|
||||
func (o Order) String() string {
|
||||
switch o {
|
||||
case OrderIn:
|
||||
return "in"
|
||||
case OrderNet:
|
||||
return "net"
|
||||
case OrderCount:
|
||||
return "count"
|
||||
case OrderTag:
|
||||
return "tag"
|
||||
}
|
||||
return "out"
|
||||
}
|
||||
|
||||
// Label says what the order actually does, for a view with room to say it.
|
||||
func (o Order) Label() string {
|
||||
switch o {
|
||||
case OrderIn:
|
||||
return "largest in first"
|
||||
case OrderNet:
|
||||
return "lowest net first"
|
||||
case OrderCount:
|
||||
return "most transactions first"
|
||||
case OrderTag:
|
||||
return "tag A→Z"
|
||||
}
|
||||
return "largest out first"
|
||||
}
|
||||
|
||||
// Column is the heading the order sorts on, so a view can mark the column the
|
||||
// rows are arranged by without keeping its own copy of this mapping.
|
||||
func (o Order) Column() string {
|
||||
switch o {
|
||||
case OrderIn:
|
||||
return "In"
|
||||
case OrderNet:
|
||||
return "Net"
|
||||
case OrderCount:
|
||||
return "N"
|
||||
case OrderTag:
|
||||
return "Tag"
|
||||
}
|
||||
return "Out"
|
||||
}
|
||||
|
||||
// Ascending reports which way the order runs. Only the tag reads naturally
|
||||
// upwards; every amount and count is asked for biggest-first.
|
||||
func (o Order) Ascending() bool { return o == OrderTag }
|
||||
|
||||
// Orders is every order, in the cycle Next follows.
|
||||
func Orders() []Order {
|
||||
out := make([]Order, 0, numOrders)
|
||||
for o := Order(0); o < numOrders; o++ {
|
||||
out = append(out, o)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ParseOrder turns a short name back into an order, naming the alternatives
|
||||
// when it cannot, since a misspelt sort would otherwise silently report in an
|
||||
// order the caller did not ask for.
|
||||
func ParseOrder(s string) (Order, error) {
|
||||
for _, o := range Orders() {
|
||||
if o.String() == s {
|
||||
return o, nil
|
||||
}
|
||||
}
|
||||
var names []string
|
||||
for _, o := range Orders() {
|
||||
names = append(names, o.String())
|
||||
}
|
||||
return OrderOut, fmt.Errorf("unknown sort %q: want one of %s", s, strings.Join(names, ", "))
|
||||
}
|
||||
|
||||
// ByTag returns totals grouped by currency and arranged within each by order.
|
||||
//
|
||||
// Matched transfer legs are left out entirely. Both legs of a transfer are
|
||||
// dropped together, so a report over everything is unchanged in total by money
|
||||
@@ -36,7 +136,7 @@ const Untagged = "(untagged)"
|
||||
// all — so with such a definition in play the report *is* short by the fee, and
|
||||
// callers are expected to show Excluded alongside it rather than leave that
|
||||
// difference unexplained.
|
||||
func ByTag(txns []model.Transaction) []TagTotal {
|
||||
func ByTag(txns []model.Transaction, order Order) []TagTotal {
|
||||
type key struct{ currency, tag string }
|
||||
acc := map[key]*TagTotal{}
|
||||
|
||||
@@ -67,13 +167,33 @@ func ByTag(txns []model.Transaction) []TagTotal {
|
||||
out = append(out, *row)
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool {
|
||||
if out[i].Currency != out[j].Currency {
|
||||
return out[i].Currency < out[j].Currency
|
||||
a, b := out[i], out[j]
|
||||
if a.Currency != b.Currency {
|
||||
return a.Currency < b.Currency
|
||||
}
|
||||
if out[i].Out != out[j].Out {
|
||||
return out[i].Out > out[j].Out
|
||||
switch order {
|
||||
case OrderIn:
|
||||
if a.In != b.In {
|
||||
return a.In > b.In
|
||||
}
|
||||
case OrderNet:
|
||||
if a.Net() != b.Net() {
|
||||
return a.Net() < b.Net()
|
||||
}
|
||||
case OrderCount:
|
||||
if a.Count != b.Count {
|
||||
return a.Count > b.Count
|
||||
}
|
||||
case OrderTag:
|
||||
// The tag is the whole order here, and also the tie-break below.
|
||||
default:
|
||||
if a.Out != b.Out {
|
||||
return a.Out > b.Out
|
||||
}
|
||||
}
|
||||
return out[i].Tag < out[j].Tag
|
||||
// Every order falls back to the tag, so rows that tie on the chosen
|
||||
// column keep a fixed position instead of shuffling between reloads.
|
||||
return a.Tag < b.Tag
|
||||
})
|
||||
return out
|
||||
}
|
||||
@@ -190,19 +310,3 @@ func Excluded(txns []model.Transaction) []TransferTotal {
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].Currency < out[j].Currency })
|
||||
return out
|
||||
}
|
||||
|
||||
// Months lists the distinct YYYY-MM present in txns, most recent first.
|
||||
func Months(txns []model.Transaction) []string {
|
||||
seen := map[string]bool{}
|
||||
for _, t := range txns {
|
||||
if len(t.Date) >= 7 {
|
||||
seen[t.Date[:7]] = true
|
||||
}
|
||||
}
|
||||
out := make([]string, 0, len(seen))
|
||||
for m := range seen {
|
||||
out = append(out, m)
|
||||
}
|
||||
sort.Sort(sort.Reverse(sort.StringSlice(out)))
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.petrovv.com/nikola/money/internal/model"
|
||||
@@ -27,7 +28,7 @@ func TestByTagLeavesTransferLegsOut(t *testing.T) {
|
||||
leg(1, -50000, &id),
|
||||
leg(2, 50000, &id),
|
||||
{Currency: "EUR", MinorDigits: 2, AmountMinor: -2000, RuleTag: "groceries"},
|
||||
})
|
||||
}, OrderOut)
|
||||
|
||||
if len(rows) != 1 || rows[0].Tag != "groceries" {
|
||||
t.Fatalf("rows = %+v, want only the groceries row", rows)
|
||||
@@ -41,7 +42,7 @@ func TestByTagLeavesTransferLegsOut(t *testing.T) {
|
||||
// account and cannot be shown to have arrived must not quietly vanish from the
|
||||
// report.
|
||||
func TestByTagKeepsUnpairedLegs(t *testing.T) {
|
||||
rows := ByTag([]model.Transaction{leg(1, -50000, nil)})
|
||||
rows := ByTag([]model.Transaction{leg(1, -50000, nil)}, OrderOut)
|
||||
if len(rows) != 1 || rows[0].Out != 50000 {
|
||||
t.Fatalf("rows = %+v, want the unpaired leg counted", rows)
|
||||
}
|
||||
@@ -133,3 +134,133 @@ func TestExcludedClaimsNoFeeAcrossCurrencies(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// row is a transaction under one tag, for the ordering tests.
|
||||
func row(tag string, amount int64) model.Transaction {
|
||||
return model.Transaction{Currency: "EUR", MinorDigits: 2, AmountMinor: amount, RuleTag: tag}
|
||||
}
|
||||
|
||||
// tags is the order the breakdown came out in.
|
||||
func tags(rows []TagTotal) []string {
|
||||
var out []string
|
||||
for _, r := range rows {
|
||||
out = append(out, r.Tag)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestByTagOrders(t *testing.T) {
|
||||
txns := []model.Transaction{
|
||||
row("rent", -100000),
|
||||
row("groceries", -2000),
|
||||
row("groceries", -3000),
|
||||
row("groceries", -1000),
|
||||
row("salary", 250000),
|
||||
row("refund", 500),
|
||||
row("refund", -400),
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
order Order
|
||||
want []string
|
||||
}{
|
||||
{OrderOut, []string{"rent", "groceries", "refund", "salary"}},
|
||||
{OrderIn, []string{"salary", "refund", "groceries", "rent"}},
|
||||
{OrderNet, []string{"rent", "groceries", "refund", "salary"}},
|
||||
{OrderCount, []string{"groceries", "refund", "rent", "salary"}},
|
||||
{OrderTag, []string{"groceries", "refund", "rent", "salary"}},
|
||||
} {
|
||||
got := tags(ByTag(txns, tc.order))
|
||||
if len(got) != len(tc.want) {
|
||||
t.Errorf("%s: rows = %v, want %v", tc.order, got, tc.want)
|
||||
continue
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != tc.want[i] {
|
||||
t.Errorf("%s: rows = %v, want %v", tc.order, got, tc.want)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Every order falls back to the tag, so rows that tie do not shuffle about
|
||||
// between one reload and the next.
|
||||
func TestByTagTiesBreakOnTheTag(t *testing.T) {
|
||||
txns := []model.Transaction{row("zebra", -1000), row("apple", -1000)}
|
||||
for _, o := range Orders() {
|
||||
if got := tags(ByTag(txns, o)); got[0] != "apple" {
|
||||
t.Errorf("%s: rows = %v, want the tie broken on the tag", o, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No order may interleave currencies: with no exchange rates here, two amounts
|
||||
// in different columns are not comparable and must not be sorted against each
|
||||
// other.
|
||||
func TestByTagNeverInterleavesCurrencies(t *testing.T) {
|
||||
txns := []model.Transaction{
|
||||
{Currency: "EUR", MinorDigits: 2, AmountMinor: -100, RuleTag: "small"},
|
||||
{Currency: "BGN", MinorDigits: 2, AmountMinor: -900000, RuleTag: "huge"},
|
||||
{Currency: "EUR", MinorDigits: 2, AmountMinor: -200, RuleTag: "medium"},
|
||||
}
|
||||
for _, o := range Orders() {
|
||||
rows := ByTag(txns, o)
|
||||
if rows[0].Currency != "BGN" || rows[1].Currency != "EUR" || rows[2].Currency != "EUR" {
|
||||
t.Errorf("%s: currencies = %+v, want them grouped", o, rows)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The cycle visits every order and comes back to where it started.
|
||||
func TestOrderNextCycles(t *testing.T) {
|
||||
seen := map[Order]bool{}
|
||||
o := OrderOut
|
||||
for range len(Orders()) {
|
||||
if seen[o] {
|
||||
t.Fatalf("order %s came round twice in one cycle", o)
|
||||
}
|
||||
seen[o] = true
|
||||
o = o.Next()
|
||||
}
|
||||
if o != OrderOut {
|
||||
t.Errorf("the cycle ended on %s, want it back at %s", o, OrderOut)
|
||||
}
|
||||
}
|
||||
|
||||
// A misspelt --sort is refused rather than silently reporting in the default
|
||||
// order, and the error names what it would have accepted.
|
||||
func TestParseOrder(t *testing.T) {
|
||||
for _, o := range Orders() {
|
||||
got, err := ParseOrder(o.String())
|
||||
if err != nil || got != o {
|
||||
t.Errorf("ParseOrder(%q) = %s, %v, want %s", o.String(), got, err, o)
|
||||
}
|
||||
}
|
||||
_, err := ParseOrder("descending")
|
||||
if err == nil {
|
||||
t.Fatal("ParseOrder accepted a name that is not an order")
|
||||
}
|
||||
for _, want := range []string{"descending", "out", "tag"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("error %q does not mention %q", err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Each order marks a different column, and only headings the table actually
|
||||
// has, or the marker would land nowhere.
|
||||
func TestOrderColumnsAreDistinct(t *testing.T) {
|
||||
headings := map[string]bool{"Tag": true, "Cur": true, "Out": true, "In": true, "Net": true, "N": true}
|
||||
seen := map[string]bool{}
|
||||
for _, o := range Orders() {
|
||||
c := o.Column()
|
||||
if !headings[c] {
|
||||
t.Errorf("%s sorts on %q, which is not a report column", o, c)
|
||||
}
|
||||
if seen[c] {
|
||||
t.Errorf("%s marks %q, which another order already marks", o, c)
|
||||
}
|
||||
seen[c] = true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user