Sha256: 29ff748a56740d5de42005abec9d8ab75102fc7b4d9073856e672919b382d56b

Contents?: true

Size: 1.95 KB

Versions: 6

Compression:

Stored size: 1.95 KB

Contents

require 'spec_helper'

describe 'Tree commands' do
  delegate :run_cmd, to: :TestCodebase
  before do
    TestCodebase.setup
  end

  after do
    TestCodebase.teardown
  end

  context 'tree-merge' do
    trees = [{'a' => '1', 'b' => '2'}, {'a' => '-1', 'c' => '3'}]
    it trees.map(&:to_json).join(', ') do
      merged = JSON.parse run_cmd('tree-merge', '-fjson', '-S', *trees.map(&:to_json))
      expect(merged).to eq trees.reduce(:merge)
    end
  end

  context 'tree-filter' do
    forest  = {'a' => '1', 'b' => '2', 'c' => {'a' => '3'}}
    pattern = '{a,c.*}'
    it "-p #{pattern.inspect} #{forest.to_json}" do
      selected = JSON.parse run_cmd('tree-filter', '-fjson', '-p', pattern, forest.to_json)
      expect(selected).to eq(forest.except('b'))
    end
  end

  context 'tree-subtract' do
    trees = [{'a' => '1', 'b' => '2'}, {'a' => '-1', 'c' => '3'}]
    it trees.map(&:to_json).join(' - ') do
      subtracted = JSON.parse run_cmd('tree-subtract', '-fjson', '-S', *trees.map(&:to_json))
      expected = {'b' => '2'}
      expect(subtracted).to eq expected
    end
  end

  context 'tree-rename-key' do
    def forest
      {'a' => {'b' => {'a' => '1'}}}
    end

    def rename_key(from, to)
      JSON.parse run_cmd('tree-rename-key', '-fjson', '-k', from, '-n', to, forest.to_json)
    end

    it 'renames root node' do
      expect(rename_key('a', 'x')).to eq(forest.tap { |f| f['x'] = f.delete('a') })
    end
    it 'renames node' do
      expect(rename_key('a.b', 'x')).to eq(forest.tap { |f| f['a']['x'] = f['a'].delete('b') })
    end
    it 'renames leaf' do
      expect(rename_key('a.b.a', 'x')).to eq(forest.tap { |f| f['a']['b']['x'] = f['a']['b'].delete('a') })
    end
  end

  context 'tree-convert' do
    def forest
      {'x' => '1', 'a' => {'b' => {'a' => '2'}}}
    end

    it 'converts to keys' do
      keys = run_cmd('tree-convert', '-fjson', '-tkeys', forest.to_json).split("\n")
      expect(keys.sort).to eq(['a.b.a', 'x'].sort)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
i18n-tasks-0.8.5 spec/commands/tree_commands_spec.rb
i18n-tasks-0.8.4 spec/commands/tree_commands_spec.rb
i18n-tasks-0.8.3 spec/commands/tree_commands_spec.rb
i18n-tasks-0.8.2 spec/commands/tree_commands_spec.rb
i18n-tasks-0.8.1 spec/commands/tree_commands_spec.rb
i18n-tasks-0.8.0 spec/commands/tree_commands_spec.rb