声称没有按预期工作

时间:2018-02-14 18:35:24

标签: python transcrypt

这是我使用 transcrypt -b -n -m (版本3.6.84)进行传输的python代码:

def test_1():
    console.log('before assert')
    assert False, "False fails"
    console.log('we should not see that if assert fails as expected')


def test_2():
    console.log('before assert')
    try:
        assert False, "False fails"
    except AssertionError as exception:
        console.log('we should see that since we catch the assertion error')
    console.log('after assert')

当我在浏览器控制台中运行test_1 / test_2时,我得到了一个奇怪的行为:

> mymodule.test_1()
before assert
we should not see that if assert fails as expected
<- undefined
> mymodule.test_2()
before assert
after assert
<- undefined

为什么没有使用assert引发异常?

1 个答案:

答案 0 :(得分:1)

来自issue 482的答案:

您需要-da开关来激活断言:

  

transcrypt -b -n -m -da

我测试过了:

def test_1():
    console.log('before assert')
    assert False, "False fails"
    console.log('we should not see that if assert fails as expected')

def test_2():
    console.log('before assert')
    try:
        assert False, "False fails"
    except AssertionError as exception:
        console.log('we should see that since we catch the assertion error')
    console.log('after assert')

try:    
    test_1()
except AssertionError as exception:
    console.log('we should see this')

test_2()

打印:

before assert
we should see this
before assert
we should see that since we catch the assertion error
after assert

Transcrypt中的许多内容都是可选的,以防止生成的JavaScript膨胀。 某些功能由命令行开关控制:

http://www.transcrypt.org/docs/html/installation_use.html#available-command-line-switches

某些功能(也)由pragma(编译器指令)控制:

http://www.transcrypt.org/docs/html/special_facilities.html#the-pragma-mechanism