github.com/MontFerret/ferret@v0.18.0/pkg/runtime/expressions/operators/operator_test.go (about) 1 package operators_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/MontFerret/ferret/pkg/runtime/core" 8 "github.com/MontFerret/ferret/pkg/runtime/expressions/operators" 9 "github.com/MontFerret/ferret/pkg/runtime/values" 10 11 . "github.com/smartystreets/goconvey/convey" 12 ) 13 14 func TestAdd(t *testing.T) { 15 Convey("Add", t, func() { 16 Convey("Int", func() { 17 Convey("1 + 2 = 3", func() { 18 arg1 := values.NewInt(1) 19 arg2 := values.NewInt(2) 20 21 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(3)) 22 So(operators.Add(arg2, arg1), ShouldEqual, values.NewInt(3)) 23 }) 24 }) 25 26 Convey("Float", func() { 27 Convey("1.1 + 2.1 = 3.2", func() { 28 arg1 := values.NewFloat(1.1) 29 arg2 := values.NewFloat(2.1) 30 31 So(operators.Add(arg1, arg2), ShouldEqual, values.NewFloat(3.2)) 32 So(operators.Add(arg2, arg1), ShouldEqual, values.NewFloat(3.2)) 33 }) 34 }) 35 36 Convey("Float & Int", func() { 37 Convey("1 + 2.1 = 3.1", func() { 38 arg1 := values.NewInt(1) 39 arg2 := values.NewFloat(2.1) 40 41 So(operators.Add(arg1, arg2), ShouldEqual, values.NewFloat(3.1)) 42 }) 43 44 Convey("1.1 + 2 = 3.1", func() { 45 arg1 := values.NewFloat(1.1) 46 arg2 := values.NewInt(2) 47 48 So(operators.Add(arg1, arg2), ShouldEqual, values.NewFloat(3.1)) 49 }) 50 }) 51 52 Convey("Int & String", func() { 53 Convey("1 + 'a' = '1a'", func() { 54 arg1 := values.NewInt(1) 55 arg2 := values.NewString("a") 56 57 So(operators.Add(arg1, arg2), ShouldEqual, values.NewString("1a")) 58 }) 59 60 Convey("'a' + 1 = 'a1'", func() { 61 arg1 := values.NewString("a") 62 arg2 := values.NewInt(1) 63 64 So(operators.Add(arg1, arg2), ShouldEqual, values.NewString("a1")) 65 }) 66 }) 67 68 Convey("Float & String", func() { 69 Convey("1.1 + 'a' = '1.1a'", func() { 70 arg1 := values.NewFloat(1.1) 71 arg2 := values.NewString("a") 72 73 So(operators.Add(arg1, arg2), ShouldEqual, values.NewString("1.1a")) 74 }) 75 76 Convey("'a' + 1.1 = 'a1.1'", func() { 77 arg1 := values.NewString("a") 78 arg2 := values.NewFloat(1.1) 79 80 So(operators.Add(arg1, arg2), ShouldEqual, values.NewString("a1.1")) 81 }) 82 }) 83 84 Convey("String", func() { 85 Convey("'1' + '2' = '12'", func() { 86 arg1 := values.NewString("1") 87 arg2 := values.NewString("2") 88 89 So(operators.Add(arg1, arg2), ShouldEqual, values.NewString("12")) 90 }) 91 92 Convey("'a' + 'b' = 'ab'", func() { 93 arg1 := values.NewString("a") 94 arg2 := values.NewString("b") 95 96 So(operators.Add(arg1, arg2), ShouldEqual, values.NewString("ab")) 97 }) 98 }) 99 100 Convey("Boolean", func() { 101 Convey("TRUE + TRUE = 2", func() { 102 arg1 := values.True 103 arg2 := values.True 104 105 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(2)) 106 }) 107 108 Convey("TRUE + FALSE = 1", func() { 109 arg1 := values.True 110 arg2 := values.False 111 112 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(1)) 113 }) 114 }) 115 116 Convey("Boolean & Int", func() { 117 Convey("TRUE + 1 = 2", func() { 118 arg1 := values.True 119 arg2 := values.NewInt(1) 120 121 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(2)) 122 }) 123 124 Convey("1 + FALSE = 1", func() { 125 arg1 := values.NewInt(1) 126 arg2 := values.False 127 128 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(1)) 129 }) 130 }) 131 132 Convey("Boolean & Float", func() { 133 Convey("TRUE + 1.2 = 2.2", func() { 134 arg1 := values.True 135 arg2 := values.NewFloat(1.2) 136 137 So(operators.Add(arg1, arg2), ShouldEqual, values.NewFloat(2.2)) 138 }) 139 140 Convey("1.2 + FALSE = 1.2", func() { 141 arg1 := values.NewFloat(1.2) 142 arg2 := values.False 143 144 So(operators.Add(arg1, arg2), ShouldEqual, values.NewFloat(1.2)) 145 }) 146 }) 147 148 Convey("None", func() { 149 Convey("NONE + NONE = 0", func() { 150 arg1 := values.None 151 arg2 := values.None 152 153 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(0)) 154 }) 155 }) 156 157 Convey("None & Int", func() { 158 Convey("NONE + 1 = 1", func() { 159 arg1 := values.None 160 arg2 := values.NewInt(1) 161 162 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(1)) 163 }) 164 165 Convey("1 + NONE = 1", func() { 166 arg1 := values.NewInt(1) 167 arg2 := values.None 168 169 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(1)) 170 }) 171 }) 172 173 Convey("None & Float", func() { 174 Convey("NONE + 1.2 = 1.2", func() { 175 arg1 := values.None 176 arg2 := values.NewFloat(1.2) 177 178 So(operators.Add(arg1, arg2), ShouldEqual, values.NewFloat(1.2)) 179 }) 180 181 Convey("1.2 + NONE = 1.2", func() { 182 arg1 := values.NewFloat(1.2) 183 arg2 := values.None 184 185 So(operators.Add(arg1, arg2), ShouldEqual, values.NewFloat(1.2)) 186 }) 187 }) 188 189 Convey("Array", func() { 190 Convey("[1] + [2] = 3", func() { 191 arg1 := values.NewArrayWith(values.NewInt(1)) 192 arg2 := values.NewArrayWith(values.NewInt(2)) 193 194 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(3)) 195 }) 196 197 Convey("[1] + [1, 1] = 3", func() { 198 arg1 := values.NewArrayWith(values.NewInt(1)) 199 arg2 := values.NewArrayWith(values.NewInt(1), values.NewInt(1)) 200 201 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(3)) 202 }) 203 204 Convey("[1, 2] + [1, 1] = 5", func() { 205 arg1 := values.NewArrayWith(values.NewInt(1), values.NewInt(2)) 206 arg2 := values.NewArrayWith(values.NewInt(1), values.NewInt(1)) 207 208 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(5)) 209 }) 210 }) 211 212 Convey("Datetime", func() { 213 Convey("NOW() + NOW() = now+now", func() { 214 arg1 := values.NewCurrentDateTime() 215 arg2 := values.NewCurrentDateTime() 216 217 expected := arg1.Time.Unix() + arg2.Time.Unix() 218 219 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(int(expected))) 220 }) 221 }) 222 223 Convey("Datetime & Int", func() { 224 Convey("NOW() + 1 = unix+1", func() { 225 arg1 := values.NewCurrentDateTime() 226 arg2 := values.NewArrayWith(values.NewInt(1)) 227 228 expected := arg1.Time.Unix() + 1 229 230 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(int(expected))) 231 }) 232 233 Convey("1 + NOW() = 1+unix", func() { 234 arg1 := values.NewArrayWith(values.NewInt(1)) 235 arg2 := values.NewCurrentDateTime() 236 237 expected := arg2.Time.Unix() + 1 238 239 So(operators.Add(arg1, arg2), ShouldEqual, values.NewInt(int(expected))) 240 }) 241 }) 242 243 Convey("Other types", func() { 244 arg1 := values.NewInt(1) 245 args := []core.Value{ 246 values.NewObject(), 247 values.NewBinary([]byte("1")), 248 } 249 250 for _, argN := range args { 251 argN := argN 252 Convey(argN.Type().String(), func() { 253 So(operators.Add(arg1, argN), ShouldEqual, values.NewInt(1)) 254 }) 255 } 256 }) 257 }) 258 } 259 260 func TestSubtract(t *testing.T) { 261 Convey("Subtract", t, func() { 262 Convey("Integer", func() { 263 Convey("3 - 2 = 1", func() { 264 arg1 := values.NewInt(3) 265 arg2 := values.NewInt(2) 266 267 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewInt(1)) 268 }) 269 }) 270 271 Convey("Float", func() { 272 Convey("3.4 - 2.2 = 1.2", func() { 273 arg1 := values.NewFloat(3.40) 274 arg2 := values.NewFloat(2.20) 275 276 So(operators.Subtract(arg1, arg2), ShouldEqual, arg1-arg2) 277 }) 278 }) 279 280 Convey("Integer & Float", func() { 281 Convey("3.1 - 2 = 1.1", func() { 282 arg1 := values.NewFloat(3.1) 283 arg2 := values.NewInt(2) 284 285 So(operators.Subtract(arg1, arg2), ShouldEqual, 1.1) 286 }) 287 288 Convey("3 - 2.1 = 0.9", func() { 289 arg1 := values.NewInt(3) 290 arg2 := values.NewFloat(2.1) 291 292 So(operators.Subtract(arg1, arg2), ShouldEqual, values.Float(arg1)-arg2) 293 }) 294 }) 295 296 Convey("String", func() { 297 Convey("'a' - 'b' = 0", func() { 298 arg1 := values.NewString("a") 299 arg2 := values.NewString("b") 300 301 out := operators.Subtract(arg1, arg2) 302 303 So(out, ShouldBeZeroValue) 304 }) 305 306 Convey("'1' - 'b' = 1", func() { 307 arg1 := values.NewString("1") 308 arg2 := values.NewString("b") 309 310 out := operators.Subtract(arg1, arg2) 311 312 So(out, ShouldEqual, values.NewInt(1)) 313 }) 314 315 Convey("'a' - '1' = NaN", func() { 316 arg1 := values.NewString("a") 317 arg2 := values.NewString("1") 318 319 out := operators.Subtract(arg1, arg2) 320 321 So(out, ShouldEqual, values.NewInt(-1)) 322 }) 323 324 Convey("'2' - '1' = 1", func() { 325 arg1 := values.NewString("2") 326 arg2 := values.NewString("1") 327 328 out := operators.Subtract(arg1, arg2) 329 330 So(out, ShouldEqual, values.NewInt(1)) 331 }) 332 }) 333 334 Convey("String & Int", func() { 335 Convey("1 - 'b' = 1", func() { 336 arg1 := values.NewInt(1) 337 arg2 := values.NewString("b") 338 339 out := operators.Subtract(arg1, arg2) 340 341 So(out, ShouldEqual, values.NewInt(1)) 342 }) 343 344 Convey("'2' - 1 = 1", func() { 345 arg1 := values.NewString("2") 346 arg2 := values.NewInt(1) 347 348 out := operators.Subtract(arg1, arg2) 349 350 So(out, ShouldEqual, values.NewInt(1)) 351 }) 352 353 Convey("'a' - '1' = -1", func() { 354 arg1 := values.NewString("a") 355 arg2 := values.NewString("1") 356 357 out := operators.Subtract(arg1, arg2) 358 359 So(out, ShouldEqual, values.NewInt(-1)) 360 }) 361 362 Convey("'2' - '1' = 1", func() { 363 arg1 := values.NewString("2") 364 arg2 := values.NewString("1") 365 366 out := operators.Subtract(arg1, arg2) 367 368 So(out, ShouldEqual, values.NewInt(1)) 369 }) 370 }) 371 372 Convey("Boolean", func() { 373 Convey("TRUE - TRUE = 0", func() { 374 arg1 := values.True 375 arg2 := values.True 376 377 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewFloat(0)) 378 }) 379 380 Convey("TRUE - FALSE = 1", func() { 381 arg1 := values.True 382 arg2 := values.False 383 384 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewFloat(1)) 385 }) 386 }) 387 388 Convey("Boolean & Int", func() { 389 Convey("2 - TRUE = 1", func() { 390 arg1 := values.NewInt(2) 391 arg2 := values.True 392 393 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewFloat(1)) 394 }) 395 396 Convey("1 - FALSE = 1", func() { 397 arg1 := values.NewInt(1) 398 arg2 := values.False 399 400 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewFloat(1)) 401 }) 402 }) 403 404 Convey("Boolean & String", func() { 405 Convey("'2' - TRUE = 1", func() { 406 arg1 := values.NewString("2") 407 arg2 := values.True 408 409 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewFloat(1)) 410 }) 411 412 Convey("a - FALSE = 1", func() { 413 arg1 := values.NewString("a") 414 arg2 := values.False 415 416 So(operators.Subtract(arg1, arg2), ShouldBeZeroValue) 417 }) 418 }) 419 420 Convey("Array", func() { 421 Convey("[2] - [1]", func() { 422 arg1 := values.NewArrayWith(values.NewInt(2)) 423 arg2 := values.NewArrayWith(values.NewInt(1)) 424 425 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewInt(1)) 426 }) 427 428 Convey("[2] - [1, 1]", func() { 429 arg1 := values.NewArrayWith(values.NewInt(2)) 430 arg2 := values.NewArrayWith(values.NewInt(1), values.NewInt(1)) 431 432 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewInt(0)) 433 }) 434 }) 435 436 Convey("Array & Int", func() { 437 Convey("2 - [1]", func() { 438 arg1 := values.NewInt(2) 439 arg2 := values.NewArrayWith(values.NewInt(1)) 440 441 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewInt(1)) 442 }) 443 444 Convey("[1, '1'] - 2", func() { 445 arg1 := values.NewArrayWith(values.NewInt(1), values.NewString("1")) 446 arg2 := values.NewInt(2) 447 448 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewInt(0)) 449 }) 450 }) 451 452 Convey("Array & Float", func() { 453 Convey("2 - [1, 1.1]", func() { 454 arg1 := values.NewInt(2) 455 arg2 := values.NewArrayWith(values.NewInt(1), values.NewFloat(1.1)) 456 457 actual := operators.Subtract(arg1, arg2) 458 expected := values.Float(arg1) - (operators.ToNumberOnly(arg2).(values.Float)) 459 460 So(actual, ShouldEqual, expected) 461 }) 462 463 Convey("[1.1, '1.1'] - 2", func() { 464 arg1 := values.NewArrayWith(values.NewFloat(1.1), values.NewString("1.1")) 465 arg2 := values.NewInt(2) 466 467 actual := operators.Subtract(arg1, arg2) 468 expected := (operators.ToNumberOnly(arg1).(values.Float)) - values.Float(arg2) 469 470 So(actual, ShouldEqual, expected) 471 }) 472 }) 473 474 Convey("Array & String", func() { 475 Convey("'2' - ['1']", func() { 476 arg1 := values.NewInt(2) 477 arg2 := values.NewArrayWith(values.NewInt(1)) 478 479 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewInt(1)) 480 }) 481 482 Convey("[1, '1'] - 2", func() { 483 arg1 := values.NewArrayWith(values.NewInt(1), values.NewString("1")) 484 arg2 := values.NewInt(2) 485 486 So(operators.Subtract(arg1, arg2), ShouldEqual, values.NewInt(0)) 487 }) 488 }) 489 490 Convey("Datetime", func() { 491 Convey("NOW() - NOW() = 0", func() { 492 arg1 := values.NewCurrentDateTime() 493 494 So(operators.Subtract(arg1, arg1), ShouldEqual, values.NewInt(0)) 495 }) 496 }) 497 498 Convey("Any", func() { 499 arg1 := values.NewInt(3) 500 args := []core.Value{ 501 values.NewObject(), 502 values.NewBinary([]byte("1")), 503 } 504 505 for _, argN := range args { 506 So(operators.Subtract(arg1, argN), ShouldEqual, values.NewInt(3)) 507 } 508 }) 509 }) 510 } 511 512 func TestMultiply(t *testing.T) { 513 Convey("Multiply", t, func() { 514 Convey("Int", func() { 515 Convey("2 * 2 = 4", func() { 516 arg1 := values.NewInt(2) 517 arg2 := values.NewInt(2) 518 519 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(4)) 520 So(operators.Multiply(arg2, arg1), ShouldEqual, values.NewInt(4)) 521 }) 522 }) 523 524 Convey("Float", func() { 525 Convey("2 * 2.1 = 4.2", func() { 526 arg1 := values.NewFloat(2.0) 527 arg2 := values.NewFloat(2.1) 528 529 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewFloat(4.2)) 530 So(operators.Multiply(arg2, arg1), ShouldEqual, values.NewFloat(4.2)) 531 }) 532 }) 533 534 Convey("Float & Int", func() { 535 Convey("2 * 2.2 = 4.4", func() { 536 arg1 := values.NewInt(2) 537 arg2 := values.NewFloat(2.2) 538 539 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewFloat(4.4)) 540 }) 541 542 Convey("2.2 * 2 = 4.4", func() { 543 arg1 := values.NewFloat(2.2) 544 arg2 := values.NewInt(2) 545 546 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewFloat(4.4)) 547 }) 548 }) 549 550 Convey("Int & String", func() { 551 Convey("1 * 'a' = 0", func() { 552 arg1 := values.NewInt(1) 553 arg2 := values.NewString("a") 554 555 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 556 }) 557 558 Convey("'a' * 1 = 0", func() { 559 arg1 := values.NewString("a") 560 arg2 := values.NewInt(1) 561 562 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 563 }) 564 }) 565 566 Convey("Float & String", func() { 567 Convey("1.1 * 'a' = 0", func() { 568 arg1 := values.NewFloat(1.1) 569 arg2 := values.NewString("a") 570 571 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 572 }) 573 574 Convey("'a' * 1.1 = 0", func() { 575 arg1 := values.NewString("a") 576 arg2 := values.NewFloat(1.1) 577 578 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 579 }) 580 }) 581 582 Convey("String", func() { 583 Convey("'2' * '2' = 4", func() { 584 arg1 := values.NewString("2") 585 arg2 := values.NewString("2") 586 587 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(4)) 588 }) 589 590 Convey("'a' * 'b' = 0", func() { 591 arg1 := values.NewString("a") 592 arg2 := values.NewString("b") 593 594 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 595 }) 596 }) 597 598 Convey("Boolean", func() { 599 Convey("TRUE * TRUE = 1", func() { 600 arg1 := values.True 601 arg2 := values.True 602 603 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(1)) 604 }) 605 606 Convey("TRUE * FALSE = 0", func() { 607 arg1 := values.True 608 arg2 := values.False 609 610 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 611 }) 612 }) 613 614 Convey("Boolean & Int", func() { 615 Convey("TRUE * 2 = 2", func() { 616 arg1 := values.True 617 arg2 := values.NewInt(2) 618 619 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(2)) 620 }) 621 622 Convey("1 * FALSE = 0", func() { 623 arg1 := values.NewInt(1) 624 arg2 := values.False 625 626 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 627 }) 628 }) 629 630 Convey("Boolean & Float", func() { 631 Convey("TRUE * 1.2 = 1.2", func() { 632 arg1 := values.True 633 arg2 := values.NewFloat(1.2) 634 635 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewFloat(1.2)) 636 }) 637 638 Convey("1.2 + FALSE = 0", func() { 639 arg1 := values.NewFloat(1.2) 640 arg2 := values.False 641 642 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 643 }) 644 }) 645 646 Convey("None", func() { 647 Convey("NONE * NONE = 0", func() { 648 arg1 := values.None 649 arg2 := values.None 650 651 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 652 }) 653 }) 654 655 Convey("None & Int", func() { 656 Convey("NONE * 1 = 0", func() { 657 arg1 := values.None 658 arg2 := values.NewInt(1) 659 660 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 661 }) 662 663 Convey("1 * NONE = 0", func() { 664 arg1 := values.NewInt(1) 665 arg2 := values.None 666 667 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(0)) 668 }) 669 }) 670 671 Convey("None & Float", func() { 672 Convey("NONE * 1.2 = 0", func() { 673 arg1 := values.None 674 arg2 := values.NewFloat(1.2) 675 676 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewFloat(0)) 677 }) 678 679 Convey("1.2 + NONE = 0", func() { 680 arg1 := values.NewFloat(1.2) 681 arg2 := values.None 682 683 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewFloat(0)) 684 }) 685 }) 686 687 Convey("Array", func() { 688 Convey("[2] * [2] = 4", func() { 689 arg1 := values.NewArrayWith(values.NewInt(2)) 690 arg2 := values.NewArrayWith(values.NewInt(2)) 691 692 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(4)) 693 }) 694 695 Convey("[2] * [2, 3] = 10", func() { 696 arg1 := values.NewArrayWith(values.NewInt(2)) 697 arg2 := values.NewArrayWith(values.NewInt(2), values.NewInt(3)) 698 699 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(10)) 700 }) 701 702 Convey("[1, 2] * [1, 1] = 6", func() { 703 arg1 := values.NewArrayWith(values.NewInt(1), values.NewInt(2)) 704 arg2 := values.NewArrayWith(values.NewInt(1), values.NewInt(1)) 705 706 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(6)) 707 }) 708 }) 709 710 Convey("Datetime", func() { 711 Convey("NOW() * NOW() = now*now", func() { 712 arg1 := values.NewCurrentDateTime() 713 arg2 := values.NewCurrentDateTime() 714 715 expected := arg1.Time.Unix() * arg2.Time.Unix() 716 717 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(int(expected))) 718 }) 719 }) 720 721 Convey("Datetime & Int", func() { 722 Convey("NOW() * 1 = unix", func() { 723 arg1 := values.NewCurrentDateTime() 724 arg2 := values.NewArrayWith(values.NewInt(1)) 725 726 expected := arg1.Time.Unix() * 1 727 728 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(int(expected))) 729 }) 730 731 Convey("1 * NOW() = unix", func() { 732 arg1 := values.NewArrayWith(values.NewInt(1)) 733 arg2 := values.NewCurrentDateTime() 734 735 expected := arg2.Time.Unix() * 1 736 737 So(operators.Multiply(arg1, arg2), ShouldEqual, values.NewInt(int(expected))) 738 }) 739 }) 740 741 Convey("Other types", func() { 742 arg1 := values.NewInt(1) 743 args := []core.Value{ 744 values.NewObject(), 745 values.NewBinary([]byte("1")), 746 } 747 748 for _, argN := range args { 749 argN := argN 750 Convey(argN.Type().String(), func() { 751 So(operators.Multiply(arg1, argN), ShouldEqual, values.NewInt(0)) 752 }) 753 } 754 }) 755 }) 756 } 757 758 func TestDivide(t *testing.T) { 759 Convey("Divide", t, func() { 760 Convey("Int", func() { 761 Convey("4 / 2 = 2", func() { 762 arg1 := values.NewInt(4) 763 arg2 := values.NewInt(2) 764 765 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(2)) 766 }) 767 768 Convey("5 / 2 = 2.5", func() { 769 arg1 := values.NewInt(5) 770 arg2 := values.NewInt(2) 771 772 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewFloat(2.5)) 773 }) 774 }) 775 776 Convey("Float", func() { 777 Convey("4.4 / 2.2 = 2", func() { 778 arg1 := values.NewFloat(4.4) 779 arg2 := values.NewFloat(2.2) 780 781 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewFloat(2)) 782 }) 783 }) 784 785 Convey("Float & Int", func() { 786 Convey("5 / 2.5 = 2", func() { 787 arg1 := values.NewInt(5) 788 arg2 := values.NewFloat(2.5) 789 790 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewFloat(2)) 791 }) 792 793 Convey("4.4 / 2 = 2.2", func() { 794 arg1 := values.NewFloat(4.4) 795 arg2 := values.NewInt(2) 796 797 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewFloat(2.2)) 798 }) 799 }) 800 801 Convey("Int & String", func() { 802 Convey("1 / 'a' = PANIC", func() { 803 arg1 := values.NewInt(1) 804 arg2 := values.NewString("a") 805 806 So(func() { 807 operators.Divide(arg1, arg2) 808 }, ShouldPanic) 809 }) 810 811 Convey("'a' / 1 = 0", func() { 812 arg1 := values.NewString("a") 813 arg2 := values.NewInt(1) 814 815 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(0)) 816 }) 817 }) 818 819 Convey("Float & String", func() { 820 Convey("1.1 / 'a' = PANIC", func() { 821 arg1 := values.NewFloat(1.1) 822 arg2 := values.NewString("a") 823 824 So(func() { 825 operators.Divide(arg1, arg2) 826 }, ShouldPanic) 827 }) 828 829 Convey("'a' / 1.1 = 0", func() { 830 arg1 := values.NewString("a") 831 arg2 := values.NewFloat(1.1) 832 833 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(0)) 834 }) 835 }) 836 837 Convey("String", func() { 838 Convey("'2' / '2' = 1", func() { 839 arg1 := values.NewString("2") 840 arg2 := values.NewString("2") 841 842 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(1)) 843 }) 844 845 Convey("'a' / 'b' = PANIC", func() { 846 arg1 := values.NewString("a") 847 arg2 := values.NewString("b") 848 849 So(func() { 850 operators.Divide(arg1, arg2) 851 }, ShouldPanic) 852 }) 853 }) 854 855 Convey("Boolean", func() { 856 Convey("TRUE / TRUE = 1", func() { 857 arg1 := values.True 858 arg2 := values.True 859 860 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(1)) 861 }) 862 863 Convey("TRUE / FALSE = PANIC", func() { 864 arg1 := values.True 865 arg2 := values.False 866 867 So(func() { 868 operators.Divide(arg1, arg2) 869 }, ShouldPanic) 870 }) 871 }) 872 873 Convey("Boolean & Int", func() { 874 Convey("TRUE / 2 = 0.5", func() { 875 arg1 := values.True 876 arg2 := values.NewInt(2) 877 878 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewFloat(0.5)) 879 }) 880 881 Convey("1 / FALSE = PANIC", func() { 882 arg1 := values.NewInt(1) 883 arg2 := values.False 884 885 So(func() { 886 operators.Divide(arg1, arg2) 887 }, ShouldPanic) 888 }) 889 }) 890 891 Convey("Boolean & Float", func() { 892 Convey("TRUE / 0.5 = 2", func() { 893 arg1 := values.True 894 arg2 := values.NewFloat(0.5) 895 896 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewFloat(2)) 897 }) 898 899 Convey("1.2 / FALSE = PANIC", func() { 900 arg1 := values.NewFloat(1.2) 901 arg2 := values.False 902 903 So(func() { 904 operators.Divide(arg1, arg2) 905 }, ShouldPanic) 906 }) 907 }) 908 909 Convey("None", func() { 910 Convey("NONE / NONE = PANIC", func() { 911 arg1 := values.None 912 arg2 := values.None 913 914 So(func() { 915 operators.Divide(arg1, arg2) 916 }, ShouldPanic) 917 }) 918 }) 919 920 Convey("None & Int", func() { 921 Convey("NONE / 1 = 0", func() { 922 arg1 := values.None 923 arg2 := values.NewInt(1) 924 925 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(0)) 926 }) 927 928 Convey("1 / NONE = PANIC", func() { 929 arg1 := values.NewInt(1) 930 arg2 := values.None 931 932 So(func() { 933 operators.Divide(arg1, arg2) 934 }, ShouldPanic) 935 }) 936 }) 937 938 Convey("None & Float", func() { 939 Convey("NONE / 1.2 = 0", func() { 940 arg1 := values.None 941 arg2 := values.NewFloat(1.2) 942 943 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewFloat(0)) 944 }) 945 946 Convey("1.2 / NONE = PANIC", func() { 947 arg1 := values.NewFloat(1.2) 948 arg2 := values.None 949 950 So(func() { 951 operators.Divide(arg1, arg2) 952 }, ShouldPanic) 953 }) 954 }) 955 956 Convey("Array", func() { 957 Convey("[2] / [2] = 1", func() { 958 arg1 := values.NewArrayWith(values.NewInt(2)) 959 arg2 := values.NewArrayWith(values.NewInt(2)) 960 961 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(1)) 962 }) 963 964 Convey("[4, 2] / [2] = 3", func() { 965 arg1 := values.NewArrayWith(values.NewInt(4), values.NewInt(2)) 966 arg2 := values.NewArrayWith(values.NewInt(2)) 967 968 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(3)) 969 }) 970 971 Convey("[4, 2] / [1, 1] = 3", func() { 972 arg1 := values.NewArrayWith(values.NewInt(4), values.NewInt(2)) 973 arg2 := values.NewArrayWith(values.NewInt(1), values.NewInt(1)) 974 975 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(3)) 976 }) 977 }) 978 979 Convey("Datetime", func() { 980 Convey("NOW() / NOW() = 1", func() { 981 arg1 := values.NewCurrentDateTime() 982 arg2 := values.NewCurrentDateTime() 983 984 expected := arg1.Time.Unix() / arg2.Time.Unix() 985 986 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(int(expected))) 987 }) 988 }) 989 990 Convey("Datetime & Int", func() { 991 Convey("NOW() / 1 = unix", func() { 992 arg1 := values.NewCurrentDateTime() 993 arg2 := values.NewArrayWith(values.NewInt(1)) 994 995 expected := arg1.Time.Unix() / 1 996 997 So(operators.Divide(arg1, arg2), ShouldEqual, values.NewInt(int(expected))) 998 }) 999 1000 SkipConvey("1 / NOW() = unix", func() { 1001 arg1 := values.NewArrayWith(values.NewInt(1)) 1002 arg2 := values.NewCurrentDateTime() 1003 1004 expected := arg2.Time.Unix() / 1 1005 1006 So(operators.Divide(arg1, arg2), ShouldEqual, expected) 1007 }) 1008 }) 1009 1010 Convey("Other types", func() { 1011 arg1 := values.NewInt(1) 1012 args := []core.Value{ 1013 values.NewObject(), 1014 values.NewBinary([]byte("1")), 1015 } 1016 1017 for _, argN := range args { 1018 argN := argN 1019 Convey(argN.Type().String(), func() { 1020 So(func() { 1021 operators.Divide(arg1, argN) 1022 }, ShouldPanic) 1023 }) 1024 } 1025 }) 1026 }) 1027 } 1028 1029 func TestPositive(t *testing.T) { 1030 Convey("converts value to positive", t, func() { 1031 Convey("when int", func() { 1032 Convey("postive", func() { 1033 So(operators.Positive(values.NewInt(1), nil), ShouldEqual, 1) 1034 }) 1035 Convey("negative", func() { 1036 So(operators.Positive(values.NewInt(-1), nil), ShouldEqual, -1) 1037 }) 1038 }) 1039 1040 Convey("when float", func() { 1041 Convey("postive", func() { 1042 So(operators.Positive(values.NewFloat(1.0), nil), ShouldEqual, 1) 1043 }) 1044 Convey("negative", func() { 1045 So(operators.Positive(values.NewFloat(-1.0), nil), ShouldEqual, -1.0) 1046 }) 1047 }) 1048 1049 Convey("non numeric", func() { 1050 Convey("string", func() { 1051 So(operators.Positive(values.String("1"), nil), ShouldEqual, "1") 1052 }) 1053 }) 1054 }) 1055 } 1056 1057 func TestNegative(t *testing.T) { 1058 Convey("converts value to negative", t, func() { 1059 Convey("when int", func() { 1060 Convey("postive", func() { 1061 So(operators.Negative(values.NewInt(1), nil), ShouldEqual, -1) 1062 }) 1063 Convey("negative", func() { 1064 So(operators.Negative(values.NewInt(-1), nil), ShouldEqual, 1) 1065 }) 1066 }) 1067 1068 Convey("when float", func() { 1069 Convey("postive", func() { 1070 So(operators.Negative(values.NewFloat(1.0), nil), ShouldEqual, -1.0) 1071 }) 1072 Convey("negative", func() { 1073 So(operators.Negative(values.NewFloat(-1.0), nil), ShouldEqual, 1.0) 1074 }) 1075 }) 1076 1077 Convey("non numeric", func() { 1078 Convey("string", func() { 1079 So(operators.Negative(values.String("1"), nil), ShouldEqual, "1") 1080 }) 1081 }) 1082 }) 1083 } 1084 1085 func TestDecrement(t *testing.T) { 1086 Convey("decrement values", t, func() { 1087 Convey("decrement int", func() { 1088 Convey("positives", func() { 1089 So(operators.Decrement(values.NewInt(1), nil), ShouldEqual, 0) 1090 }) 1091 Convey("negatives", func() { 1092 So(operators.Decrement(values.NewInt(-1), nil), ShouldEqual, -2) 1093 }) 1094 }) 1095 1096 Convey("decrement float", func() { 1097 Convey("positives", func() { 1098 So(operators.Decrement(values.NewFloat(1.1), nil), ShouldEqual, 0.10000000000000009) 1099 }) 1100 Convey("negatives", func() { 1101 So(operators.Decrement(values.NewFloat(-1.1), nil), ShouldEqual, -2.1) 1102 }) 1103 }) 1104 1105 Convey("other values", func() { 1106 So(operators.Decrement(values.None, nil), ShouldEqual, -1) 1107 }) 1108 }) 1109 } 1110 1111 func TestIncrement(t *testing.T) { 1112 Convey("increment values", t, func() { 1113 Convey("increment int", func() { 1114 Convey("positives", func() { 1115 So(operators.Increment(values.NewInt(1), nil), ShouldEqual, 2) 1116 }) 1117 Convey("negatives", func() { 1118 So(operators.Increment(values.NewInt(-1), nil), ShouldEqual, 0) 1119 }) 1120 }) 1121 1122 Convey("Increment float", func() { 1123 Convey("positives", func() { 1124 So(operators.Increment(values.NewFloat(1.1), nil), ShouldEqual, 2.1) 1125 }) 1126 Convey("negatives", func() { 1127 So(operators.Increment(values.NewFloat(-1.1), nil), ShouldEqual, -0.10000000000000009) 1128 }) 1129 }) 1130 1131 Convey("other values", func() { 1132 So(operators.Increment(values.None, nil), ShouldEqual, 1) 1133 }) 1134 }) 1135 } 1136 1137 func TestEqual(t *testing.T) { 1138 Convey("Equality in values", t, func() { 1139 Convey("1 == 1", func() { 1140 So(operators.Equal(values.NewInt(1), values.NewInt(1)), ShouldEqual, values.True) 1141 }) 1142 1143 Convey("1 != 2", func() { 1144 So(operators.Equal(values.NewInt(1), values.NewInt(2)), ShouldEqual, values.False) 1145 }) 1146 1147 Convey("'hello' == 'hello", func() { 1148 So(operators.Equal(values.String("hello"), values.String("hello")), ShouldEqual, values.True) 1149 }) 1150 1151 Convey("'foo' != 'bar", func() { 1152 So(operators.Equal(values.String("foo"), values.String("bar")), ShouldEqual, values.False) 1153 }) 1154 }) 1155 } 1156 1157 func TestNotEqual(t *testing.T) { 1158 Convey("Inequality in values", t, func() { 1159 Convey("1 == 1", func() { 1160 So(operators.NotEqual(values.NewInt(1), values.NewInt(1)), ShouldEqual, values.False) 1161 }) 1162 1163 Convey("1 != 2", func() { 1164 So(operators.NotEqual(values.NewInt(1), values.NewInt(2)), ShouldEqual, values.True) 1165 }) 1166 1167 Convey("'hello' == 'hello", func() { 1168 So(operators.NotEqual(values.String("hello"), values.String("hello")), ShouldEqual, values.False) 1169 }) 1170 1171 Convey("'foo' != 'bar", func() { 1172 So(operators.NotEqual(values.String("foo"), values.String("bar")), ShouldEqual, values.True) 1173 }) 1174 }) 1175 } 1176 1177 func TestModulus(t *testing.T) { 1178 Convey("Modulus", t, func() { 1179 Convey("5 % 2", func() { 1180 So(operators.Modulus(values.NewInt(5), values.NewInt(2)), ShouldEqual, 1) 1181 }) 1182 1183 Convey("5.0 % 2", func() { 1184 So(operators.Modulus(values.NewFloat(5.0), values.NewInt(2)), ShouldEqual, 1) 1185 }) 1186 1187 Convey("5 % 2.0", func() { 1188 So(operators.Modulus(values.NewInt(5), values.NewFloat(2.0)), ShouldEqual, 1) 1189 }) 1190 1191 Convey("5.1 % 3.2", func() { 1192 So(operators.Modulus(values.NewFloat(5.1), values.NewFloat(3.2)), ShouldEqual, 2) 1193 }) 1194 1195 Convey("Non int or float", func() { 1196 So(operators.Modulus(values.String("foo"), values.NewFloat(3.2)), ShouldEqual, 0) 1197 }) 1198 }) 1199 } 1200 1201 func TestGreater(t *testing.T) { 1202 Convey("Greater than value", t, func() { 1203 Convey("5 > 2", func() { 1204 So(operators.Greater(values.NewInt(5), values.NewInt(2)), ShouldEqual, values.True) 1205 }) 1206 1207 Convey("2 > 5", func() { 1208 So(operators.Greater(values.NewInt(2), values.NewInt(5)), ShouldEqual, values.False) 1209 }) 1210 }) 1211 } 1212 1213 func TestGreaterOrEqual(t *testing.T) { 1214 Convey("Greater or equal than value", t, func() { 1215 Convey("5 >= 5", func() { 1216 So(operators.GreaterOrEqual(values.NewInt(5), values.NewInt(5)), ShouldEqual, values.True) 1217 }) 1218 1219 Convey("2 >= 5", func() { 1220 So(operators.GreaterOrEqual(values.NewInt(2), values.NewInt(5)), ShouldEqual, values.False) 1221 }) 1222 }) 1223 } 1224 1225 func TestLess(t *testing.T) { 1226 Convey("Less than value", t, func() { 1227 Convey("1 < 5", func() { 1228 So(operators.Less(values.NewInt(1), values.NewInt(5)), ShouldEqual, values.True) 1229 }) 1230 Convey("5 < 2", func() { 1231 So(operators.Less(values.NewInt(5), values.NewInt(2)), ShouldEqual, values.False) 1232 }) 1233 }) 1234 } 1235 1236 func TestLessOrEqual(t *testing.T) { 1237 Convey("Less than value", t, func() { 1238 Convey("1 < 5", func() { 1239 So(operators.LessOrEqual(values.NewInt(1), values.NewInt(5)), ShouldEqual, values.True) 1240 }) 1241 Convey("5 < 2", func() { 1242 So(operators.LessOrEqual(values.NewInt(5), values.NewInt(2)), ShouldEqual, values.False) 1243 }) 1244 Convey("5 <= 5", func() { 1245 So(operators.LessOrEqual(values.NewInt(5), values.NewInt(5)), ShouldEqual, values.True) 1246 }) 1247 }) 1248 } 1249 1250 func TestNot(t *testing.T) { 1251 Convey("Invert truthiness", t, func() { 1252 Convey("true turns false", func() { 1253 So(operators.Not(values.NewBoolean(true), nil), ShouldEqual, values.False) 1254 }) 1255 Convey("false turns true", func() { 1256 So(operators.Not(values.NewBoolean(false), nil), ShouldEqual, values.True) 1257 }) 1258 Convey("'' turns true", func() { 1259 So(operators.Not(values.NewString(""), nil), ShouldEqual, values.True) 1260 }) 1261 Convey("'foo' turns false", func() { 1262 So(operators.Not(values.NewString("foo"), nil), ShouldEqual, values.False) 1263 }) 1264 Convey("1 turns false", func() { 1265 So(operators.Not(values.NewInt(1), nil), ShouldEqual, values.False) 1266 }) 1267 Convey("0 turns true", func() { 1268 So(operators.Not(values.NewInt(0), nil), ShouldEqual, values.True) 1269 }) 1270 Convey("1.0 turns false", func() { 1271 So(operators.Not(values.NewFloat(1), nil), ShouldEqual, values.False) 1272 }) 1273 Convey("0.0 turns true", func() { 1274 So(operators.Not(values.NewFloat(0.0), nil), ShouldEqual, values.True) 1275 }) 1276 Convey("current turns false", func() { 1277 So(operators.Not(values.NewDateTime(values.NewCurrentDateTime().Time), nil), ShouldEqual, values.False) 1278 }) 1279 Convey("zerotime turns true", func() { 1280 So(operators.Not(values.NewDateTime(values.ZeroDateTime.Time), nil), ShouldEqual, values.True) 1281 }) 1282 }) 1283 } 1284 1285 func TestToNumberOrString(t *testing.T) { 1286 Convey("Invert to numeric value", t, func() { 1287 Convey("0 turns 0", func() { 1288 So(operators.ToNumberOrString(values.NewInt(0)), ShouldEqual, 0) 1289 }) 1290 Convey("1 turns 1", func() { 1291 So(operators.ToNumberOrString(values.NewInt(1)), ShouldEqual, 1) 1292 }) 1293 Convey("-1 turns -1", func() { 1294 So(operators.ToNumberOrString(values.NewInt(-1)), ShouldEqual, -1) 1295 }) 1296 Convey("0.0 turns 0.0", func() { 1297 So(operators.ToNumberOrString(values.NewFloat(0.0)), ShouldEqual, 0.0) 1298 }) 1299 Convey("-1.0 turns -1.0", func() { 1300 So(operators.ToNumberOrString(values.NewFloat(-1.0)), ShouldEqual, -1.0) 1301 }) 1302 Convey("1.0 turns 1.0", func() { 1303 So(operators.ToNumberOrString(values.NewFloat(1.0)), ShouldEqual, 1.0) 1304 }) 1305 Convey("string type 0 turns string type 0", func() { 1306 So(operators.ToNumberOrString(values.NewString("0")), ShouldEqual, "0") 1307 }) 1308 Convey("string type 1 turns string type 1", func() { 1309 So(operators.ToNumberOrString(values.NewString("1")), ShouldEqual, "1") 1310 }) 1311 Convey("string type -1 turns string type -1", func() { 1312 So(operators.ToNumberOrString(values.NewString("-1")), ShouldEqual, "-1") 1313 }) 1314 Convey("string type 0.0 turns string type 0.0", func() { 1315 So(operators.ToNumberOrString(values.NewString("0.0")), ShouldEqual, "0.0") 1316 }) 1317 Convey("a turns a", func() { 1318 So(operators.ToNumberOrString(values.NewString("a")), ShouldEqual, "a") 1319 }) 1320 Convey("true turns 1", func() { 1321 So(operators.ToNumberOrString(values.NewBoolean(true)), ShouldEqual, 1) 1322 }) 1323 Convey("false turns 0", func() { 1324 So(operators.ToNumberOrString(values.NewBoolean(false)), ShouldEqual, 0) 1325 }) 1326 Convey("current DateTime turns unix", func() { 1327 So(operators.ToNumberOrString(values.NewDateTime(time.Now())), ShouldEqual, time.Now().Unix()) 1328 }) 1329 Convey("zero value DateTime turns 0", func() { 1330 var zero time.Time 1331 So(operators.ToNumberOrString(values.NewDateTime(zero)), ShouldEqual, 0) 1332 }) 1333 Convey("[1] turns 1", func() { 1334 arg := values.NewArrayWith(values.NewInt(1)) 1335 So(operators.ToNumberOrString(arg), ShouldEqual, 1) 1336 }) 1337 Convey("[1, 2] turns 3", func() { 1338 arg := values.NewArrayWith(values.NewInt(1), values.NewInt(2)) 1339 So(operators.ToNumberOrString(arg), ShouldEqual, 3) 1340 }) 1341 Convey("[a] turns 0", func() { 1342 arg := values.NewArrayWith(values.NewString("a")) 1343 So(operators.ToNumberOrString(arg), ShouldEqual, 0) 1344 }) 1345 Convey("[true] turns 1", func() { 1346 arg := values.NewArrayWith(values.NewBoolean(true)) 1347 So(operators.ToNumberOrString(arg), ShouldEqual, 1) 1348 }) 1349 Convey("[false] turns 0", func() { 1350 arg := values.NewArrayWith(values.NewBoolean(false)) 1351 So(operators.ToNumberOrString(arg), ShouldEqual, 0) 1352 }) 1353 }) 1354 } 1355 1356 func TestToNumberOnly(t *testing.T) { 1357 Convey("Invert to int", t, func() { 1358 Convey("0 turns 0", func() { 1359 So(operators.ToNumberOnly(values.NewInt(0)), ShouldEqual, 0) 1360 }) 1361 Convey("1 turns 1", func() { 1362 So(operators.ToNumberOnly(values.NewInt(1)), ShouldEqual, 1) 1363 }) 1364 Convey("-1 turns -1", func() { 1365 So(operators.ToNumberOnly(values.NewInt(-1)), ShouldEqual, -1) 1366 }) 1367 Convey("0.0 turns 0.0", func() { 1368 So(operators.ToNumberOnly(values.NewFloat(0.0)), ShouldEqual, 0.0) 1369 }) 1370 Convey("-1.0 turns -1.0", func() { 1371 So(operators.ToNumberOnly(values.NewFloat(-1.0)), ShouldEqual, -1.0) 1372 }) 1373 Convey("1.0 turns 1.0", func() { 1374 So(operators.ToNumberOnly(values.NewFloat(1.0)), ShouldEqual, 1.0) 1375 }) 1376 Convey("string type 0 turns 0", func() { 1377 So(operators.ToNumberOnly(values.NewString("0")), ShouldEqual, 0) 1378 }) 1379 Convey("string type 1 turns 1", func() { 1380 So(operators.ToNumberOnly(values.NewString("1")), ShouldEqual, 1) 1381 }) 1382 Convey("string type -1 turns -1", func() { 1383 So(operators.ToNumberOnly(values.NewString("-1")), ShouldEqual, -1) 1384 }) 1385 Convey("string type 0.0 turns 0.0", func() { 1386 So(operators.ToNumberOnly(values.NewString("0.0")), ShouldEqual, 0.0) 1387 }) 1388 1389 Convey("a turns 0", func() { 1390 So(operators.ToNumberOnly(values.NewString("a")), ShouldEqual, 0) 1391 }) 1392 Convey("true turns 1", func() { 1393 So(operators.ToNumberOnly(values.NewBoolean(true)), ShouldEqual, 1) 1394 }) 1395 Convey("false turns 0", func() { 1396 So(operators.ToNumberOnly(values.NewBoolean(false)), ShouldEqual, 0) 1397 }) 1398 Convey("current DateTime turns unix", func() { 1399 So(operators.ToNumberOnly(values.NewDateTime(time.Now())), ShouldEqual, time.Now().Unix()) 1400 }) 1401 Convey("zero value DateTime turns 0", func() { 1402 var zero time.Time 1403 So(operators.ToNumberOnly(values.NewDateTime(zero)), ShouldEqual, 0) 1404 }) 1405 Convey("[1] turns 1", func() { 1406 arg := values.NewArrayWith(values.NewInt(1)) 1407 So(operators.ToNumberOnly(arg), ShouldEqual, 1) 1408 }) 1409 Convey("[1, 2] turns 3", func() { 1410 arg := values.NewArrayWith(values.NewInt(1), values.NewInt(2)) 1411 So(operators.ToNumberOnly(arg), ShouldEqual, 3) 1412 }) 1413 Convey("[a] turns 0", func() { 1414 arg := values.NewArrayWith(values.NewString("a")) 1415 So(operators.ToNumberOnly(arg), ShouldEqual, 0) 1416 }) 1417 Convey("[true] turns 1", func() { 1418 arg := values.NewArrayWith(values.NewBoolean(true)) 1419 So(operators.ToNumberOnly(arg), ShouldEqual, 1) 1420 }) 1421 Convey("[false] turns 0", func() { 1422 arg := values.NewArrayWith(values.NewBoolean(false)) 1423 So(operators.ToNumberOnly(arg), ShouldEqual, 0) 1424 }) 1425 }) 1426 }