github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/methods_test.go (about) 1 package lang 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/lmorg/murex/lang/types" 8 "github.com/lmorg/murex/test/count" 9 "github.com/lmorg/murex/utils/json" 10 ) 11 12 func TestMethodExists(t *testing.T) { 13 count.Tests(t, 5) 14 15 m := newMethods() 16 17 cmds := m.dt[t.Name()] 18 if m.Exists("1", t.Name()) { 19 t.Error("Method exists") 20 t.Logf(" cmds: %s", json.LazyLogging(cmds)) 21 t.Logf(" m.dt: %s", json.LazyLogging(m.dt)) 22 } 23 24 m.Define("1", t.Name()) 25 if len(m.dt[t.Name()]) != 1 || m.dt[t.Name()][0] != "1" { 26 t.Errorf("m.dt not getting set: %v", json.LazyLogging(m.dt)) 27 } 28 29 if !m.Exists("1", t.Name()) { 30 t.Error("Method does not exist") 31 t.Logf(" cmds: %s", json.LazyLogging(cmds)) 32 t.Logf(" m.dt: %s", json.LazyLogging(m.dt)) 33 } 34 35 m.Define("2", t.Name()) 36 if len(m.dt[t.Name()]) != 2 || m.dt[t.Name()][1] != "2" { 37 t.Errorf("m.dt not getting set: %v", json.LazyLogging(m.dt)) 38 } 39 40 if !m.Exists("2", t.Name()) { 41 t.Error("Method does not exist") 42 t.Logf(" cmds: %s", json.LazyLogging(cmds)) 43 t.Logf(" m.dt: %s", json.LazyLogging(m.dt)) 44 } 45 } 46 47 func TestMethodDefine(t *testing.T) { 48 count.Tests(t, 1) 49 50 m := newMethods() 51 m.Define("1", t.Name()) 52 if len(m.dt[t.Name()]) != 1 || m.dt[t.Name()][0] != "1" { 53 t.Errorf("m.dt not getting set: %v", json.LazyLogging(m.dt)) 54 } 55 } 56 57 func TestMethods(t *testing.T) { 58 count.Tests(t, 11) 59 60 m := newMethods() 61 62 dump := m.Dump() 63 64 if len(dump) != 0 { 65 t.Errorf("Init len(m.Dump) != 0: %d", len(dump)) 66 } 67 68 get := m.Get("test") 69 if get == nil { 70 t.Errorf("m.Get() should equal []string{} not nil") 71 } 72 73 // foo 74 75 m.Define("foo", "test") 76 77 get = m.Get("test") 78 if len(get) != 1 || get[0] != "foo" { 79 t.Error(`m.Get[0] != "foo":`) 80 t.Logf(` len(get): %d`, len(get)) 81 t.Logf(` get: `+"`%s`", json.LazyLogging(get)) 82 } 83 84 dump = m.Dump() 85 if len(dump["test"]) != 1 || dump["test"][0] != "foo" { 86 t.Error(`m.Dump["test"][0] != "foo":`) 87 t.Logf(` len(dump["test"]): %d`, len(dump["test"])) 88 t.Logf(` dump["test"]: `+"`%s`", json.LazyLogging(dump["test"])) 89 } 90 91 // bar 92 93 m.Define("bar", "test") 94 95 get = m.Get("test") 96 if len(get) != 2 || get[1] != "bar" { 97 t.Error(`m.Get[1] != "bar":`) 98 t.Logf(` len(get): %d`, len(get)) 99 t.Logf(` get: `+"`%s`", json.LazyLogging(get)) 100 } 101 102 dump = m.Dump() 103 if len(dump["test"]) != 2 || dump["test"][1] != "bar" { 104 t.Error(`m.Dump["test"][1] != "foo":`) 105 t.Logf(` len(dump["test"]): %d`, len(dump["test"])) 106 t.Logf(` dump["test"]: `+"`%v`", json.LazyLogging(dump["test"])) 107 } 108 109 // foo (dedup) 110 111 m.Define("foo", "test") 112 113 get = m.Get("test") 114 if len(get) != 2 { 115 t.Errorf(`len(get) != 2: %d`, len(get)) 116 t.Logf(` get: `+"`%s`", json.LazyLogging(get)) 117 } 118 119 dump = m.Dump() 120 if len(dump["test"]) != 2 { 121 t.Errorf(`len(dump["test"]): %d`, len(dump["test"])) 122 t.Logf(` dump["test"]: `+"`%v`", json.LazyLogging(dump["test"])) 123 } 124 } 125 126 func sortedSlice(a []string) string { 127 sort.Strings(a) 128 return json.LazyLogging(a) 129 } 130 131 func TestMethodTypes(t *testing.T) { 132 m := newMethods() 133 tests := []struct { 134 Command string 135 Type string 136 Expected []string 137 }{ 138 { 139 Command: "name", 140 Type: "str", 141 Expected: []string{"str"}, 142 }, 143 { 144 Command: "age", 145 Type: types.Math, 146 Expected: []string{"bool", "int", "num", "float"}, 147 }, 148 } 149 150 count.Tests(t, len(tests)) 151 152 for _, test := range tests { 153 m.Define(test.Command, test.Type) 154 } 155 156 err := m.Degroup() 157 if err != nil { 158 t.Error(err) 159 } 160 161 for i, test := range tests { 162 actual := sortedSlice(m.Types(test.Command)) 163 expected := sortedSlice(test.Expected) 164 165 if expected != actual { 166 t.Errorf("Unexpected return in test %d", i) 167 t.Logf(" Expected: %s", expected) 168 t.Logf(" Actual: %s", actual) 169 } 170 } 171 }