#!/usr/bin/node
//---------------------------------------------------------------------
var path = require('path')
var test = require('ig-test')
var serialize = require('ig-serialize')
var parser = require('../parser').parser
//---------------------------------------------------------------------
var PAGES =
module.exports.PAGES = {
'/blank': '',
'/page': 'Page',
'/async/page': Promise.resolve('Page'),
'/includePage': '@include(/page)',
}
var P =
module.exports.P = {
__pages__: PAGES,
__parser__: parser,
path: '/',
get basepath(){
return path.dirname(this.path ?? '') },
get raw(){
return this.__pages__[this.path] ?? '' },
get text(){
return this.__parser__.parse(this, this.raw, {}) },
get: function(p){
return {
__proto__: P,
path: path.resolve(this.path, p),
} },
// XXX
resolvePathVars: function(path){
return path },
}
//---------------------------------------------------------------------
test.Setups({
empty: function(assert){
return { code: [ '', '' ] }},
// slot...
slot_empty: function(assert){
return {code: [
'@slot(slot)',
'@slot("slot")',
'@slot(\'slot\')',
'@slot(name=slot)',
'@slot(name="slot")',
'@slot(name=\'slot\')',
'',
'',
'',
'',
'',
'',
'' ]} },
slot_value: function(assert){
return {code: [
'',
'',
'@slot(slot value)',
'@slot(slot text=value)',
'value' ]} },
slot_fill: function(assert){
var ins = this.slot_value(assert).code.slice(0, -1)
return {code: [
...ins.map(function(e){
return e + '@slot(slot other)' }),
...ins.map(function(e){
return e + '' }),
...ins.map(function(e){
return e + 'other' }),
'other' ]} },
slot_fill_fill: function(assert){
var ins = this.slot_fill(assert).code.slice(0, -1)
return {code: [
...ins.map(function(e){
return e + '@slot(slot third)' }),
...ins.map(function(e){
return e + '' }),
...ins.map(function(e){
return e + 'third' }),
'third' ]} },
// nested...
slot_nested: function(assert){
return {code: [
'[[ ]]@slot(slot.content value)',
'[[ value ]]' ]} },
slot_nested_overwrite: function(assert){
return {code: [
'[[ ]]@slot(slot value)',
'value' ]} },
// nested-self...
slot_nested_nested: function(assert){
return {code: [
'[[ ]]',
'[[ ]]',
'value' ]} },
slot_nested_shown_nested: function(assert){
return {code: [
'[[ ]]',
'[[ ]]' ]} },
// slot content...
slot_content: function(assert){
return {code: [
'[[ ]]',
'[[ ]]' ]} },
slot_content_multi: function(assert){
return {code: [
'[[ ]]',
'[[ moo moo moo ]]' ]} },
slot_content_filled: function(assert){
var ins = this.slot_content(assert).code.slice(0, -1)
return {code: [
...ins.map(function(e){
return '' + e }),
'[[ content ]]' ]} },
slot_content_content_filled: function(assert){
var ins = this.slot_content_filled(assert).code
var res = ins.pop()
return {code: [
...ins.map(function(e){
return e +'(( ))' }),
'(( '+ res +' ))' ]} },
slot_content_nested: function(assert){
return {code: [
'moo[[ ]]',
'[[ moo ]]' ]} },
/* XXX SHOWN_HIDDEN
// XXX these need to be revised...
// ...do we actually need hidden/shown???
//
slot_shown: function(assert){
var ins = this.slot_value(assert).code
var expect = ins.pop()
return {code: [
...ins.map(function(i){
return i +' @slot(slot that shown)' }),
'that that' ]} },
slot_hidden: function(assert){
return {code: [
'',
'@slot(slot value hidden)',
'' ]} },
slot_hidden_value: function(assert){
var ins = this.slot_hidden(assert).code
var expect = ins.pop()
return {code: [
...ins.map(function(i){
return i +'@slot(slot other)' }),
'' ]} },
slot_hidden_shown: function(assert){
var ins = this.slot_hidden(assert).code
var expect = ins.pop()
return {code: [
...ins.map(function(i){
return i +'@slot(slot shown)' }),
'' ]} },
//*/
// XXX var...
// XXX
// XXX arg...
// XXX
// include...
// XXX these do not play well with modifiers...
include: function(assert, path='/blank', expected){
return {
page: P,
code: [
`@include(${path})`,
// XXX for some reason this does not parse int a macro....
//'',
``,
expected
?? P.get(path).raw,
],
}},
include_page: function(assert){
return this.include(assert, '/page') },
include_include_page: function(assert){
return this.include(assert, '/includePage', 'Page') },
/* XXX parser.resolve(..) does not handle promises correctly yet...
include_async: function(assert){
return this.include(assert, '/async/page') },
//*/
})
test.Modifiers({
args: function(assert, state){
;(state.state = (state.state ?? {})).args = {
number: 1,
string: 'string',
}
return state },
slot: function(assert, state){
state.code = [
...state.code
.slice(0, -1)
.map(function(e){
return `[[ ${e} ]]` }),
`[[ ${state.code.at(-1)} ]]`,
]
return state },
slot_expand: function(assert, state){
state.code = [
...state.code
.slice(0, -1)
.map(function(e){
return `[[ ]]${e}` }),
`[[ ${state.code.at(-1)} ]]`,
]
return state },
})
test.Tests({
parse: function(assert, state){
var {page, code, st} = state
page ??= {}
st ??= {}
var res
var inputs = code.slice(0, -1)
var expect = code.at(-1)
var i = 0
for(var input of inputs){
var p = serialize.partialDeepCopy(page)
var s = serialize.partialDeepCopy(st)
assert(
(res = parser.parse(
p,
input,
s))
=== expect,
'Parsing:',
'\n\t in: "'+ input +'"',
'\n\t out: "'+ res +'"',
'\n\texpected: "'+ expect +'"') } },
//asyncParse: async function(assert, state){
// return await this.parse(assert, state) },
})
test.Cases({
})
//---------------------------------------------------------------------
// make the test runnable as a standalone script...
__filename == (require.main || {}).filename
&& test.run()
//---------------------------------------------------------------------
// vim:set ts=4 sw=4 :