github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/expressions/expression_test.go (about)

     1  package expressions
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/lmorg/murex/test/count"
     8  )
     9  
    10  func TestExpressionParserOffset0(t *testing.T) {
    11  	tests := []string{
    12  		"1+2;other code",
    13  		//"(1+2);other code",
    14  		"foobar=1+2;other code",
    15  		"foobar=(1+2);other code",
    16  		"foobar=1+2;other code",
    17  		"foobar=(1+(2+3)+4);other code",
    18  		"TestExpressionsBuiltin6=1+1+1+1+(1+1)+1+1+1+1;code",
    19  		"TestExpressionsBuiltin0=1+1+1+1+1+1+1+1+1+1",
    20  		"1+1+1+1+1",
    21  		"true==true",
    22  		"1 + 1",
    23  		"3*(3+1)",
    24  		"bob=3*(3+1)",
    25  		"%[apples oranges grapes]",
    26  		"fruit=%[apples oranges grapes]",
    27  		"$a==$b",
    28  		"%[1,2,$a]",
    29  		"%[1,2,@a]",
    30  		"%[1,2,$aa]",
    31  		"%[1,2,@aa]",
    32  		"b=%[1,2,$a]",
    33  		"b=%[1,2,@a]",
    34  		"b=%[1,2,$aa]",
    35  		"b=%[1,2,@aa]",
    36  		"foobar=%[1,2,$a]",
    37  		"foobar=%[1,2,@a]",
    38  		"foobar=%[1,2,$aa]",
    39  		"foobar=%[1,2,@aa]",
    40  	}
    41  
    42  	count.Tests(t, len(tests))
    43  
    44  	for j := range tests {
    45  		expression := []rune(tests[j][0:])
    46  		split := strings.Split(tests[j], ";")
    47  
    48  		i, err := ExpressionParser(expression, 0, false)
    49  		if err != nil || string(expression[:i+1]) != split[0] {
    50  			t.Errorf("Expression did not parse correctly in test %d:", j)
    51  			t.Log("            :           1         2         3         4         5")
    52  			t.Log("            :  12345678901234567890123456789012345678901234567890")
    53  			t.Logf("  Expression: '%s'", string(expression))
    54  			t.Logf("  Error:      %v", err)
    55  			t.Logf("  exp bytes:   %v", []byte(split[0]))
    56  			t.Logf("  act bytes:   %v", []byte(string(expression[:i+1])))
    57  			t.Logf("  Expected:   '%s'", split[0])
    58  			t.Logf("  Actual:     '%s'", string(expression[:i+1]))
    59  		}
    60  	}
    61  }
    62  
    63  func TestExpressionParserOffset5(t *testing.T) {
    64  	tests := []string{
    65  		"code;1+2;other code",
    66  		//"code;(1+2);other code",
    67  		"code;foobar=1+2;other code",
    68  		"code;foobar=(1+2);other code",
    69  		"code;foobar=1+2;other code",
    70  		"code;foobar=(1+(2+3)+4);other code",
    71  		"code;TestExpressionsBuiltin6=1+1+1+1+(1+1)+1+1+1+1;code",
    72  		"code;TestExpressionsBuiltin0=1+1+1+1+1+1+1+1+1+1",
    73  		"code;3*(3+1)",
    74  		"code;bob=3*(3+1)",
    75  		"code;%[apples oranges grapes]",
    76  		"code;fruit=%[apples oranges grapes]",
    77  		"code;$a==$b",
    78  		"code;%[1,2,$a]",
    79  		"code;%[1,2,@a]",
    80  		"code;%[1,2,$aa]",
    81  		"code;%[1,2,@aa]",
    82  		"code;b=%[1,2,$a]",
    83  		"code;b=%[1,2,@a]",
    84  		"code;b=%[1,2,$aa]",
    85  		"code;b=%[1,2,@aa]",
    86  		"code;foobar=%[1,2,$a]",
    87  		"code;foobar=%[1,2,@a]",
    88  		"code;foobar=%[1,2,$aa]",
    89  		"code;foobar=%[1,2,@aa]",
    90  	}
    91  
    92  	count.Tests(t, len(tests))
    93  
    94  	for j := range tests {
    95  		expression := []rune(tests[j][5:])
    96  		split := strings.Split(tests[j], ";")
    97  
    98  		i, err := ExpressionParser(expression, 5, false)
    99  		if err != nil || string(expression[:i+1]) != split[1] {
   100  			t.Errorf("Expression did not parse correctly in test %d:", j)
   101  			t.Log("            :           1         2         3         4         5")
   102  			t.Log("            :  12345678901234567890123456789012345678901234567890")
   103  			t.Logf("  Expression: '%s'", string(expression))
   104  			t.Logf("  Error:      %v", err)
   105  			t.Logf("  Expected:   '%s'", split[1])
   106  			t.Logf("  Actual:     '%s'", string(expression[:i+1]))
   107  		}
   108  	}
   109  }
   110  
   111  /*func TestStrings(t *testing.T) {
   112  	tests := []string{
   113  		`'foobar'`,
   114  		`"foobar"`,
   115  		`(foobar)`,
   116  		`%(foobar)`,
   117  	}
   118  
   119  	for i := range tests {
   120  		mxtext:=test.MurexTest{
   121  			Std
   122  		}
   123  	}
   124  }*/