github.com/expr-lang/expr@v1.16.9/ast/print_test.go (about)

     1  package ast_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/expr-lang/expr/internal/testify/assert"
     7  	"github.com/expr-lang/expr/internal/testify/require"
     8  
     9  	"github.com/expr-lang/expr/ast"
    10  	"github.com/expr-lang/expr/parser"
    11  )
    12  
    13  func TestPrint(t *testing.T) {
    14  	tests := []struct {
    15  		input string
    16  		want  string
    17  	}{
    18  		{`nil`, `nil`},
    19  		{`true`, `true`},
    20  		{`false`, `false`},
    21  		{`1`, `1`},
    22  		{`1.1`, `1.1`},
    23  		{`"a"`, `"a"`},
    24  		{`'a'`, `"a"`},
    25  		{`a`, `a`},
    26  		{`a.b`, `a.b`},
    27  		{`a[0]`, `a[0]`},
    28  		{`a["the b"]`, `a["the b"]`},
    29  		{`a.b[0]`, `a.b[0]`},
    30  		{`a?.b`, `a?.b`},
    31  		{`x[0][1]`, `x[0][1]`},
    32  		{`x?.[0]?.[1]`, `x?.[0]?.[1]`},
    33  		{`-a`, `-a`},
    34  		{`!a`, `!a`},
    35  		{`not a`, `not a`},
    36  		{`a + b`, `a + b`},
    37  		{`a + b * c`, `a + b * c`},
    38  		{`(a + b) * c`, `(a + b) * c`},
    39  		{`a * (b + c)`, `a * (b + c)`},
    40  		{`-(a + b) * c`, `-(a + b) * c`},
    41  		{`a == b`, `a == b`},
    42  		{`a matches b`, `a matches b`},
    43  		{`a in b`, `a in b`},
    44  		{`a not in b`, `not (a in b)`},
    45  		{`a and b`, `a and b`},
    46  		{`a or b`, `a or b`},
    47  		{`a or b and c`, `a or (b and c)`},
    48  		{`a or (b and c)`, `a or (b and c)`},
    49  		{`(a or b) and c`, `(a or b) and c`},
    50  		{`a ? b : c`, `a ? b : c`},
    51  		{`a ? b : c ? d : e`, `a ? b : (c ? d : e)`},
    52  		{`(a ? b : c) ? d : e`, `(a ? b : c) ? d : e`},
    53  		{`a ? (b ? c : d) : e`, `a ? (b ? c : d) : e`},
    54  		{`func()`, `func()`},
    55  		{`func(a)`, `func(a)`},
    56  		{`func(a, b)`, `func(a, b)`},
    57  		{`{}`, `{}`},
    58  		{`{a: b}`, `{a: b}`},
    59  		{`{a: b, c: d}`, `{a: b, c: d}`},
    60  		{`{"a": b, 'c': d}`, `{a: b, c: d}`},
    61  		{`{"a": b, c: d}`, `{a: b, c: d}`},
    62  		{`{"a": b, 8: 8}`, `{a: b, "8": 8}`},
    63  		{`{"9": 9, '8': 8, "foo": d}`, `{"9": 9, "8": 8, foo: d}`},
    64  		{`[]`, `[]`},
    65  		{`[a]`, `[a]`},
    66  		{`[a, b]`, `[a, b]`},
    67  		{`len(a)`, `len(a)`},
    68  		{`map(a, # > 0)`, `map(a, # > 0)`},
    69  		{`map(a, {# > 0})`, `map(a, # > 0)`},
    70  		{`map(a, .b)`, `map(a, .b)`},
    71  		{`a.b()`, `a.b()`},
    72  		{`a.b(c)`, `a.b(c)`},
    73  		{`a[1:-1]`, `a[1:-1]`},
    74  		{`a[1:]`, `a[1:]`},
    75  		{`a[1:]`, `a[1:]`},
    76  		{`a[:]`, `a[:]`},
    77  		{`(nil ?? 1) > 0`, `(nil ?? 1) > 0`},
    78  		{`{("a" + "b"): 42}`, `{("a" + "b"): 42}`},
    79  		{`(One == 1 ? true : false) && Two == 2`, `(One == 1 ? true : false) && Two == 2`},
    80  	}
    81  
    82  	for _, tt := range tests {
    83  		t.Run(tt.input, func(t *testing.T) {
    84  			tree, err := parser.Parse(tt.input)
    85  			require.NoError(t, err)
    86  			assert.Equal(t, tt.want, tree.Node.String())
    87  		})
    88  	}
    89  }
    90  
    91  func TestPrint_MemberNode(t *testing.T) {
    92  	node := &ast.MemberNode{
    93  		Node: &ast.IdentifierNode{
    94  			Value: "a",
    95  		},
    96  		Property: &ast.StringNode{Value: "b c"},
    97  		Optional: true,
    98  	}
    99  	require.Equal(t, `a?.["b c"]`, node.String())
   100  }
   101  
   102  func TestPrint_ConstantNode(t *testing.T) {
   103  	tests := []struct {
   104  		input any
   105  		want  string
   106  	}{
   107  		{nil, `nil`},
   108  		{true, `true`},
   109  		{false, `false`},
   110  		{1, `1`},
   111  		{1.1, `1.1`},
   112  		{"a", `"a"`},
   113  		{[]int{1, 2, 3}, `[1,2,3]`},
   114  		{map[string]int{"a": 1}, `{"a":1}`},
   115  	}
   116  
   117  	for _, tt := range tests {
   118  		t.Run(tt.want, func(t *testing.T) {
   119  			node := &ast.ConstantNode{
   120  				Value: tt.input,
   121  			}
   122  			require.Equal(t, tt.want, node.String())
   123  		})
   124  	}
   125  }