Skip to content

Commit

Permalink
added: analyzer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GianlucaGuarini committed Oct 11, 2015
1 parent 44d57df commit baf4b35
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ coverage
# these files are generated anyway
test/fixtures/*
test/tags/*.js
test/tags/analyzer/*.js

!.gitmodules
!.travis.yml
8 changes: 4 additions & 4 deletions lib/analyzer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions test/expected/cmp.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
var riot = require('riot')

riot.tag('valid-tag', '<h1>{ title }</h1> <p>{ message }</p>', function(opts) {
<invalid-flagment
this.title = 'Hello world!'
this.message = 'I am hungry...'

});

<invalid-t
<h1>{ title }</h1>
<p>{ message }</p>

this.title = 'Hello world!'
this.message = 'I am hungry...'
</invalid-t

console.log('end of file')

<tag-not-closed>
<p>Hello!</p>

<tag-unmatch>
<p>Hello!</p>
</tag-unmatch>
</tag-unmatched>

var riot = require('riot')

riot.tag('valid-tag', '<h1>{ title }</h1> <p>{ message }</p>', 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', '<p>Hi!</p>', 'tag-with-style p, [riot-tag="tag-with-style"] p{ color: red }', function(opts) {

});

riot.tag('tag-with-script', '<h1>{ title }</h1> <p>{ message }</p>', 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) { }); <div> Without indent</div>', function(opts) {

});

riot.tag('component-2', '<div each="{ items }">{ item }</div>', function(opts) {

this.items = ['bla', 'bla', 'bla']
Expand Down
5 changes: 5 additions & 0 deletions test/expected/logs/folder-and-file.log
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions test/expected/logs/folder-no-file.log
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
@@ -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')
})
34 changes: 34 additions & 0 deletions test/specs/analyzer.spec.js
Original file line number Diff line number Diff line change
@@ -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')
})


})
12 changes: 6 additions & 6 deletions test/specs/api.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions test/tags/analyzer/invalid.tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var riot = require('riot')

<valid-tag>
<h1>{ title }</h1>
<p>{ message }</p>
<invalid-flagment
this.title = 'Hello world!'
this.message = 'I am hungry...'
</valid-tag>

<invalid-t
<h1>{ title }</h1>
<p>{ message }</p>

this.title = 'Hello world!'
this.message = 'I am hungry...'
</invalid-t

console.log('end of file')
2 changes: 2 additions & 0 deletions test/tags/analyzer/tag-not-closed.tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<tag-not-closed>
<p>Hello!</p>
4 changes: 4 additions & 0 deletions test/tags/analyzer/tag-unmatch.tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<tag-unmatch>
<p>Hello!</p>
</tag-unmatch>
</tag-unmatched>
29 changes: 29 additions & 0 deletions test/tags/analyzer/valid.tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var riot = require('riot')

<valid-tag>
<h1>{ title }</h1>
<p>{ message }</p>

this.title = 'Hello world!'
this.message = 'I am hungry...'
</valid-tag>

<line-tag>Hello { opts.message }!</line-tag>

<tag-with-style>
<p>Hi!</p>
<style scoped>
p { color: red }
</style>
</tag-with-style>

<tag-with-script>
<h1>{ title }</h1>
<p>{ message }</p>
<script>
this.title = 'Hello world!'
this.message = 'I am hungry...'
</script>
</tag-with-script>

console.log('end of file')
5 changes: 5 additions & 0 deletions test/tags/analyzer/without-indent.tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<without-indent>
<p>Without indent</p>
<div>
Without indent</div>
</without-indent>

0 comments on commit baf4b35

Please sign in to comment.