Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert not working as expected #482

Closed
PhE opened this issue Feb 14, 2018 · 4 comments
Closed

assert not working as expected #482

PhE opened this issue Feb 14, 2018 · 4 comments
Assignees

Comments

@PhE
Copy link

PhE commented Feb 14, 2018

Here is the python code I transcrypted with transcrypt -b -n -m (version 3.6.84) :

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')

When I run test_1/test_2 it in the browser console, I get a strange behavior :

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

Why no exception are raised with assert ?

@PhE
Copy link
Author

PhE commented Feb 14, 2018

related to #23

@JdeH
Copy link
Collaborator

JdeH commented Feb 14, 2018

You need the -da switch to activate assertions:

transcrypt -b -n -m -da

I've tested:

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()

It prints:

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

Many things in Transcrypt are optional, to prevent bloat of the generated JavaScript.
Some features are controlled by command line switches:

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

Some features are (also) controlled by pragma's (compiler directives):

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

If this works for you and if you find the time please also pose this question on stackoverflow, using the [transcrypt] and [python] tags. In that way others can benefit from it as well. We're working to improve the docs and add more examples, but still it turns out some differences with CPython aren't obvious at the start.

@JdeH JdeH self-assigned this Feb 14, 2018
@PhE
Copy link
Author

PhE commented Feb 14, 2018

It works !
Thanks !
QA question created

@PhE PhE closed this as completed Feb 14, 2018
@JdeH
Copy link
Collaborator

JdeH commented Feb 14, 2018

Fine, thanks for putting the question on SO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants