aboutsummaryrefslogtreecommitdiff
path: root/mountain_pass/extract_test.go
blob: 7eae33b2548e706afe1188ce72ec14474d2358ab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

package mountain_pass

import (
	"io"
	"strings"
	"testing"

	"github.com/paulmach/osm"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestIsMountainPass(t *testing.T) {
	cases := []struct {
		name string
		tags map[string]string
		want bool
	}{
		{"tagged", map[string]string{"mountain_pass": "yes", "name": "Col de la Gineste"}, true},
		{"legacy natural", map[string]string{"natural": "mountain_pass"}, true},
		{"not a pass", map[string]string{"natural": "saddle", "name": "Col de la Gineste"}, false},
		{"no tags", map[string]string{}, false},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			node := &osm.Node{Tags: osm.Tags{}}
			for key, value := range c.tags {
				node.Tags = append(node.Tags, osm.Tag{Key: key, Value: value})
			}
			assert.Equal(t, c.want, isMountainPass(node))
		})
	}
}

func TestParseElevation(t *testing.T) {
	cases := []struct {
		input string
		want  int
		ok    bool
	}{
		{"1320", 1320, true},
		{"1320 m", 1320, true},
		{" 447.5", 447, true},
		{"", 0, false},
		{"unknown", 0, false},
	}
	for _, c := range cases {
		value, ok := parseElevation(c.input)
		assert.Equal(t, c.want, value)
		assert.Equal(t, c.ok, ok)
	}
}

func TestCountingReaderTracksBytes(t *testing.T) {
	const content = "0123456789abcdef"
	reader := &countingReader{r: strings.NewReader(content)}

	buf := make([]byte, 4)
	var total int64
	for {
		n, err := reader.Read(buf)
		total += int64(n)
		require.Equal(t, total, reader.n)
		if err == io.EOF {
			break
		}
		require.NoError(t, err)
	}
	assert.Equal(t, int64(len(content)), reader.n)
}