go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/starlark/builtins/struct_test.go (about)

     1  // Copyright 2018 The LUCI Authors.
     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 builtins
    16  
    17  import (
    18  	"testing"
    19  
    20  	"go.starlark.net/starlark"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  )
    24  
    25  const successTest = `
    26  def assert(a, b):
    27  	if a != b:
    28  		fail("%s (%s) != %s (%s)" % (a, type(a), b, type(b)))
    29  
    30  mystruct = genstruct("mystruct")
    31  s = mystruct(a=1, b=2)
    32  assert(ctor(s), mystruct)
    33  assert(ctor(1), None)
    34  assert(ctor(struct(a=1, b=2)), "struct")
    35  `
    36  
    37  func TestStruct(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	runScript := func(code string) error {
    41  		_, err := starlark.ExecFile(&starlark.Thread{}, "main", code, starlark.StringDict{
    42  			"struct":    Struct,
    43  			"genstruct": GenStruct,
    44  			"ctor":      Ctor,
    45  
    46  			"fail": Fail, // for 'assert'
    47  		})
    48  		return err
    49  	}
    50  
    51  	Convey("Success", t, func() {
    52  		So(runScript(successTest), ShouldBeNil)
    53  	})
    54  }