Skip to content

Commit

Permalink
Add tests for combined keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
frenic committed Apr 13, 2018
1 parent e1af19f commit 8c7d16e
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 61 deletions.
164 changes: 147 additions & 17 deletions __tests__/parser.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,167 @@
import parse, { Combinator, Component } from '../src/parser';

describe('parsing of CSS syntax', () => {
it('combinators', () => {
expect(parse('something another-thing [ ] [ ] && | ||')).toMatchObject([
describe('parsing', () => {
it('parses combinators', () => {
expect(parse('something | another-thing')).toMatchObject([{}, { combinator: Combinator.SingleBar }, {}]);
expect(parse('something || another-thing')).toMatchObject([{}, { combinator: Combinator.DoubleBar }, {}]);
expect(parse('something && another-thing')).toMatchObject([{}, { combinator: Combinator.DoubleAmpersand }, {}]);
expect(parse('something another-thing')).toMatchObject([{}, { combinator: Combinator.Juxtaposition }, {}]);
});

it('parses components', () => {
expect(parse('something <something> [ something <something> ]')).toMatchObject([
{ component: Component.Keyword },
{},
{ combinator: Combinator.Juxtaposition },
{ component: Component.DataType },
{},
{ combinator: Combinator.Juxtaposition },
{
component: Component.Group,
entities: [{ component: Component.Keyword }, {}, { component: Component.DataType }],
},
]);
});

it('groups by combinator precedence', () => {
expect(parse('something | something || something && something something')).toMatchObject([
{},
{ combinator: Combinator.SingleBar },
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.DoubleBar },
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.DoubleAmpersand },
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}],
},
],
},
],
},
]);

expect(parse('something something && something || something | something')).toMatchObject([
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}],
},
{ combinator: Combinator.DoubleAmpersand },
{},
],
},
{ combinator: Combinator.DoubleBar },
{},
],
},
{ combinator: Combinator.SingleBar },
{},
]);

expect(parse('[ something | something || something && something ] something')).toMatchObject([
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.SingleBar },
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.DoubleBar },
{
component: Component.Group,
entities: [{}, { combinator: Combinator.DoubleAmpersand }, {}],
},
],
},
],
},
{ combinator: Combinator.Juxtaposition },
{},
{ combinator: Combinator.DoubleAmpersand },
{ combinator: Combinator.SingleBar },
]);

expect(parse('something [ something && something || something | something ]')).toMatchObject([
{},
{ combinator: Combinator.Juxtaposition },
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [{}, { combinator: Combinator.DoubleAmpersand }, {}],
},
{ combinator: Combinator.DoubleBar },
{},
],
},
{ combinator: Combinator.SingleBar },
{},
],
},
]);

expect(parse('something && something something || something')).toMatchObject([
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.DoubleAmpersand },
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}],
},
],
},
{ combinator: Combinator.DoubleBar },
{},
]);
});

it('components', () => {
expect(parse('something <color> [ ]')).toMatchObject([
{ component: Component.Keyword },
expect(parse('something || something something && something')).toMatchObject([
{},
{ component: Component.DataType },
{ combinator: Combinator.DoubleBar },
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}],
},
{ combinator: Combinator.DoubleAmpersand },
{},
],
},
]);

expect(parse('something | something something something')).toMatchObject([
{},
{ component: Component.Group },
{ combinator: Combinator.SingleBar },
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}, { combinator: Combinator.Juxtaposition }, {}],
},
]);
});

it('group components', () => {
expect(parse('[ something | <color> ]')).toMatchObject([
expect(parse('something something something | something')).toMatchObject([
{
component: Component.Group,
entities: [{ component: Component.Keyword }, {}, { component: Component.DataType }],
entities: [{}, { combinator: Combinator.Juxtaposition }, {}, { combinator: Combinator.Juxtaposition }, {}],
},
{ combinator: Combinator.SingleBar },
{},
]);
});
});
89 changes: 45 additions & 44 deletions __tests__/typer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import parse from '../src/parser';
import typing, { Type } from '../src/typer';

describe('typings of CSS syntax', () => {
it('types combinators', () => {
expect(typing(parse('something another-thing'))).toHaveLength(1);
expect(typing(parse('something && another-thing'))).toHaveLength(1);
expect(typing(parse('something | another-thing'))).toHaveLength(2);
expect(typing(parse('something || another-thing'))).toHaveLength(3);
});

describe('typing', () => {
it('types components', () => {
expect(typing(parse('something | 100 | <color>'))).toMatchObject([
{ type: Type.StringLiteral },
Expand All @@ -25,53 +18,61 @@ describe('typings of CSS syntax', () => {
]);
});

it('types combinators', () => {
expect(typing(parse('something | another-thing'))).toHaveLength(2);
expect(typing(parse('something || another-thing'))).toHaveLength(4);
expect(typing(parse('something && another-thing'))).toHaveLength(2);
expect(typing(parse('something another-thing'))).toHaveLength(1);
});

it('types optional components', () => {
expect(typing(parse('something another-thing? | 100'))).toMatchObject([
{ type: Type.StringLiteral },
{ type: Type.String },
{ type: Type.NumericLiteral },
]);
expect(typing(parse('something another-thing? yet-another-thing? | 100'))).toMatchObject([
{ type: Type.StringLiteral },
{ type: Type.String },
{ type: Type.NumericLiteral },
expect(typing(parse('something another-thing?'))).toMatchObject([
{ type: Type.StringLiteral, literal: 'something' },
{ type: Type.StringLiteral, literal: 'something another-thing' },
]);
expect(typing(parse('something? another-thing yet-another-thing? | 100'))).toMatchObject([
{ type: Type.String },
{ type: Type.StringLiteral },
{ type: Type.NumericLiteral },
expect(typing(parse('something another-thing? yet-another-thing?'))).toMatchObject([
{ type: Type.StringLiteral, literal: 'something another-thing yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something another-thing' },
{ type: Type.StringLiteral, literal: 'something yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something' },
]);
expect(typing(parse('something? another-thing? yet-another-thing | 100'))).toMatchObject([
{ type: Type.String },
{ type: Type.StringLiteral },
{ type: Type.NumericLiteral },
expect(typing(parse('something? another-thing yet-another-thing?'))).toMatchObject([
{ type: Type.StringLiteral, literal: 'something another-thing yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something another-thing' },
{ type: Type.StringLiteral, literal: 'another-thing yet-another-thing' },
{ type: Type.StringLiteral, literal: 'another-thing' },
]);
expect(typing(parse('something? another-thing? yet-another-thing? | 100'))).toMatchObject([
{ type: Type.StringLiteral },
{ type: Type.String },
{ type: Type.StringLiteral },
{ type: Type.StringLiteral },
{ type: Type.NumericLiteral },
expect(typing(parse('something? another-thing? yet-another-thing'))).toMatchObject([
{ type: Type.StringLiteral, literal: 'something another-thing yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something yet-another-thing' },
{ type: Type.StringLiteral, literal: 'another-thing yet-another-thing' },
{ type: Type.StringLiteral, literal: 'yet-another-thing' },
]);
expect(typing(parse('something another-thing yet-another-thing? | 100'))).toMatchObject([
{ type: Type.String },
{ type: Type.NumericLiteral },
expect(typing(parse('something? another-thing? yet-another-thing?'))).toMatchObject([
{ type: Type.StringLiteral, literal: 'something another-thing yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something another-thing' },
{ type: Type.StringLiteral, literal: 'something yet-another-thing' },
{ type: Type.StringLiteral, literal: 'another-thing yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something' },
{ type: Type.StringLiteral, literal: 'another-thing' },
{ type: Type.StringLiteral, literal: 'yet-another-thing' },
]);
expect(typing(parse('something another-thing? yet-another-thing | 100'))).toMatchObject([
{ type: Type.String },
{ type: Type.NumericLiteral },
expect(typing(parse('something another-thing yet-another-thing?'))).toMatchObject([
{ type: Type.StringLiteral, literal: 'something another-thing yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something another-thing' },
]);
expect(typing(parse('something? another-thing yet-another-thing | 100'))).toMatchObject([
{ type: Type.String },
{ type: Type.NumericLiteral },
expect(typing(parse('something another-thing? yet-another-thing'))).toMatchObject([
{ type: Type.StringLiteral, literal: 'something another-thing yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something yet-another-thing' },
]);
});

it('types optional group components', () => {
expect(typing(parse('[ something ] [ another-thing ]? | 100'))).toMatchObject([
{ type: Type.StringLiteral },
{ type: Type.String },
{ type: Type.NumericLiteral },
expect(typing(parse('something [ another-thing | yet-another-thing ]?'))).toMatchObject([
{ type: Type.StringLiteral, literal: 'something another-thing' },
{ type: Type.StringLiteral, literal: 'something yet-another-thing' },
{ type: Type.StringLiteral, literal: 'something' },
]);
});
});

0 comments on commit 8c7d16e

Please sign in to comment.