模組:PoS/testcases

來自維基辭典

說明[編輯]

呢個係 PoS 模組嘅測試模組;目前喺度嘅所有 test case 都係寫錯次序(即係寫咗 code 之後先補 test case,唔係寫 code 之前先寫 test case),所以唔能夠保證啲 test case 絕對有效,只係有好過冇。

因為測試嘅 main 函數會用 error 函數喺例如模參數錯誤嘅情況產生例外(exception),所以測試唔可以直接呼叫 main,而係要用 pcall 間接呼叫。寫法係咁:

  • 正常呼叫:b = f.main(frame);  ,當中 f 係 PoS 模組
  • pcall 形式:local a, b = pcall(f.main, frame);

咁寫先可以檢查(assertmain 函數有冇預期咁產生例外;做法係檢查 a 值,如果係 true 就係冇發生例外,正常輸出喺 b,所以應該再檢查 b 入面嘅輸出係咪預期,但係如果 a 值係 false 就出咗例外,應該檢查 b 入面有冇預期嘅錯誤訊息。

模擬模呼叫模組函數要用一個序列整一個假嘅 frame 出來,喺 test case 入面用嘅係最簡單嘅形式,係咁:

  • { getParent = function () return {
    args = { 模參數 }; }; end;
    args = { 模組參數 }; }  ,當中模組參數係指模入面 #invoke 嘅參數,而模參數係指文入面 {{PoS}} 嘅參數

-- vi: set sw=4 ts=4 ai sm: 
require 'strict';
local f = require 'Module:PoS';
local p = {};

p.test = function ()
	-- {{PoS}}
	local a, b = pcall(f.main, { getParent = function () return {
					args = {
					};
				}; end;
					args = {
					};
				});
	assert(not a and mw.ustring.match(b, '無提供語言代碼'));

	-- {{PoS|lang=en}}
	local a, b = pcall(f.main, { getParent = function () return {
					args = {
						lang = 'en';
					};
				}; end;
					args = {
					};
				});
	assert(not a and mw.ustring.match(b, '冇指定詞性'));

	-- {{PoS|lang=en|pos=foo}}
	local a, b = pcall(f.main, { getParent = function () return {
					args = {
						lang = 'en';
						pos = 'foo';
					};
				}; end;
					args = {
					};
				});
	assert(a and mw.ustring.match(b, 'Category:PoS模用咗未知詞性'));

	-- {{PoS|lang=en|pos=名詞}}
	local a, b = pcall(f.main, { getParent = function () return {
					args = {
						lang = 'en';
						pos = '名詞';
					};
				}; end;
					args = {
					};
				});
	assert(a and mw.ustring.match(b, 'Category:英文名詞'));

end

return p;