Skip to content

Commit

Permalink
+code section +bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Iuriiiii committed Oct 19, 2022
1 parent b0d1799 commit eedd55d
Show file tree
Hide file tree
Showing 16 changed files with 356 additions and 426 deletions.
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The structure pattern is similar to HTML, you will get tags, content, params and
<title>The page title</title>
</head>
<body>
<!-- This is a comment -->
<hr/>
<div class="container">
<h1>My first title</h1>
Expand All @@ -43,12 +44,13 @@ The structure pattern is similar to HTML, you will get tags, content, params and
<td>

```
[This is just a comment]
{!DOCTYPE html}
html(lang="en") {
head {
title {The page title}
}
body {
[ This is a comment ]
hr;
div(class="container") {
h1 {My first title}
Expand Down Expand Up @@ -86,7 +88,7 @@ html(lang="en") {
##### Example:

```js
import { Core } from 'tinyml-core';
import { Core } from 'tinyml-core/common';

try {
var parsed = Core.parse(`
Expand Down Expand Up @@ -157,4 +159,39 @@ This data type different to `Core.Element` element, generally the content of thi

This data type defines the content between `[` and `]` characters.

> Does not contain new methods or members by itself.
> Does not contain new methods or members by itself.
### Core.Code

This data type defines the content between unnamed `{` and `}` characters.

> Does not contain new methods or members by itself.
####

<table>
<tr>
<th>Named Block</th>
<th>Unnamed Block</th>
</tr>
<tr>
<td>

```
namedBlock {
content of named block
}
```

</td>
<td>

```
{
content of unnamed block
}
```

</td>
</tr>
</table>
98 changes: 59 additions & 39 deletions __tests__/core.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,7 @@
import { Core } from '../common';

const example = {
params: 'html(param1){}',
example1: `
This is a raw content
html{
This is another raw content
head(lang="es") {
title { Hola Mundo }
}
This is raw content again
}
Ends with raw content
`,
comment1: `
[ This is a comment {} ]
`,
code1: `
html{this is raw or should be;p{adsad} is a code}
`,
escape1: "\\{All this content should be raw\\}[qw\\d{qw}}d { \"q\"wd]\\[dark Content!!!\\]",
errors: ['{', '}', '[', ']', 'html([]){}', 'html({}){}', '"', 'html{this is raw or should be;p{adsad} is a code}}'],
short1: `
hr;
hr;head{Code}
`
};


import { Core, TokenType } from '../common';

describe('Core general tests', () => {
const tml = Core.parse(example.params);
const tml = Core.parse('html(param1){}');

test('The return type should be an Array', () => {
expect(tml).toBeInstanceOf(Array);
Expand Down Expand Up @@ -63,7 +34,17 @@ describe('Core general tests', () => {
});

describe('Intensive tests', () => {
const tml = Core.parse(example.example1);
const tml = Core.parse(`
This is a raw content
html{
This is another raw content
head(lang="es") {
title { Hola Mundo }
}
This is raw content again
}
Ends with raw content
`);
const html = tml[1] as Core.Element;
const head = html.children![1] as Core.Element;
const title = head.children![1] as Core.Element;
Expand Down Expand Up @@ -111,7 +92,9 @@ describe('Intensive tests', () => {
});

describe('Comment tests', () => {
const tml = Core.parse(example.comment1);
const tml = Core.parse(`
[ This is a comment {} ]
`);

test('2nd item should be instance of Core.Comment', () => {
expect(tml[1]).toBeInstanceOf(Core.Comment);
Expand All @@ -123,7 +106,7 @@ describe('Comment tests', () => {
});

describe('Escape tests', () => {
const tml = Core.parse(example.escape1);
const tml = Core.parse("\\{All this content should be raw\\}[qw\\d{qw}}d { \"q\"wd]\\[dark Content!!!\\]");

test('1st item should be instance of Core.Raw', () => {
expect(tml[0]).toBeInstanceOf(Core.Raw);
Expand Down Expand Up @@ -152,14 +135,19 @@ describe('Escape tests', () => {
});

describe('Errors', () => {
test('All code examples should be throw error', async () => {
for (let i = 0; i < example.errors.length; i++)
expect(() => Core.parse(example.errors[i])).toThrow(Error);
const errors = ['{', '}', '[', ']', 'html([]){}', 'html({}){}', '"', 'html{this is raw or should be;p{adsad} is a code}}'];

test('All code examples should be throw error', () => {
for (let i = 0; i < errors.length; i++)
expect(() => Core.parse(errors[i])).toThrowError();
});
});

describe('Short syntax', () => {
const tml = Core.parse(example.short1);
const tml = Core.parse(`
hr;
hr;head{Code}
`);

test('1st hr should be an element', () => {
const hr = tml[1];
Expand All @@ -180,4 +168,36 @@ describe('Short syntax', () => {
expect(head).toBeInstanceOf(Core.Element);
expect(head.get<Core.Element>().children!.length).toBeGreaterThanOrEqual(1);
});
});
});

describe('Code elements test', () => {
const tml = Core.parse('{qwijdbqwjdbqwjkdqw} html{raw content}');

test('1st element should be instance of Core.Code', () => {
expect(tml[0]).toBeInstanceOf(Core.Code);
});

test('1st element should contain "qwijdbqwjdbqwjkdqw"', () => {
expect(tml[0].toString()).toBe('qwijdbqwjdbqwjkdqw');
});

test('2nd element should be type space', () => {
expect(tml[1].tokens![0].type).toBe(TokenType.SPACE);
});

test('3rd element should be instance of Core.Element', () => {
expect(tml[2]).toBeInstanceOf(Core.Element);
});

test('3rd element tag should be "html"', () => {
expect(tml[2].get<Core.Element>().tag.text).toBe('html');
});

test('3rd element child should be instance of Core.Raw', () => {
expect(tml[2].get<Core.Element>().children![0]).toBeInstanceOf(Core.Raw);
});

test('3rd element child text should be "raw content"', () => {
expect(tml[2].get<Core.Element>().children![0].toString()).toBe('raw content');
});
});
41 changes: 33 additions & 8 deletions __tests__/tokenizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,63 @@ describe('Return Types', () => {
});
});

describe('Token Tests', () => {
const tokens = Tokenizer.tokenizate('{"hola mundo"} How are you');

test('1st token should be a separator', () => {
expect(tokens[0].type).toBe(TokenType.SEPARATOR);
});

test('2nd token should be a string', () => {
expect(tokens[1].type).toBe(TokenType.STRING);
});

test('3rd token should be a string', () => {
expect(tokens[2].type).toBe(TokenType.SEPARATOR);
});


test('4th token should be a space', () => {
expect(tokens[3].type).toBe(TokenType.SPACE);
});

test('5th token should be a identifier', () => {
expect(tokens[4].type).toBe(TokenType.IDENTIFIER);
});
});


describe('Token Types', () => {
test('Tokenizer should return just an eof token', () => {
expect(Tokenizer.tokenizate('')[0].type === TokenType.eof).toBe(true);
expect(Tokenizer.tokenizate('')[0].type === TokenType.EOF).toBe(true);
});

test('Tokenizer should return just an number token', () => {
expect(Tokenizer.tokenizate('1')[0].type === TokenType.number).toBe(true);
expect(Tokenizer.tokenizate('1')[0].type === TokenType.NUMBER).toBe(true);
});

test('Tokenizer should return just an identifier token', () => {
expect(Tokenizer.tokenizate('a')[0].type === TokenType.identifier).toBe(true);
expect(Tokenizer.tokenizate('a')[0].type === TokenType.IDENTIFIER).toBe(true);
});

test('Tokenizer should return just an eol token', () => {
expect(Tokenizer.tokenizate('\n')[0].type === TokenType.eol).toBe(true);
expect(Tokenizer.tokenizate('\n')[0].type === TokenType.EOL).toBe(true);
});

test('Tokenizer should return just an separator token', () => {
expect(Tokenizer.tokenizate('[')[0].type === TokenType.separator).toBe(true);
expect(Tokenizer.tokenizate('[')[0].type === TokenType.SEPARATOR).toBe(true);
});

test('Tokenizer should return just an operator token', () => {
expect(Tokenizer.tokenizate('+')[0].type === TokenType.operator).toBe(true);
expect(Tokenizer.tokenizate('+')[0].type === TokenType.OPERATOR).toBe(true);
});

test('Tokenizer should return just an space token', () => {
expect(Tokenizer.tokenizate(' ')[0].type === TokenType.space).toBe(true);
expect(Tokenizer.tokenizate(' ')[0].type === TokenType.SPACE).toBe(true);
});

test('Tokenizer should return just an string token', () => {
expect(Tokenizer.tokenizate('"This is a string"')[0].type === TokenType.string).toBe(true);
expect(Tokenizer.tokenizate('"This is a string"')[0].type === TokenType.STRING).toBe(true);
});
});

Expand Down
Loading

0 comments on commit eedd55d

Please sign in to comment.