Add V2 file format

This commit is contained in:
Andrew Miner
2015-01-14 15:19:02 -08:00
parent c417a24653
commit 89a8e2b539
16 changed files with 869 additions and 70 deletions
+9 -15
View File
@@ -18,20 +18,16 @@ describe 'CraftingPlan', ->
beforeEach ->
modPack = new ModPack
modPack.loadModVersionData {
dataVersion: 1
name: 'Minecraft'
version: '1.7.10'
recipes: [
{ input:'Oak Log', output:[[4, 'Oak Plank']] }
{ input:[[2, 'Oak Plank']], output:[[4, 'Stick']] }
{ input:[[4, 'Oak Plank']], output:'Crafting Table' }
{ input:[[8, 'Cobblestone']], tools:'Crafting Table', output:'Furnace' }
{ input:['Iron Ore', 'furnace fuel'], tools:'Furnace', output:'Iron Ingot' }
{ input:[[2, 'Iron Ingot'], 'Stick'], tools:'Crafting Table', output:'Iron Sword' }
]
}
modPack.loadModVersionData """
schema:2; name:Minecraft; version:1.7.10
item:Oak Plank; recipe:; input:Oak Log; pattern:... .0. ...; quantity:4
item:Stick; recipe:; input:Oak Plank; pattern:... .0. .0.; quantity:4
item:Crafting Table; recipe:; input:Oak Plank; pattern:00. 00. ...
item:Furnace; recipe:; input:Cobblestone; pattern:000 0.0 000; tools:Crafting Table
item:Iron Ingot; recipe:; input:Iron Ore, furnace fuel; pattern:.0. ... .1.; tools:Furnace
item:Iron Sword; recipe:; input:Iron Ingot, Stick; pattern:.0. .0. .1.; tools:Crafting Table
"""
plan = new CraftingPlan modPack:modPack, includingTools:false
describe 'craft', ->
@@ -81,5 +77,3 @@ describe 'CraftingPlan', ->
plan.result.toList().should.eql [
'crafting_table', 'furnace', 'iron_sword', [2, 'oak_plank'], [3, 'stick']
]
describe 'using existing inventory', ->
+6 -6
View File
@@ -19,17 +19,17 @@ describe 'ModPack', ->
beforeEach ->
minecraft = new ModVersion name:'Minecraft', version:'1.7.10', enabled:true
minecraft.addItem new Item name:'Wool'
minecraft.addItem new Item name:'Bed', recipes:['']
new Item modVersion:minecraft, name:'Wool'
new Item modVersion:minecraft, name:'Bed', recipes:['']
minecraft.registerSlug 'iron_chestplate', 'Iron Chestplate'
buildcraft = new ModVersion name:'Buildcraft', version:'4.0', enabled:false
buildcraft.addItem new Item name:'Stone Gear', recipes:['']
buildcraft.addItem new Item name:'Bed', recipes:['']
new Item modVersion:buildcraft, name:'Stone Gear', recipes:['']
new Item modVersion:buildcraft, name:'Bed', recipes:['']
industrialCraft = new ModVersion name:'Industrial Craft', version:'2.0', enabled:false
industrialCraft.addItem new Item name:'Resin'
industrialCraft.addItem new Item name:'Rubber', recipes:['']
new Item modVersion:industrialCraft, name:'Resin'
new Item modVersion:industrialCraft, name:'Rubber', recipes:['']
modPack = new ModPack modVersions:[minecraft, buildcraft, industrialCraft]
+12 -12
View File
@@ -34,11 +34,11 @@ describe 'ModVersion', ->
describe 'addItem', ->
it 'refuses to add duplicates', ->
modVersion.addItem new Item name:'Wool'
expect(-> modVersion.addItem new Item name:'Wool').to.throw Error, 'duplicate item for wool'
new Item modVersion:modVersion, name:'Wool'
expect(-> new Item modVersion:modVersion, name:'Wool').to.throw Error, 'duplicate item for Wool'
it 'adds an item indexes by its slug', ->
modVersion.addItem new Item name:'Wool'
new Item modVersion:modVersion, name:'Wool'
modVersion.items.wool.name.should.equal 'Wool'
describe 'compareTo', ->
@@ -56,26 +56,26 @@ describe 'ModVersion', ->
describe 'findItemByName', ->
it 'locates items by slugified name', ->
modVersion.addItem new Item name:'Crafting Table'
new Item modVersion:modVersion, name:'Crafting Table'
modVersion.findItemByName('Crafting Table').slug.should.equal 'crafting_table'
describe 'gatherNames', ->
it 'skips names already found', ->
modVersion.addItem new Item name:'Wool'
modVersion.addItem new Item name:'Oak Wood Planks', recipes:['foo']
new Item modVersion:modVersion, name:'Wool'
new Item modVersion:modVersion, name:'Oak Wood Planks', recipes:['foo']
names = modVersion.gatherNames {wool:true}
names.wool.should.be.true
names.oak_wood_planks.value.should.equal 'Oak Wood Planks'
it 'only includes craftable items', ->
modVersion.addItem new Item name:'Wool'
modVersion.addItem new Item name:'Oak Wood Planks', recipes:['foo']
new Item modVersion:modVersion, name:'Wool'
new Item modVersion:modVersion, name:'Oak Wood Planks', recipes:['foo']
names = modVersion.gatherNames()
_.keys(names).should.eql ['oak_wood_planks']
it 'computes the proper value and label', ->
modVersion.addItem new Item name:'Oak Wood Planks', recipes:['foo']
new Item modVersion:modVersion, name:'Oak Wood Planks', recipes:['foo']
names = modVersion.gatherNames()
names.oak_wood_planks.value.should.equal 'Oak Wood Planks'
names.oak_wood_planks.label.should.equal 'Oak Wood Planks (from Test 0.0)'
@@ -83,13 +83,13 @@ describe 'ModVersion', ->
describe 'hasRecipe', ->
it 'returns false for an unknown item', ->
modVersion.addItem new Item name:'Oak Wood Planks', recipes:['foo']
new Item modVersion:modVersion, name:'Oak Wood Planks', recipes:['foo']
modVersion.hasRecipe('Pineapple Upside-Down Cake').should.be.false
it 'returns false for a un-craftable item', ->
modVersion.addItem new Item name:'Wool'
new Item modVersion:modVersion, name:'Wool'
modVersion.hasRecipe('Wool').should.be.false
it 'returns true for a craftable item', ->
modVersion.addItem new Item name:'Oak Wood Planks', recipes:['foo']
new Item modVersion:modVersion, name:'Oak Wood Planks', recipes:['foo']
modVersion.hasRecipe('Oak Wood Planks').should.be.true
+2 -2
View File
@@ -15,7 +15,7 @@ modVersion = parser = null
########################################################################################################################
describe "V1", ->
describe "ModParserVersion.V1", ->
beforeEach ->
parser = new V1
@@ -69,7 +69,7 @@ describe "V1", ->
modVersion.items['wool'].isGatherable.should.be.true
it 'marks an existing item as gatherable', ->
modVersion.addItem new Item name:'Wool'
item = new Item modVersion:modVersion, name:'Wool'
modVersion.items['wool'].isGatherable.should.be.false
parser._parseRawMaterials ['Wool']
modVersion.items['wool'].isGatherable.should.be.true
+262
View File
@@ -0,0 +1,262 @@
###
Crafting Guide - mod_version_parsers/v2.test.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
V2 = require '../../src/scripts/models/mod_version_parsers/v2'
########################################################################################################################
baseText = parser = null
########################################################################################################################
describe 'ModVersionParser.V2', ->
beforeEach -> parser = new V2
describe 'Item', ->
beforeEach -> baseText = 'name:Alpha Bravo; version:1; '
it 'allows multiple recipes', ->
recipes = "item: Charlie;
recipe:; input:Alpha; pattern:... .0. ...;
recipe:; input:Bravo; pattern:... 0.0 ...;"
modVersion = parser.parse baseText + recipes
recipes = modVersion.items.charlie.recipes
recipes[0].input[0].itemSlug.should.equal 'alpha'
recipes[1].input[0].itemSlug.should.equal 'bravo'
describe 'name', ->
it 'adds the name when present', ->
modVersion = parser.parse baseText + 'item: Charlie'
modVersion.items.charlie.name.should.equal 'Charlie'
it 'requires a non-empty name', ->
func = -> parser.parse baseText + 'item: \n'
expect(func).to.throw Error, 'cannot be empty'
describe 'gatherable', ->
it 'adds "gatherable" when present', ->
modVersion = parser.parse baseText + 'item: Alpha Bravo; gatherable: yes'
modVersion.items.alpha_bravo.isGatherable.should.be.true
it 'does not allow a duplicate "gatherable" declaration', ->
func = -> parser.parse baseText + 'item: Alpha Bravo; gatherable: yes; gatherable: yes'
expect(func).to.throw Error, 'duplicate'
it 'requires "gatherable" to be "yes" or "no"', ->
func = -> parser.parse baseText + 'item: Alpha Bravo; gatherable: true'
expect(func).to.throw Error, 'gatherable must be'
it 'does not allow "gatherable" before "item"', ->
func = -> parser.parse baseText + 'gatherable: yes; item: Alpha Bravo; gatherable: yes'
expect(func).to.throw Error, '"gatherable" before "item"'
describe 'ModVersion', ->
it 'allows declarations in any order', ->
modVersion = parser.parse 'item: Alpha; version:1; name:Bravo Charlie'
modVersion.name.should.equal 'Bravo Charlie'
modVersion.version.should.equal '1'
modVersion.items.alpha.name.should.equal 'Alpha'
it 'does not allow duplicate item declarations', ->
func = -> parser.parse 'version:1; name:Alpha Bravo; item:Charlie; item:Charlie'
expect(func).to.throw Error, 'duplicate item for Charlie'
it 'allows multiple items', ->
modVersion = parser.parse 'name:Alpha; version:1; item:Bravo; item:Charlie'
_.keys(modVersion.items).sort().should.eql ['bravo', 'charlie']
describe 'name', ->
it 'adds "name" when present', ->
modVersion = parser.parse 'name:Alpha Bravo; version:1'
modVersion.name.should.equal 'Alpha Bravo'
it 'does not allow a duplicate "name" declaration', ->
func = -> parser.parse 'name:Alpha Bravo; version:1; name:Charlie'
expect(func).to.throw Error, 'duplicate declaration of "name"'
it 'requires a "name" declaration', ->
func = -> parser.parse 'version:1; item:Alpha'
expect(func).to.throw Error, 'the "name" declaration is required'
describe 'version', ->
it 'adds "version" when present', ->
modVersion = parser.parse 'name:Alpha Bravo; version:1'
modVersion.version.should.equal '1'
it 'does not allow a duplicate "version" declaration', ->
func = -> parser.parse 'name:Alpha Bravo; version:1; item:Charlie; version:2'
expect(func).to.throw Error, 'duplicate declaration of "version"'
it 'requires a "version" declaration', ->
func = -> parser.parse 'name:Alpha Bravo; item:Charlie'
expect(func).to.throw Error, 'the "version" declaration is required'
describe 'description', ->
it 'adds "description" when present', ->
modVersion = parser.parse 'name:Alpha; version:1; description:Charlie Delta'
modVersion.description.should.equal 'Charlie Delta'
it 'does not allow a duplicate "description" declaration', ->
func = -> parser.parse 'name:Alpha; version:1; description:Bravo; description:Charlie'
expect(func).to.throw Error, 'duplicate declaration of "description"'
describe 'Recipe', ->
beforeEach -> baseText = 'name:Alpha Bravo; version:1; item: Charlie; '
describe 'input', ->
it 'adds "input" when present', ->
modVersion = parser.parse baseText + 'recipe:; input:Alpha, Bravo, Charlie; pattern: ... 012 ...'
slugs = (s.itemSlug for s in modVersion.items.charlie.recipes[0].input)
slugs.should.eql ['alpha', 'bravo', 'charlie']
it 'requires an "input" declaration', ->
func = -> parser.parse baseText + 'recipe:; pattern: ... .0. ...'
expect(func).to.throw Error, 'the "input" declaration is required'
it 'does not allow a duplicate "input" declaration', ->
func = -> parser.parse baseText + 'recipe:; input:Alpha; pattern:....0....; input:Bravo'
expect(func).to.throw Error, 'duplicate declaration of "input"'
it 'does not allow "input" before "recipe"', ->
func = -> parser.parse baseText + 'input:Alpha, Bravo; recipe:; pattern:....0....'
expect(func).to.throw Error, 'cannot declare "input" before "recipe"'
it 'registers slugs for each input name', ->
modVersion = parser.parse baseText + 'recipe:; input:Delta, Echo, Foxtrot; pattern:...012...'
_.keys(modVersion.names).sort().should.eql ['charlie', 'delta', 'echo', 'foxtrot']
describe 'pattern', ->
it 'adds "pattern" when present', ->
modVersion = parser.parse baseText + 'recipe:; input:Alpha, Bravo; pattern:... .0. .1.'
modVersion.items.charlie.recipes[0].pattern.should.equal '... .0. .1.'
it 'requires a "pattern" declaration', ->
func = -> parser.parse baseText + 'recipe:; input:Alpha, Bravo'
expect(func).to.throw Error, 'the "pattern" declaration is required'
it 'does not allow a duplicate "pattern" declaration', ->
func = -> parser.parse baseText + 'recipe:; input:Alpha, Bravo; pattern:....0..1.; pattern:01.......'
expect(func).to.throw Error, 'duplicate declaration of "pattern"'
it 'requires pattern to be the right length', ->
func = -> parser.parse baseText + 'recipe:; input:Alpha; pattern:000'
expect(func).to.throw Error, 'a pattern must have'
it 'requires pattern to only use proper characters', ->
func = -> parser.parse baseText + 'recipe:; input:Alpha; pattern:abc def ghi'
expect(func).to.throw Error, 'a pattern must have'
it 'requires pattern to only refer to existing items', ->
func = -> parser.parse baseText + 'recipe:; input:Alpha; pattern:... 010 ...'
expect(func).to.throw Error, 'there is no input 1 in this recipe'
it 'requires all items to appear in the pattern', ->
func = -> parser.parse baseText + 'recipe:; input:Alpha, Bravo; pattern: 000 0.0 000'
expect(func).to.throw Error, 'Bravo is an input'
it 'computes the input stack sizes from the pattern', ->
modVersion = parser.parse baseText + 'recipe:; input:Alpha, Bravo, Charlie; pattern:111 .0. 2.2'
recipe = modVersion.items.charlie.recipes[0]
recipe.input[0].quantity.should.equal 1
recipe.input[1].quantity.should.equal 3
recipe.input[2].quantity.should.equal 2
it 'does not allow "pattern" before "recipe"', ->
func = -> parser.parse baseText + 'pattern:... .0. ...; recipe:; inputs:Alpha'
expect(func).to.throw Error, 'cannot declare "pattern" before "recipe"'
describe 'quantity', ->
beforeEach ->
baseText = 'name:Alpha Bravo; version:1; item: Charlie; recipe:; input:Alpha; pattern:...0.0...; '
it 'adds "quantity" when present', ->
modVersion = parser.parse baseText + 'quantity: 2'
modVersion.items.charlie.recipes[0].output[0].quantity.should.equal 2
it 'does not allow a duplicate "quantity" declaration', ->
func = -> parser.parse baseText + 'quantity:1; quantity:2'
expect(func).to.throw Error, 'duplicate declaration of "quantity"'
it 'requires quantity to be an integer', ->
func = -> parser.parse baseText + 'quantity:ten'
expect(func).to.throw Error, 'quantity must be an integer'
it 'assumes a quantity of 1 by default', ->
modVersion = parser.parse baseText
modVersion.items.charlie.recipes[0].output[0].quantity.should.equal 1
it 'does not allow "quantity" before recipe', ->
func = -> parser.parse 'name:Alpha; version:1; item:Bravo; quantity:12; recipe:;'
expect(func).to.throw Error, 'cannot declare "quantity" before "recipe"'
describe 'output', ->
beforeEach ->
baseText = 'name:Alpha; version:1; item:Bravo; recipe:; input:Charlie; pattern:... .0. ...; '
it 'adds a single item as the default output', ->
modVersion = parser.parse baseText
stack = modVersion.items.bravo.recipes[0].output[0]
stack.itemSlug.should.equal 'bravo'
stack.quantity.should.equal 1
it 'can add multiple extras with quantities', ->
modVersion = parser.parse baseText + 'extras:2 Delta, 4 Echo'
output = modVersion.items.bravo.recipes[0].output
output[0].itemSlug.should.equal 'bravo'
output[0].quantity.should.equal 1
output[1].itemSlug.should.equal 'delta'
output[1].quantity.should.equal 2
output[2].itemSlug.should.equal 'echo'
output[2].quantity.should.equal 4
it 'does not allow "extras" before "recipe"', ->
func = -> parser.parse 'name:Alpha; version:1; item:Bravo; extras:Charlie'
expect(func).to.throw Error, 'cannot declare "extras" before "recipe"'
it 'registers slugs for each output name', ->
modVersion = parser.parse baseText + 'extras:Delta, Echo'
_.keys(modVersion.names).sort().should.eql ['bravo', 'charlie', 'delta', 'echo']
it 'does not allow a duplicate "extras" declaration', ->
func = -> parser.parse baseText + 'extras:Echo; extras:Delta'
expect(func).to.throw Error, 'duplicate declaration of "extras"'
describe 'tools', ->
beforeEach ->
baseText = 'name:Alpha; version:1; item:Bravo; recipe:; input:Charlie; pattern:... .0. ...; '
it 'can add a single tool', ->
modVersion = parser.parse baseText + 'tools: Furnace'
modVersion.items.bravo.recipes[0].tools[0].itemSlug.should.equal 'furnace'
it 'can add multiple tools', ->
modVersion = parser.parse baseText + 'tools: Crafting Table, Furnace'
tools = modVersion.items.bravo.recipes[0].tools
tools[0].itemSlug.should.equal 'crafting_table'
tools[1].itemSlug.should.equal 'furnace'
it 'registers slugs for each tool name', ->
modVersion = parser.parse baseText + 'tools: Crafting Table, Furnace'
_.keys(modVersion.names).sort().should.eql ['bravo', 'charlie', 'crafting_table', 'furnace']
it 'does not allow a duplicate "tools" declaration', ->
func = -> parser.parse baseText + 'tools:Crafting Table; tools:Furnace'
expect(func).to.throw Error, 'duplicate declaration of "tools"'
+84
View File
@@ -0,0 +1,84 @@
###
Crafting Guide - string_builder.test.coffee
Copyright (c) 2015 by Redwood Labs
All rights reserved.
###
StringBuilder = require '../src/scripts/models/string_builder'
########################################################################################################################
builder = null
########################################################################################################################
describe 'StringBuilder', ->
beforeEach -> builder = new StringBuilder
describe 'call', ->
it 'works with no arguments', ->
builder.call (b)-> b.push 'foo'
builder.toString().should.equal 'foo'
it 'works with multiple arguments', ->
builder.call 'foo', 'bar', 'baz', (builder, a, b, c)-> builder.loop [a, b, c]
builder.toString().should.equal 'foo, bar, baz'
describe 'loop', ->
it 'can make an empty list', ->
builder.loop [], start:'[', end:']'
builder.toString().should.equal '[]'
it 'can make a list with a single element', ->
builder.loop ['foo'], start:'[', end:']'
builder.toString().should.equal '[foo]'
it 'can make a list with many elements', ->
builder.loop ['foo', 'bar', 'baz'], start:'[', end:']'
builder.toString().should.equal '[foo, bar, baz]'
it 'can make a list with a custom callback', ->
builder.loop ['foo', 'bar', 'baz'], start:'[', end:']', onEach:(b, i)-> b.push "\"#{i}\""
builder.toString().should.equal '["foo", "bar", "baz"]'
it 'can use a custom delimiter', ->
builder.loop ['foo', 'bar', 'baz'], delimiter:'|'
builder.toString().should.equal 'foo|bar|baz'
it 'can indent content', ->
builder.loop ['foo', 'bar', 'baz'], start:'[\n', end:'\n]', delimiter:',\n', indent:true
builder.toString().should.equal '[\n foo,\n bar,\n baz\n]'
describe 'onlyIf', ->
it 'calls the callback on true', ->
builder.onlyIf true, (b)-> b.push 'foo'
builder.toString().should.equal 'foo'
describe 'push', ->
it 'can build a simple string', ->
builder.push('foo').push(' bar').push(' baz')
builder.toString().should.equal 'foo bar baz'
it 'can build a multi-line string', ->
builder.push('foo').push('\nbar\n').push('baz')
builder.toString().should.equal 'foo\nbar\nbaz'
it 'can build an indented multi-line string', ->
builder
.push 'foo\n'
.indent()
.push 'bar\n'
.outdent()
.push 'baz'
builder.toString().should.equal 'foo\n bar\nbaz'
it 'can pick apart multiple newlines in a single chunk', ->
builder.indent().push('foo\nbar\nbaz').outdent().push('\nbif')
builder.toString().should.equal 'foo\n bar\n baz\nbif'
+3
View File
@@ -16,6 +16,7 @@ if typeof(global) is 'undefined'
global.assert = chai.assert
global.expect = chai.expect
global.should = chai.should()
global.util = require 'util'
Logger = require '../src/scripts/logger'
global.logger = new Logger level:Logger.TRACE
@@ -32,6 +33,8 @@ require './inventory_parser.test'
require './mod_pack.test'
require './mod_version.test'
require './mod_version_parsers/v1.test'
require './mod_version_parsers/v2.test'
require './string_builder.test'
mocha.checkLeaks()
mocha.globals ['LiveReload']