github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/runtime/callableiter_test.go (about)

     1  // Copyright 2018 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 grumpy
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestCallableIterator(t *testing.T) {
    22  	fun := newBuiltinFunction("TestCallableIterator", func(f *Frame, args Args, _ KWArgs) (*Object, *BaseException) {
    23  		return TupleType.Call(f, args, nil)
    24  	}).ToObject()
    25  	makeCounter := func() *Object {
    26  		cnt := 0
    27  		return newBuiltinFunction("counter", func(f *Frame, _ Args, _ KWArgs) (*Object, *BaseException) {
    28  			cnt++
    29  			return NewInt(cnt).ToObject(), nil
    30  		}).ToObject()
    31  	}
    32  	exhaustedIter := newCallableIterator(makeCounter(), NewInt(2).ToObject())
    33  	TupleType.Call(NewRootFrame(), []*Object{exhaustedIter}, nil)
    34  	cases := []invokeTestCase{
    35  		{args: wrapArgs(newCallableIterator(makeCounter(), NewInt(4).ToObject())), want: newTestTuple(1, 2, 3).ToObject()},
    36  		{args: wrapArgs(newCallableIterator(makeCounter(), NewInt(1).ToObject())), want: newTestTuple().ToObject()},
    37  		{args: wrapArgs(exhaustedIter), want: NewTuple().ToObject()},
    38  	}
    39  	for _, cas := range cases {
    40  		if err := runInvokeTestCase(fun, &cas); err != "" {
    41  			t.Error(err)
    42  		}
    43  	}
    44  }