Skip to content

Commit

Permalink
Refine specdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
infused committed Jul 25, 2010
1 parent 9774d4c commit 81d6f48
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions spec/unit/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
@column = DBF::Column.new "ColumnName", "N", 1, 0
end

it "should set the #name accessor" do
it "sets the #name accessor" do
@column.name.should == "ColumnName"
end

it "should set the #type accessor" do
it "sets the #type accessor" do
@column.type.should == "N"
end

it "should set the #length accessor" do
it "sets the #length accessor" do
@column.length.should == 1
end

it "should set the #decimal accessor" do
it "sets the #decimal accessor" do
@column.decimal.should == 0
end

Expand All @@ -36,20 +36,20 @@
end
end

context "#type_cast" do
context '#type_cast' do
context 'with type N (number)' do
context 'with 0 decimals' do
it 'should cast value to Fixnum' do
value = "135"
context 'and 0 decimals' do
it 'casts value to Fixnum' do
value = '135'
column = DBF::Column.new "ColumnName", "N", 3, 0
column.type_cast(value).should be_a Fixnum
column.type_cast(value).should == 135
end
end

context 'with more than 0 decimals' do
it "should cast value to Float" do
value = "13.5"
context 'and more than 0 decimals' do
it 'casts value to Float' do
value = '13.5'
column = DBF::Column.new "ColumnName", "N", 2, 1
column.type_cast(value).should be_a Float
column.type_cast(value).should == 13.5
Expand All @@ -58,50 +58,47 @@
end

context 'with type F (float)' do
it "should cast value to Float" do
value = "135"
it 'casts value to Float' do
value = '135'
column = DBF::Column.new "ColumnName", "F", 3, 0
column.type_cast(value).should be_a Float
column.type_cast(value).should == 135.0
end
end

context 'with type I (integer)' do
it "should cast value to Fixnum" do
it "casts value to Fixnum" do
value = "\203\171\001\000"
column = DBF::Column.new "ColumnName", "I", 3, 0
column.type_cast(value).should be_a Fixnum
column.type_cast(value).should == 96643
end
end

context 'with type L (logical/boolean)' do
it "should cast 'y' to true" do
it "casts 'y' to true" do
value = "y"
column = DBF::Column.new "ColumnName", "L", 1, 0
column.type_cast(value).should be_a TrueClass
column.type_cast(value).should == true
end

it "should cast 'n' to false" do
it "casts 'n' to false" do
value = "n"
column = DBF::Column.new "ColumnName", "L", 1, 0
column.type_cast(value).should be_a FalseClass
column.type_cast(value).should == false
end
end

context 'with type T (datetime)' do
context 'with valid datetime' do
it "should cast to DateTime" do
it "casts to DateTime" do
value = "Nl%\000\300Z\252\003"
column = DBF::Column.new "ColumnName", "T", 16, 0
column.type_cast(value).should == "2002-10-10T17:04:56+00:00"
end
end

context 'with invalid datetime' do
it "should cast to nil" do
it "casts to nil" do
value = "Nl%\000\000A\000\999"
column = DBF::Column.new "ColumnName", "T", 16, 0
column.type_cast(value).should be_nil
Expand All @@ -111,15 +108,15 @@

context 'with type D (date)' do
context 'with valid date' do
it "should cast to Date" do
it "casts to Date" do
value = "20050712"
column = DBF::Column.new "ColumnName", "D", 8, 0
column.type_cast(value).should == Date.new(2005,7,12)
end
end

context 'with invalid date' do
it "should cast to Date" do
it "casts to nil" do
value = "0"
column = DBF::Column.new "ColumnName", "D", 8, 0
column.type_cast(value).should be_nil
Expand All @@ -128,7 +125,7 @@
end

context 'with type M (memo)' do
it "should typecast memo columns to String" do
it "casts to string" do
value = 'abc'
column = DBF::Column.new "ColumnName", "M", 3, 0
column.type_cast(value).should be_a String
Expand All @@ -138,38 +135,38 @@

context "#schema_definition" do
context 'with type N (number)' do
it "should output an integer column" do
it "outputs an integer column" do
column = DBF::Column.new "ColumnName", "N", 1, 0
column.schema_definition.should == "\"column_name\", :integer\n"
end
end

it "should define a float colmn if type is (N)umber with more than 0 decimals" do
it "defines a float colmn if type is (N)umber with more than 0 decimals" do
column = DBF::Column.new "ColumnName", "N", 1, 2
column.schema_definition.should == "\"column_name\", :float\n"
end

it "should define a date column if type is (D)ate" do
it "defines a date column if type is (D)ate" do
column = DBF::Column.new "ColumnName", "D", 8, 0
column.schema_definition.should == "\"column_name\", :date\n"
end

it "should define a datetime column if type is (D)ate" do
it "defines a datetime column if type is (D)ate" do
column = DBF::Column.new "ColumnName", "T", 16, 0
column.schema_definition.should == "\"column_name\", :datetime\n"
end

it "should define a boolean column if type is (L)ogical" do
it "defines a boolean column if type is (L)ogical" do
column = DBF::Column.new "ColumnName", "L", 1, 0
column.schema_definition.should == "\"column_name\", :boolean\n"
end

it "should define a text column if type is (M)emo" do
it "defines a text column if type is (M)emo" do
column = DBF::Column.new "ColumnName", "M", 1, 0
column.schema_definition.should == "\"column_name\", :text\n"
end

it "should define a string column with length for any other data types" do
it "defines a string column with length for any other data types" do
column = DBF::Column.new "ColumnName", "X", 20, 0
column.schema_definition.should == "\"column_name\", :string, :limit => 20\n"
end
Expand All @@ -180,11 +177,11 @@
@column = DBF::Column.new "ColumnName", "N", 1, 0
end

it "should strip characters below decimal 32 and above decimal 127" do
it "strips characters below decimal 32 and above decimal 127" do
@column.strip_non_ascii_chars("--\x1F-\x68\x65\x6C\x6C\x6F world-\x80--").should == "---hello world---"
end

it "should truncate characters with decimal 0" do
it "truncates characters with decimal 0" do
@column.strip_non_ascii_chars("--\x1F-\x68\x65\x6C\x6C\x6F \x00 world-\x80--").should == "---hello "
end
end
Expand Down

0 comments on commit 81d6f48

Please sign in to comment.