Advertisement
Guest User

ex48

a guest
Feb 21st, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. This is based on an assignment from http://ruby.learncodethehardway.org/book/ex48.html
  2.  
  3. ========================= HERE is my TEST and the Failure message.
  4. ========================= it seem that the test fail once it tries to scan more than one words.
  5. ========================= since 'result' return only one pair and does not match the expected result
  6.  
  7. osiris:test $ ruby test_lexicon.rb
  8. Loaded suite test_lexicon
  9. Started
  10. north is in the lexicon
  11. direction
  12. north is in the lexicon
  13. direction
  14. F
  15. Finished in 0.001496 seconds.
  16.  
  17. 1) Failure:
  18. test_directions(LexiconTests) [test_lexicon.rb:12]:
  19. <#<struct Lexicon::Pair token=:direction, word="north">> expected but was
  20. <[#<struct Lexicon::Pair token=:direction, word="north">,
  21. #<struct Lexicon::Pair token=:direction, word="south">,
  22. #<struct Lexicon::Pair token=:direction, word="east">]>.
  23.  
  24. 1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
  25.  
  26. Test run options: --seed 34255
  27. osiris:test $
  28.  
  29. ========================= test_lexicon.rb
  30.  
  31. require 'test/unit'
  32. require_relative '../lib/lexicon'
  33.  
  34. class LexiconTests < Test::Unit::TestCase
  35.  
  36. Pair = Lexicon::Pair
  37. @@lexicon = Lexicon.new()
  38.  
  39. def test_directions()
  40. assert_equal(Pair.new(:direction, 'north'), @@lexicon.scan("north"))
  41. result = @@lexicon.scan("north south east")
  42. assert_equal(result, [Pair.new(:direction, 'north'),
  43. Pair.new(:direction, 'south'),
  44. Pair.new(:direction, 'east')])
  45. end
  46. end
  47.  
  48. ========================= lexicon.rb
  49.  
  50. class Lexicon
  51. #This is the Lexicon built with an HashesMap
  52. @@lexicon = {
  53. #direction
  54. 'north' => :direction, 'south' => :direction, 'east' => :direction,
  55. 'west' => :direction, 'down' => :direction, 'up' => :direction,
  56. 'down' => :direction, 'left' => :direction, 'right' => :direction,
  57. }
  58.  
  59. #set up the Struct:define the pair
  60. Pair = Struct.new(:token, :word)
  61.  
  62. def scan(sentence)
  63. #split the sentence in words
  64. words = sentence.split()
  65. i = 0
  66.  
  67. #Creating a loop that is going to check if each word match the lexicon
  68. #en ends once all the words have been checked
  69. while words.length >= i do
  70.  
  71. #what to do if a match is found
  72. if @@lexicon.include? words[i]
  73.  
  74. #Create a new pair of :token and :word
  75. match = Pair.new(@@lexicon[words[i]], words[i])
  76. return match
  77.  
  78. puts "#{words[i]} is in the lexicon"
  79. puts "#{@@lexicon[words[i]]}"
  80.  
  81. else
  82. puts "#{words[i]} is not a #{@@lexicon[words[i]]} in the lexicon"
  83. end
  84.  
  85. puts "....#{i}"
  86. i += 1
  87. end
  88.  
  89.  
  90. end
  91.  
  92. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement