Created
August 12, 2019 03:27
-
-
Save brandenbyers/816fc822d4cda5ff9beeb757167db8ce to your computer and use it in GitHub Desktop.
Testing failures and expectations for recipe parsing test lesson
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const expect = require('chai').expect | |
| let Ing = { | |
| raw: '', | |
| prefix: '', | |
| quantity: 0, | |
| quantityRangeEnd: 0, | |
| unit: '', | |
| name: '', | |
| comment: '', | |
| alternative: false, | |
| additional: false, | |
| } | |
| describe('Ingredient Parsing Tests', function(){ | |
| // The input will be an ingredient string; one line from a recipe's ingredient list. | |
| it('should parse basic ingredients', function(){ | |
| function parse(input) { | |
| return [{ | |
| raw: '1 cup flour', | |
| quantity: 1, | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| } | |
| let input = '1 cup flour' | |
| let expected = [{ | |
| raw: '1 cup flour', | |
| quantity: 1, | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| expect(parse(input)).to.deep.equal(expected); // Learn about deep equality: https://github.com/chaijs/deep-eql | |
| expect(parse(input)).to.be.an('array') | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(1); | |
| expect(parse(input)[0]).to.have.all.keys('raw', 'quantity', 'unit', 'name') | |
| expect(parse(input)[0]).to.not.have.keys('comment') | |
| expect(parse(input)[0].quantity).to.be.a('number') | |
| expect(parse(input)[0].unit).to.be.a('string') | |
| expect(parse(input)[0].name).to.be.a('string') | |
| expect(parse(input)).to.deep.include(expected[0]); | |
| expect(parse(input)[0]).to.deep.include({unit: 'cup'}); | |
| expect(parse(input)[0].quantity).to.equal(1); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should handle empty input', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '', | |
| }] | |
| } | |
| return undefined | |
| } | |
| let input = '' | |
| let expected = undefined | |
| //expect(parse(input, false)).to.be.undefined | |
| expect(parse(input)).to.be.undefined | |
| }); | |
| it('should return singular units', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '20 cups flour', | |
| quantity: 20, | |
| unit: "cups", | |
| name: "sugar", | |
| }] | |
| } | |
| return [{ | |
| raw: '20 cups flour', | |
| quantity: 20, | |
| unit: "cup", | |
| name: "sugar", | |
| }] | |
| } | |
| let input = '20 cups sugar' | |
| let expected = [{ | |
| raw: '20 cups flour', | |
| quantity: 20, | |
| unit: "cup", | |
| name: "sugar", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(1); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should parse comments', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '5 cups flour, sifted', | |
| quantity: 5, | |
| unit: "cup", | |
| name: "flour, sifted", | |
| }] | |
| } | |
| return [{ | |
| raw: '5 cups flour, sifted', | |
| quantity: 5, | |
| unit: "cup", | |
| name: "flour", | |
| comment: "sifted" | |
| }] | |
| } | |
| let input = '5 cups flour, sifted' | |
| let expected = [{ | |
| raw: '5 cups flour, sifted', | |
| quantity: 5, | |
| unit: "cup", | |
| name: "flour", | |
| comment: "sifted" | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(1); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should parse ingredients with fractions', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '1/2 cup flour', | |
| quantity: '1/2', | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| } | |
| return [{ | |
| raw: '1/2 cup flour', | |
| quantity: 0.5, | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| } | |
| let input = '1/2 cup flour' | |
| let expected = [{ | |
| raw: '1/2 cup flour', | |
| quantity: 0.5, | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(1); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should parse ingredients with unicode vulgar fractions', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '½ cup flour', | |
| quantity: '½', | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| } | |
| return [{ | |
| raw: '½ cup flour', | |
| quantity: 0.5, | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| } | |
| let input = '½ cup flour' | |
| let expected = [{ | |
| raw: '½ cup flour', | |
| quantity: 0.5, | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(1); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should parse ingredients with unicode fractions', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '₇⁄₈ cup flour', | |
| quantity: '₇⁄₈', | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| } | |
| return [{ | |
| raw: '₇⁄₈ cup flour', | |
| quantity: 0.875, | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| } | |
| let input = '₇⁄₈ cup flour' | |
| let expected = [{ | |
| raw: '₇⁄₈ cup flour', | |
| quantity: 0.875, | |
| unit: "cup", | |
| name: "flour", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(1); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should parse canned ingredients', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '2 15-ounce cans black beans', | |
| quantity: '2 15', | |
| unit: "ounce", | |
| name: "can", | |
| comment: "black beans", | |
| }] | |
| } | |
| return [{ | |
| raw: '2 15-ounce cans black beans', | |
| quantity: 2, | |
| unit: "15-ounce can", | |
| name: "black beans", | |
| }] | |
| } | |
| let input = '2 15-ounce cans black beans' | |
| let expected = [{ | |
| raw: '2 15-ounce cans black beans', | |
| quantity: 2, | |
| unit: "15-ounce can", | |
| name: "black beans", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(1); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should parse can size range', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '1 15- to 16-ounce can black beans', | |
| quantity: '2 15-', | |
| unit: "16-ounce", | |
| name: "can", | |
| comment: "black beans", | |
| }] | |
| } | |
| return [{ | |
| raw: '1 15- to 16-ounce can black beans', | |
| quantity: 2, | |
| unit: "15-ounce can", // opinionated: range not needed | |
| name: "black beans", | |
| }] | |
| } | |
| let input = '1 15- to 16-ounce can black beans' | |
| let expected = [{ | |
| raw: '1 15- to 16-ounce can black beans', | |
| quantity: 2, | |
| unit: "15-ounce can", | |
| name: "black beans", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(1); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should parse word numbers', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: 'two 15-ounce cans black beans', | |
| quantity: 'two', | |
| unit: "15-ounce can", | |
| name: "black beans", | |
| }] | |
| } | |
| return [{ | |
| raw: 'two 15-ounce cans black beans', | |
| quantity: 2, | |
| unit: "15-ounce can", | |
| name: "black beans", | |
| }] | |
| } | |
| let input = 'two 15-ounce cans black beans' | |
| let expected = [{ | |
| raw: 'two 15-ounce cans black beans', | |
| quantity: 2, | |
| unit: "15-ounce can", | |
| name: "black beans", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(1); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should parse \"dissolved in\"', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '2-3 tablespoons unflavored gelatin, dissolved in 1/2 cup water', | |
| quantity: 2, | |
| unit: "tablespoon", | |
| name: "unflavored gelatin", | |
| comment: "dissolved in 1/2 cup water", | |
| }] | |
| } | |
| return [{ | |
| raw: '2-3 tablespoons unflavored gelatin, dissolved in 1/2 cup water', | |
| quantity: 2, | |
| quantityEndRange: 3, | |
| unit: "tablespoon", | |
| name: "unflavored gelatin", | |
| }, { | |
| raw: 'dissolved in 1/2 cup water', | |
| prefix: "dissolved in", | |
| quantity: 0.5, | |
| unit: "cup", | |
| name: "water", | |
| }] | |
| } | |
| let input = '2-3 tablespoons unflavored gelatin, dissolved in 1/2 cup water' | |
| let expected = [{ | |
| raw: '2-3 tablespoons unflavored gelatin, dissolved in 1/2 cup water', | |
| quantity: 2, | |
| quantityEndRange: 3, | |
| unit: "tablespoon", | |
| name: "unflavored gelatin", | |
| }, { | |
| raw: 'dissolved in 1/2 cup water', | |
| prefix: "dissolved in", | |
| quantity: 0.5, | |
| unit: "cup", | |
| name: "water", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(2); | |
| expect(parse(input)).to.deep.equal(expected); | |
| }); | |
| it('should parse alternative measurements in parenthesis', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '250 grams (1 cup) cassava flour', | |
| quantity: 250, | |
| unit: "15-ounce can", | |
| name: "cassava flour", | |
| comment: "(1 cup)" | |
| }] | |
| } | |
| return [{ | |
| raw: '250 grams (1 cup) cassava flour', | |
| quantity: 250, | |
| unit: "gram", | |
| name: "cassava flour", | |
| }, { | |
| raw: '1 cup cassava flour', | |
| prefix: "or", | |
| quantity: 1, | |
| unit: "cup", | |
| name: "cassava flour", | |
| }] | |
| } | |
| let input = '250 grams (1 cup) cassava flour' | |
| let expected = [{ | |
| raw: '250 grams (1 cup) cassava flour', | |
| quantity: 250, | |
| unit: "gram", | |
| name: "cassava flour", | |
| }, { | |
| raw: '1 cup cassava flour', | |
| prefix: "or", | |
| quantity: 1, | |
| unit: "cup", | |
| name: "cassava flour", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(2); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should parse range in parenthesis', function(){ | |
| function parse(input, expected = true) { | |
| if (!expected) { | |
| return [{ | |
| raw: '2 to 3 tablespoons (20 to 30 grams) cinnamon', | |
| quantity: 2, | |
| quantityRnageEnd: 3, | |
| unit: "tablespoon", | |
| name: "cassava flour", | |
| comment: "(1 cup)" | |
| }] | |
| } | |
| return [{ | |
| raw: '2 to 3 tablespoons (20 to 30 grams) cinnamon', | |
| quantity: 2, | |
| quantityRangeEnd: 3, | |
| unit: "tablespoon", | |
| name: "cassava flour", | |
| }, { | |
| raw: '20 to 30 grams cinnamon', | |
| prefix: "or", | |
| quantity: 20, | |
| quantityRangeEnd: 30, | |
| unit: "gram", | |
| name: "cassava flour", | |
| }] | |
| } | |
| let input = '2 to 3 tablespoons (20 to 30 grams) cinnamon' | |
| let expected = [{ | |
| raw: '2 to 3 tablespoons (20 to 30 grams) cinnamon', | |
| quantity: 2, | |
| quantityRangeEnd: 3, | |
| unit: "tablespoon", | |
| name: "cassava flour", | |
| }, { | |
| raw: '20 to 30 grams cinnamon', | |
| prefix: "or", | |
| quantity: 20, | |
| quantityRangeEnd: 30, | |
| unit: "gram", | |
| name: "cassava flour", | |
| }] | |
| //expect(parse(input, false)).to.deep.equal(expected); | |
| expect(parse(input)).to.deep.equal(expected); | |
| expect(parse(input)).to.be.an('array').and.to.have.lengthOf(2); | |
| expect(parse(input)).to.have.deep.ordered.members(expected) | |
| }); | |
| it('should have correct array count', function(){ | |
| function parse(input) { | |
| // TODO: needs real function to process array of strings | |
| // All should pass | |
| // Bonus points for student if they can break the results | |
| return [] | |
| } | |
| let ingredients = [ | |
| ['2-3 tablespoons unflavored gelatin, dissolved in 1/2 cup water', 2], | |
| ['1 teaspoon salt', 1], | |
| ] | |
| for (ingredient of ingredients) { | |
| expect(parse(ingredient[0])).to.be.an('array', `Failed to parse \"${ingredient[0]}\"`).and.to.have.lengthOf(ingredient[1]); | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment