From 7058b5c185f160a63b922d5d22b1f4a86fac47f3 Mon Sep 17 00:00:00 2001 From: Julian Raschke Date: Sat, 7 Jan 2017 17:58:38 +0800 Subject: [PATCH] Add first unit tests to Gosu (#360) --- .travis.yml | 5 ++-- Rakefile | 6 ++++ appveyor.yml | 14 ++++----- test/test_constants.rb | 21 ++++++++++++++ test/test_interface.rb | 64 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 test/test_constants.rb create mode 100644 test/test_interface.rb diff --git a/.travis.yml b/.travis.yml index 472d24bf7..7704ee963 100644 --- a/.travis.yml +++ b/.travis.yml @@ -36,7 +36,7 @@ before_install: # Mac dependencies # See: https://github.com/gosu/gosu/wiki/Getting-Started-on-OS-X - if [ $TRAVIS_OS_NAME == osx ]; then brew update; brew install sdl2; fi - # Do not use RVM - it's been a mess for compiling Gosu on OS X. + # Do not use RVM - it's been a mess for compiling Gosu on macOS. - rvm use system # Print the Ruby configuration to see what's up with CXXFLAGS etc. - ruby -rrbconfig -e 'RbConfig::CONFIG.each { |k, v| puts [k, v].join("=") }' @@ -77,7 +77,6 @@ install: script: # Ruby/Gosu - - ruby -rgosu -e 'puts RUBY_VERSION, Gosu::VERSION, Gosu::LICENSES' - + - ~/rake -rgosu test # Compile C++ examples - mkdir -p examples/build && cd examples/build && cmake .. && make && cd ../.. diff --git a/Rakefile b/Rakefile index c5497bdfb..73451a88d 100644 --- a/Rakefile +++ b/Rakefile @@ -2,6 +2,7 @@ require 'rubygems' require 'fileutils' require 'date' require 'rake/extensiontask' +require 'rake/testtask' COMMON_FILES = FileList[ '.yardopts', @@ -51,3 +52,8 @@ task :update_doxygen do sh "ssh #{ENV['PROJECTS_HOST']} 'cd #{ENV['PROJECTS_ROOT']}/libgosu.org/ && " + "svn checkout https://github.com/gosu/gosu/trunk/Gosu && PATH=../doxygen/bin:$PATH doxygen'" end + +Rake::TestTask.new do |t| + t.test_files = FileList["test/test_*.rb"] + t.verbose = true +end diff --git a/appveyor.yml b/appveyor.yml index 216a688c6..2a0c0116a 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -21,16 +21,14 @@ build: verbosity: minimal after_build: - ps: >- - cd "$env:APPVEYOR_BUILD_FOLDER\lib" - - $OLD_PATH = $env:PATH - If ($env:PLATFORM -Eq "x64") { - $env:PATH = "C:\Ruby23-x64\bin;$OLD_PATH" - ruby -I. -r./gosu.rb -e "puts RUBY_VERSION, Gosu::VERSION, Gosu::LICENSES" + $env:PATH = "C:\Ruby23-x64\bin;$env:PATH" + gem install rake-compiler + rake -Ilib -rgosu test } Else { - $env:PATH = "C:\Ruby23\bin;$OLD_PATH" - ruby -I. -r./gosu.rb -e "puts RUBY_VERSION, Gosu::VERSION, Gosu::LICENSES" + $env:PATH = "C:\Ruby23\bin;$env:PATH" + gem install rake-compiler + rake -Ilib -rgosu test } test: off diff --git a/test/test_constants.rb b/test/test_constants.rb new file mode 100644 index 000000000..b9a9aab5e --- /dev/null +++ b/test/test_constants.rb @@ -0,0 +1,21 @@ +require "minitest/autorun" +require "gosu" unless defined? Gosu + +class TestConstants < Minitest::Test + def test_version + assert_match(/\A#{Gosu::MAJOR_VERSION}.#{Gosu::MINOR_VERSION}.#{Gosu::POINT_VERSION}/, + Gosu::VERSION) + end + + def test_licenses + assert_match(/Gosu/, Gosu::LICENSES) + assert_match(/SDL/, Gosu::LICENSES) + unless RUBY_PLATFORM =~ /darwin/ + assert_match(/libsndfile/, Gosu::LICENSES) + assert_match(/OpenAL/, Gosu::LICENSES) if RUBY_PLATFORM =~ /win/ + end + + # Backward compatibility + assert_equal Gosu::LICENSES, Gosu::GOSU_COPYRIGHT_NOTICE + end +end diff --git a/test/test_interface.rb b/test/test_interface.rb new file mode 100644 index 000000000..682de8def --- /dev/null +++ b/test/test_interface.rb @@ -0,0 +1,64 @@ +require "minitest/autorun" +require "gosu" unless defined? Gosu + +# Make a backup of the Gosu modules and its helpers on Numeric. +OrigGosu = Gosu +Object.send :remove_const, :Gosu +%w(gosu_to_radians radians_to_gosu degrees_to_radians radians_to_degrees).each do |helper| + Numeric.send :alias_method, :"orig_#{helper}", :"#{helper}" + Numeric.send :undef_method, :"#{helper}" +end + +# Now load Gosu's documentation, a module that is only filled with method stubs, into GosuDocs. +require_relative "../rdoc/gosu.rb" +GosuDocs = Gosu +Object.send :remove_const, :Gosu + +# And finally, restore the real Gosu module. +Gosu = OrigGosu +Object.send :remove_const, :OrigGosu +%w(gosu_to_radians radians_to_gosu degrees_to_radians radians_to_degrees).each do |helper| + Numeric.send :undef_method, :"#{helper}" + Numeric.send :alias_method, :"#{helper}", :"orig_#{helper}" + Numeric.send :undef_method, :"orig_#{helper}" +end + +class TestInterface < Minitest::Test + def test_all_constants_exist + GosuDocs.constants.each do |constant| + assert Gosu.constants.include?(constant), + "Expected constant Gosu::#{constant}" + end + end + + def test_constant_types + GosuDocs.constants.each do |constant| + case constant + when /(Kb|Gp|Ms)/, /(KB_|GP_|MS_)/, /_VERSION/ + expected_class = Integer + when :VERSION, :LICENSES + expected_class = String + else + next + end + + assert_kind_of expected_class, Gosu.const_get(constant), + "Gosu::#{constant} must be #{expected_class}, is #{Gosu.const_get(constant).class}" + end + end + + def test_no_extra_constants + Gosu.constants.each do |constant| + next if constant =~ /KB_|GP_|MS_/ # TODO: not yet documented + + next if constant == :Button # backwards compatibility + + next if constant == :ImmutableColor # implementation detail + + next if constant == :MAX_TEXTURE_SIZE # not sure if we still need this :/ + + assert GosuDocs.constants.include?(constant), + "Unexpected Gosu::#{constant}" + end + end +end