Skip to content

Commit 4634aaf

Browse files
committed
Got first test to pass, started refactoring vcGatherUsage
1 parent f6cd941 commit 4634aaf

4 files changed

Lines changed: 225 additions & 121 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!doctype html>
2+
<html>
3+
4+
<title>vcGatherUsage Tests</title>
5+
6+
<div id="mocha"></div>
7+
<div id="test-area"></div>
8+
9+
<script type="text/javascript">
10+
window.less = {async: true, fileSync: true};
11+
</script>
12+
13+
<script src="../../../node_modules/steal/steal.js"
14+
mocha="bdd"
15+
main="a2jauthor/src/variables/editor/vcGatherUsage-test"></script>
Lines changed: 120 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,127 @@
1-
import './viewer/A2J_Types'
2-
// import './viewer/A2J_Prefs'
3-
// import './viewer/A2J_SharedSus'
4-
// import './viewer/A2J_Logic'
51
import 'jquery'
6-
import './A2J_Tabs'
7-
82
import { assert } from 'chai'
93
import 'steal-mocha'
10-
describe('vcGatherUsage', function () {
11-
it('gathers usage on Page, Field, and Button level property values using variables', function () {
12-
const usageTestPage = new window.TPage()
13-
usageTestPage.fields = [new window.TField()]
14-
usageTestPage.buttons = [new window.TButton()]
4+
import { vcGatherUsage, findMacroMatches } from './vcGatherUsage'
5+
6+
describe('findMacroMatches', function () {
7+
it.only('returns matches based on A2J Macro Syntax', function () {
8+
const matches = findMacroMatches('Hello %%[Client First Name TE]%%, welcome to the interview!', 'Client First Name TE')
9+
const expectedResult = [ '[Client First Name TE]' ]
10+
console.log(matches)
11+
assert.deepEqual(matches, expectedResult, 'should find one match')
12+
})
13+
})
1514

16-
window.gGuide.pages = { usageTestPage }
15+
//Copy above function for other two branches in findMatches, make fail, go from there
1716

18-
const testProps = {
19-
page: [
20-
{ key: 'name', type: 'regex', display: 'Page Name' },
21-
{ key: 'text', type: 'regex', display: 'Question Text' },
22-
{ key: 'repeatVar', type: 'string', display: 'Counting Variable' },
23-
{ key: 'outerLoopVar', type: 'string', display: 'Outer Loop Variable' },
24-
{ key: 'learn', type: 'regex', display: 'LearnMore Prompt' },
25-
{ key: 'help', type: 'regex', display: 'LearnMore Response' },
26-
{ key: 'helpReader', type: 'regex', display: 'Video Transcript' },
27-
{ key: 'codeBefore', type: 'logic', display: 'Before Logic' },
28-
{ key: 'codeAfter', type: 'logic', display: 'After Logic' }
29-
],
30-
fields: [
31-
{ key: 'label', type: 'regex', display: 'Field Label' },
32-
{ key: 'name', type: 'string', display: 'Field Variable' },
33-
{ key: 'value', type: 'regex', display: 'Field Default Value' },
34-
{ key: 'invalidPrompt', type: 'regex', display: 'Field Custom Invalid Prompt' },
35-
{ key: 'sample', type: 'regex', display: 'Field Sample Value' }
36-
],
37-
buttons: [
38-
{ key: 'label', type: 'regex', display: 'Button Label' },
39-
{ key: 'name', type: 'string', display: 'Button Variable Name' },
40-
{ key: 'value', type: 'regex', display: 'Button Default Value' },
41-
{ key: 'repeatVar', type: 'string', display: 'Button Counting Variable' },
42-
{ key: 'url', type: 'regex', display: 'Button URL' }
43-
]
44-
}
4517

46-
const setTestProps = (targetMap, propsToSet) => {
47-
for (const entry of propsToSet) {
48-
const prop = entry.key
49-
if (entry.type === 'regex') {
50-
targetMap[prop] = 'macro style %%[Number NU]%%'
51-
} else if (entry.type === 'logic') {
52-
targetMap[prop] = 'SET [Number NU] TO 1'
53-
} else { // direct var set
54-
targetMap[prop] = 'Number NU'
55-
}
56-
}
57-
}
58-
setTestProps(usageTestPage, testProps.page)
59-
setTestProps(usageTestPage.fields[0], testProps.fields)
60-
setTestProps(usageTestPage.buttons[0], testProps.buttons)
18+
// describe('vcGatherUsage', function () {
19+
// let usageTestPage
20+
// const testProps = {
21+
// page: [
22+
// { key: 'name', type: 'regex', display: 'Page Name' },
23+
// { key: 'text', type: 'regex', display: 'Question Text' },
24+
// { key: 'repeatVar', type: 'string', display: 'Counting Variable' },
25+
// { key: 'outerLoopVar', type: 'string', display: 'Outer Loop Variable' },
26+
// { key: 'learn', type: 'regex', display: 'LearnMore Prompt' },
27+
// { key: 'help', type: 'regex', display: 'LearnMore Response' },
28+
// { key: 'helpReader', type: 'regex', display: 'Video Transcript' },
29+
// { key: 'codeBefore', type: 'logic', display: 'Before Logic' },
30+
// { key: 'codeAfter', type: 'logic', display: 'After Logic' }
31+
// ],
32+
// fields: [
33+
// { key: 'label', type: 'regex', display: 'Field Label' },
34+
// { key: 'name', type: 'string', display: 'Field Variable' },
35+
// { key: 'value', type: 'regex', display: 'Field Default Value' },
36+
// { key: 'invalidPrompt', type: 'regex', display: 'Field Custom Invalid Prompt' },
37+
// { key: 'sample', type: 'regex', display: 'Field Sample Value' }
38+
// ],
39+
// buttons: [
40+
// { key: 'label', type: 'regex', display: 'Button Label' },
41+
// { key: 'name', type: 'string', display: 'Button Variable Name' },
42+
// { key: 'value', type: 'regex', display: 'Button Default Value' },
43+
// { key: 'repeatVar', type: 'string', display: 'Button Counting Variable' },
44+
// { key: 'url', type: 'regex', display: 'Button URL' }
45+
// ]
46+
// }
6147

62-
const foundMessage = window.vcGatherUsage('Number NU')
63-
// if found, `display value` will be in foundMessage for each entry
64-
for (const entry of testProps.page) {
65-
const usedInPage = foundMessage.indexOf(entry.display) !== -1
66-
assert.isTrue(usedInPage, `should find Number NU usage in page.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
67-
}
68-
for (const entry of testProps.fields) {
69-
const usedInField = foundMessage.indexOf(entry.display) !== -1
70-
assert.isTrue(usedInField, `should find Number NU usage in field.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
71-
}
72-
for (const entry of testProps.buttons) {
73-
const usedInButton = foundMessage.indexOf(entry.display) !== -1
74-
assert.isTrue(usedInButton, `should find Number NU usage in button.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
75-
}
76-
})
77-
})
48+
// beforeEach(() => {
49+
// usageTestPage = new window.TPage()
50+
// usageTestPage.fields = [new window.TField()]
51+
// usageTestPage.buttons = [new window.TButton()]
52+
53+
// const gGuide = new window.TGuide()
54+
// window.gGuide = gGuide
55+
// window.gGuide.pages = { usageTestPage }
56+
// })
57+
58+
// afterEach(() => {
59+
// window.gGuide = null
60+
// })
61+
62+
// it('gathers usage on Page, Field, and Button level property values using variables', function () {
63+
// const setTestProps = (targetMap, propsToSet) => {
64+
// for (const entry of propsToSet) {
65+
// const prop = entry.key
66+
// if (entry.type === 'regex') {
67+
// targetMap[prop] = 'macro style %%[Number NU]%%'
68+
// } else if (entry.type === 'logic') {
69+
// targetMap[prop] = 'SET [Number NU] TO 1'
70+
// } else { // direct var set
71+
// targetMap[prop] = 'Number NU'
72+
// }
73+
// }
74+
// }
75+
// setTestProps(usageTestPage, testProps.page)
76+
// setTestProps(usageTestPage.fields[0], testProps.fields)
77+
// setTestProps(usageTestPage.buttons[0], testProps.buttons)
78+
79+
// const foundMessage = vcGatherUsage('Number NU')
80+
// // if found, `display value` will be in foundMessage for each entry
81+
// for (const entry of testProps.page) {
82+
// const usedInPage = foundMessage.indexOf(entry.display) !== -1
83+
// assert.isTrue(usedInPage, `should find Number NU usage in page.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
84+
// }
85+
// for (const entry of testProps.fields) {
86+
// const usedInField = foundMessage.indexOf(entry.display) !== -1
87+
// assert.isTrue(usedInField, `should find Number NU usage in field.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
88+
// }
89+
// for (const entry of testProps.buttons) {
90+
// const usedInButton = foundMessage.indexOf(entry.display) !== -1
91+
// assert.isTrue(usedInButton, `should find Number NU usage in button.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
92+
// }
93+
// })
94+
95+
// it('gathers explicit usage on Page, Field, and Button level property values using variables', function () {
96+
// const setTestProps = (targetMap, propsToSet) => {
97+
// for (const entry of propsToSet) {
98+
// const prop = entry.key
99+
// if (entry.type === 'regex') {
100+
// targetMap[prop] = 'macro style %%[Number NU]%%'
101+
// } else if (entry.type === 'logic') {
102+
// targetMap[prop] = 'SET [Number123 NU] TO 1'
103+
// } else { // direct var set
104+
// targetMap[prop] = 'Number foo NU'
105+
// }
106+
// }
107+
// }
108+
// setTestProps(usageTestPage, testProps.page)
109+
// setTestProps(usageTestPage.fields[0], testProps.fields)
110+
// setTestProps(usageTestPage.buttons[0], testProps.buttons)
111+
112+
// const foundMessage = vcGatherUsage('Number NU')
113+
// // if found, `display value` will be in foundMessage for each entry
114+
// for (const entry of testProps.page) {
115+
// const usedInPage = foundMessage.indexOf(entry.display) !== -1
116+
// assert.isTrue(usedInPage, `should find Number NU usage in page.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
117+
// }
118+
// for (const entry of testProps.fields) {
119+
// const usedInField = foundMessage.indexOf(entry.display) !== -1
120+
// assert.isTrue(usedInField, `should find Number NU usage in field.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
121+
// }
122+
// for (const entry of testProps.buttons) {
123+
// const usedInButton = foundMessage.indexOf(entry.display) !== -1
124+
// assert.isTrue(usedInButton, `should find Number NU usage in button.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
125+
// }
126+
// })
127+
// })

src/variables/editor/vcGatherUsage.js

Lines changed: 89 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,113 @@
1-
export function vcGatherUsage (varName) { // 2015-03-27 Search for variable or constant
1+
const pageProps = [
2+
{ key: 'name', type: 'regex', display: 'Page Name' },
3+
{ key: 'text', type: 'regex', display: 'Question Text' },
4+
{ key: 'repeatVar', type: 'string', display: 'Counting Variable' },
5+
{ key: 'outerLoopVar', type: 'string', display: 'Outer Loop Variable' },
6+
{ key: 'learn', type: 'regex', display: 'LearnMore Prompt' },
7+
{ key: 'help', type: 'regex', display: 'LearnMore Response' },
8+
{ key: 'helpReader', type: 'regex', display: 'Video Transcript' },
9+
{ key: 'codeBefore', type: 'logic', display: 'Before Logic' },
10+
{ key: 'codeAfter', type: 'logic', display: 'After Logic' }
11+
]
12+
const fieldProps = [
13+
{ key: 'label', type: 'regex', display: 'Field Label' },
14+
{ key: 'name', type: 'string', display: 'Field Variable' },
15+
{ key: 'value', type: 'regex', display: 'Field Default Value' },
16+
{ key: 'invalidPrompt', type: 'regex', display: 'Field Custom Invalid Prompt' },
17+
{ key: 'sample', type: 'regex', display: 'Field Sample Value' }
18+
]
19+
const buttonProps = [
20+
{ key: 'label', type: 'regex', display: 'Button Label' },
21+
{ key: 'name', type: 'string', display: 'Button Variable Name' },
22+
{ key: 'value', type: 'regex', display: 'Button Default Value' },
23+
{ key: 'repeatVar', type: 'string', display: 'Button Counting Variable' },
24+
{ key: 'url', type: 'regex', display: 'Button URL' }
25+
]
26+
27+
/*
28+
All three tests greedy if explicitSearch = notTrue
29+
Test 3 currently is explicit (testValue === lowerCaseVarName)
30+
Make test 3 greedy
31+
32+
-i believe this can be solved with .includes, maybe a
33+
if(testValue.includes(lowerCaseVarName)){
34+
where.push(usageItem.display)
35+
}
36+
37+
Rename three regex consts
38+
39+
-could simply be
40+
parenRegexString `^\\(\\s*${lowerCaseVarName}\\s*\\)$`
41+
percentRegexString `^\\%\\s*${lowerCaseVarName}\\s*\\%$`
42+
bracketRegexString `^\\[\\s*${lowerCaseVarName}\\s*\\]$`
43+
if explicitSearch === true, use search Regex with ^/$
44+
45+
*After above is done*
46+
If explicit, find explicit matches
47+
Either if/else or find Explicit function
48+
49+
*/
50+
51+
export const findMacroMatches = (testValue, varName) => {
52+
const lowerCaseVarName = varName.toLowerCase()
53+
const parenRegexString = `\\(\\s*${lowerCaseVarName}\\s*\\)`
54+
const percentRegexString = `\\%\\s*${lowerCaseVarName}\\s*\\%`
55+
const bracketRegexString = `\\[\\s*${lowerCaseVarName}\\s*\\]`
56+
const regexString = `${parenRegexString} | ${percentRegexString} | ${bracketRegexString}`
57+
const macroRegex = new RegExp(regexString, 'i')
58+
59+
const matches = testValue.match(macroRegex)
60+
61+
return matches
62+
}
63+
64+
export const findMatches = (searchTarget, usageItem) => {
65+
// skip check if not string value to check
66+
const prop = usageItem.key
67+
if (!searchTarget[prop]) { return }
68+
const testValue = searchTarget[prop].toLowerCase()
69+
70+
if (usageItem.type === 'regex') { // check for macro matches, `%%someVar%%`
71+
const matches = testValue.match(macroRegex)
72+
if (matches && matches.length) {
73+
where.push(usageItem.display)
74+
}
75+
} else if (usageItem.type === 'logic') {
76+
if (testValue.indexOf(lowerCaseVarName) !== -1) { // check for logic usage (no macro syntax, `set someVar to "foo"`)
77+
where.push(usageItem.display)
78+
}
79+
} else {
80+
if (testValue === lowerCaseVarName) { // check for varName itself, `someVar`
81+
where.push(usageItem.display)
82+
}
83+
}
84+
}
85+
86+
export function vcGatherUsage (varName, explicitSearch) { // 2015-03-27 Search for variable or constant
87+
// alter below line to test two search methods
88+
explicitSearch = true
289
let html = ''
390
let count = 0
491
let pageName
5-
const lowerCaseVarName = varName.toLowerCase()
6-
const regexString = `\\(\\s*${lowerCaseVarName}\\s*\\)|\\%\\s*${lowerCaseVarName}\\s*\\%|\\[\\s*${lowerCaseVarName}\\s*\\]`
7-
const macroRegex = new RegExp(regexString, 'i')
92+
893
for (pageName in window.gGuide.pages) { // Search text, buttons, help, fields and logic for variable name.
994
/** @type TPage */
1095
const where = [] // list where it's on this page
1196
const page = window.gGuide.pages[pageName]
1297

13-
const findMatches = (searchTarget, usageItem) => {
14-
// skip check if not string value to check
15-
const prop = usageItem.key
16-
if (!searchTarget[prop]) { return }
17-
const testValue = searchTarget[prop].toLowerCase()
18-
19-
if (usageItem.type === 'regex') { // check for macro matches, `%%someVar%%`
20-
const matches = testValue.match(macroRegex)
21-
if (matches && matches.length) {
22-
where.push(usageItem.display)
23-
}
24-
} else if (usageItem.type === 'logic') {
25-
if (testValue.indexOf(lowerCaseVarName) !== -1) { // check for logic usage (no macro syntax, `set someVar to "foo"`)
26-
where.push(usageItem.display)
27-
}
28-
} else {
29-
if (testValue === lowerCaseVarName) { // check for varName itself, `someVar`
30-
where.push(usageItem.display)
31-
}
32-
}
33-
}
34-
3598
// check top level page properties
36-
const pageProps = [
37-
{ key: 'name', type: 'regex', display: 'Page Name' },
38-
{ key: 'text', type: 'regex', display: 'Question Text' },
39-
{ key: 'repeatVar', type: 'string', display: 'Counting Variable' },
40-
{ key: 'outerLoopVar', type: 'string', display: 'Outer Loop Variable' },
41-
{ key: 'learn', type: 'regex', display: 'LearnMore Prompt' },
42-
{ key: 'help', type: 'regex', display: 'LearnMore Response' },
43-
{ key: 'helpReader', type: 'regex', display: 'Video Transcript' },
44-
{ key: 'codeBefore', type: 'logic', display: 'Before Logic' },
45-
{ key: 'codeAfter', type: 'logic', display: 'After Logic' }
46-
]
4799
for (const entry of pageProps) {
48100
findMatches(page, entry)
49101
}
50102

51103
// check all page fields
52-
const fieldProps = [
53-
{ key: 'label', type: 'regex', display: 'Field Label' },
54-
{ key: 'name', type: 'string', display: 'Field Variable' },
55-
{ key: 'value', type: 'regex', display: 'Field Default Value' },
56-
{ key: 'invalidPrompt', type: 'regex', display: 'Field Custom Invalid Prompt' },
57-
{ key: 'sample', type: 'regex', display: 'Field Sample Value' }
58-
]
59104
for (const field of page.fields) {
60105
for (const entry of fieldProps) {
61106
findMatches(field, entry)
62107
}
63108
}
64109

65110
// check all buttons
66-
const buttonProps = [
67-
{ key: 'label', type: 'regex', display: 'Button Label' },
68-
{ key: 'name', type: 'string', display: 'Button Variable Name' },
69-
{ key: 'value', type: 'regex', display: 'Button Default Value' },
70-
{ key: 'repeatVar', type: 'string', display: 'Button Counting Variable' },
71-
{ key: 'url', type: 'regex', display: 'Button URL' }
72-
]
73111
for (const button of page.buttons) {
74112
for (const entry of buttonProps) {
75113
findMatches(button, entry)

test/test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ import 'a2jauthor/src/pages-tab/components/var-picker/field/var-picker-field-tes
2323
// import 'a2jauthor/src/templates/edit/toolbar/toolbar-test'
2424
// import 'a2jauthor/src/templates/list/item/item-test'
2525
// import 'a2jauthor/src/templates/list/sortbar/sortbar-test'
26+
import 'a2jauthor/src/variables/editor/vcGatherUsage-test.js'

0 commit comments

Comments
 (0)