diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-03-21 01:17:38 +0100 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-03-21 01:17:38 +0100 |
commit | 09a7e8c9d013f13a1aa1ef4e9b7f397647b79967 (patch) | |
tree | 122b474e27afbc66cba1182e983ef5c8555ed12f /skate/verify_test.go | |
parent | a7e0cf191ebf8fb499e0ab9a3b6cae45727f1286 (diff) | |
download | refcat-09a7e8c9d013f13a1aa1ef4e9b7f397647b79967.tar.gz refcat-09a7e8c9d013f13a1aa1ef4e9b7f397647b79967.zip |
initial import of skate
Diffstat (limited to 'skate/verify_test.go')
-rw-r--r-- | skate/verify_test.go | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/skate/verify_test.go b/skate/verify_test.go new file mode 100644 index 0000000..3b6963c --- /dev/null +++ b/skate/verify_test.go @@ -0,0 +1,97 @@ +package skate + +import ( + "fmt" + "io/ioutil" + "strings" + "testing" +) + +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 := parseRelease(a) + if err != nil { + t.Errorf("could not load release: %s", a) + } + br, err := parseRelease(b) + if err != nil { + t.Errorf("could not load release: %s", a) + } + result := Verify(ar, br, 5) + if !statusEquals(result.Status.String(), status) { + t.Errorf("%s %s: got %s (%s), want %s (%s) ", a, b, result.Status, result.Reason, status, reason) + } + } +} + +// parseRelease uses the testdata dir to load a release. +func parseRelease(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 +} |