From aa6f88d3279e9004ccbee975b8c9581944d861b5 Mon Sep 17 00:00:00 2001 From: Ali Lloyd Date: Wed, 8 May 2019 22:33:27 +0100 Subject: [PATCH] Add tests and mechanism for running them through Travis CI This patch adds tests for the PuzzleTools library and ensures they are run through Travis CI to verify that they succeed. It adds livecode as a submodule, and instructs travis to build the LiveCode community engine and run the tests. The tests are run using the livecode submodule's test runner via a Makefile which invokes the livecode test Makefile with a few tweaked parameters. --- .gitignore | 1 + .gitmodules | 3 + .travis.yml | 106 +++++++++++++++++++++++++ livecode | 1 + tests/Makefile | 14 ++++ tests/test-scripts/test.livecodescript | 94 ++++++++++++++++++++++ 6 files changed, 219 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 .travis.yml create mode 160000 livecode create mode 100644 tests/Makefile create mode 100644 tests/test-scripts/test.livecodescript diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..795481f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tests/_lcs_test_suite.log diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..61e9691 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "livecode"] + path = livecode + url = https://github.com/livecode/livecode diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..60b24b7 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,106 @@ +sudo: required +dist: trusty + +# Build on both Linux and OSX +os: + - linux + - osx + +# Use a Travis image containing an Xcode we support +# This prevents surprise upgrades! +osx_image: xcode7.3 + +language: c++ + +compiler: + - clang + - gcc + +# Environment variables +env: + global: + - CXX_STD: "c++11" + +jdk: + - openjdk8 + +# Build using clang on mac and gcc on linux +matrix: + exclude: + - os: osx + compiler: gcc + - os: linux + compiler: clang + +# Install any required tools +before_install: + - | + if [[ "$TRAVIS_OS_NAME" == "osx" ]] ; then + rvm --default use 2.2.1 + gem install xcpretty + fi + + - | + if [[ "$TRAVIS_OS_NAME" == "linux" ]] ; then + sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test + sudo apt-get -qq update + sudo apt-get -qq install g++-4.9 + fi + +# Set up the source tree by fetching correct prebuilt objects +install: + - if [[ "$TRAVIS_OS_NAME" == "linux" ]] ; then (cd livecode/prebuilt && ./fetch-libraries.sh linux) ; fi + - if [[ "$TRAVIS_OS_NAME" == "osx" ]] ; then (cd livecode/prebuilt && ./fetch-libraries.sh mac) ; fi + +# Build the default target for LiveCode +script: | + case "${TRAVIS_OS_NAME}" in + linux) + BUILD_PLATFORM=linux + CHECK_COMMAND=xvfb-run + export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"${JAVA_HOME}/jre/lib/amd64/server" + export CXX="g++-4.9" + export CC="gcc-4.9" + ;; + osx) + BUILD_PLATFORM=mac + CHECK_COMMAND= + export XCODE_TARGET_SDK=macosx10.11 + export XCODEBUILD="set -o pipefail && xcodebuild" + export XCODEBUILD_FILTER="| xcpretty" + export JAVA_HOME=$(/usr/libexec/java_home) + ;; + esac + export MODE=debug + + make -C livecode all-${BUILD_PLATFORM} + if [ $? -ne 0 ] ; then + echo " compile failed" + exit 1 + fi + ${CHECK_COMMAND} make -C tests lcs-check + if [ $? -ne 0 ] ; then + echo " tests failed - dumping log file" + cat tests/_lcs_test_suite.log + exit 1 + fi + +addons: + # Packages needed for building LiveCode + apt: + packages: + - gawk + - libx11-dev + - libxext-dev + - libxrender-dev + - libxft-dev + - libxinerama-dev + - libxv-dev + - libxcursor-dev + - libfreetype6-dev + - libgtk2.0-dev + - libpopt-dev + - libesd0-dev + - liblcms2-dev + - xvfb + diff --git a/livecode b/livecode new file mode 160000 index 0000000..4b29ad3 --- /dev/null +++ b/livecode @@ -0,0 +1 @@ +Subproject commit 4b29ad3bf806b7bf175a93ec882187144d0b28f9 diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..72d21e9 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,14 @@ +# This Makefile takes advantage of the fact that the LiveCode engine's +# "tests/Makefile" already does pretty much everything required for +# running a test suite. Rather than duplicating it, it can +# just be included with a few tweaks to its configuration. + +# Override the default value of top_srcdir so that it points to the +# top of the engine repository. +top_srcdir = ../livecode + +LCS_TESTS_DIR = test-scripts + +# Things have now been setup enough that the engine's test Makefile +# can perform the tests without any further configuration. +include $(top_srcdir)/tests/Makefile diff --git a/tests/test-scripts/test.livecodescript b/tests/test-scripts/test.livecodescript new file mode 100644 index 0000000..de68a56 --- /dev/null +++ b/tests/test-scripts/test.livecodescript @@ -0,0 +1,94 @@ +script "TestCiphers" + +on TestSetup + local tSourceFile + put the filename of me into tSourceFile + set the itemdelimiter to slash + put "src/PuzzleTools.livecodescript" into item -3 to -1 of tSourceFile + start using stack tSourceFile +end TestSetup + +private command TestAssertUserThrows pDesc, pHandler, pError + local tError + try + dispatch pHandler to me + catch tError + end try + + TestAssert pDesc, tError is pError + + if tError is not pError then + TestDiagnostic "Expected error:" && pError + TestDiagnostic "Actual error:" && tError + end if +end TestAssertUserThrows + +on LetterToNumberThrow + get toNumber("'") +end LetterToNumberThrow + +on TestLetterToNumber + local tIndex + put 0 into tIndex + repeat for each char tChar in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + add 1 to tIndex + TestAssert "uppercase letter" && tChar && "to number correct", \ + toNumber(tChar) is tIndex + end repeat + + put 0 into tIndex + repeat for each char tChar in "abcdefghijklmnopqrstuvwxyz" + add 1 to tIndex + TestAssert "lowercase letter" && tChar && "to number correct", \ + toNumber(tChar) is tIndex + end repeat + + TestAssertUserThrows "non alphabetic input to toNumber function throws", \ + "LetterToNumberThrow", "char not alphabetical" +end TestLetterToNumber + +on TestLetterFromNumber + local tIndex + put 0 into tIndex + repeat for each char tChar in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + add 1 to tIndex + TestAssert "uppercase letter" && tChar && "from number correct", \ + fromNumber(tIndex) is tChar + end repeat + + put 0 into tIndex + repeat for each char tChar in "abcdefghijklmnopqrstuvwxyz" + add 1 to tIndex + TestAssert "lowercase letter" && tChar && "from number correct", \ + fromNumber(tIndex) is tChar + end repeat +end TestLetterFromNumber + +command _TestCaesarCipher pDesc, pSource, pShift, pExpected + local tResult, tSuccess + put caesarShift(pSource, pShift) into tResult + put tResult is pExpected into tSuccess + TestAssert pDesc, tSuccess + + // If the test failed then log the actual output of caesarShift + if not tSuccess then + TestDiagnostic pDesc && "failed" + TestDiagnostic "Expected result:" && pExpected + TestDiagnostic "Actual result:" && tResult + end if +end _TestCaesarCipher + +on TestCaesarCipher + _TestCaesarCipher "abjurer <-> nowhere (13)", "abjurer", 13, "nowhere" + _TestCaesarCipher "inkier <-> purply (7)", "inkier", 7, "purply" + _TestCaesarCipher "fusion <-> layout (6)", "fusion", 6, "layout" + _TestCaesarCipher "manful <-> thumbs (7)", "manful", 7, "thumbs" + _TestCaesarCipher "primero <-> sulphur (3)", "primero", 3, "sulphur" + _TestCaesarCipher "steeds <-> tuffet (1)", "steeds", 1, "tuffet" +end TestCaesarCipher + +on TestVigenereCipher + TestAssert "LIVECODE encoded using vigenere cipher with key TEST is EMNXVSVX", \ + vigenereShift("LIVECODE","TEST") is "EMNXVSVX" +end TestVigenereCipher +