aboutsummaryrefslogtreecommitdiffstats
path: root/skate/zipkey/zipkey_test.go
blob: 0a49a456d2301b0dd5399ee1ff9a5b0ca540f9d5 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
package zipkey

import (
	"bytes"
	"encoding/json"
	"strings"
	"testing"

	"git.archive.org/martin/cgraph/skate/must"
)

func TestZipRun(t *testing.T) {
	makeKeyFunc := func(index int) func(string) (string, error) {
		return func(s string) (string, error) {
			parts := strings.Fields(s)
			if index >= len(parts) {
				return "", nil
			}
			return parts[index], nil
		}
	}
	var cases = []struct {
		a  string
		b  string
		c  string
		kf func(string) (string, error)
	}{
		{
			"testdata/c0a",
			"testdata/c0b",
			"testdata/c0c",
			makeKeyFunc(0),
		},
		{
			"testdata/c1a",
			"testdata/c1b",
			"testdata/c1c",
			makeKeyFunc(0),
		},
		{
			"testdata/c2a",
			"testdata/c2b",
			"testdata/c2c",
			makeKeyFunc(0),
		},
		{
			"testdata/c3a",
			"testdata/c3b",
			"testdata/c3c",
			makeKeyFunc(0),
		},
		{
			"testdata/c4a",
			"testdata/c4b",
			"testdata/c4c",
			makeKeyFunc(1),
		},
		{
			"testdata/c5a",
			"testdata/c5b",
			"testdata/c5c",
			makeKeyFunc(0),
		},
	}
	for _, c := range cases {
		var (
			ar        = must.Open(c.a)
			br        = must.Open(c.b)
			cr        = strings.TrimSpace(string(must.ReadFile(c.c)))
			buf       bytes.Buffer
			groupFunc = func(g *Group) error {
				return json.NewEncoder(&buf).Encode(g)
			}
			cm = New(ar, br, c.kf, groupFunc)
		)
		if err := cm.Run(); err != nil {
			t.Errorf("[%s] failed: %v", c.a, err)
		}
		if got := strings.TrimSpace(buf.String()); cr != got {
			t.Errorf("[%s ...] got %v, want %v", c.a, got, cr)
		}
	}
}