github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/expressions/variables_test.go (about) 1 package expressions 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/test/count" 7 ) 8 9 type createIndexBlockTestT struct { 10 Name string 11 Index string 12 Flags string 13 Expected string 14 } 15 16 func TestCreateIndexBlock(t *testing.T) { 17 tests := []createIndexBlockTestT{ 18 { 19 Name: "foobar", 20 Index: "hello world", 21 }, 22 { 23 Name: "", 24 Index: "", 25 }, 26 { 27 Name: "a", 28 Index: "b", 29 }, 30 } 31 32 count.Tests(t, len(tests)) 33 34 for i, test := range tests { 35 if test.Expected == "" { 36 test.Expected = "$" + test.Name + "-> [" + test.Index + "]" 37 } 38 39 getIorE := &getVarIndexOrElementT{varName: []rune(test.Name), key: []rune(test.Index)} 40 block := createIndexBlock(getIorE) 41 if string(block) != test.Expected { 42 t.Errorf("block does not match expected in test %d", i) 43 t.Logf(" Name: '%s'", test.Name) 44 t.Logf(" Index: '%s'", test.Index) 45 t.Logf(" Expected: '%s'", test.Expected) 46 t.Logf(" Actual: '%s'", string(block)) 47 } 48 } 49 } 50 51 func TestCreateElementBlock(t *testing.T) { 52 tests := []createIndexBlockTestT{ 53 { 54 Name: "foobar", 55 Index: "hello world", 56 }, 57 { 58 Name: "", 59 Index: "", 60 }, 61 { 62 Name: "a", 63 Index: "b", 64 }, 65 } 66 67 count.Tests(t, len(tests)) 68 69 for i, test := range tests { 70 if test.Expected == "" { 71 test.Expected = "$" + test.Name + "-> [[" + test.Index + "]]" 72 } 73 74 getIorE := &getVarIndexOrElementT{varName: []rune(test.Name), key: []rune(test.Index)} 75 block := createElementBlock(getIorE) 76 if string(block) != test.Expected { 77 t.Errorf("block does not match expected in test %d", i) 78 t.Logf(" Name: '%s'", test.Name) 79 t.Logf(" Index: '%s'", test.Index) 80 t.Logf(" Expected: '%s'", test.Expected) 81 t.Logf(" Actual: '%s'", string(block)) 82 } 83 } 84 } 85 86 func TestCreateRangeBlock(t *testing.T) { 87 tests := []createIndexBlockTestT{ 88 { 89 Name: "foobar", 90 Index: "hello world", 91 Flags: "abc", 92 }, 93 { 94 Name: "", 95 Index: "", 96 Flags: "", 97 }, 98 { 99 Name: "a", 100 Index: "b", 101 Flags: "c", 102 }, 103 } 104 105 count.Tests(t, len(tests)) 106 107 for i, test := range tests { 108 if test.Expected == "" { 109 test.Expected = "$" + test.Name + "-> @[" + test.Index + "]" + test.Flags 110 } 111 112 block := createRangeBlock([]rune(test.Name), []rune(test.Index), []rune(test.Flags)) 113 if string(block) != test.Expected { 114 t.Errorf("block does not match expected in test %d", i) 115 t.Logf(" Name: '%s'", test.Name) 116 t.Logf(" Index: '%s'", test.Index) 117 t.Logf(" Expected: '%s'", test.Expected) 118 t.Logf(" Actual: '%s'", string(block)) 119 } 120 } 121 }