Sha256: 0220aece842b0401d008fd9cc834bf9d161f57a0c0c551afc6043848235a4aab

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

require 'spec_helper'

RSpec.describe Netstring do
  let :strings do
    {
      '' => '0:,',
      'x' => '1:x,',
      'xy' => '2:xy,',
    }
  end

  let :netstrings do
    strings.invert
  end

  describe '.dump' do
    it 'should dump a netstring' do
      strings.each do |string,netstring|
        expect(Netstring.dump(string)).to eq(netstring)
      end
    end

    it 'should not dump a non-string' do
      {
        nil => 'nil',
        true => 'true',
        false => 'false',
        1 => '1',
        1.1 => '1.1',
        [] => '[]',
        {} => '{}',
      }.each do |nonstring,inspect|
        expect{Netstring.dump(nonstring)}.to raise_error(Netstring::Error, "#{inspect} is not a String")
      end
    end
  end

  describe '.load' do
    it 'should load a netstring' do
      netstrings.each do |netstring,string|
        expect(Netstring.load(netstring)).to eq(string)
      end
    end

    it 'should not load a nonstring' do
      {
        nil => 'nil',
        true => 'true',
        false => 'false',
        1 => '1',
        1.1 => '1.1',
        [] => '[]',
        {} => '{}',
      }.each do |nonstring,inspect|
        expect{Netstring.load(nonstring)}.to raise_error(Netstring::Error, "#{inspect} is not a String")
      end
    end

    it 'should not load a non-netstring' do
      [
        ['1', ':', 'x', ','],
        ['1', ':', 'xy', ','],
        ['2', ':', 'x', ','],
      ].each do |tokens|
        1.upto(4) do |n|
          tokens.permutation(n) do |p|
            nonnetstring = p.join
            unless ['1:x,'].include?(nonnetstring)
              expect{Netstring.load(nonnetstring)}.to raise_error(Netstring::Error)
            end
          end
        end
      end
    end
  end

  describe '#initialize' do
    it 'should initialize a netstring' do
      expect(Netstring.new('1:x,2:xy,', 2, 1)).to eq('x')
    end
  end

  describe '#netstring' do
    it 'should return the netstring' do
      expect(Netstring.new('1:x,2:xy,', 2, 1).netstring).to eq('1:x,')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
netstring-0.0.2 spec/netstring_spec.rb