Navigation Menu

Skip to content

Commit

Permalink
Testing and fixing... things.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Distad and Alex Redington committed Apr 1, 2011
1 parent 3294811 commit b00e6c2
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 13 deletions.
9 changes: 3 additions & 6 deletions ext/bamfcsv/bamfcsv_ext.c
Expand Up @@ -98,10 +98,7 @@ VALUE build_matrix(char *buf, int bufsize) {
for (cur = buf; cur < buf+bufsize; cur++) {

if (*cur == '"') {
if (in_quote)
in_quote = 0;
else
in_quote = 1;
in_quote = !in_quote;
}

if (!in_quote) {
Expand Down Expand Up @@ -166,7 +163,7 @@ VALUE mm_parse(const char *file) {
return matrix;
}

VALUE do_the_parsing(VALUE self, VALUE file) {
VALUE read_path(VALUE self, VALUE file) {

return mm_parse(RSTRING_PTR(file));

Expand All @@ -175,6 +172,6 @@ VALUE do_the_parsing(VALUE self, VALUE file) {
void Init_bamfcsv() {

VALUE module = rb_define_module("BAMFCSV");
rb_define_module_function(module, "parse", do_the_parsing, 1);
rb_define_module_function(module, "read_path", read_path, 1);

}
4 changes: 0 additions & 4 deletions ext/bamfcsv/bamfcsv_ext.h
Expand Up @@ -13,10 +13,6 @@ struct s_Cell {
struct s_Cell *next_cell;
};

VALUE mm_parse(const char *file);

VALUE do_the_parsing(VALUE self, VALUE file);

void Init_bamfcsv();

#endif
7 changes: 7 additions & 0 deletions lib/bamfcsv.rb
Expand Up @@ -2,4 +2,11 @@

module BAMFCSV

def self.read(thing_to_read)
if String === thing_to_read
raise ArgumentError.new("#{thing_to_read} is not a flat file.") unless File.file? thing_to_read
read_path(thing_to_read)
end
end

end
1 change: 1 addition & 0 deletions spec/fixtures/bamf-comma-comma.csv
@@ -0,0 +1 @@
BAMF,,CSV
3 changes: 3 additions & 0 deletions spec/fixtures/double-quotes.csv
@@ -0,0 +1,3 @@
this is a semicolon:, ;
this is a comma:," ,"
this is a quote:," """
Empty file added spec/fixtures/empty.csv
Empty file.
2 changes: 2 additions & 0 deletions spec/fixtures/escapes.csv
@@ -0,0 +1,2 @@
this is a semicolon:, ;
this is a comma:," ,"
2 changes: 2 additions & 0 deletions spec/fixtures/one-column.csv
@@ -0,0 +1,2 @@
BAMF
CSV
2 changes: 2 additions & 0 deletions spec/fixtures/terminated-with-cr.csv
@@ -0,0 +1,2 @@
a
b
42 changes: 39 additions & 3 deletions spec/lib/bamfcsv_spec.rb
Expand Up @@ -2,12 +2,48 @@

describe BAMFCSV do
it "has a parse method" do
BAMFCSV.should respond_to(:parse)
BAMFCSV.should respond_to(:read)
end

describe "#parse" do
describe "#read" do
it "is a matrix given a filename" do
BAMFCSV.parse("spec/fixtures/test.csv").should be_instance_of Array
BAMFCSV.read("spec/fixtures/test.csv").should be_instance_of Array
end

it "is an empty array passed an empty file" do
BAMFCSV.read("spec/fixtures/empty.csv").should == []
end

it "is a 1xn matrix with a one column csv file" do
BAMFCSV.read("spec/fixtures/one-column.csv").should == [["BAMF"],["CSV"]]
end

it "interprets empty cells correctly" do
BAMFCSV.read("spec/fixtures/bamf-comma-comma.csv").should == [["BAMF","","CSV"]]
end

it "escapes cells that are quoted" do
BAMFCSV.read("spec/fixtures/escapes.csv").should == [["this is a semicolon:", " ;"],["this is a comma:", " ,"]]
end

it "escapes cells that are quoted" do
BAMFCSV.read("spec/fixtures/double-quotes.csv").should == [["this is a semicolon:", " ;"], ["this is a comma:", " ,"], ["this is quote:", " \""]]
end

it "doesn't create a row when the file terminates with [CR]LF" do
BAMFCSV.read("spec/fixtures/terminated-with-cr.csv").should == [["a"],["b"]]
end

it "raises Errno::ENOENT when the file does not exist" do
expect do
BAMFCSV.read("spec/fixtures/this-file-does-not-not-exist.csv")
end.should raise_error Errno::ENOENT
end

it "raises Errno::EISDIR when the file is not a flat file" do
expect do
BAMFCSV.read("spec/fixtures/")
end.should raise_error Errno::EISDIR
end
end
end

0 comments on commit b00e6c2

Please sign in to comment.