Debian Codegraph @

internal/types/types_test.go

raw
1 // Copyright (c) 2021-2022, Maxime Soulé
2 // All rights reserved.
3 //
4 // This source code is licensed under the BSD-style license found in the
5 // LICENSE file in the root directory of this source tree.
6
7 package types_test
8
9 import (
10 "encoding/json"
11 "errors"
12 "testing"
13
14 "github.com/maxatome/go-testdeep/internal/types"
15 )
16
17 var _ error = types.OperatorNotJSONMarshallableError("")
18
19 func TestOperatorNotJSONMarshallableError(t *testing.T) {
20 e := types.OperatorNotJSONMarshallableError("Pipo")
21
22 if e.Error() != "Pipo TestDeep operator cannot be json.Marshal'led" {
23 t.Errorf("unexpected %q", e.Error())
24 }
25
26 if e.Operator() != "Pipo" {
27 t.Errorf("unexpected %q", e.Operator())
28 }
29
30 t.Run("AsOperatorNotJSONMarshallableError", func(t *testing.T) {
31 ne, ok := types.AsOperatorNotJSONMarshallableError(e)
32 if !ok {
33 t.Error("AsOperatorNotJSONMarshallableError() returned false")
34 return
35 }
36 if ne != e {
37 t.Errorf("AsOperatorNotJSONMarshallableError(): %q ≠ %q",
38 ne.Error(), e.Error())
39 }
40
41 other := errors.New("Other error")
42 _, ok = types.AsOperatorNotJSONMarshallableError(other)
43 if ok {
44 t.Error("AsOperatorNotJSONMarshallableError() returned true")
45 return
46 }
47
48 je := &json.MarshalerError{Err: e}
49 ne, ok = types.AsOperatorNotJSONMarshallableError(je)
50 if !ok {
51 t.Error("AsOperatorNotJSONMarshallableError() returned false")
52 return
53 }
54 if ne != e {
55 t.Errorf("AsOperatorNotJSONMarshallableError(): %q ≠ %q",
56 ne.Error(), e.Error())
57 }
58
59 je.Err = other
60 _, ok = types.AsOperatorNotJSONMarshallableError(je)
61 if ok {
62 t.Error("AsOperatorNotJSONMarshallableError() returned true")
63 return
64 }
65 })
66 }
67
68 func TestRawString(t *testing.T) {
69 s := types.RawString("foo")
70 if str := s.String(); str != "foo" {
71 t.Errorf("Very weird, got %s", str)
72 }
73 }
74
75 func TestRawInt(t *testing.T) {
76 i := types.RawInt(42)
77 if str := i.String(); str != "42" {
78 t.Errorf("Very weird, got %s", str)
79 }
80 }
81
82 func TestRecvKind(t *testing.T) {
83 s := types.RecvNothing.String()
84 if s != "nothing received on channel" {
85 t.Errorf(`got: %q / expected: "nothing received on channel"`, s)
86 }
87
88 s = types.RecvClosed.String()
89 if s != "channel is closed" {
90 t.Errorf(`got: %q / expected: "channel is closed"`, s)
91 }
92 }