From d5c3c5f0f05e2f88dae1992c3269c01a0b924b49 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 10 Aug 2024 13:29:53 +0200 Subject: Add tests to the mintage formatter --- cmd/mfmt/main_test.go | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/mfmt/util_test.go | 43 ++++++++++++++++++++ 2 files changed, 151 insertions(+) create mode 100644 cmd/mfmt/main_test.go create mode 100644 cmd/mfmt/util_test.go (limited to 'cmd') diff --git a/cmd/mfmt/main_test.go b/cmd/mfmt/main_test.go new file mode 100644 index 0000000..72e3665 --- /dev/null +++ b/cmd/mfmt/main_test.go @@ -0,0 +1,108 @@ +package main + +import ( + "bytes" + "testing" +) + +func runTest(t *testing.T, in, out string, nilErr bool) { + r := bytes.NewReader([]byte(in)) + w := new(bytes.Buffer) + + err := mfmt("-", r, w) + if nilErr && err != nil { + t.Fatalf(`Expected err=nil; got "%s"`, err) + } else if !nilErr && err == nil { + t.Fatalf(`Expected err=Error; got nil`) + } + + if nilErr && w.String() != out { + t.Fatalf(`Expected w.String()="%s"; got "%s"`, out, w.String()) + } +} + +func TestSpacing(t *testing.T) { + in := ` + BEGIN 2002 + BEGIN CIRC + + +378.383.000 326.439.000 217.031.000 441.621.000 203.388.000 169.088.000 223.544.000 196.438.000 + + + 10.817.950 118.526.000 ? 43.859.072 50.913.200 9.074.200 150.000 4.679.500 +` + out := `BEGIN 2002 + +BEGIN CIRC +378.383.000 326.439.000 217.031.000 441.621.000 203.388.000 169.088.000 223.544.000 196.438.000 + 10.817.950 118.526.000 ? 43.859.072 50.913.200 9.074.200 150.000 4.679.500 +` + runTest(t, in, out, true) +} + +func TestAddDots(t *testing.T) { + in := `BEGIN 2002 + +BEGIN CIRC +378383000 326439000 217031000 441621000 203388000 169088000 223544000 196438000 + 10817950 118526000 ? 43859072 50913200 9074200 150000 4679500 +` + out := `BEGIN 2002 + +BEGIN CIRC +378.383.000 326.439.000 217.031.000 441.621.000 203.388.000 169.088.000 223.544.000 196.438.000 + 10.817.950 118.526.000 ? 43.859.072 50.913.200 9.074.200 150.000 4.679.500 +` + runTest(t, in, out, true) +} + +func TestComplete(t *testing.T) { + in := ` + BEGIN 2002 + BEGIN CIRC + + +378383000 326439000 21703...1000 441621000 203388000 .1.6.9.0.8.8.0.0.0 223544000 196438000 + + + 10817950 118526000 ? 43859072 50913200 9074..200 150000 4679500 + + BEGIN PROOF + + +378383000 123456789 ..5 441621000 203388000 .1.6.9.0.8.8.0.0.0 223544000 196438000 + + + 10817950 118526000 ? 43859072 50913200 9074..200 150000 4679500 +` + out := `BEGIN 2002 + +BEGIN CIRC +378.383.000 326.439.000 217.031.000 441.621.000 203.388.000 169.088.000 223.544.000 196.438.000 + 10.817.950 118.526.000 ? 43.859.072 50.913.200 9.074.200 150.000 4.679.500 + +BEGIN PROOF +378.383.000 123.456.789 5 441.621.000 203.388.000 169.088.000 223.544.000 196.438.000 + 10.817.950 118.526.000 ? 43.859.072 50.913.200 9.074.200 150.000 4.679.500 +` + runTest(t, in, out, true) +} + +func TestLabelAlignment(t *testing.T) { + in := `BEGIN 2002 + +BEGIN CIRC +378383000 326439000 217031000 441621000 203388000 169088000 223544000 196438000 +KNM*: 10817950 118526000 ? 43859072 50913200 9074200 150000 4679500 +MdP: 115.000.000 156.400.000 89.300.000 5.200.000 54.800.000 3.100.000 2.600.000 2.500.000 +` + out := `BEGIN 2002 + +BEGIN CIRC + 378.383.000 326.439.000 217.031.000 441.621.000 203.388.000 169.088.000 223.544.000 196.438.000 +KNM*: 10.817.950 118.526.000 ? 43.859.072 50.913.200 9.074.200 150.000 4.679.500 +MdP: 115.000.000 156.400.000 89.300.000 5.200.000 54.800.000 3.100.000 2.600.000 2.500.000 +` + runTest(t, in, out, true) +} diff --git a/cmd/mfmt/util_test.go b/cmd/mfmt/util_test.go new file mode 100644 index 0000000..e008e0b --- /dev/null +++ b/cmd/mfmt/util_test.go @@ -0,0 +1,43 @@ +/* This file contains tests for utility functions used in the mfmt + binary. Tests of the actual application are in main_test.go. */ + +package main + +import ( + "testing" + + "git.thomasvoss.com/euro-cash.eu/mintages" +) + +func TestFormatInt(t *testing.T) { + for _, x := range [...]struct { + n int + s string + }{ + {0, "0"}, + {123, "123"}, + {81758, "81.758"}, + {752759237528, "752.759.237.528"}, + {mintages.Unknown, "?"}, + } { + s := formatInt(x.n) + if s != x.s { + t.Fatalf(`Expected s="%s"; got "%s"`, x.s, s) + } + } +} + +func TestIntLen(t *testing.T) { + for _, x := range [...]struct{ x, y int }{ + {0, len("0")}, + {123, len("123")}, + {81758, len("81758")}, + {752759237528, len("752759237528")}, + {mintages.Unknown, len("?")}, + } { + n := intlen(x.x) + if n != x.y { + t.Fatalf("Expected n=%d; got %d", x.y, n) + } + } +} -- cgit v1.2.3