Skip to content

Commit

Permalink
Add first unit tests to Gosu (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlnr committed Jan 7, 2017
1 parent 4a3af3d commit 7058b5c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 11 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Expand Up @@ -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("=") }'
Expand Down Expand Up @@ -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 ../..
6 changes: 6 additions & 0 deletions Rakefile
Expand Up @@ -2,6 +2,7 @@ require 'rubygems'
require 'fileutils'
require 'date'
require 'rake/extensiontask'
require 'rake/testtask'

COMMON_FILES = FileList[
'.yardopts',
Expand Down Expand Up @@ -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
14 changes: 6 additions & 8 deletions appveyor.yml
Expand Up @@ -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
21 changes: 21 additions & 0 deletions 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
64 changes: 64 additions & 0 deletions 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

0 comments on commit 7058b5c

Please sign in to comment.