github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/inject/inject_test.go (about) 1 package inject_test 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/test/count" 7 "github.com/lmorg/murex/utils/inject" 8 ) 9 10 type TestType struct { 11 Input string 12 Inject string 13 Pos int 14 Expected string 15 Error bool 16 } 17 18 var tests = []TestType{ 19 { 20 Input: "", 21 Inject: "_", 22 Pos: 0, 23 Expected: "_", 24 Error: false, 25 }, 26 { 27 Input: "", 28 Inject: "_", 29 Pos: 1, 30 Expected: "", 31 Error: true, 32 }, 33 { 34 Input: "1", 35 Inject: "_", 36 Pos: 0, 37 Expected: "_1", 38 Error: false, 39 }, 40 { 41 Input: "1", 42 Inject: "_", 43 Pos: 1, 44 Expected: "1_", 45 Error: false, 46 }, 47 { 48 Input: "foobar", 49 Inject: "_", 50 Pos: -1, 51 Expected: "", 52 Error: true, 53 }, 54 { 55 Input: "foobar", 56 Inject: "_", 57 Pos: 0, 58 Expected: "_foobar", 59 Error: false, 60 }, 61 { 62 Input: "foobar", 63 Inject: "_", 64 Pos: 1, 65 Expected: "f_oobar", 66 Error: false, 67 }, 68 { 69 Input: "foobar", 70 Inject: "_", 71 Pos: 2, 72 Expected: "fo_obar", 73 Error: false, 74 }, 75 { 76 Input: "foobar", 77 Inject: "_", 78 Pos: 3, 79 Expected: "foo_bar", 80 Error: false, 81 }, 82 { 83 Input: "foobar", 84 Inject: "_", 85 Pos: 4, 86 Expected: "foob_ar", 87 Error: false, 88 }, 89 { 90 Input: "foobar", 91 Inject: "_", 92 Pos: 5, 93 Expected: "fooba_r", 94 Error: false, 95 }, 96 { 97 Input: "foobar", 98 Inject: "_", 99 Pos: 6, 100 Expected: "foobar_", 101 Error: false, 102 }, 103 { 104 Input: "foobar", 105 Inject: "_", 106 Pos: 7, 107 Expected: "", 108 Error: true, 109 }, 110 } 111 112 func TestString(t *testing.T) { 113 count.Tests(t, len(tests)) 114 115 for i, test := range tests { 116 actual, err := inject.String(test.Input, test.Inject, test.Pos) 117 118 if (err == nil) == test.Error { 119 t.Errorf("%s failed:", t.Name()) 120 t.Logf(" Test #: %d", i) 121 t.Logf(" Input: '%s'", test.Input) 122 t.Logf(" Inject: '%s'", test.Inject) 123 t.Logf(" Pos: %d", test.Pos) 124 t.Logf(" Expected: '%s'", test.Expected) 125 t.Logf(" Error: '%T'", test.Error) 126 t.Logf(" Actual: '%s'", actual) 127 t.Logf(" Err Msg: '%s'", err.Error()) 128 continue 129 } 130 131 if actual != test.Expected { 132 t.Errorf("%s failed:", t.Name()) 133 t.Logf(" Test #: %d", i) 134 t.Logf(" Input: '%s'", test.Input) 135 t.Logf(" Inject: '%s'", test.Inject) 136 t.Logf(" Pos: %d", test.Pos) 137 t.Logf(" Expected: '%s'", test.Expected) 138 t.Logf(" Error: '%T'", test.Error) 139 t.Logf(" Actual: '%s'", actual) 140 t.Logf(" Err Msg: '%s'", err.Error()) 141 } 142 } 143 } 144 145 func TestRune(t *testing.T) { 146 count.Tests(t, len(tests)) 147 148 for i, test := range tests { 149 r, err := inject.Rune([]rune(test.Input), []rune(test.Inject), test.Pos) 150 actual := string(r) 151 152 if (err == nil) == test.Error { 153 t.Errorf("%s failed:", t.Name()) 154 t.Logf(" Test #: %d", i) 155 t.Logf(" Input: '%s'", test.Input) 156 t.Logf(" Inject: '%s'", test.Inject) 157 t.Logf(" Pos: %d", test.Pos) 158 t.Logf(" Expected: '%s'", test.Expected) 159 t.Logf(" Error: '%T'", test.Error) 160 t.Logf(" Actual: '%s'", actual) 161 t.Logf(" Err Msg: '%s'", err.Error()) 162 continue 163 } 164 165 if actual != test.Expected { 166 t.Errorf("%s failed:", t.Name()) 167 t.Logf(" Test #: %d", i) 168 t.Logf(" Input: '%s'", test.Input) 169 t.Logf(" Inject: '%s'", test.Inject) 170 t.Logf(" Pos: %d", test.Pos) 171 t.Logf(" Expected: '%s'", test.Expected) 172 t.Logf(" Error: '%T'", test.Error) 173 t.Logf(" Actual: '%s'", actual) 174 t.Logf(" Err Msg: '%s'", err.Error()) 175 } 176 } 177 }