File: slice_test.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (116 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download | duplicates (2)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2020, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package flat_test

import (
	"testing"

	"github.com/maxatome/go-testdeep/internal/flat"
	"github.com/maxatome/go-testdeep/internal/test"
)

func TestLen(t *testing.T) {
	num, flattened := flat.Len([]any{1, 2, 3, 4})
	test.EqualInt(t, num, 4)
	test.IsTrue(t, flattened)

	num, flattened = flat.Len([]any{
		1, 2,
		flat.Slice{Slice: []int{3, 4, 5, 6}},
		flat.Slice{Slice: map[int]int{-1: -2, -3: -4}},
		7,
		flat.Slice{
			Slice: []any{
				flat.Slice{Slice: []int{8, 9}},
				flat.Slice{Slice: []int{10, 11}},
				flat.Slice{Slice: map[int]any{
					-5: -6,
					-7: flat.Slice{Slice: []int{-8, -9, -10}},
				}},
			},
		},
		12,
		flat.Slice{Slice: map[any]any{
			-11: flat.Slice{Slice: []int{-12, -13}},
		}},
	})
	test.EqualInt(t, num, 12+13)
	test.IsFalse(t, flattened)
}

func TestValues(t *testing.T) {
	sv := flat.Values(nil)
	test.IsTrue(t, sv != nil)
	test.EqualInt(t, len(sv), 0)

	sv = flat.Values([]any{1, 2})
	if test.EqualInt(t, len(sv), 2) {
		test.EqualInt(t, int(sv[0].Int()), 1)
		test.EqualInt(t, int(sv[1].Int()), 2)
	}

	sv = flat.Values([]any{
		1, 2,
		flat.Slice{Slice: []int{3, 4, 5, 6}},
		7,
		flat.Slice{
			Slice: []any{
				flat.Slice{Slice: []int{8, 9}},
				flat.Slice{Slice: []any{10, 11}},
				12,
				13,
			},
		},
		14,
		flat.Slice{
			Slice: map[int]any{
				15: flat.Slice{Slice: map[int]int{16: 17}},
			},
		},
	})
	if test.EqualInt(t, len(sv), 17) {
		for i, v := range sv {
			test.EqualInt(t, int(v.Int()), i+1)
		}
	}
}

func TestInterfaces(t *testing.T) {
	si := flat.Interfaces()
	test.IsTrue(t, si == nil)

	si = flat.Interfaces(1, 2)
	if test.EqualInt(t, len(si), 2) {
		test.EqualInt(t, si[0].(int), 1)
		test.EqualInt(t, si[1].(int), 2)
	}

	si = flat.Interfaces(
		1, 2,
		flat.Slice{Slice: []int{3, 4, 5, 6}},
		7,
		flat.Slice{
			Slice: []any{
				flat.Slice{Slice: []int{8, 9}},
				flat.Slice{Slice: []any{10, 11}},
				12,
				13,
			},
		},
		14,
		flat.Slice{
			Slice: map[int]any{
				15: flat.Slice{Slice: map[int]int{16: 17}},
			},
		},
	)
	if test.EqualInt(t, len(si), 17) {
		for i, iface := range si {
			test.EqualInt(t, iface.(int), i+1)
		}
	}
}