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

Conversation

pvcarrera
Copy link
Contributor

In the list of exercises that need a generator #396 , I saw word-count. Once I started working on it, I found the template exercises/word-count/example.tt but I couldn't find any generator associated to it, so I decided to modify the current template and create the generator that you can see in this PR.

The canonical-data.json file from x-common has less examples that the current version of word_count_test.rb Should I add these examples to x-common?

@kotp
Copy link
Member

kotp commented Oct 9, 2016

If the prior non-generated version of the test had more tests than what is in x-common, then we need to review the discussions that lead to that difference.

Do you feel like the additional tests are something that is Ruby specific? Do you feel that the additional tests may be redundant or a good addition to other languages?

@kotp
Copy link
Member

kotp commented Oct 9, 2016

BTW: Thanks for jumping in here.

@pvcarrera
Copy link
Contributor Author

pvcarrera commented Oct 10, 2016

Thanks to you and all the maintainers of this amazing project 😍

The additional cases are:

test_count_everything_just_once
test_handles_cramped_lists
test_with_apostrophes
test_with_quotations

I think this are valid test that cover cases that are not covered in the x-common cases

@Insti
Copy link
Contributor

Insti commented Oct 10, 2016

@pvcarrera create a PR in exercism/x-common adding the additional cases to the canonical-data.json file.


def object_under_test
"Phrase.new('#{input}')"
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 😢 )

end

def skipped?
index > 0
Copy link
Member

Choose a reason for hiding this comment

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

Or even index.notzero? No action needed, just an annotation.

@pvcarrera
Copy link
Contributor Author

I think I need to update this branch/PR: Merge #451, then remove the generate-word-count scripts. Also, the generated tests don't follow the project style rules. I ran rubocop -a after the test cases where generated but, after the changes I did in #454, I realised that the test must be generated with the correct format.

@kotp
Copy link
Member

kotp commented Oct 16, 2016

Don't worry about updating the branch against 451, you should be able to rebase against master.

There is another change (unsure if it is in yet, though) that sets style enforcement/checks back to 0. But this would also be on master.

The process for doing this would be on your fork of the project...

(Not tested, just written out here as a guide)

git checkout master
git pull origin master
git checkout add-word-count-generator
git rebase master

That should move your branch to the latest master branch.

@pvcarrera pvcarrera force-pushed the add-word-count-generator branch from d0c13ac to 93467be Compare October 17, 2016 07:43
@pvcarrera
Copy link
Contributor Author

Done, I rebased, squashed and forced push.

I'm not sure if this is open to discussion, but this is my opinion about PRs workflow:

Rebasing an open PR rewrites the PR history, this means that comments are not synchronized with changes anymore. I don't usually rebase/force push once a PR is open, on the other hand, I add commits and merge master in the branch as many times as necessary. Once the PR is ready, I use the GitHub Squash and merge option. This reduces the PR to one commit but still keeps a link to the closed PR request where you can find the original code with its comments.

@kotp
Copy link
Member

kotp commented Oct 17, 2016

I was only stating about rebasing from master, since I changed how the Rubocop is behaving.

Thanks for the notes on the PR workflow. It is very similar to how things are normally done here, though we reduce to "a few" if appropriate rather than always a single commit.

I did not realize we were ready for that, but you doing so, definitely signals that we are ready for "final approach" on this.

@kotp
Copy link
Member

kotp commented Oct 18, 2016

#451 Common generation script is now in master.

@pvcarrera
Copy link
Contributor Author

Cool, this PR is already up to date with #451

@kotp
Copy link
Member

kotp commented Oct 18, 2016

OK, I attempted to do a generate, might me doing something wrong since it changed recently.
Is that working for you?

@kotp
Copy link
Member

kotp commented Oct 18, 2016

After I do bin/generate word-count I am using ASSIGNMENT=word-count make test-assignment to test it locally, it is coughing due to the line 68 in the tests.

running tests for: word-count
/tmp/word-count.CuENNcRPR7/word_count_test.rb:68: syntax error, unexpected tIDENTIFIER, expecting ')'
    phrase = Phrase.new('First: don't laugh. Then: don't cry.')

@bmulvihill
Copy link
Contributor

bmulvihill commented Oct 18, 2016

Looks like its due a to a use of single quotes as you mentioned in #463. I pulled locally and changed the object_under test method as so

  def object_under_test
    "Phrase.new(\"#{input}\")"
  end

Output:

----------------------------------------------------------------
running tests for: word-count
Run options: --seed 10649

# Running:

...........

Finished in 0.001349s, 8155.1009 runs/s, 8155.1009 assertions/s.

11 runs, 11 assertions, 0 failures, 0 errors, 0 skips

@Insti
Copy link
Contributor

Insti commented Oct 18, 2016

"Phrase.new(\"#{input}\")"

This will still break if the string contains double quotes.
Much better to find a solution that correctly escapes quote characters.

@pvcarrera pvcarrera force-pushed the add-word-count-generator branch from 3439871 to 1c4ca77 Compare October 19, 2016 08:18
@pvcarrera
Copy link
Contributor Author

@Insti The input string comes from a json file, example: "input": "Joe can't tell between 'large' and large." I guess if one of the strings contains a double quote it'll come scaped, something like: "input": "Joe can't tell between \"large\" and large."

@Insti
Copy link
Contributor

Insti commented Oct 19, 2016

It's still a problem because the json is parsed before we try to insert it.

Example:

require 'json'
json = { :example => 'Contains " double quote' }.to_json
puts json
input = JSON.parse(json)['example']
puts %Q(Quote not escaped "#{input}")

Output:

{"example":"Contains \" double quote"}
Quote not escaped "Contains " double quote"

This might work though:

%Q(Quote now escaped #{input.to_json})

(Note that the surrounding quotes are now provided by the .to_json method.)
There's gotta be a nicer way to do this.

Edit: and if input is an array you'll be in trouble too.
Edit 2: %Q(Quote escaped #{input.inspect}) maybe?

@@ -4,7 +4,7 @@ def name
end

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

Choose a reason for hiding this comment

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

Sorry to be nitpicky, but..
I think I prefer using inspect here, since it's not REALLY json that we want.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, I agree inspect reveals the intention better than to_json. Thanks

@Insti
Copy link
Contributor

Insti commented Oct 19, 2016

I think this is pretty much ready now. 👍
@kotp?

@Insti
Copy link
Contributor

Insti commented Oct 19, 2016

Thanks for all your work on this @pvcarrera ❤️

@Insti Insti added the ready label Oct 19, 2016
@pvcarrera
Copy link
Contributor Author

I'm really happy to help, thanks a lot for all of your feedback 😍

@kotp kotp merged commit 37fa3b4 into exercism:master Oct 19, 2016
@kotp
Copy link
Member

kotp commented Oct 19, 2016

Thanks @pvcarrera 🎩 to @Insti

@kotp kotp removed the ready label Oct 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants