From baf4b35fbdf0cc4fce98233f8b9a71d3b1d2852a Mon Sep 17 00:00:00 2001 From: Gianluca Guarini Date: Sun, 11 Oct 2015 23:47:37 +0200 Subject: [PATCH] added: analyzer tests --- .gitignore | 1 + lib/analyzer/index.js | 8 ++-- test/expected/cmp.js | 55 ++++++++++++++++++++++++++ test/expected/logs/folder-and-file.log | 5 +++ test/expected/logs/folder-no-file.log | 5 +++ test/runner.js | 1 + test/specs/analyzer.spec.js | 34 ++++++++++++++++ test/specs/api.spec.js | 12 +++--- test/tags/analyzer/invalid.tag | 19 +++++++++ test/tags/analyzer/tag-not-closed.tag | 2 + test/tags/analyzer/tag-unmatch.tag | 4 ++ test/tags/analyzer/valid.tag | 29 ++++++++++++++ test/tags/analyzer/without-indent.tag | 5 +++ 13 files changed, 170 insertions(+), 10 deletions(-) create mode 100644 test/specs/analyzer.spec.js create mode 100644 test/tags/analyzer/invalid.tag create mode 100644 test/tags/analyzer/tag-not-closed.tag create mode 100644 test/tags/analyzer/tag-unmatch.tag create mode 100644 test/tags/analyzer/valid.tag create mode 100644 test/tags/analyzer/without-indent.tag diff --git a/.gitignore b/.gitignore index ff231e5..2484ae8 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,7 @@ coverage # these files are generated anyway test/fixtures/* test/tags/*.js +test/tags/analyzer/*.js !.gitmodules !.travis.yml diff --git a/lib/analyzer/index.js b/lib/analyzer/index.js index 3ec0b74..8fe98c2 100644 --- a/lib/analyzer/index.js +++ b/lib/analyzer/index.js @@ -58,16 +58,16 @@ function analyze(source) { type = mode } else if (m = row.match(STYLE_START)) { // Style starting - type = 'style_start'; mode = 'style'; block = '' + type = 'style_start'; mode = 'style' } else if (m = row.match(STYLE_END)) { // Style ending - type = 'style_end'; mode = 'tag'; block = '' + type = 'style_end'; mode = 'tag' } else if (m = row.match(SCRIPT_START)) { // Script starting - type = 'script_start'; mode = 'script'; block = '' + type = 'script_start'; mode = 'script' } else if (m = row.match(SCRIPT_END)) { // Script ending - type = 'script_end'; mode = 'tag'; block = '' + type = 'script_end'; mode = 'tag' } else { if (m = row.match(HTML_END_DETECTOR)) type = 'template' else type = mode diff --git a/test/expected/cmp.js b/test/expected/cmp.js index bd07ee2..59950b8 100644 --- a/test/expected/cmp.js +++ b/test/expected/cmp.js @@ -1,3 +1,58 @@ +var riot = require('riot') + +riot.tag('valid-tag', '

{ title }

{ message }

', function(opts) { +{ title } +

{ message }

+ + this.title = 'Hello world!' + this.message = 'I am hungry...' + +

Hello!

+ + +

Hello!

+
+ + +var riot = require('riot') + +riot.tag('valid-tag', '

{ title }

{ message }

', function(opts) { + + this.title = 'Hello world!' + this.message = 'I am hungry...' + +}); + +riot.tag('line-tag', 'Hello { opts.message }!', function(opts) { +}); + +riot.tag('tag-with-style', '

Hi!

', 'tag-with-style p, [riot-tag="tag-with-style"] p{ color: red }', function(opts) { + +}); + +riot.tag('tag-with-script', '

{ title }

{ message }

', function(opts) { + this.title = 'Hello world!' + this.message = 'I am hungry...' + +}); + +console.log('end of file') + +riot.tag('without-indent', 'riot.tag(\'p\', \'Without indent\', function(opts) { });
Without indent
', function(opts) { + +}); + riot.tag('component-2', '
{ item }
', function(opts) { this.items = ['bla', 'bla', 'bla'] diff --git a/test/expected/logs/folder-and-file.log b/test/expected/logs/folder-and-file.log index 2e6808c..d6d83c3 100644 --- a/test/expected/logs/folder-and-file.log +++ b/test/expected/logs/folder-and-file.log @@ -1,3 +1,8 @@ +test/tags/analyzer/invalid.tag -> test/expected/cmp.js +test/tags/analyzer/tag-not-closed.tag -> test/expected/cmp.js +test/tags/analyzer/tag-unmatch.tag -> test/expected/cmp.js +test/tags/analyzer/valid.tag -> test/expected/cmp.js +test/tags/analyzer/without-indent.tag -> test/expected/cmp.js test/tags/component-2.tag -> test/expected/cmp.js test/tags/component.tag -> test/expected/cmp.js test/tags/wrong-component.tag -> test/expected/cmp.js diff --git a/test/expected/logs/folder-no-file.log b/test/expected/logs/folder-no-file.log index 932fac1..90fb2ad 100644 --- a/test/expected/logs/folder-no-file.log +++ b/test/expected/logs/folder-no-file.log @@ -1,3 +1,8 @@ +test/tags/analyzer/invalid.tag -> test/tags/analyzer/invalid.js +test/tags/analyzer/tag-not-closed.tag -> test/tags/analyzer/tag-not-closed.js +test/tags/analyzer/tag-unmatch.tag -> test/tags/analyzer/tag-unmatch.js +test/tags/analyzer/valid.tag -> test/tags/analyzer/valid.js +test/tags/analyzer/without-indent.tag -> test/tags/analyzer/without-indent.js test/tags/component-2.tag -> test/tags/component-2.js test/tags/component.tag -> test/tags/component.js test/tags/wrong-component.tag -> test/tags/wrong-component.js diff --git a/test/runner.js b/test/runner.js index d5507f3..d5b01b5 100644 --- a/test/runner.js +++ b/test/runner.js @@ -1,5 +1,6 @@ describe('Cli Tests', function() { global.expect = require('expect.js') require('./specs/output.spec') + require('./specs/analyzer.spec') require('./specs/api.spec') }) diff --git a/test/specs/analyzer.spec.js b/test/specs/analyzer.spec.js new file mode 100644 index 0000000..fca449c --- /dev/null +++ b/test/specs/analyzer.spec.js @@ -0,0 +1,34 @@ +require('shelljs/global') + +const ANALYZER_TAGS_FOLDER = 'test/tags/analyzer/', + analyzer = require('../../lib/analyzer') + +describe('Analyzer', () => { + + it('returns no error if the tag is valid', () => { + var errors = analyzer(cat(`${ANALYZER_TAGS_FOLDER}valid.tag`)).filter((r) => r.error ) + expect(errors.length).to.equal(0) + }) + it('returns an error if the tag is not closed', () => { + var results = analyzer(cat(`${ANALYZER_TAGS_FOLDER}tag-not-closed.tag`)) + expect(results[3].error).to.equal('Last tag definition is not closed') + }) + it('returns an error if there are unmatched closing tags', () => { + var results = analyzer(cat(`${ANALYZER_TAGS_FOLDER}tag-unmatch.tag`)) + expect(results[3].error).to.equal('Closing tag unmatch') + expect(results[5].error).to.equal('Last tag definition is not closed') + }) + it('returns an error if there are no indentations within tag', () => { + var results = analyzer(cat(`${ANALYZER_TAGS_FOLDER}without-indent.tag`)) + expect(results[1].error).to.equal('Indentation needed within tag definition') + expect(results[2].error).to.equal('Indentation needed within tag definition') + }) + it('returns an error if there are invalid tag flagments', () => { + var results = analyzer(cat(`${ANALYZER_TAGS_FOLDER}invalid.tag`)) + expect(results[5].error).to.equal('Indentation needed within tag definition') + expect(results[10].error).to.equal('Invalid tag flagment') + expect(results[16].error).to.equal('Invalid tag flagment') + }) + + +}) \ No newline at end of file diff --git a/test/specs/api.spec.js b/test/specs/api.spec.js index a6277be..8085558 100644 --- a/test/specs/api.spec.js +++ b/test/specs/api.spec.js @@ -1,6 +1,6 @@ require('shelljs/global') -const EXPECTED_LOGS_DIR = 'test/expected/logs', +const TAGS_FOLDER = 'test/tags/', cli = require('../../lib') describe('API methods', () => { @@ -14,15 +14,15 @@ describe('API methods', () => { }) it('check', () => { - expect(cli.check({from: 'test/tags/wrong-component.tag'})).to.be.an('array') - expect(cli.check({from: 'test/tags/wrong-component.tag'})).to.have.length(2) - expect(cli.check({from: 'test/tags/component.tag'})).to.have.length(0) + expect(cli.check({from: `${TAGS_FOLDER}wrong-component.tag`})).to.be.an('array') + expect(cli.check({from: `${TAGS_FOLDER}wrong-component.tag`})).to.have.length(2) + expect(cli.check({from: `${TAGS_FOLDER}component.tag`})).to.have.length(0) }) it('make', () => { expect(cli.make({from: 'some/random/path.tag'}).error).to.be.a('string') - expect(cli.make({from: 'test/tags/component.tag'}).error).to.be(false) - expect(cli.make({from: 'test/tags/component.tag', to: 'test/expected/make-component.js'}).error).to.be(false) + expect(cli.make({from: `${TAGS_FOLDER}component.tag`}).error).to.be(false) + expect(cli.make({from: `${TAGS_FOLDER}component.tag`, to: 'test/expected/make-component.js'}).error).to.be(false) // check if the file exists expect(test('-e', 'test/expected/make-component.js')).to.be(true) expect(cli.make({from: 'test/tags', to: 'test/expected/make.js'}).error).to.be(false) diff --git a/test/tags/analyzer/invalid.tag b/test/tags/analyzer/invalid.tag new file mode 100644 index 0000000..520135c --- /dev/null +++ b/test/tags/analyzer/invalid.tag @@ -0,0 +1,19 @@ +var riot = require('riot') + + +

{ title }

+

{ message }

+ + +{ title } +

{ message }

+ + this.title = 'Hello world!' + this.message = 'I am hungry...' +
+

Hello!

diff --git a/test/tags/analyzer/tag-unmatch.tag b/test/tags/analyzer/tag-unmatch.tag new file mode 100644 index 0000000..8cf825b --- /dev/null +++ b/test/tags/analyzer/tag-unmatch.tag @@ -0,0 +1,4 @@ + +

Hello!

+
+ diff --git a/test/tags/analyzer/valid.tag b/test/tags/analyzer/valid.tag new file mode 100644 index 0000000..502991e --- /dev/null +++ b/test/tags/analyzer/valid.tag @@ -0,0 +1,29 @@ +var riot = require('riot') + + +

{ title }

+

{ message }

+ + this.title = 'Hello world!' + this.message = 'I am hungry...' +
+ +Hello { opts.message }! + + +

Hi!

+ +
+ + +

{ title }

+

{ message }

+ +
+ +console.log('end of file') diff --git a/test/tags/analyzer/without-indent.tag b/test/tags/analyzer/without-indent.tag new file mode 100644 index 0000000..bbda7f6 --- /dev/null +++ b/test/tags/analyzer/without-indent.tag @@ -0,0 +1,5 @@ + +

Without indent

+
+ Without indent
+