Skip to content

Commit

Permalink
fix: bug json inline into abe data
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaslabbe committed Dec 16, 2016
1 parent 15fbb47 commit 31781a6
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 15 deletions.
27 changes: 16 additions & 11 deletions src/cli/cms/data/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,25 @@ export function handleSqlRequest(str, jsonPage) {
}
}

/**
* get JSON from abe tag attribute source
*
* {{abe type='data' key='titles' desc='select titles' source='[{"title": "rouge", "id": 1},{"title": "vert", "id": 2},{"title": "blue", "id": 3}]' display="{{title}}"}}
*
* return
* [{"title": "rouge", "id": 1},{"title": "vert", "id": 2},{"title": "blue", "id": 3}]
*
* @param {String} str abe tag
* @return {String} json string
*/
export function getDataSource(str) {
var res = str.substring(str.indexOf('source=') + 8, str.length)

var reg = /([^'"]*=[\s\S]*?}})/g
var matches = res.match(reg)
if(matches != null) {
Array.prototype.forEach.call(matches, (match) => {
res = res.replace(match, '')
})
}else {
res = res.replace('}}', '')
var reg = /source=(['|"])(.*?)\1[ |\}]/g
var match = reg.exec(str)
if (match != null) {
return match[2]
}

return res.substring(0, res.length-1)
return []
}

/**
Expand Down
50 changes: 50 additions & 0 deletions test/cms/data/source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var chai = require('chai');
var sinonChai = require('sinon-chai')
var expect = chai.expect
chai.use(sinonChai)
var sinon = require('sinon');
var path = require('path');
var fse = require('fs-extra');

var config = require('../../../src/cli').config
config.set({root: path.join(process.cwd(), 'test', 'fixtures')})

var cmsData = require('../../../src/cli').cmsData;
var Manager = require('../../../src/cli').Manager;

describe('Source', function() {
before( function(done) {
Manager.instance.init()
.then(function () {
this.fixture = {
articleJsoninline: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-data-jsoninline.html'), 'utf8'),
articleArrayinline: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-data-arrayinline.html'), 'utf8')
}
done()

}.bind(this))
});

/**
*
*
*/
it('cmsData.source.valueList', function(done) {
var obj = {key:'titles'}
var json = {abe_source:{}}
cmsData.source.valueList(obj, this.fixture.articleJsoninline, json)
.then(() => {
chai.expect(json.abe_source.titles.length).to.be.equal(3);
chai.expect(json.abe_source.titles[0].title).to.be.equal("rouge");


obj = {key:'titles'}
json = {abe_source:{}}
cmsData.source.valueList(obj, this.fixture.articleArrayinline, json)
.then(() => {
chai.expect(json.abe_source.titles.length).to.be.equal(3);
done()
})
})
});
});
42 changes: 42 additions & 0 deletions test/cms/data/sql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var chai = require('chai');
var sinonChai = require('sinon-chai')
var expect = chai.expect
chai.use(sinonChai)
var sinon = require('sinon');
var path = require('path');
var fse = require('fs-extra');

var config = require('../../../src/cli').config
config.set({root: path.join(process.cwd(), 'test', 'fixtures')})

var cmsData = require('../../../src/cli').cmsData;
var Manager = require('../../../src/cli').Manager;

describe('Sql', function() {
before( function(done) {
Manager.instance.init()
.then(function () {
this.fixture = {
articleJsoninline: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-data-jsoninline.html'), 'utf8'),
articleArrayinline: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article-data-arrayinline.html'), 'utf8')
}
done()

}.bind(this))
});

/**
*
*
*/
it('cmsData.sql.getDataSource', function() {
var obj = {key:'titles'}
var json = {abe_source:{}}
var jsonString = cmsData.sql.getDataSource(this.fixture.articleJsoninline)
chai.expect(jsonString.indexOf('rouge')).to.be.above(-1);
JSON.parse(jsonString)
jsonString = cmsData.sql.getDataSource(this.fixture.articleArrayinline)
chai.expect(jsonString.indexOf('rouge')).to.be.above(-1);
JSON.parse(jsonString)
});
});
1 change: 1 addition & 0 deletions test/fixtures/templates/article-data-arrayinline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{abe type='data' key='titles' desc='select titles' source='["rouge", "vert", "blue"]'}}
1 change: 1 addition & 0 deletions test/fixtures/templates/article-data-jsoninline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{abe type='data' key='titles' desc='select titles' source='[{"title": "rouge", "id": 1},{"title": "vert", "id": 2},{"title": "blue", "id": 3}]' display="{{title}}"}}
8 changes: 4 additions & 4 deletions test/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ describe('Manager', function() {

it('getStructureAndTemplates()', function() {
const data = Manager.instance.getStructureAndTemplates()
chai.assert.equal(data['templates'][0].name, 'article-each-abe', 'failed !')
chai.assert.equal(data['templates'].length, 22, 'failed !')
chai.assert.equal(data['templates'][0].name, 'article-data-arrayinline', 'failed !')
chai.assert.equal(data['templates'].length, 24, 'failed !')
});

it('updateStructureAndTemplates()', function() {
Manager.instance.updateStructureAndTemplates()
const data = Manager.instance.getStructureAndTemplates()
chai.assert.equal(data['templates'][0].name, 'article-each-abe', 'failed !')
chai.assert.equal(data['templates'].length, 22, 'failed !')
chai.assert.equal(data['templates'][0].name, 'article-data-arrayinline', 'failed !')
chai.assert.equal(data['templates'].length, 24, 'failed !')
});

it('getList()', function() {
Expand Down

0 comments on commit 31781a6

Please sign in to comment.