Linguistics 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,239 @@
1
+ #!/usr/bin/ruby
2
+ #
3
+ # This is an abstract test case class for building Test::Unit unit tests for the
4
+ # Linguistics module. It consolidates most of the maintenance work that must be
5
+ # done to build a test file by adjusting the $LOAD_PATH appropriately, as well
6
+ # as adding some other useful methods that make building and maintaining the
7
+ # tests much easier (IMHO). See the docs for Test::Unit for more info on the
8
+ # particulars of unit testing.
9
+ #
10
+ # == Synopsis
11
+ #
12
+ # # Allow the unit test to be run from the base dir, or from tests/ or
13
+ # # similar:
14
+ # begin
15
+ # require 'tests/lingtestcase'
16
+ # rescue
17
+ # require '../lingtestcase'
18
+ # end
19
+ #
20
+ # class MySomethingTest < Linguistics::TestCase
21
+ # def setup
22
+ # super()
23
+ # @foo = 'bar'
24
+ # end
25
+ #
26
+ # def test_00_something
27
+ # obj = nil
28
+ # assert_nothing_raised { obj = MySomething::new }
29
+ # assert_instance_of MySomething, obj
30
+ # assert_respond_to :myMethod, obj
31
+ # end
32
+ # end
33
+ #
34
+ # == Rcsid
35
+ #
36
+ # $Id: lingtestcase.rb 78 2005-07-13 19:58:43Z ged $
37
+ #
38
+ # == Authors
39
+ #
40
+ # * Michael Granger <ged@FaerieMUD.org>
41
+ #
42
+ #:include: COPYRIGHT
43
+ #
44
+ #---
45
+ #
46
+ # Please see the file COPYRIGHT in the 'docs' directory for licensing details.
47
+ #
48
+
49
+ begin
50
+ basedir = File::dirname( File::dirname(__FILE__) )
51
+ unless $LOAD_PATH.include?( "#{basedir}/lib" )
52
+ $LOAD_PATH.unshift "#{basedir}/lib"
53
+ end
54
+ end
55
+
56
+ require "test/unit"
57
+
58
+ require "linguistics"
59
+
60
+
61
+ module Linguistics
62
+
63
+ ### The abstract base class for Linguistics test cases.
64
+ class TestCase < Test::Unit::TestCase
65
+
66
+ @methodCounter = 0
67
+ @setupBlocks = []
68
+ @teardownBlocks = []
69
+ class << self
70
+ attr_accessor :methodCounter, :setupBlocks, :teardownBlocks
71
+ end
72
+
73
+
74
+ ### Inheritance callback -- adds @setupBlocks and @teardownBlocks ivars
75
+ ### and accessors to the inheriting class.
76
+ def self::inherited( klass )
77
+ klass.module_eval {
78
+ @setupBlocks = []
79
+ @teardownBlocks = []
80
+
81
+ class << self
82
+ attr_accessor :setupBlocks, :teardownBlocks
83
+ end
84
+ }
85
+ klass.methodCounter = 0
86
+ end
87
+
88
+
89
+
90
+ ### Output the specified <tt>msgs</tt> joined together to
91
+ ### <tt>STDERR</tt> if <tt>$DEBUG</tt> is set.
92
+ def self::debugMsg( *msgs )
93
+ return unless $DEBUG
94
+ self.message "DEBUG>>> %s" % msgs.join('')
95
+ end
96
+
97
+ ### Output the specified <tt>msgs</tt> joined together to
98
+ ### <tt>STDOUT</tt>.
99
+ def self::message( *msgs )
100
+ $stderr.puts msgs.join('')
101
+ $stderr.flush
102
+ end
103
+
104
+
105
+ ### Add a setup block for the current testcase
106
+ def self::addSetupBlock( &block )
107
+ self.methodCounter += 1
108
+ newMethodName = "setup_#{self.methodCounter}".intern
109
+ define_method( newMethodName, &block )
110
+ self.setupBlocks.push newMethodName
111
+ end
112
+
113
+ ### Add a teardown block for the current testcase
114
+ def self::addTeardownBlock( &block )
115
+ self.methodCounter += 1
116
+ newMethodName = "teardown_#{self.methodCounter}".intern
117
+ define_method( newMethodName, &block )
118
+ self.teardownBlocks.unshift newMethodName
119
+ end
120
+
121
+
122
+ #############################################################
123
+ ### I N S T A N C E M E T H O D S
124
+ #############################################################
125
+
126
+ ### A dummy test method to allow this Test::Unit::TestCase to be
127
+ ### subclassed without complaining about the lack of tests.
128
+ def test_0_dummy
129
+ end
130
+
131
+
132
+ ### Forward-compatibility method for namechange in Test::Unit
133
+ def setup( *args )
134
+ self.class.setupBlocks.each {|sblock|
135
+ debugMsg "Calling setup block method #{sblock}"
136
+ self.send( sblock )
137
+ }
138
+ super( *args )
139
+ end
140
+ alias_method :set_up, :setup
141
+
142
+
143
+ ### Forward-compatibility method for namechange in Test::Unit
144
+ def teardown( *args )
145
+ super( *args )
146
+ self.class.teardownBlocks.each {|tblock|
147
+ debugMsg "Calling teardown block method #{tblock}"
148
+ self.send( tblock )
149
+ }
150
+ end
151
+ alias_method :tear_down, :teardown
152
+
153
+
154
+ ### Instance alias for the like-named class method.
155
+ def message( *msgs )
156
+ self.class.message( *msgs )
157
+ end
158
+
159
+
160
+ ### Instance alias for the like-named class method
161
+ def debugMsg( *msgs )
162
+ self.class.debugMsg( *msgs )
163
+ end
164
+
165
+
166
+ ### Output a separator line made up of <tt>length</tt> of the specified
167
+ ### <tt>char</tt>.
168
+ def writeLine( length=75, char="-" )
169
+ $stderr.puts "\r" + (char * length )
170
+ end
171
+
172
+
173
+ ### Output a header for delimiting tests
174
+ def printTestHeader( desc )
175
+ return unless $VERBOSE || $DEBUG
176
+ message ">>> %s <<<" % desc
177
+ end
178
+
179
+
180
+ ### Try to force garbage collection to start.
181
+ def collectGarbage
182
+ a = []
183
+ 1000.times { a << {} }
184
+ a = nil
185
+ GC.start
186
+ end
187
+
188
+
189
+ ### Output the name of the test as it's running if in verbose mode.
190
+ def run( result )
191
+ $stderr.puts self.name if $VERBOSE || $DEBUG
192
+ super
193
+ end
194
+
195
+
196
+ #############################################################
197
+ ### E X T R A A S S E R T I O N S
198
+ #############################################################
199
+
200
+ ### Negative of assert_respond_to
201
+ def assert_not_respond_to( obj, meth )
202
+ msg = "%s expected NOT to respond to '%s'" %
203
+ [ obj.inspect, meth ]
204
+ assert_block( msg ) {
205
+ !obj.respond_to?( meth )
206
+ }
207
+ end
208
+
209
+
210
+ ### Assert that the instance variable specified by +sym+ of an +object+
211
+ ### is equal to the specified +value+. The '@' at the beginning of the
212
+ ### +sym+ will be prepended if not present.
213
+ def assert_ivar_equal( value, object, sym )
214
+ sym = "@#{sym}".intern unless /^@/ =~ sym.to_s
215
+ msg = "Instance variable '%s'\n\tof <%s>\n\texpected to be <%s>\n" %
216
+ [ sym, object.inspect, value.inspect ]
217
+ msg += "\tbut was: <%s>" % object.instance_variable_get(sym)
218
+ assert_block( msg ) {
219
+ value == object.instance_variable_get(sym)
220
+ }
221
+ end
222
+
223
+
224
+ ### Assert that the specified +object+ has an instance variable which
225
+ ### matches the specified +sym+. The '@' at the beginning of the +sym+
226
+ ### will be prepended if not present.
227
+ def assert_has_ivar( sym, object )
228
+ sym = "@#{sym}" unless /^@/ =~ sym.to_s
229
+ msg = "Object <%s> expected to have an instance variable <%s>" %
230
+ [ object.inspect, sym ]
231
+ assert_block( msg ) {
232
+ object.instance_variables.include?( sym.to_s )
233
+ }
234
+ end
235
+
236
+ end # class TestCase
237
+
238
+ end # module Linguistics
239
+
@@ -0,0 +1,99 @@
1
+ #!/usr/bin/ruby -w
2
+ #
3
+ # Unit test for the 'use' function of the Linguistics module.
4
+ # $Id: use.tests.rb 78 2005-07-13 19:58:43Z ged $
5
+ #
6
+ # Copyright (c) 2003 The FaerieMUD Consortium.
7
+ #
8
+
9
+ unless defined? Linguistics::TestCase
10
+ testsdir = File::dirname( File::expand_path( __FILE__ ) )
11
+ $LOAD_PATH.unshift testsdir unless $LOAD_PATH.include?( testsdir )
12
+
13
+ require 'lingtestcase'
14
+ end
15
+
16
+
17
+ ### This test suite tests the language-installation function of the Linguistics
18
+ ### module.
19
+ module Linguistics
20
+ class UseTestCase < Linguistics::TestCase
21
+
22
+ LanguageCodes = [ :en, :EN, 'en', 'EN', 'En', 'eN' ]
23
+ BogusLanguageCodes = [ :zz, :ry, :qi ]
24
+ MissingLanguageCodes = [ :ja, :fr, :es ]
25
+
26
+ TestArray = %w{stone stick hammer stone lantern}
27
+ TestString = "banner"
28
+ TestNumber = 5
29
+
30
+ def test_00_UseEnglish
31
+ printTestHeader "Linguistics: Use <language>"
32
+
33
+ # Test the only (currently) valid codes
34
+ LanguageCodes.each do |code|
35
+ assert_nothing_raised {
36
+ Linguistics::use( code )
37
+ }
38
+ end
39
+
40
+ # Test bogus codes
41
+ BogusLanguageCodes.each do |code|
42
+ assert_raises( RuntimeError ) {
43
+ Linguistics::use( code )
44
+ }
45
+ end
46
+
47
+ # Test valid, but missing languages (might fail for implementors of new
48
+ # languages).
49
+ MissingLanguageCodes.each do |code|
50
+ assert_raises( LoadError ) {
51
+ Linguistics::use( code )
52
+ }
53
+ end
54
+
55
+ end
56
+
57
+
58
+ def test_10_InflectorMethod
59
+ printTestHeader "Linguistics: Inflector method (core classes)"
60
+ rval = nil
61
+
62
+ # This shouldn't be necessary, but it's here for completeness
63
+ Linguistics::use( :en )
64
+
65
+ [ TestArray, TestString, TestNumber ].each do |obj|
66
+ debugMsg "obj.class.instance_variables = %s" %
67
+ obj.class.instance_variables.inspect
68
+
69
+ assert_respond_to obj, :en
70
+ assert_nothing_raised {
71
+ rval = obj.en
72
+ }
73
+ assert_kind_of Linguistics::LanguageProxyClass, rval
74
+ end
75
+ end
76
+
77
+
78
+ def test_20_SpecifyClasses
79
+ printTestHeader "Linguistics: Extend specific classes"
80
+
81
+ assert_nothing_raised( "One class, not in an array" ) {
82
+ Linguistics::use( :en, :classes => Symbol )
83
+ }
84
+ assert_respond_to :foo, :en
85
+
86
+ assert_nothing_raised( "One class, in an array" ) {
87
+ Linguistics::use( :en, :classes => [IO] )
88
+ }
89
+ assert_respond_to $stderr, :en
90
+
91
+ assert_nothing_raised( "Two classes, in an Array" ) {
92
+ Linguistics::use( :en, :classes => [Class, Range] )
93
+ }
94
+ assert_respond_to Array, :en
95
+ assert_respond_to( (1..5), :en )
96
+ end
97
+
98
+ end
99
+ end