require 'spec_helper'

describe 'Compile' do
  
  before do
    setup_mygame
  end
  
  after(:all) do
    teardown_mygame
  end
  
  it 'should ignore js to js' do
    
    js = 're.e("test");'
    
    out = Entityjs::Compile.script_to_js('script.js', js)
    
    out.should == js
    
  end
  
  it 'should ignore file with no extension' do
    
    js = 're.e("test");'
    
    out = Entityjs::Compile.script_to_js('script', js)
    
    out.should == js
  end
  
  it 'should convert coffee script to js' do
    coffee = 'square = (x) -> x * x'
    
    js = Entityjs::Compile.script_to_js('blah.coffee', coffee)
    
    js.should == "var square;\n\nsquare = function(x) {\n  return x * x;\n};\n"
  end
  
  it 'should convert to entity' do
    
    data = '{"ok":10}'
    comps = ['levels', 'level1.json']
    
    entity = Entityjs::Compile.to_entity(comps, data)
    
    entity.should match /ok/
    entity.should_not match /levels/
    entity.should match /level/
    entity.should match /level1\.json/
  end
  
  it 'should convert xml to json' do
    
    IO.stub(:read).and_return('<?xml ?><root><alice sid="4"><bob sid="1">charlie</bob><bob sid="2">david</bob></alice></root>')
    
    r = Entityjs::Compile.script_to_js('convert.xml')
    
    r.should match /alice/
    r.should_not match /root/
    r.should_not match /<\?xml.*\?>/
  end
  
  it 'should convert tmx to json' do
    
    IO.stub(:read).and_return(%q(
    <?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="37" height="15" tilewidth="25" tileheight="25">
 <tileset firstgid="1" name="Sheet" tilewidth="25" tileheight="25">
  <image source="tiles.png" width="200" height="25"/>
 </tileset>
 <tileset firstgid="9" name="Objects" tilewidth="25" tileheight="25">
  <image source="items.png" width="250" height="50"/>
 </tileset>
 <layer name="Back" width="37" height="15" opacity="0.43">
  <data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0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0,16,16,16,16,16,0,0,0,0,16,16,16,16,16,16,16,0,0,0,0,0,0,0,0,0,16,16,16,16,0,0,0,
0,0,0,0,0,15,15,15,15,0,0,0,0,0,0,15,15,15,15,15,15,0,0,0,0,0,0,0,0,0,0,15,15,15,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
 </layer>
</map>
))
    
    r = Entityjs::Compile.script_to_js('blap.tmx')
    
    r.should_not match /map/
    r.should match /"\$":\[\[0/
  end
  
  it 'should tmx should convert to json' do
    
    data = %q(<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="40" height="20" tilewidth="25" tileheight="25">
 <tileset firstgid="1" name="tiles" tilewidth="25" tileheight="25">
  <image source="../images/tiles.png" width="200" height="25"/>
 </tileset>
 <layer name="Tile Layer 1" width="40" height="20">
  <data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,1,1,1,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
</data>
 </layer>
 <objectgroup name="Hero" width="40" height="20">
  <object name="hero" x="43" y="356"/>
 </objectgroup>
</map>)


    r = Entityjs::Compile.script_to_js('level1.tmx', data)
    
    r.should_not match /map/
    r.should match /"\$":\[\[0/
  end
  
  it 'should return object for invalid xml' do
    xml = '<sdf>'
    
    lambda {
    Entityjs::Compile.xml_to_json('sdf.xml', xml)
    }.should raise_error
  end
  
  it 'should return json from invalid tmx' do
    tmx = "<root><bob>10</bob></root>"
    
    Entityjs::Compile.to_json('.tmx', tmx).should == '{"bob":{"$":10}}'
  end
  
  it 'should convert blank tmx to json' do
    tmx = ''
    
    Entityjs::Compile.to_json('.xml', tmx).should == '{}'
  end
  
end