-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to String and Translation refactoring, close #85
- Loading branch information
Showing
9 changed files
with
166 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
guard 'rspec', :cli => "--color --format progress" do | ||
watch(%r{^spec/.+_spec\.rb$}) | ||
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } | ||
watch('spec/spec_helper.rb') { "spec" } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
class HashUtil | ||
def self.to_param(hash, namespace = nil) # taken from Rails | ||
hash.collect do |key, value| | ||
value.to_query(namespace ? "#{namespace}[#{key}]" : key) | ||
end.sort * '&' | ||
def self.to_params(hash) | ||
params = '' | ||
stack = [] | ||
|
||
hash.each do |k, v| | ||
if v.is_a?(Hash) | ||
stack << [k,v] | ||
else | ||
params << "#{k}=#{v}&" | ||
end | ||
end | ||
|
||
stack.each do |parent, hash| | ||
hash.each do |k, v| | ||
if v.is_a?(Hash) | ||
stack << ["#{parent}[#{k}]", v] | ||
else | ||
params << "#{parent}[#{k}]=#{v}&" | ||
end | ||
end | ||
end | ||
|
||
params.chop! # trailing & | ||
params | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
require 'spec_helper' | ||
|
||
describe WebTranslateIt::String do | ||
|
||
let(:api_key) { "8b669bfc326eff878e2532b67dd001ad55998963" } | ||
|
||
describe "#initialize" do | ||
it "should assign api_key and many parameters" do | ||
string = WebTranslateIt::String.new(api_key, { "id" => 1234, "key" => "bacon"}) | ||
string.api_key.should == api_key | ||
string.id.should == 1234 | ||
string.key.should == "bacon" | ||
end | ||
end | ||
|
||
describe "#save" do | ||
it "should create a new String" do | ||
WebTranslateIt::Util.http_connection do |connection| | ||
string = WebTranslateIt::String.new(api_key, { "key" => "bacon" }) | ||
string.save(connection) | ||
string.id.should_not be_nil | ||
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id) | ||
string_fetched.should_not be_nil | ||
string_fetched.should be_a(WebTranslateIt::String) | ||
string_fetched.id.should == string.id | ||
string.delete(connection) | ||
end | ||
end | ||
|
||
it "should update an existing String" do | ||
WebTranslateIt::Util.http_connection do |connection| | ||
string = WebTranslateIt::String.new(api_key, { "key" => "bacony" }) | ||
string.save(connection) | ||
string.key = "bacon changed" | ||
string.save(connection) | ||
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id) | ||
string_fetched.key.should == "bacon changed" | ||
string.delete(connection) | ||
end | ||
end | ||
|
||
it "should create a new String with Translation" do | ||
translation1 = WebTranslateIt::Translation.new(api_key, { "locale" => "en", "text" => "Hello" }) | ||
translation2 = WebTranslateIt::Translation.new(api_key, { "locale" => "fr", "text" => "Bonjour" }) | ||
|
||
string = WebTranslateIt::String.new(api_key, { "key" => "bacon", "translations" => [translation1, translation2] }) | ||
WebTranslateIt::Util.http_connection do |connection| | ||
string.save(connection) | ||
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id) | ||
string_fetched.translation_for(connection, "en").should_not be_nil | ||
string_fetched.translation_for(connection, "en").text.should == "Hello" | ||
string_fetched.translation_for(connection, "fr").should_not be_nil | ||
string_fetched.translation_for(connection, "fr").text.should == "Bonjour" | ||
string_fetched.translation_for(connection, "es").should be_nil | ||
string.delete(connection) | ||
end | ||
end | ||
|
||
it "should update a String and save its Translation" do | ||
translation1 = WebTranslateIt::Translation.new(api_key, { "locale" => "en", "text" => "Hello" }) | ||
translation2 = WebTranslateIt::Translation.new(api_key, { "locale" => "fr", "text" => "Bonjour" }) | ||
|
||
string = WebTranslateIt::String.new(api_key, { "key" => "bacon" }) | ||
WebTranslateIt::Util.http_connection do |connection| | ||
string.save(connection) | ||
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id) | ||
string_fetched.translation_for(connection, "fr").should be_nil | ||
|
||
string_fetched.translations = [translation1, translation2] | ||
string_fetched.save(connection) | ||
|
||
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id) | ||
string_fetched.translation_for(connection, "en").should_not be_nil | ||
string_fetched.translation_for(connection, "en").text.should == "Hello" | ||
string_fetched.translation_for(connection, "fr").should_not be_nil | ||
string_fetched.translation_for(connection, "fr").text.should == "Bonjour" | ||
string.delete(connection) | ||
end | ||
end | ||
end | ||
|
||
describe "#delete" do | ||
it "should delete a String" do | ||
string = WebTranslateIt::String.new(api_key, { "key" => "bacon" }) | ||
WebTranslateIt::Util.http_connection do |connection| | ||
string.save(connection) | ||
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id) | ||
string_fetched.should_not be_nil | ||
|
||
string_fetched.delete(connection) | ||
string_fetched = WebTranslateIt::String.find(connection, api_key, string.id) | ||
string_fetched.should be_nil | ||
end | ||
end | ||
end | ||
|
||
describe "#find_all" do | ||
it "should find many strings" do | ||
WebTranslateIt::Util.http_connection do |connection| | ||
string1 = WebTranslateIt::String.new(api_key, { "key" => "bacon" }) | ||
string1.save(connection) | ||
string2 = WebTranslateIt::String.new(api_key, { "key" => "tart" }) | ||
string2.save(connection) | ||
string3 = WebTranslateIt::String.new(api_key, { "key" => "bacon tart" }) | ||
string3.save(connection) | ||
|
||
strings = WebTranslateIt::String.find_all(connection, api_key, { "key" => "bacon" }) | ||
strings.count.should == 2 | ||
strings[0].key.should == "bacon" | ||
strings[1].key.should == "bacon tart" | ||
|
||
strings = WebTranslateIt::String.find_all(connection, api_key, { "key" => "tart" }) | ||
strings.count.should == 2 | ||
strings[0].key.should == "tart" | ||
strings[1].key.should == "bacon tart" | ||
|
||
strings = WebTranslateIt::String.find_all(connection, api_key, { "key" => "bacon tart" }) | ||
strings.count.should == 1 | ||
strings[0].key.should == "bacon tart" | ||
|
||
string1.delete(connection) | ||
string2.delete(connection) | ||
string3.delete(connection) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters