From 49419a39ee6895d0732754ac6bd4461d38e4092e Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Mon, 2 Mar 2020 19:06:21 -0800 Subject: [PATCH] test green for run from parsed epoch latl --- src/reducers/reducer.epochs.test.js | 22 +++++++++++----------- src/reducers/reducer.init.js | 2 +- src/reducers/reducer.latl.js | 10 +++++----- src/reducers/reducer.latl.test.js | 25 ++++++++++++++++++++----- src/reducers/reducer.lexicon.test.js | 16 ++++++++-------- src/reducers/reducer.results.js | 9 ++++++--- src/reducers/reducer.results.test.js | 28 ++++++++++++++-------------- 7 files changed, 65 insertions(+), 47 deletions(-) diff --git a/src/reducers/reducer.epochs.test.js b/src/reducers/reducer.epochs.test.js index f2e4cd4..37e0e8e 100644 --- a/src/reducers/reducer.epochs.test.js +++ b/src/reducers/reducer.epochs.test.js @@ -5,7 +5,7 @@ describe('Epochs', () => { beforeEach(()=> { state.epochs = [ { - name: 'epoch 1', + name: 'epoch-1', changes: [''], parent: null } @@ -18,45 +18,45 @@ describe('Epochs', () => { }); it('epochs addition returns new epochs list', () => { - const action = {type: 'ADD_EPOCH', value: { name: 'epoch 2', changes: [''], parent: null}}; + const action = {type: 'ADD_EPOCH', value: { name: 'epoch-2', changes: [''], parent: null}}; expect(stateReducer(state, action)).toEqual({...state, epochs: [...state.epochs, action.value]}) }) - it('epoch name mutation returns new epochs list with mutation', () => { - const firstAction = {type: 'ADD_EPOCH', value: { name: 'epoch 2', changes: ['']}}; + it('epoch-name mutation returns new epochs list with mutation', () => { + const firstAction = {type: 'ADD_EPOCH', value: { name: 'epoch-2', changes: ['']}}; const secondAction = {type: 'SET_EPOCH', value: { index: 0, name: 'proto-lang'}}; const secondState = stateReducer(state, firstAction); expect(stateReducer(secondState, secondAction)).toEqual( {...state, epochs: [ {name: 'proto-lang', changes: [''], parent: null}, - {name: 'epoch 2', changes: [''], parent: null} + {name: 'epoch-2', changes: [''], parent: null} ] } ); }); it('epoch changes mutation returns new epochs list with mutation', () => { - const firstAction = {type: 'ADD_EPOCH', value: { name: 'epoch 2', changes: ['']}}; + const firstAction = {type: 'ADD_EPOCH', value: { name: 'epoch-2', changes: ['']}}; const secondAction = {type: 'SET_EPOCH', value: { index: 0, changes: ['n>t/_#', '[+plosive]>[+nasal -plosive]/_n']}}; const secondState = stateReducer(state, firstAction); expect(stateReducer(secondState, secondAction)).toEqual( {...state, epochs: [ - {name: 'epoch 1', changes: ['n>t/_#', '[+plosive]>[+nasal -plosive]/_n'], parent: null}, - {name: 'epoch 2', changes: [''], parent: null} + {name: 'epoch-1', changes: ['n>t/_#', '[+plosive]>[+nasal -plosive]/_n'], parent: null}, + {name: 'epoch-2', changes: [''], parent: null} ] } ); }); it('epochs returned with deleted epoch removed', () => { - const firstAction = {type: 'ADD_EPOCH', value: { name: 'epoch 2', changes: ['']}}; + const firstAction = {type: 'ADD_EPOCH', value: { name: 'epoch-2', changes: ['']}}; const stateWithTwoEpochs = stateReducer(state, firstAction); - const secondAction = {type: 'REMOVE_EPOCH', value: {index: 0, name: 'epoch 1'}} + const secondAction = {type: 'REMOVE_EPOCH', value: {index: 0, name: 'epoch-1'}} expect(stateReducer(stateWithTwoEpochs, secondAction)).toEqual({ ...state, - epochs: [{ name: 'epoch 2', changes: [''], parent: null}] + epochs: [{ name: 'epoch-2', changes: [''], parent: null}] }); }); diff --git a/src/reducers/reducer.init.js b/src/reducers/reducer.init.js index f7a27c5..6b15fab 100644 --- a/src/reducers/reducer.init.js +++ b/src/reducers/reducer.init.js @@ -9,7 +9,7 @@ export const initState = (changesArgument: number): stateType => { const state = { epochs: [ { - name: 'epoch 1', + name: 'epoch-1', changes: [ '[+ occlusive - nasal]>[+ occlusive + nasal]/n_.', 'a>ɯ/._#', diff --git a/src/reducers/reducer.latl.js b/src/reducers/reducer.latl.js index 1dbc825..d02798a 100644 --- a/src/reducers/reducer.latl.js +++ b/src/reducers/reducer.latl.js @@ -90,6 +90,9 @@ const parseReferent = (tree, token, index, tokens) => { tree[tree.length - 1] = {...lastNode, name: token.value, type: 'epoch' } return tree; } + case 'epoch': { + return [...tree, { type: 'rule', value: token.value } ] + } case 'rule': { tree[tree.length - 1] = {...lastNode, value: lastNode.value + token.value } return tree; @@ -264,17 +267,14 @@ export const buildTree = tokens => { export const generateAST = latl => { // tokenize - const tokens = tokenize(latl); - + const tokens = tokenize(latl.trim()); // build tree const tree = buildTree(tokens); - return tree; } export const parseLatl = (state, action) => { const latl = state.latl; - console.log(latl) const AST = generateAST(latl); Object.entries(AST).forEach(([key, value]) => state[key] = value); return { ...state } @@ -295,7 +295,7 @@ const tokenTypes = [ ['slash', `\/`], ['dot', `\\.`], ['underscore', `\\_`], - [`referent`, `[A-Za-z]+`], + [`referent`, `[A-Za-z]+[\\w\\-\\_]*`], ['equal', `=`], [`lineBreak`, `\\n`] // [`whiteSpace`, `\\s+`] diff --git a/src/reducers/reducer.latl.test.js b/src/reducers/reducer.latl.test.js index 2acba20..a922749 100644 --- a/src/reducers/reducer.latl.test.js +++ b/src/reducers/reducer.latl.test.js @@ -45,25 +45,40 @@ describe('LATL', () => { const state = initState(); const setAction = { type: 'SET_LATL', - value: epochDefinitionLatl + value: runEpochLatl } const latlState = stateReducer(state, setAction); const parseState = parseLatl(latlState, {}) - expect(parseState).toStrictEqual(epochState); + // expect(parseState).toStrictEqual(epochState); parseState.lexicon[0].epoch = 'PROTO' const runState = stateReducer(parseState, {type: 'RUN', value:{}}) - console.log(runState) + expect(runState).toStrictEqual({...runState, results: runEpochResults}) }) }) const epochDefinitionLatl = ` ; comment *PROTO -[+ FEATURE]>[- FEATURE]/._. -n>m/#_. + [+ FEATURE]>[- FEATURE]/._. + n>m/#_. |CHILD ` +const runEpochLatl = ` +; comment +*PROTO + a>u/._. +|epoch-1 +` + +const runEpochResults = [ + { + pass: 'epoch-1', + parent: 'PROTO', + lexicon: [ 'untu', 'unut', 'unət', 'unnu', 'tun', 'əntu' ] + } +] + const tokenizedEpoch = [ { type: "semicolon", value: "; comment" }, { type: "star", value: "*" }, { type: "referent", value: "PROTO" }, { type: 'lineBreak', value: '' }, diff --git a/src/reducers/reducer.lexicon.test.js b/src/reducers/reducer.lexicon.test.js index d7256ac..486b335 100644 --- a/src/reducers/reducer.lexicon.test.js +++ b/src/reducers/reducer.lexicon.test.js @@ -3,8 +3,8 @@ import {stateReducer} from './reducer'; describe('Lexicon', () => { const state = { epochs: [ - { name: 'epoch 1', changes:[''] }, - { name: 'epoch 2', changes:[''] } + { name: 'epoch-1', changes:[''] }, + { name: 'epoch-2', changes:[''] } ] } state.lexicon = [ @@ -28,16 +28,16 @@ describe('Lexicon', () => { }); it('lexicon addition with epoch returns updated lexicon with correct epoch', () => { - const action = {type: 'ADD_LEXEME', value: {lexeme:'ntʰa', epoch: 'epoch 2'}} + const action = {type: 'ADD_LEXEME', value: {lexeme:'ntʰa', epoch: 'epoch-2'}} expect(stateReducer(state, action)).toEqual({...state, lexicon:[...state.lexicon, {lexeme:'ntʰa', epoch:state.epochs[1]}]}); }); it('lexicon set returns updated lexicon with correct epoch', () => { const newLexicon = [ - {lexeme:'anta', epoch:'epoch 1'}, - {lexeme:'anat', epoch:'epoch 1'}, - {lexeme:'anət', epoch:'epoch 1'}, - {lexeme:'anna', epoch:'epoch 1'} + {lexeme:'anta', epoch:'epoch-1'}, + {lexeme:'anat', epoch:'epoch-1'}, + {lexeme:'anət', epoch:'epoch-1'}, + {lexeme:'anna', epoch:'epoch-1'} ] const action = {type: 'SET_LEXICON', value: newLexicon} expect(stateReducer(state, action)).toEqual({...state, lexicon:[ @@ -58,7 +58,7 @@ describe('Lexicon', () => { const inputLexicon = [ {lexeme:'anta'}, {lexeme:'anat'}, - {lexeme:'anət', epoch:'epoch 2'}, + {lexeme:'anət', epoch:'epoch-2'}, {lexeme:'anna'} ] const action = {type: 'SET_LEXICON', value: inputLexicon} diff --git a/src/reducers/reducer.results.js b/src/reducers/reducer.results.js index 715fa1c..e9e7b71 100644 --- a/src/reducers/reducer.results.js +++ b/src/reducers/reducer.results.js @@ -263,12 +263,15 @@ export const run = (state: stateType, action: resultsAction): stateType => { const passResults = state.epochs.reduce((results, epoch, _) => { const { phones, features, lexicon } = state; let lexiconBundle; - if ( epoch.parent && results[epoch.parent] ) { - lexiconBundle = results.find(result => result.pass === epoch.parent).lexicon + if ( epoch.parent ) { + lexiconBundle = results.find(result => result.pass === epoch.parent) } - if (!epoch.parent || !results[epoch.parent]) { + if (!lexiconBundle) { lexiconBundle = formBundleFromLexicon(lexicon)(phones); } + else { + lexiconBundle = lexiconBundle.lexicon + } const ruleBundle = decomposeRules(epoch, phones); const passResults = transformLexicon(lexiconBundle)(ruleBundle)(features) const pass = { pass: epoch.name, lexicon: passResults } diff --git a/src/reducers/reducer.results.test.js b/src/reducers/reducer.results.test.js index 43b7b9e..3e00014 100644 --- a/src/reducers/reducer.results.test.js +++ b/src/reducers/reducer.results.test.js @@ -130,7 +130,7 @@ describe('Results', () => { state = initState(1) expect(stateReducer(state, action).results).toEqual([ { - pass: 'epoch 1', + pass: 'epoch-1', lexicon: [ 'anna', 'anat', 'anət', 'anna', 'tan', 'ənna' ] @@ -143,7 +143,7 @@ describe('Results', () => { state = initState(2) expect(stateReducer(state, action).results).toEqual([ { - pass: 'epoch 1', + pass: 'epoch-1', lexicon: [ 'annɯ', 'anat', 'anət', 'annɯ', 'tan', 'ənnɯ' ] @@ -156,7 +156,7 @@ describe('Results', () => { state = initState(3) expect(stateReducer(state, action).results).toEqual([ { - pass: 'epoch 1', + pass: 'epoch-1', lexicon: [ 'annɯ', 'anat', 'ant', 'annɯ', 'tan', 'nnɯ' ] @@ -169,7 +169,7 @@ describe('Results', () => { state = initState(4) expect(stateReducer(state, action).results).toEqual([ { - pass: 'epoch 1', + pass: 'epoch-1', lexicon: [ 'annɯ', 'anat', 'ant', 'annɯ', 'tʰan', 'nnɯ' ] @@ -182,7 +182,7 @@ describe('Results', () => { state = initState(5) expect(stateReducer(state, action).results).toEqual([ { - pass: 'epoch 1', + pass: 'epoch-1', lexicon: [ 'annu', 'anat', 'ant', 'annu', 'tʰan', 'nnu' ] @@ -195,7 +195,7 @@ describe('Results', () => { // state = initState(6) // expect(stateReducer(state, action).results).toEqual([ // { - // pass: 'epoch 1', + // pass: 'epoch-1', // lexicon: [ // 'annu', 'anta', 'ant', 'annu', 'tʰan', 'nnu' // ] @@ -207,7 +207,7 @@ describe('Results', () => { const action = {type: 'RUN'}; state = initState(5); const newEpoch = { - name: 'epoch 2', + name: 'epoch-2', changes: [ '[+ sonorant ]>0/#_.', 'n>0/#_n' @@ -216,13 +216,13 @@ describe('Results', () => { state.epochs = [ ...state.epochs, newEpoch ] expect(stateReducer(state, action).results).toEqual([ { - pass: 'epoch 1', + pass: 'epoch-1', lexicon: [ 'annu', 'anat', 'ant', 'annu', 'tʰan', 'nnu' ] }, { - pass: 'epoch 2', + pass: 'epoch-2', lexicon: [ 'nta', 'nat', 'nət', 'na', 'tan', 'nta' ] @@ -234,8 +234,8 @@ describe('Results', () => { const action = {type: 'RUN'}; state = initState(5); const newEpoch = { - name: 'epoch 2', - parent: 'epoch 1', + name: 'epoch-2', + parent: 'epoch-1', changes: [ '[+ sonorant ]>0/#_.' ] @@ -243,14 +243,14 @@ describe('Results', () => { state.epochs = [ ...state.epochs, newEpoch ] expect(stateReducer(state, action).results).toEqual([ { - pass: 'epoch 1', + pass: 'epoch-1', lexicon: [ 'annu', 'anat', 'ant', 'annu', 'tʰan', 'nnu' ] }, { - pass: 'epoch 2', - parent: 'epoch 1', + pass: 'epoch-2', + parent: 'epoch-1', lexicon: [ 'nnu', 'nat', 'nt', 'nnu', 'tʰan', 'nu' ]