forked from emilybache/TDDwithMockObjectsAndDesignPrinciples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ruby solution for the UnicodeFileToHtmlTextConverter problem
- Loading branch information
Sam Serpoosh
committed
Sep 12, 2012
1 parent
f1f84cb
commit ccc1cc6
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...-Serpoosh/code/unicode_file_to_html_text_converter/unicode_file_to_html_text_converter.rb
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,30 @@ | ||
class UnicodeFileToHtmTextConverter | ||
def initialize(full_file_name_with_path) | ||
@full_file_name_with_path = full_file_name_with_path | ||
@html = "" | ||
end | ||
|
||
def convert_to_html | ||
text = read_content | ||
text.each_line { |line| @html += to_html(line) } | ||
|
||
@html | ||
end | ||
|
||
private | ||
def read_content | ||
FileReader.read_content(@full_file_name_with_path) | ||
end | ||
|
||
def to_html(text) | ||
converted = HttpUtility.html_encode(text) | ||
converted += "<br />" | ||
end | ||
end | ||
|
||
class FileReader | ||
def self.read_content(file_path) | ||
text = File.open(file_path).read # Not exactly the same thing as the # CShapr code but same functionality! | ||
text.gsub!(/\r\n?/, "\n") | ||
end | ||
end |
33 changes: 33 additions & 0 deletions
33
...spec/unicode_file_to_html_text_converter_spec/unicode_file_to_html_text_converter_spec.rb
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,33 @@ | ||
require_relative "../../code/unicode_file_to_html_text_converter/unicode_file_to_html_text_converter" | ||
|
||
class HttpUtility; end | ||
|
||
describe UnicodeFileToHtmTextConverter do | ||
let(:file_path) { stub } | ||
let(:foobar) { "foobar" } | ||
|
||
before do | ||
@converter = UnicodeFileToHtmTextConverter.new(file_path) | ||
end | ||
|
||
it "reads the file content" do | ||
FileReader.should_receive(:read_content).with(file_path). | ||
and_return(foobar) | ||
HttpUtility.stub(:html_encode => foobar) | ||
|
||
@converter.convert_to_html | ||
end | ||
|
||
it "converts to html on a line basis" do | ||
FileReader.stub(:read_content => foobar) | ||
HttpUtility.should_receive(:html_encode).and_return("<b>foobar</b>") | ||
|
||
html = @converter.convert_to_html | ||
html.should == "<b>foobar</b><br />" | ||
end | ||
end | ||
|
||
# Another way of doing this which specially more suitable for | ||
# statically typed languages is inject the FileReader & | ||
# HttpUtility through the constructor according to | ||
# Dependency Inversion Principle |
Binary file removed
BIN
-12 KB
TDDMicroExercises.YoursSolutions/Sam-Serpoosh/code/telemetry_system/.telemetry_client.rb.swp
Binary file not shown.
Binary file removed
BIN
-12 KB
...s.YoursSolutions/Sam-Serpoosh/code/telemetry_system/.telemetry_diagnostic_controls.rb.swp
Binary file not shown.