github.com/goplus/llgo@v0.8.3/chore/llpyg/pysig/parse_test.go (about)

     1  /*
     2   * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package pysig
    18  
    19  import "testing"
    20  
    21  func TestParse(t *testing.T) {
    22  	type testCase struct {
    23  		sig  string
    24  		args []*Arg
    25  	}
    26  	cases := []testCase{
    27  		{"(start=None, *, unit: 'str | None' = None) -> 'TimedeltaIndex'", []*Arg{
    28  			{Name: "start", DefVal: "None"},
    29  			{Name: "*"},
    30  			{Name: "unit", Type: "'str | None'", DefVal: "None"},
    31  		}},
    32  		{"()", nil},
    33  		{"(a =", []*Arg{{Name: "a"}}},
    34  		{"(a) -> int", []*Arg{{Name: "a"}}},
    35  		{"(a: int)", []*Arg{{Name: "a", Type: "int"}}},
    36  		{"(a: int = 1, b: float)", []*Arg{{Name: "a", Type: "int", DefVal: "1"}, {Name: "b", Type: "float"}}},
    37  		{"(a = <1>, b = 2.0)", []*Arg{{Name: "a", DefVal: "<1>"}, {Name: "b", DefVal: "2.0"}}},
    38  		{"(a: 'Suffixes' = ('_x', '_y'))", []*Arg{{Name: "a", Type: "'Suffixes'", DefVal: "('_x', '_y')"}}},
    39  	}
    40  	for _, c := range cases {
    41  		args := Parse(c.sig)
    42  		if len(args) != len(c.args) {
    43  			t.Fatalf("%s: len(args) = %v, want %v", c.sig, len(args), len(c.args))
    44  		}
    45  		for i, arg := range args {
    46  			want := c.args[i]
    47  			if arg.Name != want.Name || arg.Type != want.Type || arg.DefVal != want.DefVal {
    48  				t.Fatalf("%s: args[%v] = %v, want %v", c.sig, i, arg, want)
    49  			}
    50  		}
    51  	}
    52  }