github.com/pygolin/runtime@v0.0.0-20201208210830-a62e3cd39798/bool_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 TestBoolCompare(t *testing.T) {
    22  	cases := []invokeTestCase{
    23  		{args: wrapArgs(true, true), want: compareAllResultEq},
    24  		{args: wrapArgs(true, false), want: compareAllResultGT},
    25  		{args: wrapArgs(true, 1), want: compareAllResultEq},
    26  		{args: wrapArgs(true, -1), want: compareAllResultGT},
    27  		{args: wrapArgs(false, 0), want: compareAllResultEq},
    28  		{args: wrapArgs(false, 1000), want: compareAllResultLT},
    29  	}
    30  	for _, cas := range cases {
    31  		if err := runInvokeTestCase(compareAll, &cas); err != "" {
    32  			t.Error(err)
    33  		}
    34  	}
    35  }
    36  
    37  func TestBoolCreate(t *testing.T) {
    38  	cases := []invokeTestCase{
    39  		{args: wrapArgs(None), wantExc: mustCreateException(TypeErrorType, `'__new__' requires a 'type' object but received a "NoneType"`)},
    40  		{args: wrapArgs(BoolType), want: False.ToObject()},
    41  		{args: wrapArgs(BoolType, None), want: False.ToObject()},
    42  		{args: wrapArgs(BoolType, ""), want: False.ToObject()},
    43  		{args: wrapArgs(BoolType, true), want: True.ToObject()},
    44  		{args: wrapArgs(BoolType, newObject(ObjectType)), want: True.ToObject()},
    45  		{args: wrapArgs(ObjectType), wantExc: mustCreateException(TypeErrorType, "bool.__new__(object): object is not a subtype of bool")},
    46  		{args: wrapArgs(BoolType, "foo", "bar"), wantExc: mustCreateException(TypeErrorType, "bool() takes at most 1 argument (2 given)")},
    47  	}
    48  	for _, cas := range cases {
    49  		if err := runInvokeMethodTestCase(BoolType, "__new__", &cas); err != "" {
    50  			t.Error(err)
    51  		}
    52  	}
    53  }
    54  
    55  func TestBoolStrRepr(t *testing.T) {
    56  	cases := []invokeTestCase{
    57  		{args: wrapArgs(true), want: NewStr("True").ToObject()},
    58  		{args: wrapArgs(false), want: NewStr("False").ToObject()},
    59  	}
    60  	for _, cas := range cases {
    61  		if err := runInvokeTestCase(wrapFuncForTest(ToStr), &cas); err != "" {
    62  			t.Error(err)
    63  		}
    64  		if err := runInvokeTestCase(wrapFuncForTest(Repr), &cas); err != "" {
    65  			t.Error(err)
    66  		}
    67  	}
    68  }