Module:Array/testcases

local export = {}

local Array = require "Module:array"

local function collect(iter)
	local vals = Array()
	for val in iter do
		vals:insert(val)
	end
	return vals
end

local total_tests, successes = 0, 0
local function assert(val)
	total_tests = total_tests + 1
	if val then
		successes = successes + 1
	end
end

function export.run_tests()
	local ok, ret = pcall(function ()
		assert(Array(1, 2, 3):map(function (x) return x * x end):concat ", " == "1, 4, 9")
		
		assert(Array(1, 2, 3, 4, 5):filter(function (x) return x % 2 == 0 end):concat ", " == "2, 4")
		
		assert(Array():type() == "array")
		
		assert(Array(1, nil, 2, nil, 3):compress():concat ", " == "1, 2, 3")
		assert(collect(Array(1, nil, 2, nil, 3):sparseIpairs()):concat ", " == "1, 3, 5")
		
		assert(Array("a", "b", "c"):toSet()["a"] == true)
		assert(Array("a", "b", "c"):to_set()["a"] == true)
		
		assert(Array(2, 4, 8):all(function (x) return x % 2 == 0 end) == true)
		assert(Array(1, 2, 3):some(function (x) return x % 2 == 0 end) == true)
		
		assert(Array(1, 2, 3, nil, 4):length() == 3)
		
		assert(Array(1, 2, 3, nil, 4):contains(3) == true)
		assert(Array(1, 2, 3, nil, 4):contains(4) == false)
		
		assert(Array.keysToList(Array("a", "b", "c"):invert()):concat ", " == "a, b, c")
		
		assert(Array(1, nil, 2, nil, 3):maxIndex() == 5)
		assert(Array(1, nil, 2, nil, 3):max_index() == 5)
		
		assert(Array(1, nil, 2, nil, 3):num_keys():concat ", " == "1, 3, 5")
		
		assert(collect(Array("a", "b", "c"):reverseIpairs()):concat ", " == "3, 2, 1")
		
		assert(Array("a", "b", "c"):reverse():concat ", " == "c, b, a")
		
		assert(Array("a", "b", "c"):serial_comma_join { dontTag = true } == "a, b, and c")
		
		assert(Array(1, 1, 2, 3, 1, 2, 3):removeDuplicates():concat ", " == "1, 2, 3")
		
		assert(Array(1, 2, 3, 4, 5):slice(1, 3):concat ", " == "1, 2, 3")
		assert(Array(1, 2, 3, 4, 5):slice(1, -3):concat ", " == "1, 2, 3")
		
		assert(Array(1, 2, 3):adjust_index(-1) == 3)
		
		assert((Array(1, 2, 3) + Array(4, 5, 6)):concat ", " == "1, 2, 3, 4, 5, 6")
	end)
	
	local err
	if not ok then
		err = ret
	end
	
	local color = successes < total_tests and "red" or "green"
	
	return ('<span style="color: %s;">%d out of %d assertions passed</span>%s')
		:format(color, successes, total_tests, err and '\n\n<span class="error">' .. err .. '</span>' or "")
end

return export

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.