<commit_before>#!/usr/bin/ruby -w

begin
	require 'tests/muesunittest'
rescue
	require '../muesunittest'
end

require 'mues'
require 'mues/Exceptions'
require 'thread'


class ResponseTestObject
	def oneMethod
		return true
	end
	def twoMethod
		return true
	end
	def redMethod
		return true
	end
	def blueMethod
		return true
	end
end

class LibraryFunctionsTestCase < MUES::TestCase

	### Test instantiation with various arguments
	def test_checkSafeLevel

		# Run this in a thread so we can reduce our $SAFE temporarily
		t = Thread.new {
			$SAFE = 3
			checkSafeLevel()
			raise StandardError, "Failed."
		}

		assert_raises( SecurityError ) { t.join }
		assert_nothing_raised { checkSafeLevel() }
	end

	def test_checkType
		assert_raises( TypeError ) {
			checkType( "a string", Numeric )
		}
		assert_raises( TypeError ) {
			checkType( "a string", Numeric, Hash )
		}
		assert_nothing_raised {
			checkType( "a string", Numeric, Hash, String )
		}
	end

	def test_checkEachType
		things = [ "a string", 5, {'a' => 'test', 'hash' => 'object'} ]


		assert_raises( TypeError ) {
			checkEachType( things, Numeric )
		}
		assert_raises( TypeError ) {
			checkEachType( things, Numeric, Hash )
		}
		assert_nothing_raised {
			checkEachType( things, Numeric, Hash, String )
		}
	end


	def test_checkResponse
		testObj = ResponseTestObject::new

		assert_nothing_raised {
			checkResponse( testObj, :oneMethod )
		}
		assert_nothing_raised {
			checkResponse( testObj, :oneMethod, :twoMethod )
		}
		assert_nothing_raised {
			checkResponse( testObj, :oneMethod, :twoMethod, :redMethod )
		}
		assert_nothing_raised {
			checkResponse( testObj, :oneMethod, :twoMethod, :redMethod, :blueMethod )
		}
		assert_raises( TypeError ) {
			checkResponse( testObj, :oneMethod, :twoMethod, :redMethod, :blueMethod, :fooMethod )
		}
	end

	def test_checkEachResponse
		testObjs = []
		10.times { testObjs << ResponseTestObject::new }

		assert_nothing_raised {
			checkEachResponse( testObjs, :oneMethod )
		}
		assert_nothing_raised {
			checkEachResponse( testObjs, :oneMethod, :twoMethod )
		}
		assert_nothing_raised {
			checkEachResponse( testObjs, :oneMethod, :twoMethod, :redMethod )
		}
		assert_nothing_raised {
			checkEachResponse( testObjs, :oneMethod, :twoMethod, :redMethod, :blueMethod )
		}
		assert_raises( TypeError ) {
			checkEachResponse( testObjs, :oneMethod, :twoMethod, :redMethod, :blueMethod, :fooMethod )
		}
		assert_raises( TypeError ) {
			checkEachResponse( testObjs + ["a string"], :oneMethod )
		}
	end

	def test_abstractDeclaration
		anonClass = nil
		anonSubClass = nil
		testObj = nil

		# Define a test class with one virtual method
		assert_nothing_raised {
			anonClass = Class::new {|klass|
				abstract :foo
			}
		}

		assert_instance_of Class, anonClass
		assert_nothing_raised { testObj = anonClass.new }
		assert_raises( MUES::VirtualMethodError ) { testObj.foo }

		# Now override the virtual method in a subclass and test again.
		assert_nothing_raised {
			anonSubClass = Class::new( anonClass ) {
				class_eval %Q{
					def foo
						return true
					end
				}
			}
		}

		assert_instance_of Class, anonSubClass
		assert_nothing_raised { testObj = anonSubClass.new }
		assert_nothing_raised { testObj.foo }
	end



end # class LibraryFunctionsTestCase
<commit_msg>- Updated to test more of abstract() and abstract_arity().<commit_after>#!/usr/bin/ruby -w

begin
	require 'tests/muesunittest'
rescue
	require '../muesunittest'
end

require 'mues'
require 'mues/Exceptions'
require 'thread'


class ResponseTestObject
	def oneMethod
		return true
	end
	def twoMethod
		return true
	end
	def redMethod
		return true
	end
	def blueMethod
		return true
	end
end


class LibraryFunctionsTestCase < MUES::TestCase

	include MUES::TypeCheckFunctions, MUES::SafeCheckFunctions

	### Test instantiation with various arguments
	def test_checkSafeLevel

		# Run this in a thread so we can reduce our $SAFE temporarily
		t = Thread.new {
			$SAFE = 3
			checkSafeLevel()
			raise StandardError, "Failed."
		}

		assert_raises( SecurityError ) { t.join }
		assert_nothing_raised { checkSafeLevel() }
	end

	def test_checkType
		assert_raises( TypeError ) {
			checkType( "a string", Numeric )
		}
		assert_raises( TypeError ) {
			checkType( "a string", Numeric, Hash )
		}
		assert_nothing_raised {
			checkType( "a string", Numeric, Hash, String )
		}
	end

	def test_checkEachType
		things = [ "a string", 5, {'a' => 'test', 'hash' => 'object'} ]


		assert_raises( TypeError ) {
			checkEachType( things, Numeric )
		}
		assert_raises( TypeError ) {
			checkEachType( things, Numeric, Hash )
		}
		assert_nothing_raised {
			checkEachType( things, Numeric, Hash, String )
		}
	end


	def test_checkResponse
		testObj = ResponseTestObject::new

		assert_nothing_raised {
			checkResponse( testObj, :oneMethod )
		}
		assert_nothing_raised {
			checkResponse( testObj, :oneMethod, :twoMethod )
		}
		assert_nothing_raised {
			checkResponse( testObj, :oneMethod, :twoMethod, :redMethod )
		}
		assert_nothing_raised {
			checkResponse( testObj, :oneMethod, :twoMethod, :redMethod, :blueMethod )
		}
		assert_raises( TypeError ) {
			checkResponse( testObj, :oneMethod, :twoMethod, :redMethod, :blueMethod, :fooMethod )
		}
	end

	def test_checkEachResponse
		testObjs = []
		10.times { testObjs << ResponseTestObject::new }

		assert_nothing_raised {
			checkEachResponse( testObjs, :oneMethod )
		}
		assert_nothing_raised {
			checkEachResponse( testObjs, :oneMethod, :twoMethod )
		}
		assert_nothing_raised {
			checkEachResponse( testObjs, :oneMethod, :twoMethod, :redMethod )
		}
		assert_nothing_raised {
			checkEachResponse( testObjs, :oneMethod, :twoMethod, :redMethod, :blueMethod )
		}
		assert_raises( TypeError ) {
			checkEachResponse( testObjs, :oneMethod, :twoMethod, :redMethod, :blueMethod, :fooMethod )
		}
		assert_raises( TypeError ) {
			checkEachResponse( testObjs + ["a string"], :oneMethod )
		}
	end

	def test_abstractDeclaration
		anonClass = nil
		anonSubClass = nil
		testObj = nil

		assert_nothing_raised {
			anonClass = Class::new( MUES::Object ) {
				self.class_eval {
					include MUES::AbstractClass
					abstract :foo
					abstract_arity :fooArity, 1
				}
			}
		}
		assert_instance_of Class, anonClass
		
		# Try a concrete version with overriding method with insufficient arity
		# -- should succeed, as correct arity isn't checked for until
		# instantiation
		assert_nothing_raised {
			anonSubClass = Class::new( anonClass ) {
				self.class_eval %{
					def fooArity
						return "fooArity"
					end
				}
			}
		}
		assert_instance_of Class, anonSubClass
		assert_raises( MUES::VirtualMethodError ) { anonSubClass.new }

		# Now make a concrete version with the correct arity, but this one
		# should fail when foo is called.
		assert_nothing_raised {
			anonSubClass = Class::new( anonClass ) {
				self.class_eval %{
					def fooArity( arg )
						return "fooArity"
					end
				}
			}
		}
		assert_instance_of Class, anonSubClass
		assert_nothing_raised { testObj = anonSubClass.new }
		assert_raises( MUES::VirtualMethodError ) { testObj.foo }

		# Now make a concrete version with the correct arity, and an overriding
		# foo method.
		assert_nothing_raised {
			anonSubClass = Class::new( anonClass ) {
				self.class_eval %{
					def foo
						return "foo"
					end

					def fooArity( arg )
						return "fooArity"
					end
				}
			}
		}
		assert_instance_of Class, anonSubClass
		assert_nothing_raised { testObj = anonSubClass.new }
		assert_nothing_raised { testObj.foo }

		# Now make a concrete version with the correct (negative) arity, and an
		# overriding foo method.
		assert_nothing_raised {
			anonSubClass = Class::new( anonClass ) {
				self.class_eval %{
					def foo
						return "foo"
					end

					def fooArity( arg="default" )
						return "fooArity"
					end
				}
			}
		}
		assert_instance_of Class, anonSubClass
		assert_nothing_raised { testObj = anonSubClass.new }
		assert_nothing_raised { testObj.foo }

	end



end # class LibraryFunctionsTestCase

{'fraction_non_alphanumeric': 0.08973723351512147, 'fraction_numerical': 0.0011155180961824492, 'mean_word_length': 3.6994758299359347, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7226071', 'n_tokens_mistral': 3172, 'n_tokens_neox': 2632, 'n_words': 666}