| 1 |
// Copyright (c) 2018, 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 td |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"reflect" |
| 12 |
"sort" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/helpers/tdutil" |
| 15 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 16 |
"github.com/maxatome/go-testdeep/internal/types" |
| 17 |
"github.com/maxatome/go-testdeep/internal/util" |
| 18 |
) |
| 19 |
|
| 20 |
type tdSetResultKind uint8 |
| 21 |
|
| 22 |
const ( |
| 23 |
itemsSetResult tdSetResultKind = iota |
| 24 |
keysSetResult |
| 25 |
) |
| 26 |
|
| 27 |
// Implements fmt.Stringer. |
| 28 |
func (k tdSetResultKind) String() string { |
| 29 |
switch k { |
| 30 |
case itemsSetResult: |
| 31 |
return "item" |
| 32 |
case keysSetResult: |
| 33 |
return "key" |
| 34 |
default: |
| 35 |
return "?" |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
type tdSetResult struct { |
| 40 |
types.TestDeepStamp |
| 41 |
Missing []reflect.Value |
| 42 |
Extra []reflect.Value |
| 43 |
Kind tdSetResultKind |
| 44 |
Sort bool |
| 45 |
} |
| 46 |
|
| 47 |
func (r tdSetResult) IsEmpty() bool { |
| 48 |
return len(r.Missing) == 0 && len(r.Extra) == 0 |
| 49 |
} |
| 50 |
|
| 51 |
func (r tdSetResult) Summary() ctxerr.ErrorSummary { |
| 52 |
var summary ctxerr.ErrorSummaryItems |
| 53 |
|
| 54 |
if len(r.Missing) > 0 { |
| 55 |
var missing string |
| 56 |
|
| 57 |
if len(r.Missing) > 1 { |
| 58 |
if r.Sort { |
| 59 |
sort.Stable(tdutil.SortableValues(r.Missing)) |
| 60 |
} |
| 61 |
missing = fmt.Sprintf("Missing %d %ss", len(r.Missing), r.Kind) |
| 62 |
} else { |
| 63 |
missing = fmt.Sprintf("Missing %s", r.Kind) |
| 64 |
} |
| 65 |
|
| 66 |
summary = append(summary, ctxerr.ErrorSummaryItem{ |
| 67 |
Label: missing, |
| 68 |
Value: util.ToString(r.Missing), |
| 69 |
}) |
| 70 |
} |
| 71 |
|
| 72 |
if len(r.Extra) > 0 { |
| 73 |
var extra string |
| 74 |
|
| 75 |
if len(r.Extra) > 1 { |
| 76 |
if r.Sort { |
| 77 |
sort.Stable(tdutil.SortableValues(r.Extra)) |
| 78 |
} |
| 79 |
extra = fmt.Sprintf("Extra %d %ss", len(r.Extra), r.Kind) |
| 80 |
} else { |
| 81 |
extra = fmt.Sprintf("Extra %s", r.Kind) |
| 82 |
} |
| 83 |
|
| 84 |
summary = append(summary, ctxerr.ErrorSummaryItem{ |
| 85 |
Label: extra, |
| 86 |
Value: util.ToString(r.Extra), |
| 87 |
}) |
| 88 |
} |
| 89 |
|
| 90 |
return summary |
| 91 |
} |