File: json_test.go

package info (click to toggle)
golang-github-blang-semver 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, experimental, sid
  • size: 176 kB
  • sloc: makefile: 3
file content (49 lines) | stat: -rw-r--r-- 1,234 bytes parent folder | download | duplicates (3)
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
package semver

import (
	"encoding/json"
	"strconv"
	"testing"
)

func TestJSONMarshal(t *testing.T) {
	versionString := "3.1.4-alpha.1.5.9+build.2.6.5"
	v, err := Parse(versionString)
	if err != nil {
		t.Fatal(err)
	}

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

	quotedVersionString := strconv.Quote(versionString)

	if string(versionJSON) != quotedVersionString {
		t.Fatalf("JSON marshaled semantic version not equal: expected %q, got %q", quotedVersionString, string(versionJSON))
	}
}

func TestJSONUnmarshal(t *testing.T) {
	versionString := "3.1.4-alpha.1.5.9+build.2.6.5"
	quotedVersionString := strconv.Quote(versionString)

	var v Version
	if err := json.Unmarshal([]byte(quotedVersionString), &v); err != nil {
		t.Fatal(err)
	}

	if v.String() != versionString {
		t.Fatalf("JSON unmarshaled semantic version not equal: expected %q, got %q", versionString, v.String())
	}

	badVersionString := strconv.Quote("3.1.4.1.5.9.2.6.5-other-digits-of-pi")
	if err := json.Unmarshal([]byte(badVersionString), &v); err == nil {
		t.Fatal("expected JSON unmarshal error, got nil")
	}

	if err := json.Unmarshal([]byte("3.1"), &v); err == nil {
		t.Fatal("expected JSON unmarshal error, got nil")
	}
}