test.lua (21400B)
1 local tester = require "util.tester" 2 3 package.path = "../?.lua;" .. package.path 4 5 local lume = require "lume" 6 7 local tests = {} 8 local testeq = tester.test.equal 9 10 -- lume.clamp 11 tests["lume.clamp"] = function() 12 testeq( lume.clamp(8, 5, 10), 8 ) 13 testeq( lume.clamp(12, 5, 10), 10 ) 14 testeq( lume.clamp(-1, 5, 10), 5 ) 15 testeq( lume.clamp(-1, -10, 10), -1 ) 16 testeq( lume.clamp(-100, -10, 10), -10 ) 17 testeq( lume.clamp(13, 8, 8), 8 ) 18 testeq( lume.clamp(3, 8, 8), 8 ) 19 end 20 21 -- lume.round 22 tests["lume.round"] = function() 23 testeq( lume.round(.5), 1 ) 24 testeq( lume.round(-.5), -1 ) 25 testeq( lume.round(2.4), 2 ) 26 testeq( lume.round(123, 10), 120 ) 27 testeq( lume.round(129, 64), 128 ) 28 testeq( lume.round(-123.45, .1), -123.5 ) 29 testeq( lume.round(0), 0 ) 30 end 31 32 -- lume.sign 33 tests["lume.sign"] = function() 34 testeq( lume.sign(-10), -1 ) 35 testeq( lume.sign(10), 1 ) 36 testeq( lume.sign(0), 1 ) 37 end 38 39 -- lume.lerp 40 tests["lume.lerp"] = function() 41 testeq( lume.lerp(100, 200, .5), 150 ) 42 testeq( lume.lerp(100, 200, .25), 125 ) 43 testeq( lume.lerp(100, 200, 2), 200 ) 44 testeq( lume.lerp(100, 200, -2), 100 ) 45 end 46 47 -- lume.smooth 48 tests["lume.smooth"] = function() 49 testeq( lume.smooth(100, 200, .5), 150 ) 50 testeq( lume.smooth(100, 200, 0), 100 ) 51 testeq( lume.smooth(100, 200, 1), 200 ) 52 testeq( lume.smooth(100, 200, 2), 200 ) 53 testeq( lume.smooth(100, 200, -2), 100 ) 54 end 55 56 -- lume.pingpong 57 tests["lume.pingpong"] = function() 58 testeq( lume.pingpong(0), 0 ) 59 testeq( lume.pingpong(1.5), .5 ) 60 testeq( lume.pingpong(-.2), .2 ) 61 testeq( lume.pingpong(-1.6), .4 ) 62 testeq( lume.pingpong(1.8), .2 ) 63 end 64 65 -- lume.distance 66 tests["lume.distance"] = function() 67 testeq( lume.distance(15, 20, 15, 20), 0 ) 68 testeq( lume.distance(13, 44, 156, 232), 236.205419074 ) 69 testeq( lume.distance(-23, 66, -232, 123), 216.633330769 ) 70 local x = lume.distance(13, 15, -2, 81) 71 testeq( lume.distance(13, 15, -2, 81, true), x * x ) 72 end 73 74 -- lume.angle 75 tests["lume.angle"] = function() 76 testeq( lume.angle(10, 10, 10, 10), math.rad(0) ) 77 testeq( lume.angle(10, 10, 20, 10), math.rad(0) ) 78 testeq( lume.angle(10, 10, 5, 10), math.rad(180) ) 79 testeq( lume.angle(10, 10, 20, 20), math.rad(45) ) 80 testeq( lume.angle(10, 10, 10, 30), math.rad(90) ) 81 end 82 83 -- lume.vector 84 tests["lume.vector"] = function() 85 local function cmp(a, b) return math.abs(a - b) < 10e-6 end 86 local x, y 87 x, y = lume.vector(0, 10) 88 testeq( cmp(x, 10) and cmp(y, 0), true ) 89 x, y = lume.vector(math.pi, 100) 90 testeq( cmp(x, -100) and cmp(y, 0), true ) 91 x, y = lume.vector(math.pi * 0.25, 100) 92 testeq( cmp(x, 70.71067811865476) and cmp(y, 70.71067811865476), true ) 93 end 94 95 -- lume.random 96 tests["lume.random"] = function() 97 testeq( type(lume.random()), "number" ) 98 testeq( type(lume.random(1)), "number" ) 99 testeq( type(lume.random(1, 2)), "number" ) 100 end 101 102 -- lume.randomchoice 103 tests["lume.randomchoice"] = function() 104 local t = {} 105 for i = 0, 1000 do 106 t[lume.randomchoice({"a", "b", "c", "d"})] = true 107 end 108 testeq( t.a and t.b and t.c and t.d, true ) 109 testeq( lume.randomchoice({true}), true ) 110 end 111 112 -- lume.weightedchoice 113 tests["lume.weightedchoice"] = function() 114 testeq( lume.weightedchoice( {a = 1} ), "a" ) 115 testeq( lume.weightedchoice( {a = 0, b = 1} ), "b" ) 116 tester.test.error( lume.weightedchoice, {} ) 117 tester.test.error( lume.weightedchoice, { a = 0, b = 0 } ) 118 tester.test.error( lume.weightedchoice, { a = 1, b = -1 } ) 119 end 120 121 -- lume.push 122 tests["lume.push"] = function() 123 local t = { 1, 2 } 124 lume.push(t, 3, 4) 125 testeq(t, { 1, 2, 3, 4 }) 126 lume.push(t, 5, nil, 6, nil, 7) 127 testeq(t, { 1, 2, 3, 4, 5, 6, 7 }) 128 lume.push(t) 129 testeq(t, { 1, 2, 3, 4, 5, 6, 7 }) 130 local x, y = lume.push(t, 123, 456) 131 testeq(x, 123) 132 testeq(y, 456) 133 end 134 135 -- lume.remove 136 tests["lume.remove"] = function() 137 local t = { 1, 2, 3, 4, 5 } 138 lume.remove(t, 3) 139 testeq(t, { 1, 2, 4, 5 }) 140 lume.remove(t, 1) 141 testeq(t, { 2, 4, 5 }) 142 lume.remove(t, 5) 143 testeq(t, { 2, 4 }) 144 local x = lume.remove(t, 123) 145 testeq(x, 123) 146 end 147 148 -- lume.clear 149 tests["lume.clear"] = function() 150 local t = { 1, 2, 3 } 151 lume.clear(t) 152 testeq(t, {}) 153 local m = { a = 1, b = 2, c = 3 } 154 lume.clear(m) 155 testeq(m, {}) 156 testeq( lume.clear(t) == t, true ) 157 end 158 159 -- lume.extend 160 tests["lume.extend"] = function() 161 local t = { a = 10, b = 20, c = 30 } 162 testeq( lume.extend(t) == t, true ) 163 lume.extend(t, { d = 40 }, { e = 50 }) 164 testeq( t, { a = 10, b = 20, c = 30, d = 40, e = 50 } ) 165 lume.extend(t, { a = "cat", b = "dog" }, { b = "owl", c = "fox" }) 166 testeq( t, { a = "cat", b = "owl", c = "fox", d = 40, e = 50 } ) 167 end 168 169 -- lume.shuffle 170 tests["lume.shuffle"] = function() 171 local t = {1, 2, 3, 4, 5} 172 t = lume.shuffle(t) 173 table.sort(t) 174 testeq( t, {1, 2, 3, 4, 5} ) 175 testeq( lume.shuffle({}), {} ) 176 end 177 178 -- lume.sort 179 tests["lume.sort"] = function() 180 local t = { 1, 5, 2, 4, 3 } 181 local fn = function(a, b) return a > b end 182 testeq( t == lume.sort(t), false ) 183 testeq( lume.sort(t), { 1, 2, 3, 4, 5 } ) 184 testeq( lume.sort(t, fn), { 5, 4, 3, 2, 1 } ) 185 testeq( t, { 1, 5, 2, 4, 3 } ) 186 local t = { { id = 2 }, { id = 3 }, { id = 1 } } 187 testeq( lume.sort(t, "id"), { { id = 1 }, { id = 2 }, { id = 3 } }) 188 end 189 190 -- lume.array 191 tests["lume.array"] = function() 192 local t = lume.array(pairs({a=0, b=0, c=0})) 193 table.sort(t) 194 testeq( t, {"a", "b", "c"} ) 195 testeq( lume.array(ipairs({0, 0, 0})), {1, 2, 3} ) 196 end 197 198 -- lume.each 199 tests["lume.each"] = function() 200 local acc = 1 201 lume.each({1, 2, 3}, function(x) acc = acc + x end) 202 testeq( acc, 7 ) 203 204 local acc = 1 205 local f = function(o, x) acc = acc + x end 206 local f2 = function() end 207 local t = {a = {f = f}, b = {f = f}, c = {f = f2}} 208 lume.each(t, "f", 10) 209 testeq( acc, 21 ) 210 end 211 212 -- lume.map 213 tests["lume.map"] = function() 214 testeq( lume.map({1, 2, 3}, function(x) return x * 2 end), {2, 4, 6} ) 215 testeq( lume.map({a=2,b=3}, function(x) return x * 2 end), {a=4,b=6} ) 216 local t = {{ id = 10 }, { id = 20 }, { id = 30 }} 217 testeq( lume.map(t, "id"), { 10, 20, 30 }) 218 end 219 220 -- lume.all 221 tests["lume.all"] = function() 222 testeq( lume.all({true, true, false, true}), false ) 223 testeq( lume.all({true, true, true, true}), true ) 224 testeq( lume.all({2, 3, 4, 5}, function(x) return x % 2 == 0 end), false ) 225 testeq( lume.all({2, 4, 6, 8}, function(x) return x % 2 == 0 end), true ) 226 testeq( lume.all({{ x = 1 }, {}, { x = 3 }}, "x"), false ) 227 testeq( lume.all({{ x = 1 }, { x = 2 }, { x = 3 }}, "x"), true ) 228 testeq( lume.all({{ x = 1 }, { x = 2 }, { x = 3 }}, { x = 2 }), false ) 229 testeq( lume.all({{ x = 2 }, { x = 2 }, { x = 2 }}, { x = 2 }), true ) 230 end 231 232 -- lume.any 233 tests["lume.any"] = function() 234 testeq( lume.any({true, true, false, true}), true ) 235 testeq( lume.any({false, false, false}), false ) 236 testeq( lume.any({2, 3, 4, 5}, function(x) return x % 2 == 0 end), true ) 237 testeq( lume.any({1, 3, 5, 7}, function(x) return x % 2 == 0 end), false ) 238 local t = {{ id = 10 }, { id = 20 }, { id = 30 }} 239 testeq( lume.any(t, { id = 10 }), true ) 240 testeq( lume.any(t, { id = 40 }), false ) 241 end 242 243 -- lume.reduce 244 tests["lume.reduce"] = function() 245 local concat = function(a, b) return a .. b end 246 local add = function(a, b) return a + b end 247 local any = function(a, b) return a or b end 248 testeq( lume.reduce({"cat", "dog"}, concat, ""), "catdog" ) 249 testeq( lume.reduce({"cat", "dog"}, concat, "pig"), "pigcatdog" ) 250 testeq( lume.reduce({"me", "ow"}, concat), "meow" ) 251 testeq( lume.reduce({1, 2, 3, 4}, add), 10 ) 252 testeq( lume.reduce({1, 2, 3, 4}, add, 5), 15 ) 253 testeq( lume.reduce({1}, add), 1 ) 254 testeq( lume.reduce({}, concat, "potato"), "potato" ) 255 testeq( lume.reduce({a=1, b=2}, add, 5), 8 ) 256 testeq( lume.reduce({a=1, b=2}, add), 3 ) 257 testeq( lume.reduce({false, false, false}, any), false ) 258 testeq( lume.reduce({false, true, false}, any), true ) 259 tester.test.error(lume.reduce, {}, add) 260 end 261 262 -- lume.unique 263 tests["lume.unique"] = function() 264 testeq( lume.unique({}), {} ) 265 local t = lume.unique({1, 2, 3, 2, 5, 6, 6}) 266 table.sort(t) 267 testeq( t, {1, 2, 3, 5, 6} ) 268 local t = lume.unique({"a", "b", "c", "b", "d"}) 269 table.sort(t) 270 testeq( t, {"a", "b", "c", "d"} ) 271 end 272 273 -- lume.filter 274 tests["lume.filter"] = function() 275 local t = lume.filter({1, 2, 3, 4, 5}, function(x) return x % 2 == 0 end ) 276 testeq( t, {2, 4} ) 277 local t = lume.filter({a=1, b=2, c=3}, function(x) return x == 2 end, true) 278 testeq( t, {b=2} ) 279 local t = lume.filter({{ x=1, y=1 }, { x=2, y=2 }, { x=1, y=3 }}, { x = 1 }) 280 testeq( t, {{ x=1, y=1 }, {x=1, y=3}} ) 281 end 282 283 -- lume.reject 284 tests["lume.reject"] = function() 285 local t = lume.reject({1, 2, 3, 4, 5}, function(x) return x % 2 == 0 end ) 286 testeq( t, {1, 3, 5} ) 287 local t = lume.reject({a=1, b=2, c=3}, function(x) return x == 2 end, true) 288 testeq( t, {a=1, c=3} ) 289 local t = lume.reject({{ x=1, y=1 }, { x=2, y=2 }, { x=1, y=3 }}, { x = 1 }) 290 testeq( t, {{ x=2, y=2 }} ) 291 end 292 293 -- lume.merge 294 tests["lume.merge"] = function() 295 testeq( lume.merge(), {} ) 296 testeq( lume.merge({x=1, y=2}), {x=1, y=2} ) 297 testeq( lume.merge({a=1, b=2}, {b=3, c=4}), {a=1, b=3, c=4} ) 298 end 299 300 -- lume.concat 301 tests["lume.concat"] = function() 302 testeq( lume.concat(nil), {} ) 303 testeq( lume.concat({1, 2, 3}), {1, 2, 3} ) 304 testeq( lume.concat({1, 2, 3}, {4, 5, 6}), {1, 2, 3, 4, 5, 6} ) 305 testeq( lume.concat({1, 2, 3}, {4, 5, 6}, nil, {7}), {1, 2, 3, 4, 5, 6, 7} ) 306 end 307 308 -- lume.find 309 tests["lume.find"] = function() 310 testeq( lume.find({"a", "b", "c"}, "b"), 2 ) 311 testeq( lume.find({"a", "b", "c"}, "c"), 3 ) 312 testeq( lume.find({a=1, b=5, c=7}, 5), "b" ) 313 end 314 315 -- lume.match 316 tests["lume.match"] = function() 317 local t = { "a", "b", "c", "d" } 318 local t2 = { a = 1, b = 2, c = 3, d = 4 } 319 local t3 = { {x=1, y=2}, {x=3, y=4}, {x=5, y=6} } 320 local v, k = lume.match(t, function(x) return x > "c" end) 321 testeq( v, "d" ) 322 testeq( k, 4 ) 323 local v, k = lume.match(t, function(x) return x < "b" end) 324 testeq( v, "a" ) 325 testeq( k, 1 ) 326 local v, k = lume.match(t2, function(x) return x < 2 end) 327 testeq( v, 1 ) 328 testeq( k, "a" ) 329 local v, k = lume.match(t2, function(x) return x > 5 end) 330 testeq( v, nil ) 331 testeq( k, nil ) 332 local v, k = lume.match(t3, { x = 3, y = 4 }) 333 testeq( k, 2 ) 334 end 335 336 -- lume.count 337 tests["lume.count"] = function() 338 local t = { a = 1, b = 2, c = 5, [13] = 22, z = 8 } 339 testeq( lume.count(t), 5 ) 340 testeq( lume.count(t, function(x) return x % 2 == 0 end ), 3 ) 341 local a = { 5, 6, 7, 8, 9 } 342 testeq( lume.count(a), #a ) 343 local t = { { n = 20 }, { n = 30 }, { n = 40 }, { n = 20 } } 344 testeq( lume.count(t, { n = 20 }), 2 ) 345 testeq( lume.count(t, { n = 30 }), 1 ) 346 testeq( lume.count(t, { n = 50 }), 0 ) 347 end 348 349 -- lume.slice 350 tests["lume.slice"] = function() 351 testeq( lume.slice({"a", "b", "c", "d", "e"}, 2, 4), {"b", "c", "d"} ) 352 testeq( lume.slice({"a", "b", "c", "d", "e"}, 2, -2), {"b", "c", "d"} ) 353 testeq( lume.slice({"a", "b", "c", "d", "e"}, 3, -1), {"c", "d", "e"} ) 354 testeq( lume.slice({"a", "b", "c", "d", "e"}, 3), {"c", "d", "e"} ) 355 testeq( lume.slice({"a", "b", "c", "d", "e"}, 4), {"d", "e"} ) 356 testeq( lume.slice({"a", "b", "c", "d", "e"}, 1, 1), {"a"} ) 357 testeq( lume.slice({"a", "b", "c", "d", "e"}, 2, 1), {} ) 358 testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, -2), {"c", "d"} ) 359 testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, 1), {} ) 360 testeq( lume.slice({"a", "b", "c", "d", "e"}, 0, 1), {"a"} ) 361 testeq( lume.slice({"a", "b", "c", "d", "e"}, 0, 0), {} ) 362 testeq( lume.slice({"a", "b", "c", "d", "e"}, -3), {"c", "d", "e"} ) 363 testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, 900), {"c", "d", "e"} ) 364 end 365 366 -- lume.first 367 tests["lume.first"] = function() 368 local t = { "a", "b", "c", "d", "e" } 369 testeq( lume.first(t), "a" ) 370 testeq( lume.first(t, 1), { "a" } ) 371 testeq( lume.first(t, 2), { "a", "b" } ) 372 end 373 374 -- lume.last 375 tests["lume.last"] = function() 376 local t = { "a", "b", "c", "d", "e" } 377 testeq( lume.last(t), "e" ) 378 testeq( lume.last(t, 1), { "e" } ) 379 testeq( lume.last(t, 2), { "d", "e" } ) 380 end 381 382 -- lume.invert 383 tests["lume.invert"] = function() 384 testeq( lume.invert({}), {} ) 385 testeq( lume.invert{a = "x", b = "y"}, {x = "a", y = "b"} ) 386 testeq( lume.invert{a = 1, b = 2}, {"a", "b"} ) 387 testeq( lume.invert(lume.invert{a = 1, b = 2}), {a = 1, b = 2} ) 388 end 389 390 -- lume.pick 391 tests["lume.pick"] = function() 392 local t = { cat = 10, dog = 20, fox = 30, owl = 40 } 393 testeq( lume.pick(t, "cat", "dog"), { cat = 10, dog = 20 } ) 394 testeq( lume.pick(t, "fox", "owl"), { fox = 30, owl = 40 } ) 395 testeq( lume.pick(t, "owl"), { owl = 40 } ) 396 testeq( lume.pick(t), {} ) 397 end 398 399 -- lume.keys 400 tests["lume.keys"] = function() 401 testeq( lume.keys({}), {} ) 402 local t = lume.keys({ aaa = 1, bbb = 2, ccc = 3 }) 403 table.sort(t) 404 testeq( t, {"aaa", "bbb", "ccc"} ) 405 local t = lume.keys({ "x", "x", "x" }) 406 testeq( t, {1, 2, 3} ) 407 end 408 409 -- lume.clone 410 tests["lume.clone"] = function() 411 local t = {6, 7, 4, 5} 412 testeq( lume.clone(t) ~= t, true ) 413 testeq( lume.clone(t), {6, 7, 4, 5} ) 414 testeq( lume.clone({x=2, y="a"}), {x=2, y="a"} ) 415 end 416 417 -- lume.fn 418 tests["lume.fn"] = function() 419 local f = lume.fn(function(a, b) return a + b end, 10) 420 testeq( f(5), 15 ) 421 tester.test.error( lume.fn, 123 ) 422 end 423 424 -- lume.once 425 tests["lume.once"] = function() 426 local f = lume.once(function(a, b) return a + b end, 10) 427 testeq( f(5), 15 ) 428 testeq( f(5), nil ) 429 tester.test.error( lume.once, 123 ) 430 end 431 432 -- lume.memoize 433 tests["lume.memoize"] = function() 434 local f = lume.memoize( 435 function(a, b, c) 436 return tostring(a) .. tostring(b) .. tostring(c) 437 end) 438 testeq( f("hello", nil, 15), "hellonil15" ) 439 testeq( f("hello", nil, 15), "hellonil15" ) 440 testeq( f(), "nilnilnil" ) 441 testeq( f(), "nilnilnil" ) 442 local f2 = lume.memoize(function() end) 443 testeq( f2(), nil ) 444 end 445 446 -- lume.combine 447 tests["lume.combine"] = function() 448 local acc = 0 449 local a = function(x, y) acc = acc + x + y end 450 local b = function(x, y) acc = acc + x * y end 451 local fn = lume.combine(a, b) 452 fn(10, 20) 453 testeq( acc, 230 ) 454 acc = 0 455 fn = lume.combine(nil, a, nil, b, nil) 456 fn(10, 20) 457 testeq( acc, 230 ) 458 local x = false 459 fn = lume.combine(function() x = true end) 460 fn() 461 testeq( x, true ) 462 testeq( type(lume.combine(nil)), "function" ) 463 testeq( type(lume.combine()), "function" ) 464 end 465 466 -- lume.call 467 tests["lume.call"] = function() 468 local add = function(a, b) return a + b end 469 testeq( lume.call(), nil ) 470 testeq( lume.call(nil, 1, 2, 3), nil ) 471 testeq( lume.call(add, 1, 2), 3 ) 472 end 473 474 -- lume.time 475 tests["lume.time"] = function() 476 local t, a, b, c = lume.time(function(x) return 50, 60, x end, 70) 477 testeq( type(t), "number" ) 478 testeq( {a, b, c}, {50, 60, 70} ) 479 end 480 481 -- lume.lambda 482 tests["lume.lambda"] = function() 483 testeq( lume.lambda "x->x*x"(10), 100 ) 484 testeq( lume.lambda "x->x*x"(20), 400 ) 485 testeq( lume.lambda "x,y -> 2*x+y"(10,5), 25 ) 486 testeq( lume.lambda "a, b -> a / b"(1, 2), .5 ) 487 testeq( lume.lambda "a -> 'hi->' .. a"("doggy"), "hi->doggy" ) 488 testeq( lume.lambda "A1,_->A1.._"("te","st"), "test" ) 489 testeq( lume.lambda "->"(1,2,3), nil ) 490 tester.test.error( lume.lambda, "abc" ) 491 tester.test.error( lume.lambda, "" ) 492 tester.test.error( lume.lambda, "a,b->a->b" ) 493 tester.test.error( lume.lambda, "(a),b->a+b" ) 494 end 495 496 -- lume.serialize / lume.deserialize 497 tests["lume.serialize, lume.deserialize"] = function() 498 local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} } 499 local s = lume.serialize(t) 500 testeq( lume.deserialize(s), t ) 501 testeq( lume.deserialize(lume.serialize(math.huge)), math.huge ) 502 testeq( lume.deserialize(lume.serialize(-math.huge)), -math.huge ) 503 local x = lume.deserialize(lume.serialize(0 / 0)) -- nan 504 testeq( x ~= x, true ) 505 end 506 507 -- lume.split 508 tests["lume.split"] = function() 509 testeq( lume.split("cat dog pig"), {"cat", "dog", "pig"} ) 510 testeq( lume.split("cat,dog,pig", ","), {"cat", "dog", "pig"} ) 511 testeq( lume.split("cat,dog;pig", ";"), {"cat,dog", "pig"} ) 512 testeq( lume.split("cat,dog,,pig", ","), {"cat", "dog", "", "pig"} ) 513 testeq( lume.split(";;;cat;", ";"), {"", "", "", "cat", ""} ) 514 testeq( lume.split("cat.dog", "."), {"cat", "dog"} ) 515 testeq( lume.split("cat%dog", "%"), {"cat", "dog"} ) 516 testeq( lume.split("1<>2<>3", "<>"), {"1", "2", "3"} ) 517 tester.test.error( lume.split, "abc", "" ) 518 end 519 520 -- lume.trim 521 tests["lume.trim"] = function() 522 testeq( lume.trim(" hello world "), "hello world" ) 523 testeq( lume.trim("-=-hello-world===", "-="), "hello-world" ) 524 testeq( lume.trim("***hello world*-*", "*"), "hello world*-" ) 525 testeq( lume.trim("...hello world.", "."), "hello world" ) 526 testeq( lume.trim("^.hello world]^", "^.]"), "hello world" ) 527 end 528 529 -- lume.wordwrap 530 tests["lume.wordwrap"] = function() 531 local str = "A small string with some words and then some more words" 532 local b = "A small string with \nsome words and then \nsome more words" 533 local fn = function(str) return #str >= 20 end 534 testeq( lume.wordwrap(str), str ) 535 testeq( lume.wordwrap(str, 20), b ) 536 testeq( lume.wordwrap(str, fn), b ) 537 end 538 539 -- lume.format 540 tests["lume.format"] = function() 541 local str = lume.format("a {a} in a {b}", {a = "mouse", b = "house"}) 542 testeq( str, "a mouse in a house" ) 543 testeq( lume.format("number {num}", {num = 13}), "number 13" ) 544 testeq( lume.format("{missing} {keys}", {}), "{missing} {keys}" ) 545 testeq( lume.format("A {missing} table"), "A {missing} table" ) 546 testeq( lume.format("{1} idx {2}", {"an", "test"}), "an idx test" ) 547 testeq( lume.format("bad idx {-1}", {"x"}), "bad idx {-1}" ) 548 testeq( lume.format("empty {}", {"idx"}), "empty {}" ) 549 end 550 551 -- lume.trace 552 tests["lume.trace"] = function() 553 local oldprint = print 554 local file, line, msg 555 print = function(x) 556 file, line, msg = x:match("(.-):(.-): (.*)") 557 end 558 lume.trace("Hi world", 123.456, 1, nil) 559 print = oldprint 560 testeq( file:match(".lua$"), ".lua" ) 561 testeq( tonumber(line) ~= nil, true ) 562 testeq( msg, "Hi world 123.46 1 nil" ) 563 end 564 565 -- lume.dostring 566 tests["lume.dostring"] = function() 567 testeq( lume.dostring([[return "hello!"]]), "hello!" ) 568 testeq( lume.dostring([[return 12345]]), 12345 ) 569 end 570 571 -- lume.uuid 572 tests["lume.uuid"] = function() 573 testeq( type(lume.uuid()), "string" ) 574 testeq( #lume.uuid(), 36 ) 575 end 576 577 -- lume.hotswap 578 tests["lume.hotswap"] = function() 579 local ok, err = lume.hotswap("bad_module_name") 580 testeq( ok, nil ) 581 testeq( type(err), "string" ) 582 end 583 584 -- lume.ripairs 585 tests["lume.ripairs"] = function() 586 local t = { "a", "b", false, "c" } 587 local r = {} 588 for i, v in lume.ripairs(t) do 589 table.insert(r, { i, v }) 590 end 591 testeq( r, { { 4, "c" }, { 3, false }, { 2, "b" }, { 1, "a" } }) 592 tester.test.error(lume.ripairs, nil) 593 end 594 595 -- lume.color 596 tests["lume.color"] = function() 597 testeq({ lume.color("#ff0000") }, { 1, 0, 0, 1 } ) 598 testeq({ lume.color("#00ff00") }, { 0, 1, 0, 1 } ) 599 testeq({ lume.color("#0000ff") }, { 0, 0, 1, 1 } ) 600 testeq({ lume.color("rgb( 255, 255, 255 )") }, { 1, 1, 1, 1 } ) 601 testeq({ lume.color("rgb (0, 0, 0)") }, { 0, 0, 0, 1 } ) 602 testeq({ lume.color("rgba(255, 255, 255, .5)") }, { 1, 1, 1, .5 } ) 603 testeq({ lume.color("#ffffff", 2) }, { 2, 2, 2, 2 } ) 604 testeq({ lume.color("rgba(255, 255, 255, 1)", 3) }, { 3, 3, 3, 3 } ) 605 tester.test.error(lume.color, "#ff00f") 606 tester.test.error(lume.color, "#xyzxyz") 607 tester.test.error(lume.color, "rgba(hello)") 608 tester.test.error(lume.color, "rgba()") 609 tester.test.error(lume.color, "rgba(1, 1, 1, 1") 610 end 611 612 -- lume.chain 613 tests["lume.chain"] = function() 614 local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result() 615 testeq( t, { 2, 4 } ) 616 testeq( lume.chain(10):result(), 10 ) 617 local t = lume({1, 2}):map(function(x) return x * 2 end):result() 618 testeq( t, { 2, 4 } ) 619 end 620 621 622 tester.dotests(tests) 623 tester.test.global() 624 tester.printresults()