File: reflect_go117_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 (47 lines) | stat: -rw-r--r-- 1,219 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
// Copyright (c) 2021, 2022, 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.

//go:build go1.17 && !go1.20
// +build go1.17,!go1.20

package types_test

import (
	"reflect"
	"testing"

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

// go1.17 allows to convert []T to *[n]T.
func TestIsTypeOrConvertible_go117(t *testing.T) {
	type ArrP *[5]int

	ok, convertible := types.IsTypeOrConvertible(
		reflect.ValueOf([]int{1, 2, 3, 4, 5}),
		reflect.TypeOf((ArrP)(nil)))
	test.IsTrue(t, ok)
	test.IsTrue(t, convertible)

	ok, convertible = types.IsTypeOrConvertible(
		reflect.ValueOf([]int{1, 2, 3, 4}), // not enough items
		reflect.TypeOf((ArrP)(nil)))
	test.IsFalse(t, ok)
	test.IsFalse(t, convertible)

	ok, convertible = types.IsTypeOrConvertible(
		reflect.ValueOf([]int{1, 2, 3, 4, 5}),
		reflect.TypeOf(&struct{}{}))
	test.IsFalse(t, ok)
	test.IsFalse(t, convertible)

	ok, convertible = types.IsTypeOrConvertible(
		reflect.ValueOf([]int{1, 2, 3, 4, 5}),
		reflect.TypeOf([5]int{}))
	test.IsFalse(t, ok)
	test.IsFalse(t, convertible)
}