Skip to content

Instantly share code, notes, and snippets.

@richardsondx
Created February 21, 2012 08:59
Show Gist options
  • Save richardsondx/1875293 to your computer and use it in GitHub Desktop.
Save richardsondx/1875293 to your computer and use it in GitHub Desktop.
ex48 Test
osiris:test $ ruby test_lexicon.rb
Loaded suite test_lexicon
Started
F
Finished in 0.001428 seconds.
1) Failure:
test_directions(LexiconTests) [test_lexicon.rb:12]:
<#<struct Lexicon::Pair token=:direction, word="north">> expected but was
<[#<struct Lexicon::Pair token=:direction, word="north">,
#<struct Lexicon::Pair token=:direction, word="south">,
#<struct Lexicon::Pair token=:direction, word="east">]>.
1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
Test run options: --seed 45841
class Lexicon
#This is the Lexicon built with an HashesMap
@@lexicon = {
#direction
'north' => :direction, 'south' => :direction, 'east' => :direction,
'west' => :direction, 'down' => :direction, 'up' => :direction,
'down' => :direction, 'left' => :direction, 'right' => :direction,
}
#set up the Struct:define the pair
Pair = Struct.new(:token, :word)
def scan(sentence)
#split the sentence in words
words = sentence.split()
i = 0
#Creating a loop that is going to check if each word match the lexicon
#en ends once all the words have been checked
while words.length >= i do
#what to do if a match is found
if @@lexicon.include? words[i]
#Create a new pair of :token and :word
match = Pair.new(@@lexicon[words[i]], words[i])
return match
puts "#{words[i]} is in the lexicon"
puts "#{@@lexicon[words[i]]}"
else
puts "#{words[i]} is not a #{@@lexicon[words[i]]} in the lexicon"
end
puts "....#{i}"
i += 1
end
end
end
This is based on an assignment from http://ruby.learncodethehardway.org/book/ex48.html
it seem that the test fail once it tries to scan more than one words,
since 'result' return only one pair and does not match the expected result
require 'test/unit'
require_relative '../lib/lexicon'
class LexiconTests < Test::Unit::TestCase
Pair = Lexicon::Pair
@@lexicon = Lexicon.new()
def test_directions()
assert_equal(Pair.new(:direction, 'north'), @@lexicon.scan("north"))
result = @@lexicon.scan("north south east")
assert_equal(result, [Pair.new(:direction, 'north'),
Pair.new(:direction, 'south'),
Pair.new(:direction, 'east')])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment