From 3a7f8ccf246a5929458a7f818c1d4c125ac72892 Mon Sep 17 00:00:00 2001 From: Rob Findley Date: Wed, 1 Jul 2020 18:10:02 -0400 Subject: [PATCH] go/types: add tests for AssignableTo and ConvertibleTo These exported functions are mostly trivial wrappers, but do make certain assumptions about how the underlying Checker APIs can be called. Add some simple tests. Change-Id: I68e9ae875353c12d118ec961a6f3834385fbbb97 Reviewed-on: https://go-review.googlesource.com/c/go/+/241262 Run-TryBot: Robert Findley TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- src/go/types/api_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/go/types/api_test.go b/src/go/types/api_test.go index fe3950a52d..798c09bbff 100644 --- a/src/go/types/api_test.go +++ b/src/go/types/api_test.go @@ -1232,6 +1232,41 @@ func F(){ } } +func TestConvertibleTo(t *testing.T) { + for _, test := range []struct { + v, t Type + want bool + }{ + {Typ[Int], Typ[Int], true}, + {Typ[Int], Typ[Float32], true}, + {newDefined(Typ[Int]), Typ[Int], true}, + {newDefined(new(Struct)), new(Struct), true}, + {newDefined(Typ[Int]), new(Struct), false}, + {Typ[UntypedInt], Typ[Int], true}, + } { + if got := ConvertibleTo(test.v, test.t); got != test.want { + t.Errorf("ConvertibleTo(%v, %v) = %t, want %t", test.v, test.t, got, test.want) + } + } +} + +func TestAssignableTo(t *testing.T) { + for _, test := range []struct { + v, t Type + want bool + }{ + {Typ[Int], Typ[Int], true}, + {Typ[Int], Typ[Float32], false}, + {newDefined(Typ[Int]), Typ[Int], false}, + {newDefined(new(Struct)), new(Struct), true}, + {Typ[UntypedBool], Typ[Bool], true}, + } { + if got := AssignableTo(test.v, test.t); got != test.want { + t.Errorf("AssignableTo(%v, %v) = %t, want %t", test.v, test.t, got, test.want) + } + } +} + func TestIdentical_issue15173(t *testing.T) { // Identical should allow nil arguments and be symmetric. for _, test := range []struct { -- 2.48.1