Sha256: d3f58c93ba479fa1a11ae2cb283a62715d29217ab20fa9ca206b9025eda8dc5e

Contents?: true

Size: 1.57 KB

Versions: 51

Compression:

Stored size: 1.57 KB

Contents

require 'spec_helper'
require 'cgi'

describe RightSupport::Net::StringEncoder do
  #Alias for brevity's sake
  ENCODINGS = RightSupport::Net::StringEncoder::ENCODINGS

  before(:all) do
    #Generate some random binary test vectors with varying
    #lengths, 2-1024 bytes
    @strings = []
    (1..10).each do |i|
      s = ''
      i.times { s << rand(256) }

      if RUBY_VERSION >= '1.9'
        @strings << s.force_encoding(Encoding::ASCII_8BIT)
      else
        @strings << s
      end
    end
  end

  context :initialize do
    it 'accepts a glob of encodings' do
      obj = RightSupport::Net::StringEncoder.new(:base64, :url)
      obj.decode(obj.encode('moo')).should == 'moo'
    end

    it 'accepts an Array of encodings' do
      obj = RightSupport::Net::StringEncoder.new([:base64, :url])
      obj.decode(obj.encode('moo')).should == 'moo'
    end

    context 'when unknown encodings are specified' do
      it 'raises ArgumentError' do
        lambda do
          RightSupport::Net::StringEncoder.new(:xyzzy, :foobar)
        end.should raise_error(ArgumentError)
      end
    end
  end

  #Ensure that encodings are symmetrical and commutative by testing round-trip
  #for all combinations.
  (1..ENCODINGS.length).each do |n|
    context "when using any #{n} encoding#{n > 1 ? 's' : ''}" do
      ENCODINGS.combination(n).each do |list|
        it "round-trips #{list.join(', ')}" do
          obj = RightSupport::Net::StringEncoder.new(*list)
          @strings.each do |str|
            obj.decode(obj.encode(str)).should == str
          end
        end
      end
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
right_support-2.11.3 spec/net/string_encoder_spec.rb
right_support-2.11.2 spec/net/string_encoder_spec.rb
right_support-2.10.1 spec/net/string_encoder_spec.rb
right_support-2.9.6 spec/net/string_encoder_spec.rb
right_support-2.9.5 spec/net/string_encoder_spec.rb
right_support-2.9.4 spec/net/string_encoder_spec.rb
right_support-2.9.3 spec/net/string_encoder_spec.rb
right_support-2.9.2 spec/net/string_encoder_spec.rb
right_support-2.9.1 spec/net/string_encoder_spec.rb
right_support-2.8.46 spec/net/string_encoder_spec.rb
right_support-2.8.45 spec/net/string_encoder_spec.rb
right_support-2.8.44 spec/net/string_encoder_spec.rb
right_support-2.8.43 spec/net/string_encoder_spec.rb
right_support-2.8.42 spec/net/string_encoder_spec.rb
right_support-2.8.41 spec/net/string_encoder_spec.rb
right_support-2.8.40 spec/net/string_encoder_spec.rb
right_support-2.8.39 spec/net/string_encoder_spec.rb
right_support-2.8.38 spec/net/string_encoder_spec.rb
right_support-2.8.37 spec/net/string_encoder_spec.rb
right_support-2.8.36 spec/net/string_encoder_spec.rb