~kota/tide

37edc3b8df4b8a409f1f9587dc9e2a64e8aa817a — Dakota Walsh 1 year, 4 months ago 82f55f4
add display test
3 files changed, 58 insertions(+), 7 deletions(-)

M display.go
A display_test.go
M main.go
M display.go => display.go +7 -7
@@ 20,15 20,15 @@ func displayTerm(index int, tides *[]Tide, now time.Time) {
	nextDuration := fmtDuration(nextTide.Time.Sub(now))
	rising := getRising(prevTide, nextTide)
	height := getHeight(prevTide, nextTide, now)
	fmt.Printf("%.2fm", height)
	fmt.Fprintf(out, "%.2fm", height)
	if rising {
		fmt.Printf("⬆ - high tide (%.2fm) in %v\n",
		fmt.Fprintf(out, "⬆ - high tide (%.2fm) in %v\n",
			nextTide.Height, nextDuration)
	} else {
		fmt.Printf("⬇ - low tide (%.2fm) in %v\n",
		fmt.Fprintf(out, "⬇ - low tide (%.2fm) in %v\n",
			nextTide.Height, nextDuration)
	}
	fmt.Printf("%s", graph(prevTide, nextTide, now))
	fmt.Fprintf(out, "%s", graph(prevTide, nextTide, now))
}

func displaySimple(index int, tides *[]Tide, now time.Time) {


@@ 36,11 36,11 @@ func displaySimple(index int, tides *[]Tide, now time.Time) {
	nextTide := (*tides)[index]
	rising := getRising(prevTide, nextTide)
	height := getHeight(prevTide, nextTide, now)
	fmt.Printf("%.2fm", height)
	fmt.Fprintf(out, "%.2fm", height)
	if rising {
		fmt.Printf("⬆\n")
		fmt.Fprintf(out, "⬆\n")
	} else {
		fmt.Printf("⬇\n")
		fmt.Fprintf(out, "⬇\n")
	}
}


A display_test.go => display_test.go +48 -0
@@ 0,0 1,48 @@
// tides - Print tidal data information for Aotearoa
// Copyright (C) 2021 Dakota Walsh
// GPL3+ See LICENSE in this repo for details.
package main

import (
	"bytes"
	"testing"
	"time"
)

var (
	referenceTime = time.Date(2006, time.January, 02, 15, 04, 05, 0, time.UTC)
)

func TestDisplaySimple(t *testing.T) {
	tides := []Tide{
		{
			Height: 2.0,
			Time:   referenceTime,
		},
		{
			Height: 0.0,
			Time:   referenceTime.Add(time.Hour * 8),
		},
		{
			Height: 2.0,
			Time:   referenceTime.Add(time.Hour * 16),
		},
	}
	var tests = []struct {
		time time.Time
		want string
	}{
		{referenceTime, "2.00m⬇\n"},
		{referenceTime.Add(time.Hour * 2), "1.71m⬇\n"},
		{referenceTime.Add(time.Hour * 4), "1.00m⬇\n"},
		{referenceTime.Add(time.Hour * 8), "0.00m⬇\n"},
	}
	for _, test := range tests {
		out = new(bytes.Buffer) // captured output
		displaySimple(1, &tides, test.time)
		got := out.(*bytes.Buffer).String()
		if got != test.want {
			t.Errorf("got = %q\nwant = %q", got, test.want)
		}
	}
}

M main.go => main.go +3 -0
@@ 6,6 6,7 @@ package main
import (
	"encoding/csv"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"


@@ 15,6 16,8 @@ import (
	"github.com/mattn/go-isatty"
)

var out io.Writer = os.Stdout // modified during testing

// TZ is the timezone for the imported times.
const TZ = "NZ"