/**
 * @license
 * Copyright (c) 2017 Google Inc. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * Code distributed by Google as part of this project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */

import {parser} from '../build/manifest-parser.js';
const parse = parser.parse;
import {assert} from './chai-web.js';

describe('manifest parser', function() {
  it('parses an empy manifest', () => {
    parse('');
  });
  it('parses a trivial recipe', () => {
    parse(`recipe Recipe &tag1 &tag2`);
  });
  it('parses with indentation', () => {
    parse(`
      recipe Recipe`);
  });
  it('parses recipes that map views', () => {
    parse(`
      recipe Thing
        map #someTag
        map 'some-id' #someTag`);
  });
  it('parses recipes with particles', () => {
    parse(`
      recipe Recipe
        SomeParticle`);
  });
  it('parses recipes that connect particles to views', () => {
    parse(`
      recipe Recipe
        SomeParticle
          a -> #something
          b <- #somethingElse
          * = #someOtherParticle`);
  });
  it('parses trivial particles', () => {
    parse(`
      particle SomeParticle`);
  });
  it('parses recipes that name views and particles', () => {
    parse(`
      recipe Recipe
        SomeParticle as thing
        map #thing as anotherThing`);
  });
  it('parses manifests with comments', () => {
    parse(`
    // comment
      recipe // comment
        // comment
           // comment
        A//comment
   // comment
        // comment
        B    //comment
      `);
  });
  it('parses recipes with recipe level connections', () => {
    parse(`
      recipe
        X -> Y
        X.a -> Y.a
        a = b
        a.a = b.b
        X.a #tag <- a.y`);
  });
  it('parses manifests with views', () => {
    parse(`
      schema Person
        Text lastName
        Text firstName
        description \`person\`
          plural \`people\`
          value \`\${firstName} \${lastName}\`
      store Store0 of [Person] in 'person.json'
        description \`my view\`
      store Store1 of Person 'some-id' @7 in 'people.json'`);
  });
  it('fails to parse a nonsense argument list', () => {
    try {
      parse(`
        particle AParticle
          Nonsense()`);
      assert.fail('this parse should have failed, no nonsense!');
    } catch (e) {
      assert(e.message.includes('Nonsense'),
          'bad error: '+e);
    }
  });
  it('parses particles with optional handles', () => {
    parse(`
      particle MyParticle
        MyParticle(in MyThing mandatory, in MyThing? optional1, out [MyThing]? optional2)`);
  });
  it('parses manifests with search', () => {
    parse(`
      recipe
        search \`hello World!\`
      `);
  });
  it('parses manifests with search and tokens', () => {
    parse(`
      recipe
        search \`Hello dear world\`
          tokens \`hello\` \`World\` // \`dear\`
      `);
  });
  it('parses manifests particle verbs', () => {
    parse(`
      particle SomeParticle
        jump(in Energy energy, out Height height)
        affordance dom`);
  });
  it('parses recipe with particle verbs', () => {
    parse(`
      recipe
        particle can jump
          * <- energy
          * -> height`);
  });
  it('parses recipe with particle verb shorthand', () => {
    parse(`
      recipe
        &jump
          * <- energy
          * <- height`);
  });
  it('parses inline schemas', () => {
    parse(`
      particle Foo
        Foo(in MySchema {Text value} mySchema)
    `);
    parse(`
      particle Foo
        Foo(in [MySchema {Text value}] mySchema)
    `);
    parse(`
      particle Foo
        Foo(in [* {Text value, Number num}] anonSchema)
    `);
    parse(`
      particle Foo
        Foo(in * {(Text or Number) value} union)
    `);
    parse(`
      particle Foo
        Foo(in * {value} optionalType)
    `);
  });
  it('parses a schema with a bytes field', () => {
    parse(`
      schema Avatar
        Text name
        Bytes profileImage
      `);
  });
});

@@ -2631,19 +2631,8 @@
     
-MyParticle(
 in M
@@ -2647,17 +2647,24 @@
 andatory
-,
+%0A       
  in MyTh
@@ -2677,17 +2677,24 @@
 ptional1
-,
+%0A       
  out %5BMy
@@ -2710,17 +2710,16 @@
 ptional2
-)
 %60);%0A  %7D)
@@ -3120,13 +3120,8 @@
     
-jump(
 in E
@@ -3132,17 +3132,24 @@
 y energy
-,
+%0A       
  out Hei
@@ -3158,17 +3158,16 @@
 t height
-)
 %0A       
@@ -3573,20 +3573,16 @@
         
-Foo(
 in MySch
@@ -3598,33 +3598,32 @@
  value%7D mySchema
-)
 %0A    %60);%0A    par
@@ -3650,28 +3650,24 @@
 Foo%0A        
-Foo(
 in %5BMySchema
@@ -3685,25 +3685,24 @@
 e%7D%5D mySchema
-)
 %0A    %60);%0A   
@@ -3737,20 +3737,16 @@
         
-Foo(
 in %5B* %7BT
@@ -3779,17 +3779,16 @@
 onSchema
-)
 %0A    %60);
@@ -3823,28 +3823,24 @@
 Foo%0A        
-Foo(
 in * %7B(Text 
@@ -3862,17 +3862,16 @@
 e%7D union
-)
 %0A    %60);
@@ -3914,12 +3914,8 @@
     
-Foo(
 in *
@@ -3935,17 +3935,16 @@
 onalType
-)
 %0A    %60);

Update deprecated particle syntax in manifest-parser-test. (#1380)