summaryrefslogtreecommitdiffhomepage
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/mfmt/main_test.go108
-rw-r--r--cmd/mfmt/util_test.go43
2 files changed, 151 insertions, 0 deletions
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)
+ }
+ }
+}