Skip to content

Add word count generator #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions exercises/word-count/example.tt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ require_relative 'word_count'
# <%= sha1 %>
class PhraseTest < Minitest::Test<% test_cases.each do |test_case| %>
def <%= test_case.name %><% if test_case.skipped? %>
skip<% end %><% if test_case.raises_error? %>
assert_raises(ArgumentError) { <%= test_case.do %> }<% else %>
assert_equal <%= test_case.expected %>, <%= test_case.do %><% end %>
skip<% end %>
phrase = <%= test_case.object_under_test %>
counts = <%= test_case.expected %>
assert_equal counts, phrase.word_count
end
<% end %>
<%= IO.read(XRUBY_LIB + '/bookkeeping.md') %>
Expand Down
86 changes: 38 additions & 48 deletions exercises/word-count/word_count_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,107 +4,97 @@
require_relative 'word_count'

# Test data version:

# 5b5e807
class PhraseTest < Minitest::Test
def test_count_one_word
phrase = Phrase.new('word')
counts = { 'word' => 1 }
assert_equal counts, phrase.word_count
end

def test_count_one_of_each
skip
phrase = Phrase.new('one of each')
counts = { 'one' => 1, 'of' => 1, 'each' => 1 }
phrase = Phrase.new("word")
counts = {"word"=>1}
assert_equal counts, phrase.word_count
end

def test_count_multiple_occurrences
def test_count_one_of_each_word
skip
phrase = Phrase.new('one fish two fish red fish blue fish')
counts = { 'one' => 1, 'fish' => 4, 'two' => 1, 'red' => 1, 'blue' => 1 }
phrase = Phrase.new("one of each")
counts = {"one"=>1, "of"=>1, "each"=>1}
assert_equal counts, phrase.word_count
end

def test_count_everything_just_once
def test_multiple_occurrences_of_a_word
skip
phrase = Phrase.new('all the kings horses and all the kings men')
phrase.word_count # count it an extra time
counts = {
'all' => 2, 'the' => 2, 'kings' => 2,
'horses' => 1, 'and' => 1, 'men' => 1
}
phrase = Phrase.new("one fish two fish red fish blue fish")
counts = {"one"=>1, "fish"=>4, "two"=>1, "red"=>1, "blue"=>1}
assert_equal counts, phrase.word_count
end

def test_ignore_punctuation
def test_handles_cramped_lists
skip
phrase = Phrase.new('car : carpet as java : javascript!!&@$%^&')
counts = {
'car' => 1, 'carpet' => 1, 'as' => 1,
'java' => 1, 'javascript' => 1
}
phrase = Phrase.new("one,two,three")
counts = {"one"=>1, "two"=>1, "three"=>1}
assert_equal counts, phrase.word_count
end

def test_handles_cramped_lists
def test_handles_expanded_lists
skip
phrase = Phrase.new('one,two,three')
counts = { 'one' => 1, 'two' => 1, 'three' => 1 }
phrase = Phrase.new("one,\ntwo,\nthree")
counts = {"one"=>1, "two"=>1, "three"=>1}
assert_equal counts, phrase.word_count
end

def test_handles_expanded_lists
def test_ignore_punctuation
skip
phrase = Phrase.new("one,\ntwo,\nthree")
counts = { 'one' => 1, 'two' => 1, 'three' => 1 }
phrase = Phrase.new("car: carpet as java: javascript!!&@$%^&")
counts = {"car"=>1, "carpet"=>1, "as"=>1, "java"=>1, "javascript"=>1}
assert_equal counts, phrase.word_count
end

def test_include_numbers
skip
phrase = Phrase.new('testing, 1, 2 testing')
counts = { 'testing' => 2, '1' => 1, '2' => 1 }
phrase = Phrase.new("testing, 1, 2 testing")
counts = {"testing"=>2, "1"=>1, "2"=>1}
assert_equal counts, phrase.word_count
end

def test_normalize_case
skip
phrase = Phrase.new('go Go GO')
counts = { 'go' => 3 }
phrase = Phrase.new("go Go GO Stop stop")
counts = {"go"=>3, "stop"=>2}
assert_equal counts, phrase.word_count
end

def test_with_apostrophes
skip
phrase = Phrase.new("First: don't laugh. Then: don't cry.")
counts = {
'first' => 1, "don't" => 2, 'laugh' => 1,
'then' => 1, 'cry' => 1
}
counts = {"first"=>1, "don't"=>2, "laugh"=>1, "then"=>1, "cry"=>1}
assert_equal counts, phrase.word_count
end

def test_with_quotations
skip
phrase = Phrase.new("Joe can't tell between 'large' and large.")
counts = {
'joe' => 1, "can't" => 1, 'tell' => 1,
'between' => 1, 'large' => 2, 'and' => 1
}
counts = {"joe"=>1, "can't"=>1, "tell"=>1, "between"=>1, "large"=>2, "and"=>1}
assert_equal counts, phrase.word_count
end

# Problems in exercism evolve over time,
# as we find better ways to ask questions.
# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of BookKeeping.
# If you're curious, read more about constants on RubyDoc:
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html

def test_bookkeeping
skip
assert_equal 1, BookKeeping::VERSION
end
end

19 changes: 19 additions & 0 deletions lib/word_count_cases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class WordCountCase < OpenStruct
def name
'test_%s' % description.tr(' ', '_')
end

def object_under_test
%Q(Phrase.new(#{input.inspect}))
end
Copy link
Contributor

@Insti Insti Oct 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for not calling this do 👍 (like everybody else seems to 😢 )


def skipped?
index.nonzero?
end
end

WordCountCases = proc do |data|
JSON.parse(data)['cases'].map.with_index do |row, i|
WordCountCase.new(row.merge('index' => i))
end
end