github.com/pygolin/runtime@v0.0.0-20201208210830-a62e3cd39798/param_test.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package runtime
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestParamSpecValidate(t *testing.T) {
    22  	testFunc := newBuiltinFunction("TestParamSpecValidate", func(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
    23  		if len(args) < 1 {
    24  			return nil, f.RaiseType(TypeErrorType, "not enough args")
    25  		}
    26  		val, raised := ToNative(f, args[0])
    27  		if raised != nil {
    28  			return nil, raised
    29  		}
    30  		s, ok := val.Interface().(*ParamSpec)
    31  		if !ok {
    32  			return nil, f.RaiseType(TypeErrorType, "expected ParamSpec arg")
    33  		}
    34  		validated := make([]*Object, s.Count)
    35  		if raised := s.Validate(f, validated, args[1:], kwargs); raised != nil {
    36  			return nil, raised
    37  		}
    38  		return NewTuple(validated...).ToObject(), nil
    39  	})
    40  	cases := []invokeTestCase{
    41  		invokeTestCase{args: wrapArgs(NewParamSpec("f1", nil, false, false)), want: NewTuple().ToObject()},
    42  		invokeTestCase{args: wrapArgs(NewParamSpec("f2", []Param{{"a", nil}}, false, false), 123), want: newTestTuple(123).ToObject()},
    43  		invokeTestCase{args: wrapArgs(NewParamSpec("f2", []Param{{"a", nil}}, false, false)), kwargs: wrapKWArgs("a", "apple"), want: newTestTuple("apple").ToObject()},
    44  		invokeTestCase{args: wrapArgs(NewParamSpec("f2", []Param{{"a", nil}}, false, false)), kwargs: wrapKWArgs("b", "bear"), wantExc: mustCreateException(TypeErrorType, "f2() got an unexpected keyword argument 'b'")},
    45  		invokeTestCase{args: wrapArgs(NewParamSpec("f2", []Param{{"a", nil}}, false, false)), wantExc: mustCreateException(TypeErrorType, "f2() takes at least 1 arguments (0 given)")},
    46  		invokeTestCase{args: wrapArgs(NewParamSpec("f2", []Param{{"a", nil}}, false, false), 1, 2, 3), wantExc: mustCreateException(TypeErrorType, "f2() takes 1 arguments (3 given)")},
    47  		invokeTestCase{args: wrapArgs(NewParamSpec("f3", []Param{{"a", nil}, {"b", nil}}, false, false), 1, 2), want: newTestTuple(1, 2).ToObject()},
    48  		invokeTestCase{args: wrapArgs(NewParamSpec("f3", []Param{{"a", nil}, {"b", nil}}, false, false), 1), kwargs: wrapKWArgs("b", "bear"), want: newTestTuple(1, "bear").ToObject()},
    49  		invokeTestCase{args: wrapArgs(NewParamSpec("f3", []Param{{"a", nil}, {"b", nil}}, false, false)), kwargs: wrapKWArgs("b", "bear", "a", "apple"), want: newTestTuple("apple", "bear").ToObject()},
    50  		invokeTestCase{args: wrapArgs(NewParamSpec("f3", []Param{{"a", nil}, {"b", nil}}, false, false), 1), kwargs: wrapKWArgs("a", "alpha"), wantExc: mustCreateException(TypeErrorType, "f3() got multiple values for keyword argument 'a'")},
    51  		invokeTestCase{args: wrapArgs(NewParamSpec("f4", []Param{{"a", nil}, {"b", None}}, false, false), 123), want: newTestTuple(123, None).ToObject()},
    52  		invokeTestCase{args: wrapArgs(NewParamSpec("f4", []Param{{"a", nil}, {"b", None}}, false, false), 123, "bar"), want: newTestTuple(123, "bar").ToObject()},
    53  		invokeTestCase{args: wrapArgs(NewParamSpec("f4", []Param{{"a", nil}, {"b", None}}, false, false)), kwargs: wrapKWArgs("a", 123, "b", "bar"), want: newTestTuple(123, "bar").ToObject()},
    54  		invokeTestCase{args: wrapArgs(NewParamSpec("f5", []Param{{"a", nil}}, true, false), 1), want: newTestTuple(1, NewTuple()).ToObject()},
    55  		invokeTestCase{args: wrapArgs(NewParamSpec("f5", []Param{{"a", nil}}, true, false), 1, 2, 3), want: newTestTuple(1, newTestTuple(2, 3)).ToObject()},
    56  		invokeTestCase{args: wrapArgs(NewParamSpec("f6", []Param{{"a", nil}}, false, true), "bar"), want: newTestTuple("bar", NewDict()).ToObject()},
    57  		invokeTestCase{args: wrapArgs(NewParamSpec("f6", []Param{{"a", nil}}, false, true)), kwargs: wrapKWArgs("a", "apple", "b", "bear"), want: newTestTuple("apple", newTestDict("b", "bear")).ToObject()},
    58  		invokeTestCase{args: wrapArgs(NewParamSpec("f6", []Param{{"a", nil}}, false, true), "bar"), kwargs: wrapKWArgs("b", "baz", "c", "qux"), want: newTestTuple("bar", newTestDict("b", "baz", "c", "qux")).ToObject()},
    59  	}
    60  	for _, cas := range cases {
    61  		if err := runInvokeTestCase(testFunc.ToObject(), &cas); err != "" {
    62  			t.Error(err)
    63  		}
    64  	}
    65  }