diff options
| author | ruki <[email protected]> | 2019-06-09 15:58:33 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-06-09 15:58:33 +0800 |
| commit | 3a1db57753528053ab1c457675257efe7250bc87 (patch) | |
| tree | f6a0a74de240153123268fd483e3cdd3d0ee0050 /tests | |
| parent | e165a8e43abed4eb2eca64c3b5adefb57cd65600 (diff) | |
| parent | 8049d92b3e9aef35e4f124e5ae361bdd1f5c5435 (diff) | |
Merge pull request #444 from OpportunityLiu/test
Improve test library
Diffstat (limited to 'tests')
37 files changed, 523 insertions, 347 deletions
diff --git a/tests/modules/devel/git/test.lua b/tests/modules/devel/git/test.lua index ddcfc94b2..0fbae4b1e 100644 --- a/tests/modules/devel/git/test.lua +++ b/tests/modules/devel/git/test.lua @@ -1,21 +1,17 @@ import("devel.git") -function test_asgiturl() - assert(git.asgiturl("http://github.com/a/b") == "https://github.com/a/b.git") - assert(git.asgiturl("http://github.com/a/b/") == "https://github.com/a/b.git") - assert(git.asgiturl("HTTP://github.com//a/b/") == "https://github.com/a/b.git") - assert(git.asgiturl("http://github.com//a/b/s") == nil) - assert(git.asgiturl("https://github.com/a/b") == "https://github.com/a/b.git") - assert(git.asgiturl("https://github.com/a/b.git") == "https://github.com/a/b.git") - assert(git.asgiturl("HTTPS://GITHUB.com/a/b.git.git") == "https://github.com/a/b.git.git") +function test_asgiturl(t) + t:are_equal(git.asgiturl("http://github.com/a/b"), "https://github.com/a/b.git") + t:are_equal(git.asgiturl("http://github.com/a/b/"), "https://github.com/a/b.git") + t:are_equal(git.asgiturl("HTTP://github.com//a/b/"), "https://github.com/a/b.git") + t:are_equal(git.asgiturl("http://github.com//a/b/s"), nil) + t:are_equal(git.asgiturl("https://github.com/a/b"), "https://github.com/a/b.git") + t:are_equal(git.asgiturl("https://github.com/a/b.git"), "https://github.com/a/b.git") + t:are_equal(git.asgiturl("HTTPS://GITHUB.com/a/b.git.git"), "https://github.com/a/b.git.git") - assert(git.asgiturl("github:a/b") == "https://github.com/a/b.git") - assert(git.asgiturl("github:a/b.git") == "https://github.com/a/b.git.git") - assert(git.asgiturl("GitHub://a/b/") == "https://github.com/a/b.git") - assert(git.asgiturl("github:a/b/c") == nil) -end - -function main() - test_asgiturl() + t:are_equal(git.asgiturl("github:a/b"), "https://github.com/a/b.git") + t:are_equal(git.asgiturl("github:a/b.git"), "https://github.com/a/b.git.git") + t:are_equal(git.asgiturl("GitHub://a/b/"), "https://github.com/a/b.git") + t:are_equal(git.asgiturl("github:a/b/c"), nil) end
\ No newline at end of file diff --git a/tests/modules/hello/test.lua b/tests/modules/hello/test.lua index 431a82072..1f4f98a02 100755 --- a/tests/modules/hello/test.lua +++ b/tests/modules/hello/test.lua @@ -1,4 +1,4 @@ -function main() +function test_hello(context) print("hello") end diff --git a/tests/modules/lib/detect/test.lua b/tests/modules/lib/detect/test.lua index c44084ddc..b6937cfaf 100755 --- a/tests/modules/lib/detect/test.lua +++ b/tests/modules/lib/detect/test.lua @@ -1,14 +1,14 @@ import("lib.detect.find_toolname") -function main() - assert(find_toolname("xcrun -sdk macosx clang") == "clang") - assert(find_toolname("xcrun -sdk macosx clang++") == "clangxx") - assert(find_toolname("/usr/bin/arm-linux-gcc") == "gcc") - assert(find_toolname("/usr/bin/arm-linux-g++") == "gxx") - assert(find_toolname("/usr/bin/arm-linux-ar") == "ar") - assert(find_toolname("link.exe -lib") == "link") - assert(find_toolname("arm-android-clang++") == "clangxx") - assert(find_toolname("pkg-config") == "pkg_config") - assert(find_toolname("gcc-5") == "gcc") +function test_find_toolname(t) + t:are_equal(find_toolname("xcrun -sdk macosx clang"), "clang") + t:are_equal(find_toolname("xcrun -sdk macosx clang++"), "clangxx") + t:are_equal(find_toolname("/usr/bin/arm-linux-gcc"), "gcc") + t:are_equal(find_toolname("/usr/bin/arm-linux-g++"), "gxx") + t:are_equal(find_toolname("/usr/bin/arm-linux-ar"), "ar") + t:are_equal(find_toolname("link.exe -lib"), "link") + t:are_equal(find_toolname("arm-android-clang++"), "clangxx") + t:are_equal(find_toolname("pkg-config"), "pkg_config") + t:are_equal(find_toolname("gcc-5"), "gcc") end diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua index 681a2ec75..a0ff57ec5 100644 --- a/tests/modules/os/test.lua +++ b/tests/modules/os/test.lua @@ -1,38 +1,62 @@ -function main() +function test_cpdir(t) -- get mclock local tm = os.mclock() -- test cpdir os.mkdir("test1") - assert(os.exists("test1")) + t:require(os.exists("test1")) os.cp("test1","test2") - assert(os.exists("test2")) + t:require(os.exists("test2")) os.rmdir("test1") - assert(not os.exists("test1")) + t:require_not(os.exists("test1")) io.writefile("test2/awd","awd") os.rmdir("test2") - assert(not os.exists("test2")) + t:require_not(os.exists("test2")) + -- assert mclock + t:require(os.mclock() >= tm) +end + +function test_rename(t) + -- get mclock + local tm = os.mclock() -- test rename os.mkdir("test1") - assert(os.exists("test1")) + t:require(os.exists("test1")) os.mv("test1","test2") - assert(not os.exists("test1")) - assert(os.exists("test2")) + t:require_not(os.exists("test1")) + t:require(os.exists("test2")) os.rmdir("test2") - assert(not os.exists("test2")) + t:require_not(os.exists("test2")) + -- assert mclock + t:require(os.mclock() >= tm) +end + +function test_cp_mvdir_into_another_dir(t) + -- get mclock + local tm = os.mclock() -- test cp/mvdir into another dir os.mkdir("test1") os.mkdir("test2") - assert(os.exists("test1") and os.exists("test2")) + t:require(os.exists("test1")) + t:require(os.exists("test2")) os.cp("test1","test2") - assert(os.exists("test2/test1")) + t:require(os.exists("test2/test1")) os.mv("test1","test2/test1") - assert(not os.exists("test1")) - assert(os.exists("test2/test1/test1")) + t:require_not(os.exists("test1")) + t:require(os.exists("test2/test1/test1")) os.rmdir("test2") - assert(not os.exists("test2")) + t:require_not(os.exists("test2")) + -- assert mclock + t:require(os.mclock() >= tm) +end + +function test_setenv(t) + -- get mclock + local tm = os.mclock() -- test setenv os.setenv("__AWD","DWA") - assert(os.getenv("__AWD")=="DWA") + t:are_equal(os.getenv("__AWD"), "DWA") + os.setenv("__AWD","DWA2") + t:are_equal(os.getenv("__AWD"), "DWA2") -- assert mclock - assert(os.mclock()>=tm) -end + t:require(os.mclock() >= tm) +end
\ No newline at end of file diff --git a/tests/modules/process/test.lua b/tests/modules/process/test.lua index 7b5501994..aa57a56cc 100644 --- a/tests/modules/process/test.lua +++ b/tests/modules/process/test.lua @@ -1,52 +1,36 @@ -function main() + +local inftimeout = 5000 + +function test_single_process(t) -- single process test - local inftimeout = 5000 local stdout = os.tmpfile() local stderr = os.tmpfile() for i = 1, 2 do local pro = process.open("echo -n awd", stdout, stderr) process.wait(pro, inftimeout) process.close(pro) - assert(io.readfile(stdout) == "awd") + t:are_equal(io.readfile(stdout), "awd") end +end +function test_hack(t) -- hack test - local ok = try - { - function () - process.wait("awd", inftimeout) - return true - end - } - assert(ok == nil) + t:will_raise(function () + process.wait("awd", inftimeout) + end) - assert(process.close("awd") == nil) + t:require_not(process.close("awd")) - ok = try - { - function () - process.waitlist("awd", inftimeout) - return true - end - } - assert(ok == nil) + t:will_raise(function () + process.waitlist("awd", inftimeout) + end) - ok = try - { - function () - process.waitlist({}, inftimeout) - return true - end - } - assert(ok == nil) + t:will_raise(function () + process.waitlist({}, inftimeout) + end) - ok = try - { - function () - process.waitlist({"awd"}, inftimeout) - return true - end - } - assert(ok == nil) + t:will_raise(function () + process.waitlist({"awd"}, inftimeout) + end) end diff --git a/tests/modules/semver/test.lua b/tests/modules/semver/test.lua index cc1f0c75f..332285df8 100755 --- a/tests/modules/semver/test.lua +++ b/tests/modules/semver/test.lua @@ -2,122 +2,79 @@ import("core.base.semver") -- select version -function _check_semver_select(results, required_ver, versions, tags, branches) +function _check_semver_select(t, results, required_ver, versions, tags, branches) -- select it local version, source = semver.select(required_ver, versions or {}, tags or {}, branches or {}) - if (version.version or version) ~= results[1] or source ~= results[2] then - print("semver.select(\"%s\", {\"%s\"}, {\"%s\"}, {\"%s\"})" - , required_ver - , table.concat(versions or {}, "\", \"") - , table.concat(tags or {}, "\", \"") - , table.concat(branches or {}, "\", \"")) - raise("{\"%s\", \"%s\"} != {\"%s\", \"%s\"}", version.version or version, source or "", results[1] or "", results[2] or "") - end + t:are_equal((version.version or version), results[1]) + t:are_equal(source, results[2]) end -- test select version -function _test_semver_select() +function test_semver_select(t) - _check_semver_select({"1.5.1", "versions"} + _check_semver_select(t, {"1.5.1", "versions"} , ">=1.5.0 <1.6.0" , {"1.4.0", "1.5.0", "1.5.1"}) - _check_semver_select({"1.5.1", "versions"} + _check_semver_select(t, {"1.5.1", "versions"} , "^1.5.0" ,{"1.4.0", "1.5.0", "1.5.1"}) - _check_semver_select({"master", "branches"} + _check_semver_select(t, {"master", "branches"} , "master" , {"1.4.0", "1.5.0", "1.5.1"} , {"v1.2.0", "v1.6.0"} , {"master", "dev"}) - _check_semver_select({"1.5.1", "versions"} + _check_semver_select(t, {"1.5.1", "versions"} , "lastest" , {"1.4.0", "1.5.0", "1.5.1"}) - - print("semver.select: ok!") end -- select version -function _check_semver_satisfies(expected, version, range) +function _check_semver_satisfies(t, expected, version, range) -- select it local result = semver.satisfies(version, range) - if expected ~= result then - print("semver.satisfies(\"%s\", \"%s\")" - , version - , range) - raise("\"%s\" != \"%s\"", expected, result) - end + t:are_equal(result, expected) end -- test satisfies version -function _test_semver_satisfies() +function test_semver_satisfies(t) + + _check_semver_satisfies(t, true, "1.5.1", ">=1.5.0 <1.6.0") + _check_semver_satisfies(t, true, "1.5.1", "^1.5.0") + _check_semver_satisfies(t, true, "1.5.1", "~1.5.0") + _check_semver_satisfies(t, true, "1.6.0", "^1.5.0") - _check_semver_satisfies(true, "1.5.1", ">=1.5.0 <1.6.0") - _check_semver_satisfies(true, "1.5.1", "^1.5.0") + _check_semver_satisfies(t, false, "1.6.1", "~1.5.0") + _check_semver_satisfies(t, false, "2.5.1", "^1.5.0") + _check_semver_satisfies(t, false, "1.4.1", ">=1.5.0 <1.6.0") - print("semver.satisfies: ok!") end -- parse version -function _check_semver_parse(version_str, major, minor, patch, prerelease, build) +function _check_semver_parse(t, version_str, major, minor, patch, prerelease, build) -- create it local version = semver.parse(version_str) - if version.major ~= major then - raise("major: \"%s\" != \"%s\"", version.major or "", major or "") - end - - if version.minor ~= minor then - raise("minor: \"%s\" != \"%s\"", version.minor or "", minor or "") - end + t:are_equal(version.major, major) + t:are_equal(version.minor, minor) + t:are_equal(version.patch, patch) + t:are_equal(version.prerelease, prerelease or {}) + t:are_equal(version.build, build or {}) - if version.patch ~= patch then - raise("patch: \"%s\" != \"%s\"", version.patch or "", patch or "") - end - - if table.concat(version.prerelease or {}, ".") ~= table.concat(prerelease or {}, ".") then - raise("prerelease: \"%s\" != \"%s\"" - , table.concat(version.prerelease or {}, ".") or "" - , table.concat(prerelease or {}, ".") or "") - end - - if table.concat(version.build or {}, ".") ~= table.concat(build or {}, ".") then - raise("build: \"%s\" != \"%s\"" - , table.concat(version.build or {}, ".") or "" - , table.concat(build or {}, ".") or "") - end end -- test parse version -function _test_semver_parse() +function test_semver_parse(t) - _check_semver_parse("1.2.3", 1, 2, 3) - _check_semver_parse("1.2.3-beta", 1, 2, 3, {"beta"}) - _check_semver_parse("1.2.3-beta+77", 1, 2, 3, {"beta"}, {"77"}) - _check_semver_parse("v1.2.3-alpha.1+77", 1, 2, 3, {"alpha", "1"}, {"77"}) - _check_semver_parse("v3.2.1-alpha.1+77.foo", 3, 2, 1, {"alpha", "1"}, {"77", "foo"}) + _check_semver_parse(t, "1.2.3", 1, 2, 3) + _check_semver_parse(t, "1.2.3-beta", 1, 2, 3, {"beta"}) + _check_semver_parse(t, "1.2.3-beta+77", 1, 2, 3, {"beta"}, {77}) + _check_semver_parse(t, "v1.2.3-alpha.1+77", 1, 2, 3, {"alpha", 1}, {77}) + _check_semver_parse(t, "v3.2.1-alpha.1+77.foo", 3, 2, 1, {"alpha", 1}, {77, "foo"}) - print("semver.parse: ok!") -end - --- --- run tests: --- --- $ make test name=semver --- -function main() - - -- test semver - _test_semver_parse() - - -- test semver satisfies - _test_semver_satisfies() - - -- test select version - _test_semver_select() -end +end
\ No newline at end of file diff --git a/tests/modules/string/test.lua b/tests/modules/string/test.lua index 46689dd0f..04ee715ca 100644 --- a/tests/modules/string/test.lua +++ b/tests/modules/string/test.lua @@ -1,48 +1,40 @@ -function assert_array_eq(t1, t2) - assert(table.is_array(t1)) - assert(table.is_array(t2)) - assert(#t1 == #t2, "#t1(%d) != #t2(%d)", #t1, #t2) - for idx, value in ipairs(t1) do - assert(value == t2[idx], "value[%d]: %s != %s", idx, value, t2[idx]) - end -end -function test_endswith() - assert(not ("rc"):endswith("xcadas")) - assert(("aaaccc"):endswith("ccc")) +function test_endswith(t) + t:require(("aaaccc"):endswith("ccc")) + t:require(("aaaccc"):endswith("aaaccc")) + t:require_not(("rc"):endswith("xcadas")) + t:require_not(("aaaccc "):endswith("%s")) end -function test_startswith() - assert(("aaaccc"):startswith("aaa")) +function test_startswith(t) + t:require(("aaaccc"):startswith("aaa")) + t:require(("aaaccc"):startswith("aaaccc")) + t:require_not(("rc"):startswith("xcadas")) + t:require_not((" aaaccc"):startswith("%s")) end -function test_split() +function test_split(t) -- pattern match and ignore empty string - assert_array_eq(("1\n\n2\n3"):split('\n'), {"1", "2", "3"}) - assert_array_eq(("abc123123xyz123abc"):split('123'), {"abc", "xyz", "abc"}) - assert_array_eq(("abc123123xyz123abc"):split('[123]+'), {"abc", "xyz", "abc"}) - + t:are_equal(("1\n\n2\n3"):split('\n'), {"1", "2", "3"}) + t:are_equal(("abc123123xyz123abc"):split('123'), {"abc", "xyz", "abc"}) + t:are_equal(("abc123123xyz123abc"):split('[123]+'), {"abc", "xyz", "abc"}) + -- plain match and ignore empty string - assert_array_eq(("1\n\n2\n3"):split('\n', {plain = true}), {"1", "2", "3"}) - assert_array_eq(("abc123123xyz123abc"):split('123', {plain = true}), {"abc", "xyz", "abc"}) + t:are_equal(("1\n\n2\n3"):split('\n', {plain = true}), {"1", "2", "3"}) + t:are_equal(("abc123123xyz123abc"):split('123', {plain = true}), {"abc", "xyz", "abc"}) -- pattern match and contains empty string - assert_array_eq(("1\n\n2\n3"):split('\n', {strict = true}), {"1", "", "2", "3"}) - assert_array_eq(("abc123123xyz123abc"):split('123', {strict = true}), {"abc", "", "xyz", "abc"}) - assert_array_eq(("abc123123xyz123abc"):split('[123]+', {strict = true}), {"abc", "xyz", "abc"}) + t:are_equal(("1\n\n2\n3"):split('\n', {strict = true}), {"1", "", "2", "3"}) + t:are_equal(("abc123123xyz123abc"):split('123', {strict = true}), {"abc", "", "xyz", "abc"}) + t:are_equal(("abc123123xyz123abc"):split('[123]+', {strict = true}), {"abc", "xyz", "abc"}) -- plain match and contains empty string - assert_array_eq(("1\n\n2\n3"):split('\n', {plain = true, strict = true}), {"1", "", "2", "3"}) - assert_array_eq(("abc123123xyz123abc"):split('123', {plain = true, strict = true}), {"abc", "", "xyz", "abc"}) + t:are_equal(("1\n\n2\n3"):split('\n', {plain = true, strict = true}), {"1", "", "2", "3"}) + t:are_equal(("abc123123xyz123abc"):split('123', {plain = true, strict = true}), {"abc", "", "xyz", "abc"}) -- limit split count - assert_array_eq(("1\n\n2\n3"):split('\n', {limit = 2}), {"1", "2\n3"}) - assert_array_eq(("1\n\n2\n3"):split('\n', {limit = 2, strict = true}), {"1", "\n2\n3"}) - assert_array_eq(("1.2.3.4.5"):split('%.', {limit = 3}), {"1", "2", "3.4.5"}) -end - -function main() - test_split() - test_startswith() - test_endswith() + t:are_equal(("1\n\n2\n3"):split('\n', {limit = 2}), {"1", "2\n3"}) + t:are_equal(("1\n\n2\n3"):split('\n', {limit = 2, strict = true}), {"1", "\n2\n3"}) + t:are_equal(("1.2.3.4.5"):split('%.', {limit = 3}), {"1", "2", "3.4.5"}) + t:are_equal(("123.45"):split('%.', {limit = 3}), {"123", "45"}) end diff --git a/tests/projects/c++/console/test.lua b/tests/projects/c++/console/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/c++/console/test.lua +++ b/tests/projects/c++/console/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/c++/precompiled_header/test.lua b/tests/projects/c++/precompiled_header/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/c++/precompiled_header/test.lua +++ b/tests/projects/c++/precompiled_header/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/c++/shared_library/test.lua b/tests/projects/c++/shared_library/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/c++/shared_library/test.lua +++ b/tests/projects/c++/shared_library/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/c++/static_library/test.lua b/tests/projects/c++/static_library/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/c++/static_library/test.lua +++ b/tests/projects/c++/static_library/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/c/console/test.lua b/tests/projects/c/console/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/c/console/test.lua +++ b/tests/projects/c/console/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/c/precompiled_header/test.lua b/tests/projects/c/precompiled_header/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/c/precompiled_header/test.lua +++ b/tests/projects/c/precompiled_header/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/c/shared_library/test.lua b/tests/projects/c/shared_library/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/c/shared_library/test.lua +++ b/tests/projects/c/shared_library/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/c/static library with spaces/test.lua b/tests/projects/c/static library with spaces/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/c/static library with spaces/test.lua +++ b/tests/projects/c/static library with spaces/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/c/static_library/test.lua b/tests/projects/c/static_library/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/c/static_library/test.lua +++ b/tests/projects/c/static_library/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/dlang/console/test.lua b/tests/projects/dlang/console/test.lua index ba71889a5..b423189da 100644 --- a/tests/projects/dlang/console/test.lua +++ b/tests/projects/dlang/console/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build() + t:build() end end diff --git a/tests/projects/dlang/shared_library/test.lua b/tests/projects/dlang/shared_library/test.lua index ba71889a5..b423189da 100644 --- a/tests/projects/dlang/shared_library/test.lua +++ b/tests/projects/dlang/shared_library/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build() + t:build() end end diff --git a/tests/projects/dlang/static_library/test.lua b/tests/projects/dlang/static_library/test.lua index ba71889a5..b423189da 100644 --- a/tests/projects/dlang/static_library/test.lua +++ b/tests/projects/dlang/static_library/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build() + t:build() end end diff --git a/tests/projects/go/console/test.lua b/tests/projects/go/console/test.lua index ba71889a5..b423189da 100644 --- a/tests/projects/go/console/test.lua +++ b/tests/projects/go/console/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build() + t:build() end end diff --git a/tests/projects/go/static_library/test.lua b/tests/projects/go/static_library/test.lua index ba71889a5..b423189da 100644 --- a/tests/projects/go/static_library/test.lua +++ b/tests/projects/go/static_library/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build() + t:build() end end diff --git a/tests/projects/objc++/console/test.lua b/tests/projects/objc++/console/test.lua index 08bb16360..f73079781 100644 --- a/tests/projects/objc++/console/test.lua +++ b/tests/projects/objc++/console/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build({iphoneos = true}) + t:build({iphoneos = true}) end end diff --git a/tests/projects/objc/console/test.lua b/tests/projects/objc/console/test.lua index 08bb16360..f73079781 100644 --- a/tests/projects/objc/console/test.lua +++ b/tests/projects/objc/console/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build({iphoneos = true}) + t:build({iphoneos = true}) end end diff --git a/tests/projects/other/merge_object/test.lua b/tests/projects/other/merge_object/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/other/merge_object/test.lua +++ b/tests/projects/other/merge_object/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/other/merge_static_library/test.lua b/tests/projects/other/merge_static_library/test.lua index 84f48ef1c..b76241be2 100644 --- a/tests/projects/other/merge_static_library/test.lua +++ b/tests/projects/other/merge_static_library/test.lua @@ -1,9 +1,6 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project - build() + t:build() end diff --git a/tests/projects/package/basic/test.lua b/tests/projects/package/basic/test.lua index 2db93991f..a4b905689 100644 --- a/tests/projects/package/basic/test.lua +++ b/tests/projects/package/basic/test.lua @@ -1,10 +1,7 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- TODO -- build project --- build() +-- t:build() end diff --git a/tests/projects/rust/console/test.lua b/tests/projects/rust/console/test.lua index ba71889a5..b423189da 100644 --- a/tests/projects/rust/console/test.lua +++ b/tests/projects/rust/console/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build() + t:build() end end diff --git a/tests/projects/rust/static_library/test.lua b/tests/projects/rust/static_library/test.lua index ba71889a5..b423189da 100644 --- a/tests/projects/rust/static_library/test.lua +++ b/tests/projects/rust/static_library/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build() + t:build() end end diff --git a/tests/projects/swift/console/test.lua b/tests/projects/swift/console/test.lua index 08bb16360..f73079781 100644 --- a/tests/projects/swift/console/test.lua +++ b/tests/projects/swift/console/test.lua @@ -1,11 +1,8 @@ --- imports -import("...build") - -- main entry -function main() +function main(t) -- build project if os.host() == "macosx" then - build({iphoneos = true}) + t:build({iphoneos = true}) end end diff --git a/tests/run.lua b/tests/run.lua new file mode 100644 index 000000000..3658e4d22 --- /dev/null +++ b/tests/run.lua @@ -0,0 +1,64 @@ +-- imports +import("core.project.task") +import("core.base.option") + +local params = {} + +if option.get("quiet") then table.insert(params, "-q") end +if option.get("yes") then table.insert(params, "-y") end +if option.get("verbose") then table.insert(params, "-v") end +if option.get("diagnosis") then table.insert(params, "-D") end + +function _run_test(script) + + assert(script:endswith("test.lua")) + + local old_dir = os.cd(os.scriptdir()) + os.execv("xmake", table.join("lua", params, "./runner.lua", script)) + os.cd(old_dir) + +end + +-- run test with the given name +function _run_test_filter(name) + + local tests = {} + + -- find the test script + for _, script in ipairs(os.files(path.join(os.scriptdir(), "**", name, "**", "test.lua"))) do + table.insert(tests, path.absolute(script)) + end + for _, script in ipairs(os.files(path.join(os.scriptdir(), name, "**", "test.lua"))) do + table.insert(tests, path.absolute(script)) + end + for _, script in ipairs(os.files(path.join(os.scriptdir(), "**", name, "test.lua"))) do + table.insert(tests, path.absolute(script)) + end + for _, script in ipairs(os.files(path.join(os.scriptdir(), name, "test.lua"))) do + table.insert(tests, path.absolute(script)) + end + + tests = table.unique(tests) + if #tests == 0 then + utils.warning("no test have run, please check your filter .. (%s)", name) + else + cprint("> %d test(s) found", #tests) + if option.get("diagnosis") then + for _, v in ipairs(tests) do + cprint("> %s", v) + end + end + + for _, v in ipairs(tests) do + _run_test(v) + end + cprint("> %d test(s) succeed", #tests) + end +end + +-- main entry +function main(name) + + -- run the given test + return _run_test_filter(name or "/") +end diff --git a/tests/runner.lua b/tests/runner.lua new file mode 100644 index 000000000..b4325edc2 --- /dev/null +++ b/tests/runner.lua @@ -0,0 +1,76 @@ +import("core.base.option") +import("test_utils.test_assert") +import("test_utils.print_error") +import("test_utils.build", { alias = "test_build" }) + +function main(script) + + if os.isdir(script) then + script = path.join(script, "test.lua") + end + script = path.absolute(script) + assert(path.filename(script) == "test.lua", "file should named `test.lua`") + assert(os.isfile(script), "should be a file") + + -- disable statistics + os.setenv("XMAKE_STATS", "false") + + -- init test context + local context = + { + filename = script + } + context = test_assert(context) + function context:build(argv) + test_build(argv) + end + + local root = path.directory(script) + + local verbose = option.get("verbose") or option.get("diagnosis") + + -- trace + cprint(">> testing %s ...", path.relative(root)) + + -- get test functions + local data = import("test", { rootdir = root, anonymous = true }) + + if data.main then + -- ignore everthing when we found a main function + data = { test_main = data.main } + end + + -- enter script directory + local old_dir = os.cd(root) + + -- run test + local succeed_count = 0 + for k, v in pairs(data) do + if k:startswith("test") and type(v) == "function" then + if verbose then print(">> running %s ...", k) end + context.func = v + context.funcname = k + try + { + function () + v(context) + end, + catch + { + function (error) + if not error:find("aborting because of ") then + context:print_error(error, v, "unhandled error") + else + raise(error) + end + end + } + } + succeed_count = succeed_count + 1 + end + end + if verbose then print(">> finished %d test method(s) ...", succeed_count) end + + -- leave script directory + os.cd(old_dir) +end diff --git a/tests/test.lua b/tests/test.lua deleted file mode 100644 index faa7755e0..000000000 --- a/tests/test.lua +++ /dev/null @@ -1,48 +0,0 @@ --- imports -import("core.project.task") - --- run test with the given name -function _run_test(name) - - -- find the test script - for _, script in ipairs(os.files(path.join(os.scriptdir(), "**", name, "test.lua"))) do - - -- trace - print("testing %s ...", script) - - -- enter script directory - os.cd(path.directory(script)) - - -- run test - task.run("lua", {script = path.filename(script)}) - break - end -end - --- main entry -function main(name) - - -- disable statistics - os.setenv("XMAKE_STATS", "false") - - -- run the given test - if name then - return _run_test(name) - end - - -- run all tests - for _, script in ipairs(os.files(path.join(os.scriptdir(), "**", "test.lua"))) do - - -- trace - print("testing %s ...", script) - - -- enter script directory - local oldir = os.cd(path.directory(script)) - - -- run test - task.run("lua", {script = path.filename(script)}) - - -- leave script directory - os.cd(oldir) - end -end diff --git a/tests/test/test.lua b/tests/test/test.lua new file mode 100644 index 000000000..5f0321da4 --- /dev/null +++ b/tests/test/test.lua @@ -0,0 +1,58 @@ +function test_are_same(t) + t:are_same(1, 1) + t:are_same("1", "1") + t:are_same(nil, nil) + t:are_same(true, true) + t:are_same(1.34, 1.34) + t:are_same(test_are_same, test_are_same) + + t:are_not_same({}, {}) + t:are_not_same(1, "1") +end + +function test_are_equal(t) + t:are_equal(1, 1) + t:are_equal("1", "1") + t:are_equal(nil, nil) + t:are_equal(true, true) + t:are_equal(1.34, 1.34) + t:are_equal(test_are_same, test_are_same) + t:are_equal({}, {}) + t:are_equal({ a = 1 }, { a = 1 }) + t:are_equal({ a = { a= 1}}, { a = {a=1} }) + + t:are_not_equal(1, "1") + t:are_not_equal({ a = 1 }, { a = 1, b = 2 }) + t:are_not_equal({ a = 1, c = 3 }, { a = 1, b = 2 }) + t:are_not_equal({ a = 1}, { a = 1, b = 2 }) + t:are_not_equal({ a = { a= 1}}, { a = {a=2} }) +end + + +function test_require(t) + t:require(true) + t:require({}) + t:require(0) + t:require("") + + t:require_not(false) + t:require_not(nil) +end + +function test_will_raise(t) + t:will_raise(function () + raise("error: xxx") + end, "error") + t:will_raise(function () + raise(" ") + end, "%s") + t:will_raise(function () + raise("") + end) + + t:will_raise(function () + print("A test failed message will be printed") + t:will_raise(function() end) + end, "aborting because of ${red}failed assertion${reset}") +end + diff --git a/tests/build.lua b/tests/test_utils/build.lua index 7725f1ddc..7725f1ddc 100644 --- a/tests/build.lua +++ b/tests/test_utils/build.lua diff --git a/tests/test_utils/check.lua b/tests/test_utils/check.lua new file mode 100644 index 000000000..bc9ddb6b6 --- /dev/null +++ b/tests/test_utils/check.lua @@ -0,0 +1,38 @@ + + +function _tablelength(table) + local count = 0 + for _ in pairs(table) do count = count + 1 end + return count +end + +function _get_rep(value) + if type(value) == "nil" then return "(nil)" end + if type(value) == "string" then return "`" .. value .. "`" end + return tostring(value) +end + +function same(actual, expacted) + return actual == expacted, _get_rep(actual), _get_rep(expacted) +end + +function equal(actual, expacted) + local same, ap, ep = same(actual, expacted) + if same then return true, ap, ep end + if type(expacted) == "table" and type(actual) == "table" then + local al = _tablelength(actual) + local el = _tablelength(expacted) + if al ~= el then + return false, vformat("{...(%d elements)}", al), vformat("{...(%d elements)}", el) + end + for k, v in pairs(expacted) do + local av = actual[k] + local eq, app, epp = equal(av, v) + if not eq then + return false, vformat("{[%s] = %s, ...}", k, app), vformat("{[%s] = %s, ...}", k, epp) + end + end + return true, "{...}", "{...}" + end + return false, ap, ep +end
\ No newline at end of file diff --git a/tests/test_utils/print_error.lua b/tests/test_utils/print_error.lua new file mode 100644 index 000000000..dcf1e82bb --- /dev/null +++ b/tests/test_utils/print_error.lua @@ -0,0 +1,31 @@ + +function main(t, message, funcs_or_filename, abort_reason) + local funcs = nil + if type(funcs_or_filename) == "function" then + funcs = { debug.getinfo(funcs_or_filename) } + elseif type(funcs_or_filename) == "table" then + funcs = {} + for _, v in ipairs(funcs_or_filename) do + table.insert(funcs, debug.getinfo(v)) + end + else + assert(type(funcs_or_filename) == "string") + funcs = {} + local uplevel = 2 + local func = nil + repeat + func = debug.getinfo(uplevel) + if (func and func.source:endswith(funcs_or_filename)) then + table.insert(funcs, func) + end + uplevel = uplevel + 1 + until func == nil + end + cprint(">> ${red}test failed:${reset} %s", message) + for _, func in ipairs(funcs) do + local line = (func.currentline >= 0) and func.currentline or func.linedefined + local name = (func.func == t.func) and t.funcname or func.name or "(anonymous)" + cprint(">> function %s ${underline}%s${reset}:${bright}%d${reset}", name, func.source, line) + end + raise("aborting because of ${red}%s${reset} ...", abort_reason or "failed assertion") +end
\ No newline at end of file diff --git a/tests/test_utils/test_assert.lua b/tests/test_utils/test_assert.lua new file mode 100644 index 000000000..42a276d87 --- /dev/null +++ b/tests/test_utils/test_assert.lua @@ -0,0 +1,70 @@ + +import("print_error") +import("check") + +local test_assert = { print_error = print_error.main } + +function test_assert:require(value) + if not value then + self:print_error(vformat("require ${green}true${reset} but got ${red}%s${reset}", value), self.filename) + end +end + +function test_assert:require_not(value) + if value then + self:print_error(vformat("require ${green}false${reset} but got ${red}%s${reset}", value), self.filename) + end +end + +function test_assert:are_same(actual, expacted) + local r, ap, ep = check.same(actual, expacted) + if not r then + self:print_error(format("expacted: ${green}%s${reset}, actual ${red}%s${reset}", ep, ap), self.filename) + end +end + +function test_assert:are_not_same(actual, expacted) + local r, ap, ep = check.same(actual, expacted) + if r then + self:print_error(format("expacted: ${green}%s${reset}, actual ${red}%s${reset}", ep, ap), self.filename) + end +end + + +function test_assert:are_equal(actual, expacted) + local r, ap, ep = check.equal(actual, expacted) + if not r then + self:print_error(format("expacted: ${green}%s${reset}, actual ${red}%s${reset}", ep, ap), self.filename) + end +end + +function test_assert:are_not_equal(actual, expacted) + local r, ap, ep = check.equal(actual, expacted) + if r then + self:print_error(format("expacted: ${green}%s${reset}, actual ${red}%s${reset}", ep, ap), self.filename) + end +end + +test_assert._will_raise_stack = {} +function test_assert:will_raise(func, message_pattern) + table.insert(self._will_raise_stack, 1, debug.getinfo(2).func) + try{ + func, + finally{ + function (ok, error) + local funcs = { func, unpack(self._will_raise_stack) } + if ok then + self:print_error("expected raise but finished successfully", funcs) + elseif message_pattern and not string.find(error, message_pattern) then + self:print_error(format("expected raise with message ${green}%s${reset} but got ${red}%s${reset}", message_pattern, error), funcs) + end + end + } + } + table.remove(self._will_raise_stack, 1) +end + +function main(context) + table.join2(context, test_assert) + return context +end
\ No newline at end of file |
