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 | |
| parent | e165a8e43abed4eb2eca64c3b5adefb57cd65600 (diff) | |
| parent | 8049d92b3e9aef35e4f124e5ae361bdd1f5c5435 (diff) | |
Merge pull request #444 from OpportunityLiu/test
Improve test library
45 files changed, 704 insertions, 396 deletions
diff --git a/.appveyor.yml b/.appveyor.yml index 48a726de3..b4bc90e4f 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -5,37 +5,91 @@ os: - Visual Studio 2017 platform: - - Win32 - - Win64 + - x86 + - x64 install: + # prepare tools + - ps: Push-Location C:/ + - ps: New-Item ./winenv/bin -ItemType Directory + # NSIS + - ps: Invoke-WebRequest "https://svwh.dl.sourceforge.net/project/nsis/NSIS 3/3.04/nsis-3.04.zip" -UseBasicParsing -OutFile ./nsis.zip + - ps: Expand-Archive ./nsis.zip -DestinationPath ./nsis + - ps: Move-Item ./nsis/*/* ./nsis + # unzip + - ps: Invoke-WebRequest "https://svwh.dl.sourceforge.net/project/gnuwin32/unzip/5.51-1/unzip-5.51-1-bin.zip" -UseBasicParsing -OutFile .\unzip.zip + - ps: Expand-Archive ./unzip.zip -DestinationPath ./unzip + - ps: Copy-Item ./unzip/bin/unzip.exe ./winenv/bin + # curl + - ps: Invoke-WebRequest "https://curl.haxx.se/windows/dl-7.65.1/curl-7.65.1-win$(if ($env:platform -eq 'x64') { '64' } else { '32' })-mingw.zip" -UseBasicParsing -OutFile .\curl.zip + - ps: Expand-Archive ./curl.zip -DestinationPath ./curl + - ps: Move-Item ./curl/*/* ./curl + - ps: Copy-Item ./curl/bin/curl.exe ./winenv/bin + - ps: Copy-Item ./curl/bin/curl-ca-bundle.crt ./winenv/bin + # + - ps: Pop-Location + # install xmake - ps: ./scripts/get.ps1 -branch $env:APPVEYOR_REPO_BRANCH +before_build: + - ps: (Get-Command xmake).FileVersionInfo.ProductVersion + build_script: - - ps: Get-Command xmake - - ps: xmake --version - - ps: xmake build --project=core - - ps: |- - Set-AppveyorBuildVariable -Name XMAKE_PROGRAM_DIR -Value $(Join-Path $(Get-Location) "xmake") - .\core\build\xmake.exe --version - Copy-Item -Force core\build\xmake.exe $(Get-Command xmake).Source + # self build + - ps: Push-Location ./core + - ps: xmake config --arch=$env:platform + - ps: xmake build + - ps: Pop-Location + # use new xmake + - ps: Set-AppveyorBuildVariable -Name XMAKE_PROGRAM_DIR -Value $(Resolve-Path ./xmake) + - ps: Copy-Item -Force .\core\build\xmake.exe $(Get-Command xmake).Path + - ps: (Get-Command xmake).FileVersionInfo.ProductVersion after_build: - - ps: |- - Copy-Item .\core\build\xmake.exe .\xmake - Copy-Item .\*.md .\xmake - Compress-Archive -Path .\xmake -DestinationPath .\archive.zip -CompressionLevel Optimal - Push-AppveyorArtifact .\xmake\xmake.exe -FileName xmake.exe -DeploymentName "xmake-executable" - Push-AppveyorArtifact .\archive.zip -FileName xmake.zip -DeploymentName "xmake-archive" + # publish exe + - ps: Push-AppveyorArtifact .\core\build\xmake.exe -FileName xmake.exe -DeploymentName "xmake-executable" + # compose & publish installer + - ps: Copy-Item C:\winenv\ . -Recurse + - ps: $version = (Get-Command xmake).FileVersionInfo + - ps: C:\nsis\makensis.exe + /DMAJOR=$($version.ProductMajorPart) + /DMINOR=$($version.ProductMinorPart) + /DALTER=$($version.ProductBuildPart) + /DBUILD=$($($version.ProductVersion -split '\+')[1]) + /D$($env:platform) + .\scripts\installer.nsi + - ps: Push-AppveyorArtifact .\scripts\xmake.exe -FileName xmake-installer.exe -DeploymentName "xmake-installer" + # publish zip archive + - ps: Copy-Item .\*.md .\xmake + - ps: Copy-Item C:\winenv\ .\xmake -Recurse + - ps: Copy-Item .\core\build\xmake.exe .\xmake + - ps: Compress-Archive -Path .\xmake\* -DestinationPath .\archive.zip -CompressionLevel Optimal + - ps: Push-AppveyorArtifact .\archive.zip -FileName xmake.zip -DeploymentName "xmake-archive" test_script: - ps: xmake --version + - ps: Push-Location ./tests + - ps: |- + $tests = Get-ChildItem test.lua -Recurse | ForEach-Object { + $testname = "$(Resolve-Path $_.Directory -Relative)" + $filename = "$(Resolve-Path $_ -Relative)" + $fullname = "$($_.FullName)" + Add-AppveyorTest -Name $testname -Framework "xmake-test" -FileName $filename -Outcome None + return @{ testname = $testname; filename = $filename; fullname = $fullname } + } + - ps: $all_success = $true - ps: |- - Add-AppveyorTest -Name "tests" -Framework "xmake-test" -FileName ./tests/test.lua -Outcome Running - $time = Measure-Command { - $results = xmake lua --verbose --diagnosis tests\test.lua 2>&1 - $outcome = if ($?) { "Passed" } else { "Failed" } - $stdout = [string]::Join("`n", $results) + $tests | ForEach-Object { + $testname = $_.testname + $filename = $_.filename + Update-AppveyorTest -Name $testname -Framework "xmake-test" -FileName $filename -Outcome Running + $time = Measure-Command { + $results = xmake lua --verbose --diagnosis runner.lua $_.fullname + $outcome = if ($?) { "Passed" } else { $all_success = $false; "Failed" } + $stdout = [string]::Join("`n", $results) + } + Update-AppveyorTest -Name $testname -Framework "xmake-test" -FileName $filename -Outcome $outcome -Duration $time.TotalMilliseconds -StdOut $stdout } - Update-AppveyorTest -Name "tests" -Framework "xmake-test" -FileName ./tests/test.lua -Outcome $outcome -Duration $time.TotalMilliseconds -StdOut $stdout + - ps: Pop-Location + - ps: if ($all_success) { Write-Host "All tests passed!" } else { Write-Error "Some tests failed!" } diff --git a/.gitignore b/.gitignore index cd7ecbe72..33300c4b9 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,4 @@ core/.config.mak winenv xmake-ppa !xmake/actions/build -.vscode/
\ No newline at end of file +.vscode
\ No newline at end of file diff --git a/.travis.yml b/.travis.yml index e03dccb80..c7aef4428 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,5 +28,5 @@ script: - export XMAKE_PROGRAM_DIR=~/.local/share/xmake - cp core/build/xmake "$XMAKE_PROGRAM_DIR" || sudo cp core/build/xmake "$XMAKE_PROGRAM_DIR" - xmake --version - - travis_wait 60 xmake lua -D tests/test.lua + - travis_wait 60 xmake lua -v -D tests/run.lua diff --git a/core/src/demo/xmake.lua b/core/src/demo/xmake.lua index 3184c0240..fd4abbe1f 100644 --- a/core/src/demo/xmake.lua +++ b/core/src/demo/xmake.lua @@ -20,22 +20,22 @@ target("demo") add_includedirs("$(projectdir)", "$(projectdir)/src") -- add the common source files - add_files("**.c") - + add_files("**.c") + -- add the resource files (it will be enabled after publishing new version) if is_plat("windows") then add_files("*.rc") end - + -- add links if is_plat("windows") then - add_links("ws2_32", "advapi32") - elseif is_plat("android") then - add_links("m", "c") + add_links("ws2_32", "advapi32") + elseif is_plat("android") then + add_links("m", "c") elseif is_plat("macosx") then add_ldflags("-all_load", "-pagezero_size 10000", "-image_base 100000000") else - add_links("pthread", "dl", "m", "c") + add_links("pthread", "dl", "m", "c") end -- copy target to the build directory diff --git a/core/src/demo/xmake.rc b/core/src/demo/xmake.rc index 1f3a7dbf8..0b5b17fbe 100644 --- a/core/src/demo/xmake.rc +++ b/core/src/demo/xmake.rc @@ -1 +1,43 @@ -IDI_APP ICON DISCARDABLE "xmake.ico"
+#include "xmake.config.h" +#include "winres.h" + +#define _STR(x) #x +#define STR(x) _STR(x) + +VS_VERSION_INFO VERSIONINFO + FILEVERSION XM_CONFIG_VERSION_MAJOR, XM_CONFIG_VERSION_MINOR, XM_CONFIG_VERSION_ALTER, 0 + PRODUCTVERSION XM_CONFIG_VERSION_MAJOR, XM_CONFIG_VERSION_MINOR, XM_CONFIG_VERSION_ALTER, 0 + FILEFLAGSMASK VS_FFI_FILEFLAGSMASK +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + FILEOS VOS_NT_WINDOWS32 + FILETYPE VFT_APP + FILESUBTYPE VFT2_UNKNOWN +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "000004B0" + BEGIN + VALUE "Comments", "A cross-platform build utility based on Lua\nwebsite: https://xmake.io" + VALUE "CompanyName", "The TBOOX Open Source Group" + VALUE "FileDescription", "XMake build utility" + VALUE "FileVersion", XM_CONFIG_VERSION "+" STR(XM_CONFIG_VERSION_BUILD) + VALUE "InternalName", "xmake" + VALUE "LegalCopyright", "Copyright (C) 2015-2019 Ruki Wang, tboox.org, xmake.io\nCopyright (C) 2005-2015 Mike Pall, luajit.org" + VALUE "LegalTrademarks", "" + VALUE "OriginalFilename", "xmake.exe" + VALUE "ProductName", "XMake" + VALUE "ProductVersion", XM_CONFIG_VERSION "+" STR(XM_CONFIG_VERSION_BUILD) + END + END + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x0, 1200 + END +END + + +IDI_APP ICON DISCARDABLE "xmake.ico" @@ -97,7 +97,7 @@ uninstall: @echo ok! test: - @xmake lua --backtrace tests/test.lua $(name) + @xmake lua --verbose tests/run.lua $(name) @echo ok! .PHONY: tip build install uninstall diff --git a/scripts/get.ps1 b/scripts/get.ps1 index 8bce62861..d471affd5 100644 --- a/scripts/get.ps1 +++ b/scripts/get.ps1 @@ -5,7 +5,7 @@ param ( [string]$branch = "master", [string]$version = "v2.2.6", - [string]$installdir = $(Join-Path $(if($env:HOME) { $env:HOME } else { "C:\" }) 'xmake') + [string]$installdir = $(Join-Path $(if($HOME) { $HOME } else { "C:\" }) 'xmake') ) function myExit($code) { @@ -24,13 +24,15 @@ function writeLogoLine($msg) { Write-Host $msg -BackgroundColor White -ForegroundColor DarkBlue } -writeLogoLine ' _ ' -writeLogoLine ' __ ___ __ __ __ _| | ______ ' -writeLogoLine ' \ \/ / | \/ |/ _ | |/ / __ \ ' -writeLogoLine ' > < | \__/ | /_| | < ___/ ' -writeLogoLine ' /_/\_\_|_| |_|\__ \|_|\_\____| getter ' -writeLogoLine ' ' -writeLogoLine ' ' +if (-not $env:CI) { + writeLogoLine ' _ ' + writeLogoLine ' __ ___ __ __ __ _| | ______ ' + writeLogoLine ' \ \/ / | \/ |/ _ | |/ / __ \ ' + writeLogoLine ' > < | \__/ | /_| | < ___/ ' + writeLogoLine ' /_/\_\_|_| |_|\__ \|_|\_\____| getter ' + writeLogoLine ' ' + writeLogoLine ' ' +} if ($PSVersionTable.PSVersion.Major -lt 5) { writeErrorTip 'Sorry but PowerShell v5+ is required' diff --git a/scripts/installer.nsi b/scripts/installer.nsi index aac0a9ea8..a24d12ab5 100644 --- a/scripts/installer.nsi +++ b/scripts/installer.nsi @@ -11,20 +11,50 @@ !include "WordFunc.nsh"
!include "WinMessages.nsh"
+; xmake version Information
+!ifndef MAJOR
+ !define MAJOR 2
+!endif
+!ifndef MINOR
+ !define MINOR 2
+!endif
+!ifndef ALTER
+ !define ALTER 6
+!endif
+!ifndef BUILD
+ !define BUILD 201906070000
+!endif
+
+!define VERSION ${MAJOR}.${MINOR}.${ALTER}
+!define VERSION_FULL ${VERSION}+${BUILD}
+
+!ifdef x64
+ !define ARCH x64
+!else
+ !define ARCH x86
+!endif
+
;--------------------------------
; The name of the installer
-Name "xmake-v2.2.6"
+Name "XMake - v${VERSION}"
; The file to write
OutFile "xmake.exe"
; The default installation directory
-InstallDir $PROGRAMFILES\xmake
+!ifdef x64
+ InstallDir $PROGRAMFILES64\XMake
+!else
+ InstallDir $PROGRAMFILES\XMake
+!endif
; Request application privileges for Windows Vista
RequestExecutionLevel admin
+; Set DPI Aware
+ManifestDPIAware true
+
;--------------------------------
; Interface Settings
@@ -60,13 +90,17 @@ RequestExecutionLevel admin ;--------------------------------
; Version Information
-VIProductVersion "2.2.6.0526"
-VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "XMake"
-VIAddVersionKey /LANG=${LANG_ENGLISH} "Comments" "website: https://xmake.io"
-VIAddVersionKey /LANG=${LANG_ENGLISH} "CompanyName" "The TBOOX Open Source Group"
-VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" "Copyright 2015-2019 tboox.org"
-VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "A cross-platform build utility based on Lua"
-VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "2.2.6"
+
+VIProductVersion ${VERSION}.0
+VIFileVersion ${VERSION}.0
+VIAddVersionKey /LANG=0 ProductName XMake
+VIAddVersionKey /LANG=0 Comments "A cross-platform build utility based on Lua$\nwebsite: https://xmake.io"
+VIAddVersionKey /LANG=0 CompanyName "The TBOOX Open Source Group"
+VIAddVersionKey /LANG=0 LegalCopyright "Copyright (C) 2015-2019 Ruki Wang, tboox.org, xmake.io$\nCopyright (C) 2005-2015 Mike Pall, luajit.org"
+VIAddVersionKey /LANG=0 FileDescription "XMake Installer - v${VERSION}"
+VIAddVersionKey /LANG=0 OriginalFilename "xmake-${ARCH}.exe"
+VIAddVersionKey /LANG=0 FileVersion ${VERSION_FULL}
+VIAddVersionKey /LANG=0 ProductVersion ${VERSION_FULL}
;--------------------------------
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 |
