Skip to content

Commit

Permalink
cache tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kla committed Oct 26, 2024
1 parent 18b5d80 commit b7c5a06
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
6 changes: 2 additions & 4 deletions tests/fixtures/circular_expressions.staxfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
stax:
vars:
value1: ${{ get stax.vars.value2 }}
value2: ${{ get stax.vars.value1 }}
value1: ${{ get value2 }}
value2: ${{ get value1 }}
2 changes: 2 additions & 0 deletions tests/fixtures/duplicate_expressions.staxfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
value1: ${{ test arg1 }}
value2: ${{ test arg1 }}
25 changes: 25 additions & 0 deletions tests/unit/yamler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,29 @@ describe('YamlER', () => {

await expect(promise).rejects.toThrow('Maximum expression parsing iterations (100) exceeded. Possible circular reference in expressions.')
})

it('caches identical expressions regardless of path', async () => {
let callCount = 0
const seenExpressions = new Set()
const cachingCallback = (attributes, path, key, args) => {
// Create an expression identifier that ignores path
const expressionId = `${key}:${args.join(',')}`

if (!seenExpressions.has(expressionId)) {
callCount++
seenExpressions.add(expressionId)
}

return `result-${expressionId}`
}

const yamlWithDuplicateExpressions = resolve(fixturesDir, 'duplicate_expressions.staxfile')
const result = await loadFile(yamlWithDuplicateExpressions, cachingCallback)

// Both values should be the same since they use the same expression (key + args)
expect(result.value1).toBe('result-test:arg1')
expect(result.value2).toBe('result-test:arg1')
expect(callCount).toBe(1)
expect(seenExpressions.size).toBe(1)
})
})

0 comments on commit b7c5a06

Please sign in to comment.