Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
},
"dependencies": {
"@algorandfoundation/algorand-typescript": "^1.0.0-beta.20",
"@algorandfoundation/puya-ts": "^1.0.0-beta.30",
"@algorandfoundation/puya-ts": "^1.0.0-beta.34",
"elliptic": "^6.5.7",
"js-sha256": "^0.11.0",
"js-sha3": "^0.9.3",
Expand Down
95 changes: 95 additions & 0 deletions tests/artifacts/switch-statements/contract.algo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import type { bytes, uint64 } from '@algorandfoundation/algorand-typescript'
import { assert, Bytes, Contract, GlobalState, Uint64 } from '@algorandfoundation/algorand-typescript'

export class DemoContract extends Contract {
run() {
assert(this.test_uint64(1) === 3)
assert(this.test_uint64(2) === 3)
assert(this.test_uint64(3) === 1)
assert(this.test_uint64(4) === 3)

assert(this.test_break(1) === 11)
assert(this.test_break(2) === 12)
assert(this.test_break(3) === 10)
assert(this.test_break(4) === 14)
assert(this.test_break(5) === 50)

assert(this.test_bytes(Bytes('hmmm')))
assert(this.test_bytes(Bytes.fromHex('ff')))
assert(this.test_bytes(Bytes.fromBase64('ZHNmc2Rmc2Q=')))
assert(this.test_bytes(Bytes.fromBase32('ONSGMZ3OMJTGOZDGMRSGM===')))
assert(!this.test_bytes(Bytes()))
}

private test_uint64(x: uint64): uint64 {
switch (x) {
case 1:
case 2:
case Uint64(4):
return 3
default: {
return 1
}
}
}

private test_break(x: uint64): uint64 {
let i: uint64 = 10
switch (x) {
case 1:
case 2:
case Uint64(4):
i += x
break
case 5:
i *= x
}
return i
}

private test_bytes(x: bytes): boolean {
switch (x) {
case Bytes('hmmm'):
case Bytes.fromHex('Ff'):
case Bytes.fromBase64('ZHNmc2Rmc2Q='):
case Bytes.fromBase32('ONSGMZ3OMJTGOZDGMRSGM==='):
return true
}
return false
}

evalCount = GlobalState<uint64>()

private increaseEvalAndReturn(n: uint64) {
this.evalCount.value++
return n
}

public test_side_effects(n: uint64) {
this.evalCount.value = 0

switch (n) {
case this.increaseEvalAndReturn(n - 1):
break
case this.increaseEvalAndReturn(n):
break
case this.increaseEvalAndReturn(n + 1):
break
}

assert(this.evalCount.value === 2, 'Only two functions should be evaluated')
}

public test_non_trivial_termination_of_clause(n: uint64, y: uint64): uint64 {
switch (n) {
case 1:
if (y % 2 === 0) {
return y
} else {
return n
}
default:
return y * n
}
}
}
17 changes: 17 additions & 0 deletions tests/switch-statements.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { beforeAll, describe } from 'vitest'
import { createArc4TestFixture } from './test-fixture'

describe('switch statements', () => {
const [test, localnetFixture] = createArc4TestFixture('tests/artifacts/switch-statements/contract.algo.ts', { DemoContract: {} })
beforeAll(async () => {
await localnetFixture.newScope()
})

test('runs', async ({ appClientDemoContract }) => {
await appClientDemoContract.send.call({ method: 'run', args: [] })
})

test('test_side_effects', async ({ appClientDemoContract }) => {
await appClientDemoContract.send.call({ method: 'test_side_effects', args: [5] })
})
})
49 changes: 25 additions & 24 deletions tests/test-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { AppFactory, AppFactoryDeployParams } from '@algorandfoundation/alg
import type { AssetCreateParams } from '@algorandfoundation/algokit-utils/types/composer'
import { nullLogger } from '@algorandfoundation/algokit-utils/types/logging'
import type { AlgorandFixture } from '@algorandfoundation/algokit-utils/types/testing'
import { compile, LoggingContext } from '@algorandfoundation/puya-ts'
import { compile, CompileOptions, LoggingContext, processInputPaths } from '@algorandfoundation/puya-ts'
import type { Use } from '@vitest/runner/types'
import { OnApplicationComplete } from 'algosdk'
import fs from 'fs'
Expand Down Expand Up @@ -237,32 +237,33 @@ async function compilePath(
const logCtx = LoggingContext.create()

return await logCtx.run(async () => {
await compile({
outputAwstJson: false,
outputAwst: false,
paths: [path],
outDir: tempDir.dirPath,
dryRun: false,
logLevel: 'error' as Parameters<typeof compile>[0]['logLevel'],
skipVersionCheck: true,
await compile(
new CompileOptions({
outputAwstJson: false,
outputAwst: false,
filePaths: processInputPaths({ paths: [path], outDir: tempDir.dirPath }),
dryRun: false,
logLevel: 'error' as Parameters<typeof compile>[0]['logLevel'],
skipVersionCheck: true,

outputSsaIr: false,
outputOptimizationIr: false,
outputDestructuredIr: false,
outputMemoryIr: false,
outputSsaIr: false,
outputOptimizationIr: false,
outputDestructuredIr: false,
outputMemoryIr: false,

debugLevel: 1,
targetAvmVersion: 10,
cliTemplateDefinitions: {},
templateVarsPrefix: 'TMPL_',
localsCoalescingStrategy: 'root_operand' as Parameters<typeof compile>[0]['localsCoalescingStrategy'],
debugLevel: 1,
targetAvmVersion: 10,
cliTemplateDefinitions: {},
templateVarsPrefix: 'TMPL_',
localsCoalescingStrategy: 'root_operand' as Parameters<typeof compile>[0]['localsCoalescingStrategy'],

outputArc32: false,
outputTeal: false,
outputSourceMap: true,
optimizationLevel: 0,
...options,
})
outputArc32: false,
outputTeal: false,
outputSourceMap: true,
optimizationLevel: 0,
...options,
}),
)
for (const log of logCtx.logEvents) {
switch (log.level) {
case 'error':
Expand Down
Loading