github.com/google/skylark@v0.0.0-20181101142754-a5f7082aabed/resolve/resolve_test.go (about)

     1  // Copyright 2017 The Bazel Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package resolve_test
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/google/skylark/internal/chunkedfile"
    12  	"github.com/google/skylark/resolve"
    13  	"github.com/google/skylark/skylarktest"
    14  	"github.com/google/skylark/syntax"
    15  )
    16  
    17  func TestResolve(t *testing.T) {
    18  	filename := skylarktest.DataFile("skylark/resolve", "testdata/resolve.sky")
    19  	for _, chunk := range chunkedfile.Read(filename, t) {
    20  		f, err := syntax.Parse(filename, chunk.Source, 0)
    21  		if err != nil {
    22  			t.Error(err)
    23  			continue
    24  		}
    25  
    26  		// A chunk may set options by containing e.g. "option:float".
    27  		resolve.AllowNestedDef = option(chunk.Source, "nesteddef")
    28  		resolve.AllowLambda = option(chunk.Source, "lambda")
    29  		resolve.AllowFloat = option(chunk.Source, "float")
    30  		resolve.AllowSet = option(chunk.Source, "set")
    31  		resolve.AllowGlobalReassign = option(chunk.Source, "global_reassign")
    32  
    33  		if err := resolve.File(f, isPredeclared, isUniversal); err != nil {
    34  			for _, err := range err.(resolve.ErrorList) {
    35  				chunk.GotError(int(err.Pos.Line), err.Msg)
    36  			}
    37  		}
    38  		chunk.Done()
    39  	}
    40  }
    41  
    42  func option(chunk, name string) bool {
    43  	return strings.Contains(chunk, "option:"+name)
    44  }
    45  
    46  func TestDefVarargsAndKwargsSet(t *testing.T) {
    47  	source := "def f(*args, **kwargs): pass\n"
    48  	file, err := syntax.Parse("foo.sky", source, 0)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	if err := resolve.File(file, isPredeclared, isUniversal); err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	fn := file.Stmts[0].(*syntax.DefStmt)
    56  	if !fn.HasVarargs {
    57  		t.Error("HasVarargs not set")
    58  	}
    59  	if !fn.HasKwargs {
    60  		t.Error("HasKwargs not set")
    61  	}
    62  }
    63  
    64  func TestLambdaVarargsAndKwargsSet(t *testing.T) {
    65  	resolve.AllowLambda = true
    66  	source := "f = lambda *args, **kwargs: 0\n"
    67  	file, err := syntax.Parse("foo.sky", source, 0)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	if err := resolve.File(file, isPredeclared, isUniversal); err != nil {
    72  		t.Fatal(err)
    73  	}
    74  	lam := file.Stmts[0].(*syntax.AssignStmt).RHS.(*syntax.LambdaExpr)
    75  	if !lam.HasVarargs {
    76  		t.Error("HasVarargs not set")
    77  	}
    78  	if !lam.HasKwargs {
    79  		t.Error("HasKwargs not set")
    80  	}
    81  }
    82  
    83  func isPredeclared(name string) bool { return name == "M" }
    84  
    85  func isUniversal(name string) bool { return name == "U" || name == "float" }