2017-09-22 6 views
3

私は単一のファイルのLuaスクリプト、例えばscript.luaからユニットテスト関数を使用したいと思います。スクリプトは次のようになります、次のとおりです。1つのファイルLuaスクリプトの中でどのように関数をテストできますか?

-- some fields from gvsp dissector which shall be post processed in custom dissector 
gvsp_field0_f = Field.new("gvsp.<field0-name>") 
gvsp_field1_f = Field.new("gvsp.<field1-name>") 

-- custom protocol declaration 
custom_protocol = Proto("custom","Custom Postdissector") 

-- custom protocol field declarations 
field0_f = ProtoField.string("custom.<field0-name>","Custom Field 0") 
field1_f = ProtoField.string("custom.<field1-name>","Custom Field 1") 

-- register custom protocol as postdissector 
register_postdissector(custom_protocol) 

function custom_protocol.dissector(buffer,pinfo,tree) 
    -- local field values of "pre" dissector which are analyzed 
    local gvsp_field0_value = gvsp_field0_f() 
    local gvsp_field1_value = gvsp_field1_f() 

    -- functions which shell be unit tested 
    function0(...) 
    function1(...) 
end 

function0(...) 
    -- implementation 
end 

function1(...) 
    -- implementation 
end 

だが、私は(おそらく物事を簡単になるだろう)別のモジュールファイルにスクリプトファイルから機能を分離したくないとしましょう。 script.luaファイル内または別のtest_script.luaファイル内のscript.luaに定義されている関数に対して、テストを定義するにはどうすればよいですか(統合するのが簡単なので、好ましくはluaunitですが、他のツールも同様です)。

+0

これは_how _に_ _あなたが物事を定義する_a _に依存します。あなたは 'local'を使っていますか(それは事態をかなり困難にします)、あるいは'ローカル '_ENV'ironmentsを(もっと簡単に)使用しますか?コードはどのように構造化されていますか?関数などを定義する方法の小さなコードサンプルを追加してください(実際のコードでなくても、あなたのものを定義する方法に一致するダミー関数だけでなくてもかまいません)。現在、意味のある情報答え。 – nobody

+0

スクリプトは、 'dissector'プロトコルで使われているファイルの最後に' local'関数が定義されたhttps://wiki.wireshark.org/Lua/Dissectors#postdissectorsのポストディセクターとよく似ています。しかし、私はこの構造に固執しません。 – thinwybk

答えて

0

個別のスクリプトとユニットテストの実行を有効にするには、少なくとも3つのファイルが必要です(この例では、単一ファイルで構成されるユニットテストフレームワークluaunitがプロジェクトディレクトリに統合されています)。この例では、すべてのファイルが同じディレクトリにあります。スクリプトscript.luaはその中に関数を定義していないかもしれませんが、モジュールmodule.luaから必要なすべての関数をインポートする必要があります。

-- script imports module functions 
module = require('module') 

-- ... and uses it to print the result of the addition function 
result = module.addtwo(1,1) 
print(result) 

module.luaその機能が自動的に他のスクリプトファイルまたはモジュールを通じて輸入のために登録されているLua module skeletonにaccoring実装されています。

-- capture the name searched for by require 
local NAME=... 

-- table for our functions 
local M = { } 

-- A typical local function that is also published in the 
-- module table. 
local function addtwo(a,b) return a+b end 
M.addtwo = addtwo 

-- Shorthand form is less typing and doesn't use a local variable 
function M.subtwo(x) return x-2 end 

return M 

test_module.luaその実行のためのモジュールの機能と輸入luaunit.lua(ユニットテストフレームワーク)のためのユニットテストを含んでいます。 test_module.luaの内容は次のとおりです。

luaunit = require('luaunit') 
script = require('module') 

function testAddPositive() 
    luaunit.assertEquals(module.addtwo(1,1),2) 
end 

os.exit(luaunit.LuaUnit.run()) 

あなたがlua test_module.luaを実行してテストを実行した場合のテストは、スクリプト機能とは別に実行されます。

. 
Ran 1 tests in 0.000 seconds, 1 success, 0 failures 
OK 

スクリプトが出力2lua script.luaと通常通り実行されます。

2

簡単な答え:できません!

私は数年前にLuaチームに質問しました。スクリプトが実行中または含まれているかどうかをスクリプトが知る方法がないためです(例: 'require'd)。

近い将来にこのような機能を追加することに興味はないようです。

+0

別の 'test_script.lua'でテストを実装するのはどうでしょうか? – thinwybk

+0

私は分かりません。そのことについて何?単にファイルをテストファイルからテストし、必要に応じてパブリック関数を呼び出すだけです。 – tonypdmtr

+0

以下の例では、単体テストに加えてスクリプトの主要機能が実行されています。私はスクリプトの主な機能を単体テストの実行中に実行させたくありません。 – thinwybk

関連する問題