require 'spec_helper' require 'pry' require 'exa' describe Exa do before do Exa.clean_slate! end it 'should store a value' do Exa.remember('x', 5) Exa.remember('y', 7) expect(Exa.recall('x').value).to eq(5) expect(Exa.recall('y').value).to eq(7) end it 'has shorthand' do Exa['hi'] = 'hello' expect( Exa['hi'].value ).to eq('hello') end it 'should store a structured value' do Exa.remember('/hello/world', 5) expect(Exa.recall('/hello/world').value).to eq(5) # shorthand expect(Exa['/hello/world'].value).to eq(5) end it 'should navigate' do Exa.remember("/a/b/c", 'd') expect(Exa["/a/b/c"].value).to eq('d') expect(Exa["/a/b/c"].path).to eq('/a/b/c') expect(Exa["/a"].children).to eq([Exa["/a/b"]]) expect(Exa["/a"].parent).to eq(Exa["/"]) expect(Exa['/'].path).to eq('/') end describe 'filesystem-like structures' do before do Exa["/usr/joe/minutes/alpha"] = "hello" Exa["/usr/joe/books/ch01/sec01/intro"] = "welcome" Exa["/usr/mal/minutes/beta"] = "world" end it 'should unify/virtualize' do Exa["/usr/mal/friends"].unify(overlay: Exa["/usr/joe"]) expect( Exa["/usr/mal/friends/minutes"] ).to be_virtual expect( Exa["/usr/mal/friends/minutes/alpha"] ).to be_virtual expect( Exa["/usr/mal/friends/minutes/alpha"].value ).to eq('hello') expect( Exa["/usr/mal/friends/books/ch01/sec01/intro"].value ).to eq('welcome') end it 'should symlink' do # okay, make a union mount so we have virtual paths... Exa["/usr/joe/friends"].unify(overlay: Exa["/usr/mal"]) expect( Exa['/usr/joe/friends/minutes'] ).to be_virtual # we should be able to *symlink* to virtual paths # which we can't otherwise mount directly (since virtual) Exa['/news'].link(source: Exa['/usr/joe/friends/minutes']) expect( Exa['/news/beta'] ).to be_symbolic expect( Exa['/news/beta'].value ).to eq('world') expect( Exa['/news/beta'].path ).to eq('/usr/joe/friends/minutes/beta') end describe 'path expansion' do it 'should expand root path' do expect( Exa.expand('/') ).to eq([ Exa['/'] ]) end it 'should expand user path without a slash' do expect( Exa.expand('usr')).to eq([ Exa['/usr'] ]) end it 'should expand double stars' do expect( Exa.expand('**/joe') ).to eq([ Exa['/usr/joe'] ]) end it 'should expand single stars' do expect( Exa.expand('/usr/*') ).to eq([ Exa['/usr/joe'], Exa['/usr/mal'] ]) end it 'should expand nested single stars' do expect( Exa.expand('/usr/joe/*/*') ).to eq([ Exa['/usr/joe/minutes/alpha'], Exa['/usr/joe/books/ch01'] ]) end it 'should expand double dots' do expect( Exa.expand('/usr/..') ).to eq([ Exa['/'] ]) expect( Exa.expand('/usr/joe/..') ).to eq([ Exa['/usr'] ]) end it 'should not find expansions for inexistent paths' do expect( Exa.expand('/does_not_exist') ).to eq([]) expect( Exa.expand('/also/does/not/exist') ).to eq([]) end end it 'should create/nav arbitrary structures' do abel_route = '/universes/ea/regions/blessed_isles/cities/numenor/people/abel' cain_route = '/universes/ea/regions/blessed_isles/cities/numenor/people/cain' Exa[abel_route] = 'hi from abel!' Exa[cain_route] = 'hi from cain!' expect( Exa.expand('/**/people/*') ).to eq([ Exa[abel_route], Exa[cain_route] ]) end it 'should perform some fs-like operations' do Exa['/tmp/hello'] = 'world' Exa['/tmp/hello'].copy(Exa['/tmp/there']) Exa['/tmp/hello'].delete expect( Exa['/tmp/there'].value ).to eq('world') expect( Exa['/tmp/hello'].value ).to eq(nil) end end end