spec/util_cache_spec.lua
changeset 12775 e9fcc69ea508
parent 12575 c4337ff4f1c4
child 13178 8ec7b7d6556f
equal deleted inserted replaced
12774:249bf1a53866 12775:e9fcc69ea508
     2 local cache = require "util.cache";
     2 local cache = require "util.cache";
     3 
     3 
     4 describe("util.cache", function()
     4 describe("util.cache", function()
     5 	describe("#new()", function()
     5 	describe("#new()", function()
     6 		it("should work", function()
     6 		it("should work", function()
       
     7 			do
       
     8 				local c = cache.new(1);
       
     9 				assert.is_not_nil(c);
       
    10 
       
    11 				assert.has_error(function ()
       
    12 					cache.new(0);
       
    13 				end);
       
    14 				assert.has_error(function ()
       
    15 					cache.new(-1);
       
    16 				end);
       
    17 				assert.has_error(function ()
       
    18 					cache.new("foo");
       
    19 				end);
       
    20 			end
     7 
    21 
     8 			local c = cache.new(5);
    22 			local c = cache.new(5);
     9 
    23 
    10 			local function expect_kv(key, value, actual_key, actual_value)
    24 			local function expect_kv(key, value, actual_key, actual_value)
    11 				assert.are.equal(key, actual_key, "key incorrect");
    25 				assert.are.equal(key, actual_key, "key incorrect");
   334 				assert.spy(i).was_called();
   348 				assert.spy(i).was_called();
   335 				assert.spy(i).was_called_with("b", "2");
   349 				assert.spy(i).was_called_with("b", "2");
   336 				assert.spy(i).was_called_with("c", "3");
   350 				assert.spy(i).was_called_with("c", "3");
   337 				assert.spy(i).was_called_with("d", "4");
   351 				assert.spy(i).was_called_with("d", "4");
   338 		end);
   352 		end);
       
   353 
       
   354 		local function vs(t)
       
   355 			local vs_ = {};
       
   356 			for v in t:values() do
       
   357 				vs_[#vs_+1] = v;
       
   358 			end
       
   359 			return vs_;
       
   360 		end
       
   361 
       
   362 		it(":values works", function ()
       
   363 			local t = cache.new(3);
       
   364 			t:set("k1", "v1");
       
   365 			t:set("k2", "v2");
       
   366 			assert.same({"v2", "v1"}, vs(t));
       
   367 			t:set("k3", "v3");
       
   368 			assert.same({"v3", "v2", "v1"}, vs(t));
       
   369 			t:set("k4", "v4");
       
   370 			assert.same({"v4", "v3", "v2"}, vs(t));
       
   371 		end);
       
   372 
       
   373 		it(":resize works", function ()
       
   374 			local c = cache.new(5);
       
   375 			for i = 1, 5 do
       
   376 				c:set(("k%d"):format(i), ("v%d"):format(i));
       
   377 			end
       
   378 			assert.same({"v5", "v4", "v3", "v2", "v1"}, vs(c));
       
   379 			assert.has_error(function ()
       
   380 				c:resize(-1);
       
   381 			end);
       
   382 			assert.has_error(function ()
       
   383 				c:resize(0);
       
   384 			end);
       
   385 			assert.has_error(function ()
       
   386 				c:resize("foo");
       
   387 			end);
       
   388 			c:resize(3);
       
   389 			assert.same({"v5", "v4", "v3"}, vs(c));
       
   390 		end);
   339 	end);
   391 	end);
   340 end);
   392 end);