aboutsummaryrefslogtreecommitdiffstats
path: root/skate/verify_test.go
blob: 2a88f7783797c98b941be801b22f72f589b982a3 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package skate

import (
	"fmt"
	"io/ioutil"
	"strings"
	"testing"

	json "github.com/segmentio/encoding/json"
)

func TestSlugifyString(t *testing.T) {
	var cases = []struct {
		s      string
		result string
	}{
		{"", ""},
		{" ", ""},
		{" Optimize everything", "optimize everything"},
		{"ABCü~", "abc"},
	}
	for _, c := range cases {
		got := slugifyString(c.s)
		if got != c.result {
			t.Errorf("slugifyString: '%v', want '%v', got '%v'", c.s, c.result, got)
		}
	}
}
func TestLooksLikeComponent(t *testing.T) {
	var cases = []struct {
		a, b   string
		result bool
	}{
		{"", "", false},
		{"100.1", "100", true},
		{"100ABC.123", "100ABC", true},
		{"100ABC", "100ABC.1", true},
	}
	for _, c := range cases {
		got := looksLikeComponent(c.a, c.b)
		if got != c.result {
			t.Errorf("looksLikeComponent: %v %v, want %v, got %v", c.a, c.b, c.result, got)
		}
	}
}

func TestVerify(t *testing.T) {
	data, err := ioutil.ReadFile("testdata/verify.csv")
	if err != nil {
		t.Errorf("could not load test data: %v", err)
	}
	cases := strings.Split(string(data), "\n")
	t.Logf("running %d test cases from https://git.io/JtjRL", len(cases))
	for _, line := range cases {
		line = strings.TrimSpace(line)
		fields := strings.Split(line, ",")
		if len(fields) < 4 {
			continue
		}
		a, b, status, reason := fields[0], fields[1], fields[2], fields[3]
		if status == "" {
			continue
		}
		ar, err := testdataRelease(a)
		if err != nil {
			t.Errorf("could not load release: %s", a)
		}
		br, err := testdataRelease(b)
		if err != nil {
			t.Errorf("could not load release: %s", a)
		}
		result := Verify(ar, br)
		if !statusEquals(result.Status.String(), status) {
			t.Errorf("%s %s: got %s (%s), want %s (%s) ", a, b, result.Status, result.Reason, status, reason)
		}
	}
}

// testdataRelease uses the testdata dir to load a release.
func testdataRelease(ident string) (*Release, error) {
	filename := fmt.Sprintf("testdata/release/%s", ident)
	data, err := ioutil.ReadFile(filename)
	if err != nil {
		return nil, err
	}
	var r Release
	if err := json.Unmarshal(data, &r); err != nil {
		return nil, err
	}
	return &r, nil
}

// statusEquals compares status variants (from fuzzycat, skate), e.g.
// StatusExact, Status.EXACT
func statusEquals(s, t string) bool {
	s = strings.Replace(strings.ToLower(s), ".", "", -1)
	t = strings.Replace(strings.ToLower(t), ".", "", -1)
	return s == t
}