lunar_cheese 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,114 @@
1
+ #coding: utf-8
2
+
3
+ require_relative '../spec_helper'
4
+
5
+ module LunarCheese
6
+
7
+ describe LuaTableDumpParser do
8
+ let(:p) { LuaTableDumpParser.new }
9
+
10
+ context 'parsing tables' do
11
+ before(:each) { p.root = :table }
12
+
13
+ context 'parsing' do
14
+
15
+ context 'valid tables' do
16
+
17
+ context 'flat tables' do
18
+
19
+ it 'should parse empty tables' do
20
+ expect(parse('{}')).to eq({})
21
+ end
22
+
23
+ it 'should parse array-like tables' do
24
+ expect(parse('{1,2,3,4}')).to eq({ 0 => 1, 1 => 2, 2 => 3, 3 => 4 })
25
+ expect(parse('{"Switzerland","Germany","France"}')).to eq({ 0 => 'Switzerland', 1 => 'Germany', 2 => 'France' })
26
+ expect(parse('{false,false,true,false}')).to eq({ 0 => false, 1 => false, 2 => true, 3 => false })
27
+ end
28
+
29
+ it 'should parse hash-like tables' do
30
+ expect(parse('{x=10}')).to eq({ :x => 10 })
31
+ expect(parse('{name="John",age=25,salary=20.5,on_fire=true}')).to eq({ :name => 'John', :age => 25, :salary => 20.5, :on_fire => true })
32
+ end
33
+
34
+ it 'should parse mixed tables' do
35
+ expect(parse('{pos="relative",10,5,20}')).to eq({ :pos => 'relative', 0 => 10, 1 => 5, 2 => 20 })
36
+ expect(parse('{"a",[1]=5,6,foo="bar"}')).to eq({ 0 => 'a', :'[1]' => 5, 1 => 6, :foo => 'bar' })
37
+ end
38
+
39
+ end
40
+
41
+ context 'nested tables' do
42
+
43
+ it 'should parse array-like tables' do
44
+ expect(parse('{1,{1.1,1.2},2}')).to eq({ 0 => 1, 1 => { 0 => 1.1, 1 => 1.2 }, 2 => 2 })
45
+ expect(parse('{{29.2,119.2},{83.1,134}}')).to eq({ 0 => { 0 => 29.2, 1 => 119.2 }, 1 => { 0 => 83.1, 1 => 134 } })
46
+ end
47
+
48
+ it 'should parse hash-like tables' do
49
+ expect(parse('{name={first_name="John",last_name="Madden"}}')).to eq({ :name => { :first_name => 'John', :last_name => 'Madden' } })
50
+ end
51
+
52
+ it 'should parse mixed tables' do
53
+ expect(parse('{1,2,coords={3,4},5,foo="bar",{10,20}}')).to eq({ 0 => 1, 1 => 2, :coords => { 0 => 3, 1 => 4 }, 2 => 5, :foo => 'bar', 3 => { 0 => 10, 1 => 20 } })
54
+ expect(parse('{coordinates={21,33}}')).to eq({ :coordinates => { 0 => 21, 1 => 33 } })
55
+ expect(parse('{{id=1,price=5.0},{id=2,price=1.3}}')).to eq({ 0 => { :id => 1, :price => 5.0 }, 1 => { :id => 2, :price => 1.3 } })
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+
62
+ context 'invalid tables' do
63
+
64
+ it 'should not parse tables with missing commas' do
65
+ expect(parse('{foo="bar"baz="boo"}')).to be_nil
66
+ end
67
+
68
+ it 'should not parse tables which are missing the leading or trailing bracket' do
69
+ expect(parse('{foo="bar"')).to be_nil
70
+ expect(parse('foo="bar"}')).to be_nil
71
+ end
72
+
73
+ it 'should not parse tables which have too many commas' do
74
+ expect(parse('{,}')).to be_nil
75
+ expect(parse('{,foo="bar"}')).to be_nil
76
+ expect(parse('{foo="bar",}')).to be_nil
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ context 'dumping' do
86
+
87
+ it 'should dump array-like tables with integer indices' do
88
+ expect({ 0 => 1, 1 => 2, 2 => 3, 3 => 4 }.to_lua).to eq '{1,2,3,4}'
89
+ expect({ 0 => 1, 1 => { 0 => 1.1, 1 => 1.2 }, 2 => 2 }.to_lua).to eq '{1,{1.1,1.2},2}'
90
+ expect({ 0 => 'Switzerland', 1 => 'Germany', 2 => 'France' }.to_lua).to eq '{"Switzerland","Germany","France"}'
91
+ expect({ 0 => false, 1 => false, 2 => true, 3 => false }.to_lua).to eq '{false,false,true,false}'
92
+ end
93
+
94
+ it 'should dump array-like tables with mixed indices' do
95
+ expect({ 0 => 'a', :'[1]' => 5, 1 => 6, :'[5]' => 'bar' }.to_lua).to eq '{"a",[1]=5,6,[5]="bar"}'
96
+ end
97
+
98
+ it 'should dump hash-like tables' do
99
+ expect({ :name => 'John', :age => 25, :salary => 20.5, :on_fire => true }.to_lua).to eq '{name="John",age=25,salary=20.5,on_fire=true}'
100
+ expect({ :name => { :first_name => 'John', :last_name => 'Madden' } }.to_lua).to eq '{name={first_name="John",last_name="Madden"}}'
101
+ end
102
+
103
+ it 'should dump mixed tables' do
104
+ expect({ :pos => 'relative', 0 => 10, 1 => 5, 2 => 20 }.to_lua).to eq '{pos="relative",10,5,20}'
105
+ expect({ 0 => 1, 1 => 2, :coords => { 0 => 3, 1 => 4 }, 2 => 5, :foo => 'bar', 3 => { 0 => 10, 1 => 20 } }.to_lua).to eq '{1,2,coords={3,4},5,foo="bar",{10,20}}'
106
+ expect({ :coordinates => { 0 => 21, 1 => 33 } }.to_lua).to eq '{coordinates={21,33}}'
107
+ expect({ 0 => { :id => 1, :price => 5.0 }, 1 => { :id => 2, :price => 1.3 } }.to_lua).to eq '{{id=1,price=5.0},{id=2,price=1.3}}'
108
+ end
109
+
110
+ end
111
+
112
+ end
113
+
114
+ end
@@ -0,0 +1,14 @@
1
+ #coding: utf-8
2
+
3
+ require_relative '../lib/lunar_cheese'
4
+ require_relative '../lib/lunar_cheese/dump'
5
+
6
+ def parse(input)
7
+ ast = p.parse(input)
8
+
9
+ if ast.respond_to?(:to_rb)
10
+ ast.to_rb
11
+ else
12
+ nil
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lunar_cheese
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - Michael Senn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: treetop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: 'A gem consisting of two parts: A Treetop-based grammar which allows
42
+ to parse save games of the game "Don''t Starve" to Ruby objects, and additions to
43
+ Ruby core classes to dump those back into the same format.'
44
+ email: morrolan@morrolan.ch
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .yardopts
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/lunar_cheese.rb
54
+ - lib/lunar_cheese/ast.rb
55
+ - lib/lunar_cheese/dump.rb
56
+ - lib/lunar_cheese/grammar.rb
57
+ - lib/lunar_cheese/grammar.treetop
58
+ - lunar_cheese.gemspec
59
+ - spec/lunar_cheese/assignments_spec.rb
60
+ - spec/lunar_cheese/booleans_spec.rb
61
+ - spec/lunar_cheese/floats_spec.rb
62
+ - spec/lunar_cheese/identifiers_spec.rb
63
+ - spec/lunar_cheese/integers_spec.rb
64
+ - spec/lunar_cheese/strings_spec.rb
65
+ - spec/lunar_cheese/tables_spec.rb
66
+ - spec/spec_helper.rb
67
+ homepage: https://bitbucket.org/Lavode/lunar-cheese
68
+ licenses:
69
+ - Apache 2.0
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.0.3
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Treetop grammar & additions to core classes to convert "Don't Starve" save
91
+ games to Ruby and vice versa.
92
+ test_files:
93
+ - spec/lunar_cheese/assignments_spec.rb
94
+ - spec/lunar_cheese/booleans_spec.rb
95
+ - spec/lunar_cheese/floats_spec.rb
96
+ - spec/lunar_cheese/identifiers_spec.rb
97
+ - spec/lunar_cheese/integers_spec.rb
98
+ - spec/lunar_cheese/strings_spec.rb
99
+ - spec/lunar_cheese/tables_spec.rb
100
+ - spec/spec_helper.rb
101
+ has_rdoc: