File: version_test.go

package info (click to toggle)
golang-github-apparentlymart-go-versions 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 292 kB
  • sloc: makefile: 2
file content (48 lines) | stat: -rw-r--r-- 918 bytes parent folder | download
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
package versions

import (
	"encoding/json"
	"testing"

	"github.com/go-test/deep"
)

func TestVersionJSON(t *testing.T) {
	v := Version{
		Major:      1,
		Minor:      2,
		Patch:      3,
		Prerelease: "beta.1",
		Metadata:   "tci.12345",
	}

	j, err := json.Marshal(v)
	if err != nil {
		t.Fatal(err)
	}

	if got, want := string(j), `"1.2.3-beta.1+tci.12345"`; got != want {
		t.Errorf("wrong result\ngot:  %s\nwant: %s", got, want)
	}

	var v2 Version
	err = json.Unmarshal(j, &v2)
	if err != nil {
		t.Fatal(err)
	}

	for _, problem := range deep.Equal(v2, v) {
		t.Error(problem)
	}

	bad := []byte(`"garbage"`)
	err = json.Unmarshal(bad, &v2)
	var errText string
	if err != nil {
		errText = err.Error()
	}
	if got, want := errText, `invalid specification; required format is three positive integers separated by periods`; got != want {
		t.Errorf("wrong error for garbage\ngot:  %s\nwant: %s", got, want)
	}

}