Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is based on an assignment from http://ruby.learncodethehardway.org/book/ex48.html
- ========================= HERE is my TEST and the Failure message.
- ========================= 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
- osiris:test $ ruby test_lexicon.rb
- Loaded suite test_lexicon
- Started
- north is in the lexicon
- direction
- north is in the lexicon
- direction
- F
- Finished in 0.001496 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 34255
- osiris:test $
- ========================= test_lexicon.rb
- 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
- ========================= lexicon.rb
- 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement