Commits

Michael Granger committed 254a4a3

Improve coverage.

Comments (0)

Files changed (10)

 ^ChangeLog$
 ^doc/
 \.(pdf|svg)$
+coverage/
 
 BEGIN {
 	require 'pathname'
+	$stderr.puts "__FILE__ is: #{__FILE__}"
 	basedir = Pathname.new( __FILE__ ).dirname.expand_path
 	libdir = basedir + "lib"
 
 sequel -v3.38.0
 sqlite3 -v1.3.6
 loggability -v0.5.0
+simplecov -v0.6.4
+
 	self.dependency 'loggability', '~> 0.5'
 	self.dependency 'sqlite3',     '~> 1.3', :developer
 	self.dependency 'rspec',       '~> 2.7', :developer
+	self.dependency 'simplecov',   '~> 0.6', :developer
 
 	self.spec_extras[:licenses] = ["BSD"]
 	self.spec_extras[:post_install_message] = %{
 # Rebuild the ChangeLog immediately before release
 task :prerelease => 'ChangeLog'
 
+# Simplecov
+desc "Build a coverage report"
+task :coverage do
+	ENV["COVERAGE"] = 'yes'
+	Rake::Task[:spec].invoke
+end

lib/wordnet/lexicon.rb

 
 			when Range
 				self.log.debug "  limiting to range of senses: %p" % [ arg ]
-				dataset = dataset.limit( arg.end - arg.begin, arg.begin - 1 )
+				dataset = dataset.limit( arg.entries.length, arg.begin - 1 )
 
 			when Regexp
 				self.log.debug "  filter: definition =~ %p" % [ arg ]
 	### Return a human-readable string representation of the Lexicon, suitable for
 	### debugging.
 	def inspect
-		return "#<%p:%0#x %s>" % [ self.class, self.object_id * 2, self.db.url ]
+		return "#<%p:%0#x %s>" % [
+			self.class,
+			self.object_id * 2,
+			self.db.url || self.db.adapter_scheme
+		]
 	end
 
 

lib/wordnet/model.rb

 			end
 
 			self.descendents.each do |subclass|
-				self.log.info "Resetting database connection for: %p to: %p" % [ subclass, newdb ]
+				self.log.debug "Resetting database connection for: %p to: %p" % [ subclass, newdb ]
 				subclass.db = newdb
 			end
 		end

spec/lib/helpers.rb

 	$LOAD_PATH.unshift( libdir.to_s ) unless $LOAD_PATH.include?( libdir.to_s )
 }
 
+# SimpleCov test coverage reporting; enable this using the :coverage rake task
+if ENV['COVERAGE']
+	$stderr.puts "\n\n>>> Enabling coverage report.\n\n"
+	require 'simplecov'
+	SimpleCov.start do
+		add_filter 'spec'
+		add_group "Needing tests" do |file|
+			file.covered_percent < 90
+		end
+	end
+end
+
 require 'rspec'
 require 'loggability/spechelpers'
 require 'wordnet'

spec/wordnet/lexicon_spec.rb

 		reset_logging()
 	end
 
+
 	context "the default_db_uri method" do
 
 		it "uses the wordnet-defaultdb database gem (if available)" do
 
 	context "with the default database", :requires_database => true do
 
-		before( :all ) do
-			@lexicon = WordNet::Lexicon.new
-		end
+		let( :lexicon ) { WordNet::Lexicon.new }
 
 		context "via its index operator" do
 
 			it "can look up a Synset by ID" do
-				rval = @lexicon[ 101219722 ]
+				rval = lexicon[ 101219722 ]
 				rval.should be_a( WordNet::Synset )
 				rval.words.map( &:to_s ).should include( 'carrot' )
 			end
 
 			it "can look up a Word by ID" do
-				rval = @lexicon[ 21338 ]
+				rval = lexicon[ 21338 ]
 				rval.should be_a( WordNet::Word )
 				rval.lemma.should == 'carrot'
 			end
 
 			it "can look up the synset for a word and a sense" do
-				ss = @lexicon[ :boot, 3 ]
+				ss = lexicon[ :boot, 3 ]
 				ss.should be_a( WordNet::Synset )
 				ss.definition.should == 'footwear that covers the whole foot and lower leg'
 			end
 
+			it "can look up all synsets for a particular word" do
+				sss = lexicon.lookup_synsets( :tree )
+				sss.should have( 7 ).members
+				sss.all? {|ss| ss.should be_a(WordNet::Synset) }
+			end
+
+			it "can constrain fetched synsets to a certain range of results" do
+				sss = lexicon.lookup_synsets( :tree, 1..4 )
+				sss.should have( 4 ).members
+			end
+
+			it "can constrain fetched synsets to a certain (exclusive) range of results" do
+				sss = lexicon.lookup_synsets( :tree, 1...4 )
+				sss.should have( 3 ).members
+			end
+
 		end
 
 	end
 
 	context "with a PostgreSQL database", :requires_pg do
 
-		before( :all ) do
-			@lexicon = WordNet::Lexicon.new( 'postgres://localhost/wordnet30' )
-		end
+		let( :lexicon ) { WordNet::Lexicon.new(adapter: 'postgres', host: 'localhost', database: 'wordnet30') }
 
 		context "via its index operator" do
 
 			it "can look up a Synset by ID" do
-				rval = @lexicon[ 101219722 ]
+				rval = lexicon[ 101219722 ]
 				rval.should be_a( WordNet::Synset )
 				rval.words.map( &:to_s ).should include( 'carrot' )
 			end
 
 			it "can look up a Word by ID" do
-				rval = @lexicon[ 21338 ]
+				rval = lexicon[ 21338 ]
 				rval.should be_a( WordNet::Word )
 				rval.lemma.should == 'carrot'
 			end
 
 			it "can look up the synset for a word and a sense" do
-				ss = @lexicon[ :boot, 3 ]
+				ss = lexicon[ :boot, 3 ]
 				ss.should be_a( WordNet::Synset )
 				ss.definition.should == 'footwear that covers the whole foot and lower leg'
 			end
 
 			it "can look up a synset for a word and a substring of its definition" do
-				ss = @lexicon[ :boot, 'kick' ]
+				ss = lexicon[ :boot, 'kick' ]
 				ss.should be_a( WordNet::Synset )
 				ss.definition.should =~ /kick/i
 			end
 
 			it "can look up a synset for a word and a part of speech" do
-				ss = @lexicon[ :boot, :verb ]
+				ss = lexicon[ :boot, :verb ]
 				ss.should be_a( WordNet::Synset )
 				ss.definition.should =~ /cause to load/i
 			end
 
 			it "can look up a synset for a word and an abbreviated part of speech" do
-				ss = @lexicon[ :boot, :n ]
+				ss = lexicon[ :boot, :n ]
 				ss.should be_a( WordNet::Synset )
 				ss.definition.should =~ /act of delivering/i
 			end
+
+			it "can constrain fetched synsets with a Regexp match against its definition" do
+				sss = lexicon.lookup_synsets( :tree, /plant/ )
+				sss.should have( 2 ).members
+			end
+
+			it "can constrain fetched synsets via lexical domain" do
+				sss = lexicon.lookup_synsets( :tree, 'noun.shape' )
+				sss.should have( 1 ).member
+				sss.first.should == WordNet::Synset[ 113912260 ]
+			end
+
+			it "can constrain fetched synsets via part of speech as a single-letter Symbol" do
+				sss = lexicon.lookup_synsets( :tree, :n )
+				sss.should have( 3 ).members
+				sss.should include(
+					WordNet::Synset[ 113912260 ],
+					WordNet::Synset[ 111348160 ],
+					WordNet::Synset[ 113104059 ]
+				)
+			end
+
+			it "can constrain fetched synsets via part of speech as a Symbol word" do
+				sss = lexicon.lookup_synsets( :tree, :verb )
+				sss.should have( 4 ).members
+				sss.should include(
+					WordNet::Synset[ 200319111 ],
+					WordNet::Synset[ 201145163 ],
+					WordNet::Synset[ 201616293 ],
+					WordNet::Synset[ 201934205 ]
+				)
+			end
+
+			it "includes the database adapter name in its inspect output" do
+				lexicon.inspect.should include( "postgres" )
+			end
+
 		end
 	end
 

spec/wordnet/word_spec.rb

 
 	before( :all ) do
 		setup_logging( :fatal )
-		@lexicon = WordNet::Lexicon.new
-	end
-
-	before( :each ) do
-		# 'run'
-		@word = @lexicon[ 113377 ]
 	end
 
 	after( :all ) do
 	end
 
 
-	it "knows what senses it has" do
-		senses = @word.senses
-		senses.should be_an( Array )
-		senses.should have( 57 ).members
-		senses.first.should be_a( WordNet::Sense )
+	let( :lexicon ) { WordNet::Lexicon.new }
+
+
+	context "the Word for 'run'" do
+
+		let( :word ) { lexicon[113377] }
+
+		it "knows what senses it has" do
+			senses = word.senses
+			senses.should be_an( Array )
+			senses.should have( 57 ).members
+			senses.first.should be_a( WordNet::Sense )
+		end
+
+		it "knows what synsets it has" do
+			synsets = word.synsets
+
+			# Should have one synset per sense
+			synsets.should have( word.senses.length ).members
+			synsets.first.senses.should include( word.senses.first )
+		end
+
+		it "has a dataset for selecting noun synsets" do
+			word.nouns.should be_a( Sequel::Dataset )
+			word.nouns.should have( 16 ).members
+			ss = word.nouns.all
+			ss.should include(
+				lexicon[ :run, 'sequence' ],
+				lexicon[ :run, 'baseball' ],
+				lexicon[ :run, 'act of running' ],
+				lexicon[ :run, 'testing' ]
+			)
+		end
+
+		it "has a dataset for selecting verb synsets" do
+			word.verbs.should be_a( Sequel::Dataset )
+			word.verbs.should have( 41 ).members
+			ss = word.verbs.all
+			ss.should include(
+				lexicon[ :run, 'compete' ],
+				lexicon[ :run, 'be diffused' ],
+				lexicon[ :run, 'liquid' ],
+				lexicon[ :run, 'move fast' ]
+			)
+		end
+
 	end
 
-	it "knows what synsets it has" do
-		senses = @word.senses
-		synsets = @word.synsets
 
-		synsets.should have( senses.length ).members
-		synsets.first.senses.should include( senses.first )
+	context "the Word for 'light'" do
+
+		let( :word ) { lexicon[77458] }
+
+		it "has a dataset for selecting adjective synsets" do
+			word.adjectives.should be_a( Sequel::Dataset )
+			word.adjectives.should have( 8 ).members
+			ss = word.adjectives.all
+			ss.should include(
+				lexicon[ :light, 'weight' ],
+				lexicon[ :light, 'emit', :adjective ],
+				lexicon[ :light, 'color' ]
+			)
+		end
+
+		it "has a dataset for selecting adjective-satellite synsets" do
+			word.adjective_satellites.should be_a( Sequel::Dataset )
+			word.adjective_satellites.should have( 17 ).members
+			ss = word.adjective_satellites.all
+			ss.should include(
+				lexicon[ :light, 'soil' ],
+				lexicon[ :light, 'calories' ],
+				lexicon[ :light, 'entertainment' ]
+			)
+		end
+
+	end
+
+
+	context "the Word for 'lightly'" do
+
+		let( :word ) { lexicon[77549] }
+
+		it "has a dataset for selecting adverb synsets" do
+			word.adverbs.should be_a( Sequel::Dataset )
+			word.adverbs.should have( 7 ).members
+			ss = word.adverbs.all
+			ss.should include(
+				lexicon[ :lightly, 'indifference' ],
+				lexicon[ :lightly, 'indulging' ],
+				lexicon[ :lightly, 'little weight' ],
+				lexicon[ :lightly, 'quantity' ]
+			)
+		end
+
 	end
 
 end