github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/variables_test.go (about) 1 package lang 2 3 import ( 4 "strconv" 5 "strings" 6 "testing" 7 8 "github.com/lmorg/murex/lang/types" 9 "github.com/lmorg/murex/test/count" 10 ) 11 12 // TestVariablesDefault tests with the F_DEFAULTS fork flag set 13 func TestVariablesDefaults(t *testing.T) { 14 testVariables(t, F_DEFAULTS, "F_DEFAULTS") 15 } 16 17 // testVariables is the main testing function for variables 18 func testVariables(t *testing.T, flags int, details string) { 19 t.Log("Testing with flags: " + details) 20 const ( 21 origNum = 123.123 22 origInt = 45678 23 origStr = "abcABC" 24 origBool = true 25 copyNum = 987.789 26 copyInt = 65432 27 copyStr = "xyzXYZ" 28 copyBool = false 29 ) 30 31 InitEnv() 32 33 p := NewTestProcess() 34 35 // Create a referenced variable table 36 count.Tests(t, 4) 37 fork := p.Fork(flags) 38 copy := fork.Variables 39 40 err := copy.Set(p, "number", copyNum, types.Number) 41 if err != nil { 42 t.Error("Unable to set number in copy. " + err.Error()) 43 } 44 45 err = copy.Set(p, "integer", copyInt, types.Integer) 46 if err != nil { 47 t.Error("Unable to set integer in copy. " + err.Error()) 48 } 49 50 err = copy.Set(p, "string", copyStr, types.String) 51 if err != nil { 52 t.Error("Unable to set string in copy. " + err.Error()) 53 } 54 55 err = copy.Set(p, "boolean", copyBool, types.Boolean) 56 if err != nil { 57 t.Error("Unable to set boolean in copy. " + err.Error()) 58 } 59 60 // test GetValue 61 count.Tests(t, 4) 62 63 v := panicErr(copy.GetValue("number")) 64 if v.(float64) != copyNum { 65 t.Error("Copy var table not returning correct number using GetValue.") 66 } 67 68 v = panicErr(copy.GetValue("integer")) 69 if v.(int) != copyInt { 70 t.Error("Copy var table not returning correct integer using GetValue.") 71 } 72 73 v = panicErr(copy.GetValue("string")) 74 if v.(string) != copyStr { 75 t.Error("Copy var table not returning correct string using GetValue.") 76 } 77 78 v = panicErr(copy.GetValue("boolean")) 79 if v.(bool) != copyBool { 80 t.Error("Copy var table not returning correct boolean using GetValue.") 81 } 82 83 // test GetString on copy 84 count.Tests(t, 4) 85 86 if v, err := copy.GetString("number"); err != nil || v != types.FloatToString(copyNum) { 87 t.Error("Copy var table not returning correct numeric converted value using GetString.") 88 } 89 90 if v, err := copy.GetString("integer"); err != nil || v != strconv.Itoa(copyInt) { 91 t.Error("Copy var table not returning correct numeric converted value using GetString.") 92 } 93 94 if v, err := copy.GetString("string"); err != nil || v != copyStr { 95 t.Error("Copy var table not returning correct string converted value using GetString.") 96 } 97 98 s, err := copy.GetString("boolean") 99 if types.IsTrue([]byte(s), 0) != copyBool || err != nil { 100 t.Error("Copy var table not returning correct boolean converted value using GetString.") 101 } 102 } 103 104 func panicErr(v interface{}, err error) interface{} { 105 if err != nil { 106 panic(err) 107 } 108 109 return v 110 } 111 112 // TestReservedVariables tests the Vars structure 113 func TestReservedVariables(t *testing.T) { 114 p := NewTestProcess() 115 116 reserved := []string{ 117 "SELF", 118 "ARGS", 119 "PARAMS", 120 "MUREX_EXE", 121 "MUREX_ARGS", 122 "HOSTNAME", 123 "PWD", 124 "0", "1", "8", "9", 125 "00", "10", "50", "99", "100", 126 "_", 127 } 128 129 count.Tests(t, len(reserved)) 130 131 for _, name := range reserved { 132 err := GlobalVariables.Set(p, name, "foobar", types.String) 133 if err == nil || !strings.Contains(err.Error(), "reserved") { 134 t.Errorf("`%s` is not a reserved variable", name) 135 } 136 } 137 }