Sha256: 0ba4cd4d7d9aeae9099c580ac04f4903826795899387df08faa8be275e7e2e7c

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

require 'spec_helper'

describe Billy::JSONUtils do
  describe 'sorting' do
    describe '#sort_hash_keys' do
      it 'sorts simple Hashes' do
        data     = {c: 'three',a: 'one',b: 'two'}
        expected = {a: 'one',b: 'two',c: 'three'}
        expect(Billy::JSONUtils::sort_hash_keys(data)).to eq expected
      end

      it 'does not sort simple Arrays' do
        data     = [3,1,2,'two','three','one']
        expect(Billy::JSONUtils::sort_hash_keys(data)).to eq data
      end

      it 'does not sort multi-dimensional Arrays' do
        data     = [[3,2,1],[5,4,6],['b','c','a']]
        expect(Billy::JSONUtils::sort_hash_keys(data)).to eq data
      end

      it 'sorts multi-dimensional Hashes' do
        data     = {c: {l: 2,m: 3,k: 1},a: {f: 3,e: 2,d: 1},b: {i: 2,h: 1,j: 3}}
        expected = {a: {d: 1,e: 2,f: 3},b: {h: 1,i: 2,j: 3},c: {k: 1,l: 2,m: 3}}
        expect(Billy::JSONUtils::sort_hash_keys(data)).to eq expected
      end

      it 'sorts abnormal data structures' do
        data     = {b: [['b','c','a'],{ab: 5,aa: 4, ac: 6},[3,2,1],{ba: true,bc: false, bb: nil}],a: {f: 3,e: 2,d: 1}}
        expected = {a: {d: 1,e: 2,f: 3},b: [['b','c','a'],{aa: 4,ab: 5,ac: 6},[3,2,1],{ba: true, bb: nil,bc: false}]}
        expect(Billy::JSONUtils::sort_hash_keys(data)).to eq expected
      end
    end

    describe 'sort_json' do
      it 'sorts JSON' do
        data     = '{"c":"three","a":"one","b":"two"}'
        expected = '{"a":"one","b":"two","c":"three"}'
        expect(Billy::JSONUtils::sort_json(data)).to eq expected
      end
    end
  end

  describe 'json?' do
    let(:json) { {a: '1'}.to_json }
    let(:non_json) { 'Not JSON.' }

    it 'identifies JSON' do
      expect(Billy::JSONUtils::json?(json)).to be true
    end
    it 'identifies non-JSON' do
      expect(Billy::JSONUtils::json?(non_json)).to be false
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
puffing-billy-0.4.1 spec/lib/billy/resource_utils_spec.rb
puffing-billy-0.4.0 spec/lib/billy/resource_utils_spec.rb
puffing-billy-0.3.0 spec/lib/billy/resource_utils_spec.rb