This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interest_test.go
88 lines (71 loc) · 2.49 KB
/
interest_test.go
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
// interest_test.go
//
// Author:: Chirantan Mitra
// Copyright:: Copyright (c) 2015-2017. All rights reserved
// License:: MIT
package main
import (
"reflect"
"strings"
"testing"
)
func TestInterestUnmarshalJSONWithDisplayNameAlias(t *testing.T) {
input := []byte(`"Foo:>Foo1"`)
interest := &Interest{}
err := interest.UnmarshalJSON(input)
if err != nil {
t.Fatalf("Expected no error creating interests from valid JSON: %s", err)
}
expectedInterest := &Interest{Name: "Foo", DisplayName: "Foo1"}
if !reflect.DeepEqual(interest, expectedInterest) {
t.Errorf("Expected interest name to equal the given display name %+v, but was %+v", expectedInterest, interest)
}
}
func TestInterestUnmarshalJSONWithoutDisplayNameAlias(t *testing.T) {
input := []byte(`"Foo"`)
interest := &Interest{}
err := interest.UnmarshalJSON(input)
if err != nil {
t.Fatalf("Expected no error creating interests from valid JSON: %s", err)
}
expectedInterest := &Interest{Name: "Foo", DisplayName: "Foo"}
if !reflect.DeepEqual(interest, expectedInterest) {
t.Errorf("Expected interest name to equal name %+v, but was %+v", expectedInterest, interest)
}
}
func TestInterestUnmarshalJSONWithMultipleAliases(t *testing.T) {
input := []byte(`"Foo:>Foo1:>Foo2"`)
interest := &Interest{}
err := interest.UnmarshalJSON(input)
if err != nil {
t.Fatalf("Expected no error creating interests from valid JSON: %s", err)
}
expectedInterest := &Interest{Name: "Foo", DisplayName: "Foo1"}
if !reflect.DeepEqual(interest, expectedInterest) {
t.Errorf("Expected interest name to use the first display name %+v, but was %+v", expectedInterest, interest)
}
}
func TestInterestUnmarshalJSONWithEmptyAlias(t *testing.T) {
input := []byte(`"Foo:>"`)
interest := &Interest{}
err := interest.UnmarshalJSON(input)
if err != nil {
t.Fatalf("Expected no error creating interests from valid JSON: %s", err)
}
expectedInterest := &Interest{Name: "Foo", DisplayName: "Foo"}
if !reflect.DeepEqual(interest, expectedInterest) {
t.Errorf("Expected interest name to equal use the name %+v, but was %+v", expectedInterest, interest)
}
}
func TestInterestUnmarshalInvalidJSON(t *testing.T) {
input := []byte{}
interest := &Interest{}
err := interest.UnmarshalJSON(input)
if err == nil {
t.Fatal("Expected error creating interests from empty JSON")
}
expectedErrMsgPart := ""
if !strings.Contains(err.Error(), expectedErrMsgPart) {
t.Errorf("Expected error %q to contain %q, but didn't", err.Error(), expectedErrMsgPart)
}
}