github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/lang/expressions/block_test.go (about) 1 package expressions 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/lang/expressions/functions" 7 "github.com/lmorg/murex/test/count" 8 ) 9 10 func TestBlockPropertiesCrLf(t *testing.T) { 11 count.Tests(t, 1) 12 block := []rune("out 1\nout 2\nout 3") 13 blk := NewBlock(block) 14 err := blk.ParseBlock() 15 if err != nil { 16 t.Fatal(err) 17 } 18 19 if len(blk.Functions) != 3 { 20 t.Fatalf("expecting 3 functions, instead got %d", len(blk.Functions)) 21 } 22 23 for i := range blk.Functions { 24 if !blk.Functions[i].Properties.NewChain() { 25 t.Errorf("function %d != NewChain() <func>", i) 26 } 27 28 if blk.Functions[i].Properties != functions.P_NEW_CHAIN { 29 t.Errorf("function %d != P_NEW_CHAIN <int>", i) 30 } 31 } 32 } 33 34 func TestBlockPropertiesCrLfNoParams(t *testing.T) { 35 count.Tests(t, 1) 36 block := []rune("1\n2\n3") 37 blk := NewBlock(block) 38 err := blk.ParseBlock() 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 if len(blk.Functions) != 3 { 44 t.Fatalf("expecting 3 functions, instead got %d", len(blk.Functions)) 45 } 46 47 for i := range blk.Functions { 48 if !blk.Functions[i].Properties.NewChain() { 49 t.Errorf("function %d != NewChain() <func>", i) 50 } 51 52 if blk.Functions[i].Properties != functions.P_NEW_CHAIN { 53 t.Errorf("function %d != P_NEW_CHAIN <int>", i) 54 } 55 } 56 } 57 58 func TestBlockPropertiesCrLfx2(t *testing.T) { 59 count.Tests(t, 1) 60 block := []rune("out 1\n\nout 2\n\nout 3") 61 blk := NewBlock(block) 62 err := blk.ParseBlock() 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 if len(blk.Functions) != 3 { 68 t.Fatalf("expecting 3 functions, instead got %d", len(blk.Functions)) 69 } 70 71 for i := range blk.Functions { 72 if !blk.Functions[i].Properties.NewChain() { 73 t.Errorf("function %d != NewChain() <func>", i) 74 } 75 76 if blk.Functions[i].Properties != functions.P_NEW_CHAIN { 77 t.Errorf("function %d != P_NEW_CHAIN <int>", i) 78 } 79 } 80 } 81 82 func TestBlockPropertiesSemiColon(t *testing.T) { 83 count.Tests(t, 1) 84 block := []rune("out 1;out 2;out 3") 85 blk := NewBlock(block) 86 err := blk.ParseBlock() 87 if err != nil { 88 t.Fatal(err) 89 } 90 91 if len(blk.Functions) != 3 { 92 t.Fatalf("expecting 3 functions, instead got %d", len(blk.Functions)) 93 } 94 95 for i := range blk.Functions { 96 if !blk.Functions[i].Properties.NewChain() { 97 t.Errorf("function %d != NewChain() <func>", i) 98 } 99 100 if blk.Functions[i].Properties != functions.P_NEW_CHAIN { 101 t.Errorf("function %d != P_NEW_CHAIN <int>", i) 102 } 103 } 104 } 105 106 func TestBlockPropertiesSemiColonCrLF(t *testing.T) { 107 count.Tests(t, 1) 108 block := []rune("out 1;\nout 2;\nout 3") 109 blk := NewBlock(block) 110 err := blk.ParseBlock() 111 if err != nil { 112 t.Fatal(err) 113 } 114 115 if len(blk.Functions) != 3 { 116 t.Fatalf("expecting 3 functions, instead got %d", len(blk.Functions)) 117 } 118 119 for i := range blk.Functions { 120 if !blk.Functions[i].Properties.NewChain() { 121 t.Errorf("function %d != NewChain() <func>", i) 122 } 123 124 if blk.Functions[i].Properties != functions.P_NEW_CHAIN { 125 t.Errorf("function %d != P_NEW_CHAIN <int>", i) 126 } 127 } 128 } 129 130 func TestBlockPropertiesPosixPipe(t *testing.T) { 131 count.Tests(t, 1) 132 block := []rune("out 1|out 2|out 3") 133 blk := NewBlock(block) 134 err := blk.ParseBlock() 135 if err != nil { 136 t.Fatal(err) 137 } 138 139 if len(blk.Functions) != 3 { 140 t.Fatalf("expecting 3 functions, instead got %d", len(blk.Functions)) 141 } 142 143 i := 0 144 145 if !blk.Functions[i].Properties.NewChain() { 146 t.Errorf("function %d != NewChain() <func>", i) 147 } 148 149 if !blk.Functions[i].Properties.PipeOut() { 150 t.Errorf("function %d != PipeOut() <func>", i) 151 } 152 153 if blk.Functions[i].Properties != functions.P_NEW_CHAIN|functions.P_PIPE_OUT { 154 t.Errorf("function %d != P_NEW_CHAIN|P_PIPE_OUT <int>", i) 155 } 156 157 i = 1 158 159 if !blk.Functions[i].Properties.Method() { 160 t.Errorf("function %d != Method() <func>", i) 161 } 162 163 if !blk.Functions[i].Properties.PipeOut() { 164 t.Errorf("function %d != PipeOut() <func>", i) 165 } 166 167 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_PIPE_OUT|functions.P_FOLLOW_ON { 168 t.Errorf("function %d != P_METHOD|P_PIPE_OUT|P_FOLLOW_ON <int>", i) 169 } 170 171 i = 2 172 173 if !blk.Functions[i].Properties.Method() { 174 t.Errorf("function %d != Method() <func>", i) 175 } 176 177 if blk.Functions[i].Properties.PipeOut() { 178 t.Errorf("function %d == PipeOut() <func>", i) 179 } 180 181 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_FOLLOW_ON { 182 t.Errorf("function %d != P_METHOD|P_FOLLOW_ON <int>", i) 183 } 184 } 185 186 func TestBlockPropertiesArrowPipe(t *testing.T) { 187 count.Tests(t, 1) 188 block := []rune("out 1->out 2->out 3") 189 blk := NewBlock(block) 190 err := blk.ParseBlock() 191 if err != nil { 192 t.Fatal(err) 193 } 194 195 if len(blk.Functions) != 3 { 196 t.Fatalf("expecting 3 functions, instead got %d", len(blk.Functions)) 197 } 198 199 i := 0 200 201 if !blk.Functions[i].Properties.NewChain() { 202 t.Errorf("function %d != NewChain() <func>", i) 203 } 204 205 if !blk.Functions[i].Properties.PipeOut() { 206 t.Errorf("function %d != PipeOut() <func>", i) 207 } 208 209 if blk.Functions[i].Properties != functions.P_NEW_CHAIN|functions.P_PIPE_OUT { 210 t.Errorf("function %d != P_NEW_CHAIN|P_PIPE_OUT <int>", i) 211 } 212 213 i = 1 214 215 if !blk.Functions[i].Properties.Method() { 216 t.Errorf("function %d != Method() <func>", i) 217 } 218 219 if !blk.Functions[i].Properties.PipeOut() { 220 t.Errorf("function %d != PipeOut() <func>", i) 221 } 222 223 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_PIPE_OUT|functions.P_FOLLOW_ON { 224 t.Errorf("function %d != P_METHOD|P_PIPE_OUT|P_FOLLOW_ON <int>", i) 225 } 226 227 i = 2 228 229 if !blk.Functions[i].Properties.Method() { 230 t.Errorf("function %d != Method() <func>", i) 231 } 232 233 if blk.Functions[i].Properties.PipeOut() { 234 t.Errorf("function %d == PipeOut() <func>", i) 235 } 236 237 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_FOLLOW_ON { 238 t.Errorf("function %d != P_METHOD|P_FOLLOW_ON <int>", i) 239 } 240 } 241 242 func TestBlockPropertiesQuestionMark(t *testing.T) { 243 count.Tests(t, 1) 244 block := []rune("out 1 ? out 2 ? out 3") 245 blk := NewBlock(block) 246 err := blk.ParseBlock() 247 if err != nil { 248 t.Fatal(err) 249 } 250 251 if len(blk.Functions) != 3 { 252 t.Fatalf("expecting 3 functions, instead got %d", len(blk.Functions)) 253 } 254 255 i := 0 256 257 if !blk.Functions[i].Properties.NewChain() { 258 t.Errorf("function %d != NewChain() <func>", i) 259 } 260 261 if !blk.Functions[i].Properties.PipeErr() { 262 t.Errorf("function %d != PipeErr() <func>", i) 263 } 264 265 if blk.Functions[i].Properties != functions.P_NEW_CHAIN|functions.P_PIPE_ERR { 266 t.Errorf("function %d != P_NEW_CHAIN|P_PIPE_ERR <int>", i) 267 } 268 269 i = 1 270 271 if !blk.Functions[i].Properties.Method() { 272 t.Errorf("function %d != Method() <func>", i) 273 } 274 275 if !blk.Functions[i].Properties.PipeErr() { 276 t.Errorf("function %d != PipeErr() <func>", i) 277 } 278 279 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_PIPE_ERR|functions.P_FOLLOW_ON { 280 t.Errorf("function %d != P_METHOD|P_PIPE_ERR|P_FOLLOW_ON <int>", i) 281 } 282 283 i = 2 284 285 if !blk.Functions[i].Properties.Method() { 286 t.Errorf("function %d != Method() <func>", i) 287 } 288 289 if blk.Functions[i].Properties.PipeErr() { 290 t.Errorf("function %d == PipeErr() <func>", i) 291 } 292 293 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_FOLLOW_ON { 294 t.Errorf("function %d != P_METHOD|P_FOLLOW_ON <int>", i) 295 } 296 } 297 298 func TestBlockPropertiesFormatGeneric(t *testing.T) { 299 count.Tests(t, 1) 300 block := []rune("out 1 => out 2 => out 3") 301 blk := NewBlock(block) 302 err := blk.ParseBlock() 303 if err != nil { 304 t.Fatal(err) 305 } 306 307 if len(blk.Functions) != 5 { 308 t.Fatalf("expecting 5 functions, instead got %d", len(blk.Functions)) 309 } 310 311 i := 0 312 313 if !blk.Functions[i].Properties.NewChain() { 314 t.Errorf("function %d != NewChain() <func>", i) 315 } 316 317 if !blk.Functions[i].Properties.PipeOut() { 318 t.Errorf("function %d != PipeOut() <func>", i) 319 } 320 321 if blk.Functions[i].Properties != functions.P_NEW_CHAIN|functions.P_PIPE_OUT { 322 t.Errorf("function %d != P_NEW_CHAIN|P_PIPE_OUT <int>", i) 323 } 324 325 i = 1 326 327 if !blk.Functions[i].Properties.Method() { 328 t.Errorf("function %d != Method() <func>", i) 329 } 330 331 if !blk.Functions[i].Properties.PipeOut() { 332 t.Errorf("function %d != PipeOut() <func>", i) 333 } 334 335 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_PIPE_OUT|functions.P_FOLLOW_ON { 336 t.Errorf("function %d != P_METHOD|P_PIPE_OUT|P_FOLLOW_ON <int>", i) 337 } 338 339 i = 2 340 341 if !blk.Functions[i].Properties.Method() { 342 t.Errorf("function %d != Method() <func>", i) 343 } 344 345 if !blk.Functions[i].Properties.PipeOut() { 346 t.Errorf("function %d != PipeOut() <func>", i) 347 } 348 349 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_PIPE_OUT|functions.P_FOLLOW_ON { 350 t.Errorf("function %d != P_METHOD|P_PIPE_OUT|P_FOLLOW_ON <int>", i) 351 } 352 353 i = 3 354 355 if !blk.Functions[i].Properties.Method() { 356 t.Errorf("function %d != Method() <func>", i) 357 } 358 359 if !blk.Functions[i].Properties.PipeOut() { 360 t.Errorf("function %d != PipeOut() <func>", i) 361 } 362 363 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_PIPE_OUT|functions.P_FOLLOW_ON { 364 t.Errorf("function %d != P_METHOD|P_PIPE_OUT|P_FOLLOW_ON <int>", i) 365 } 366 367 i = 4 368 369 if !blk.Functions[i].Properties.Method() { 370 t.Errorf("function %d != Method() <func>", i) 371 } 372 373 if blk.Functions[i].Properties.PipeOut() { 374 t.Errorf("function %d == PipeOut() <func>", i) 375 } 376 377 if blk.Functions[i].Properties != functions.P_METHOD|functions.P_FOLLOW_ON { 378 t.Errorf("function %d != P_METHOD|P_FOLLOW_ON <int>", i) 379 } 380 }