|
| 1 | +package interpreter_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/formancehq/numscript/internal/interpreter" |
| 9 | +) |
| 10 | + |
| 11 | +func TestBetween0And1Inclusive(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + in string |
| 14 | + want *big.Rat |
| 15 | + wantErr bool |
| 16 | + }{ |
| 17 | + { |
| 18 | + in: "0%", |
| 19 | + want: big.NewRat(0, 1), |
| 20 | + }, |
| 21 | + { |
| 22 | + in: "0.0%", |
| 23 | + want: big.NewRat(0, 1), |
| 24 | + }, |
| 25 | + { |
| 26 | + in: "0/1", |
| 27 | + want: big.NewRat(0, 1), |
| 28 | + }, |
| 29 | + { |
| 30 | + in: "0/25", |
| 31 | + want: big.NewRat(0, 1), |
| 32 | + }, |
| 33 | + { |
| 34 | + in: "0/100", |
| 35 | + want: big.NewRat(0, 1), |
| 36 | + }, |
| 37 | + { |
| 38 | + in: "1%", |
| 39 | + want: big.NewRat(1, 100), |
| 40 | + }, |
| 41 | + { |
| 42 | + in: "1/100", |
| 43 | + want: big.NewRat(1, 100), |
| 44 | + }, |
| 45 | + { |
| 46 | + in: "10/1000", |
| 47 | + want: big.NewRat(1, 100), |
| 48 | + }, |
| 49 | + { |
| 50 | + in: "50/100", |
| 51 | + want: big.NewRat(50, 100), |
| 52 | + }, |
| 53 | + { |
| 54 | + in: "50%", |
| 55 | + want: big.NewRat(50, 100), |
| 56 | + }, |
| 57 | + { |
| 58 | + in: "50.0%", |
| 59 | + want: big.NewRat(50, 100), |
| 60 | + }, |
| 61 | + { |
| 62 | + in: "1/1", |
| 63 | + want: big.NewRat(1, 1), |
| 64 | + }, |
| 65 | + { |
| 66 | + in: "100/100", |
| 67 | + want: big.NewRat(1, 1), |
| 68 | + }, |
| 69 | + { |
| 70 | + in: "100.0%", |
| 71 | + want: big.NewRat(1, 1), |
| 72 | + }, |
| 73 | + { |
| 74 | + in: "100%", |
| 75 | + want: big.NewRat(1, 1), |
| 76 | + }, |
| 77 | + // Now for the failures. We don't check negative numbers in this test because |
| 78 | + // those are a parsing failure, not a range failure. |
| 79 | + { |
| 80 | + in: "100.1%", |
| 81 | + wantErr: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + in: "101%", |
| 85 | + wantErr: true, |
| 86 | + }, |
| 87 | + { |
| 88 | + in: "101/100", |
| 89 | + wantErr: true, |
| 90 | + }, |
| 91 | + { |
| 92 | + in: "2/1", |
| 93 | + wantErr: true, |
| 94 | + }, |
| 95 | + { |
| 96 | + in: "3/2", |
| 97 | + wantErr: true, |
| 98 | + }, |
| 99 | + } |
| 100 | + |
| 101 | + for _, test := range tests { |
| 102 | + t.Run(test.in, func(t *testing.T) { |
| 103 | + got, err := interpreter.ParsePortionSpecific(test.in) |
| 104 | + if test.wantErr { |
| 105 | + if err == nil { |
| 106 | + t.Fatal("should have errored") |
| 107 | + } |
| 108 | + if !strings.Contains(err.Error(), "between") { |
| 109 | + t.Fatal("wrong error") |
| 110 | + } |
| 111 | + return |
| 112 | + } |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("ParsePortionSpecific(%q): %v", test.in, err) |
| 115 | + } |
| 116 | + if test.want.Cmp(got) != 0 { |
| 117 | + t.Fatalf("ParsePortionSpecific(%q) = %q, want %q", test.in, got, test.want) |
| 118 | + } |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestInvalidFormat(t *testing.T) { |
| 124 | + _, err := interpreter.ParsePortionSpecific("this is not a portion") |
| 125 | + if err == nil { |
| 126 | + t.Fatal("should have errored") |
| 127 | + } |
| 128 | + if !strings.Contains(err.Error(), "format") { |
| 129 | + t.Fatal("wrong error") |
| 130 | + } |
| 131 | +} |
0 commit comments