import test from 'ava';

import { grammar } from '../../src/grammar';

test('should parse simple link', (t) => {
  const link = 'test.md';
  const expect = {
    link: {
      url: 'test.md',
      placeholder: 'test.md',
      index: 0,
    },
    scopeReferences: [],
    descendantReferences: [],
  };

  const output = grammar.parse(link);

  t.deepEqual(output, expect);
});

test('should parse link with fallback', (t) => {
  const link = 'animal || fox.md';
  const expect = {
    link: {
      url: 'animal',
      placeholder: 'animal',
      index: 0,
    },
    scopeReferences: [{
      url: 'fox.md',
      placeholder: 'animal',
      index: 10,
    }],
    descendantReferences: [],
  };

  const output = grammar.parse(link);

  t.deepEqual(output, expect);
});

test('should parse link with references', (t) => {
  const link = 'animal vulpis:fox.md reset: singlestring:\'A\' doublestring:"B"';
  const expect = {
    link: {
      url: 'animal',
      placeholder: 'animal',
      index: 0,
    },
    scopeReferences: [],
    descendantReferences: [
      {
        url: 'fox.md',
        placeholder: 'vulpis',
        index: 14,
      },
      {
        url: '""',
        placeholder: 'reset',
        index: 27,
      },
      {
        url: '\'A\'',
        placeholder: 'singlestring',
        index: 41,
      },
      {
        url: '"B"',
        placeholder: 'doublestring',
        index: 58,
      },
    ],
  };

  const output = grammar.parse(link);

  t.deepEqual(output, expect);
});

test('should parse link with fallback and references', (t) => {
  const link = 'animal || fox.md canine:dog.md';
  const expect = {
    link: {
      url: 'animal',
      placeholder: 'animal',
      index: 0,
    },
    scopeReferences: [{
      url: 'fox.md',
      placeholder: 'animal',
      index: 10,
    }],
    descendantReferences: [{
      url: 'dog.md',
      placeholder: 'canine',
      index: 24,
    }],
  };

  const output = grammar.parse(link);

  t.deepEqual(output, expect);
});

@@ -882,17 +882,22 @@
 tring:%5C'
-A
+foobar
 %5C' doubl
@@ -905,17 +905,25 @@
 string:%22
-B
+fizz-buzz
 %22';%0A  co
@@ -1278,9 +1278,14 @@
  '%5C'
-A
+foobar
 %5C'',
@@ -1377,9 +1377,17 @@
 : '%22
-B
+fizz-buzz
 %22',%0A
@@ -1442,10 +1442,10 @@
 ex: 
-58
+63
 ,%0A  

add explicit test for dash within string transcludes