| 1 | package activitypub |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | |
| 7 | "golang.org/x/text/language" |
| 8 | ) |
| 9 | |
| 10 | func TestLangRef_GobEncode(t *testing.T) { |
| 11 | tests := []struct { |
| 12 | name string |
| 13 | l LangRef |
| 14 | want []byte |
| 15 | wantErr bool |
| 16 | }{ |
| 17 | { |
| 18 | name: "Nil lang ref", |
| 19 | l: NilLangRef, |
| 20 | want: gobValue([]byte(language.Und.String())), |
| 21 | wantErr: false, |
| 22 | }, |
| 23 | { |
| 24 | name: "invalid text", |
| 25 | l: MakeRef([]byte("ana are")), |
| 26 | want: gobValue([]byte(language.Und.String())), |
| 27 | wantErr: false, |
| 28 | }, |
| 29 | { |
| 30 | name: "valid English", |
| 31 | l: MakeRef([]byte("en")), |
| 32 | want: gobValue([]byte(language.English.String())), |
| 33 | wantErr: false, |
| 34 | }, |
| 35 | { |
| 36 | name: "valid French Canadian", |
| 37 | l: MakeRef([]byte("fr_ca")), |
| 38 | want: gobValue([]byte(language.CanadianFrench.String())), |
| 39 | wantErr: false, |
| 40 | }, |
| 41 | } |
| 42 | for _, tt := range tests { |
| 43 | t.Run(tt.name, func(t *testing.T) { |
| 44 | got, err := tt.l.GobEncode() |
| 45 | if (err != nil) != tt.wantErr { |
| 46 | t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr) |
| 47 | return |
| 48 | } |
| 49 | if !reflect.DeepEqual(got, tt.want) { |
| 50 | t.Errorf("GobEncode() got = %#v, want %#v", got, tt.want) |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestLangRefValue_GobEncode(t *testing.T) { |
| 57 | type fields struct { |
| 58 | Ref LangRef |
| 59 | Value Content |
| 60 | } |
| 61 | tests := []struct { |
| 62 | name string |
| 63 | fields fields |
| 64 | want []byte |
| 65 | wantErr bool |
| 66 | }{ |
| 67 | { |
| 68 | name: "empty", |
| 69 | fields: fields{}, |
| 70 | want: []byte{}, |
| 71 | wantErr: false, |
| 72 | }, |
| 73 | { |
| 74 | name: "some values", |
| 75 | fields: fields{ |
| 76 | Ref: MakeRef([]byte("ana")), |
| 77 | Value: Content("are mere"), |
| 78 | }, |
| 79 | want: gobValue(kv{K: []byte("ana"), V: []byte("are mere")}), |
| 80 | wantErr: false, |
| 81 | }, |
| 82 | } |
| 83 | for _, tt := range tests { |
| 84 | t.Run(tt.name, func(t *testing.T) { |
| 85 | l := LangRefValue{ |
| 86 | Ref: tt.fields.Ref, |
| 87 | Value: tt.fields.Value, |
| 88 | } |
| 89 | got, err := l.GobEncode() |
| 90 | if (err != nil) != tt.wantErr { |
| 91 | t.Errorf("GobEncode() error = %v, wantErr %v", err, tt.wantErr) |
| 92 | return |
| 93 | } |
| 94 | if !reflect.DeepEqual(got, tt.want) { |
| 95 | t.Errorf("GobEncode() got = %#v, want %#v", got, tt.want) |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestLangRef_UnmarshalJSON(t *testing.T) { |
| 102 | lang := "en-US" |
| 103 | rawJson := `"` + lang + `"` |
| 104 | |
| 105 | var a LangRef |
| 106 | _ = a.UnmarshalJSON([]byte(rawJson)) |
| 107 | |
| 108 | if a.String() != lang { |
| 109 | t.Errorf("Invalid json unmarshal for %T. Expected %q, found %q", lang, lang, a) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestLangRef_Equal(t *testing.T) { |
| 114 | type args struct { |
| 115 | } |
| 116 | tests := []struct { |
| 117 | name string |
| 118 | l LangRef |
| 119 | other LangRef |
| 120 | want bool |
| 121 | }{ |
| 122 | { |
| 123 | name: "empty", |
| 124 | l: LangRef{}, |
| 125 | other: LangRef{}, |
| 126 | want: true, |
| 127 | }, |
| 128 | { |
| 129 | name: "und is zero", |
| 130 | l: und, |
| 131 | other: LangRef{}, |
| 132 | want: true, |
| 133 | }, |
| 134 | { |
| 135 | name: "und", |
| 136 | l: und, |
| 137 | other: und, |
| 138 | want: true, |
| 139 | }, |
| 140 | { |
| 141 | name: "und vs en", |
| 142 | l: und, |
| 143 | other: English, |
| 144 | want: false, |
| 145 | }, |
| 146 | { |
| 147 | name: "en", |
| 148 | l: English, |
| 149 | other: English, |
| 150 | want: true, |
| 151 | }, |
| 152 | } |
| 153 | for _, tt := range tests { |
| 154 | t.Run(tt.name, func(t *testing.T) { |
| 155 | if got := tt.l.Equal(tt.other); got != tt.want { |
| 156 | t.Errorf("Equal() = %v, want %v", got, tt.want) |
| 157 | } |
| 158 | }) |
| 159 | } |
| 160 | } |