Permalink
Browse files

Specs for the Orient::Encoder.

Remove unused #sanitized_properties
  • Loading branch information...
1 parent 3118a4f commit cf04ca9bf945a51dae6c0ec6df2fa9e8478fca5a @pangloss committed Sep 20, 2012
Showing with 104 additions and 7 deletions.
  1. +1 −0 .gitignore
  2. +2 −0 .rspec
  3. +5 −0 Gemfile
  4. +2 −7 lib/pacer-orient/encoder.rb
  5. +78 −0 spec/pacer-orient/encoder_spec.rb
  6. +16 −0 spec/spec_helper.rb
View
@@ -1,2 +1,3 @@
lib/*.jar
pkg
+Gemfile.lock
View
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
@@ -2,3 +2,8 @@ source "http://rubygems.org"
# Specify your gem's dependencies in pacer-graph.gemspec
gemspec
+
+group :development do
+ gem 'rspec', '~> 2.10.0'
+ gem 'pacer', path: '../pacer'
+end
@@ -1,14 +1,9 @@
+require 'java'
+
module Pacer::Orient
class Encoder
JavaDate = java.util.Date
- def self.sanitize_properties(props)
- pairs = props.map do |name, value|
- [name, encode_property(value)]
- end
- Hash[pairs]
- end
-
def self.encode_property(value)
case value
when nil
@@ -0,0 +1,78 @@
+require 'spec_helper'
+
+module Pacer::Orient
+ describe Encoder do
+ let(:original) do
+ { :string => ' bob ',
+ :symbol => :abba,
+ :empty => '',
+ :integer => 121,
+ :float => 100.001,
+ :time => Time.utc(1999, 11, 9, 9, 9, 1),
+ :object => { :a => 1, 1 => :a },
+ :set => Set[1, 2, 3],
+ :nested_array => [1, 'a', [2]],
+ :ok_string => 'string value'
+ }
+ end
+
+ describe '#encode_property' do
+ subject do
+ pairs = original.map do |name, value|
+ [name, Encoder.encode_property(value)]
+ end
+ Hash[pairs]
+ end
+
+ it { should_not equal(original) }
+
+ specify 'string should be stripped' do
+ subject[:string].should == 'bob'
+ end
+
+ specify 'empty string becomes nil' do
+ subject[:empty].should be_nil
+ end
+
+ specify 'numbers should be javafied' do
+ subject[:integer].should == 121
+ subject[:float].should == 100.001
+ end
+
+ specify 'time is unmodified' do
+ subject[:time].should == Time.utc(1999, 11, 9, 9, 9, 1)
+ end
+
+ specify 'everything else should be binary' do
+ subject[:set].to_a.should == Marshal.dump(original[:set]).to_java_bytes.to_a
+ subject[:object].to_a.should == Marshal.dump(original[:object]).to_java_bytes.to_a
+ end
+ end
+
+ describe '#decode_property' do
+ it 'should round-trip cleanly' do
+ # remove values that get cleaned up when encoded
+ original.delete :string
+ original.delete :empty
+
+ original.values.each do |value|
+ encoded = Encoder.encode_property(value)
+ decoded = Encoder.decode_property(encoded)
+ decoded.should == value
+ end
+ end
+
+ it 'should strip strings' do
+ encoded = Encoder.encode_property(' a b c ')
+ decoded = Encoder.decode_property(encoded)
+ decoded.should == 'a b c'
+ end
+
+ it 'empty strings -> nil' do
+ encoded = Encoder.encode_property(' ')
+ decoded = Encoder.decode_property(encoded)
+ decoded.should be_nil
+ end
+ end
+ end
+end
View
@@ -0,0 +1,16 @@
+require 'rubygems'
+Bundler.setup(:default, :development)
+require 'pacer'
+require 'pacer-orient'
+
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper.rb"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+end

0 comments on commit cf04ca9

Please sign in to comment.