Giter Site home page Giter Site logo

yusuf8ahmed / wasmite Goto Github PK

View Code? Open in Web Editor NEW
22.0 2.0 1.0 76 KB

Now WebAssembly has proper testing, unit-testing and debugging ๐Ÿค—

License: Apache License 2.0

Python 100.00%
wasm webassemby unit-testing testing-tools test-automation wasmer wasm-bytecode debug debugging-tool debugger

wasmite's Introduction

What is the Wasmite project

Since WebAssembly is the future of the web. I decide to create Wasmite, a python package for unit-testing your wasm or wat code. Wasmite is based on wasmer and the python standard library package unittest. Documentation for can be found here: documentation for unittest and documentation for wasmer

This project was formerly an extension of my Rust/Python Web framework Wasp, so some section of the code may refer to it's earlier name (Native)

Wasmite looks for tests in python files whose names start with test_*.py and runs every test_* function it discovers. The testing folder has more examples.

Having any problems or questions create a issue, i will be happy to help :)

Installation

This project requires python 3 and doesn't support 3.9

pip install wasmite

Project Goals:

  • Import wasm or wat module successfully
  • Access functions within module
  • Type checking of parameters and the result of functions
  • Release to PyPi for public to use
  • Allow Wasmite ...
    • Export Python functions
    • Export Global Instances
    • Export Memory Instances
  • More complex examples in testing folder
  • Receive community on how to improve

Examples:

from wasmite import WasmiteCase, WasmModule
from wasmite import FunctionTypes, Function, Global, Value, main
from wasmite import I32

def sum(x: int, y: int) -> int:
    """ python function to be imported into WASM  """
    return x + y

class Test(WasmiteCase):
    # create a variable the hold all the functions from a specific wasm file.
    module = WasmModule("test_wasm.wasm")
    # import python function into WASM 
    # type annotations on the function is necessary 
    module.register("math", {
        "sum": Function(module.store, sum),
        "seven": Global(module.store, Value.i32(7), mutable=True)
    })
    # start up the module and return the exports (this is mandatory)
    exports = module.get_exports()
    
    def test_add(self):
        # test add function
        result = self.exports.add(1,2)
        self.assertEqual(result, 3) 
        
    def test_sub(self):
        # test the sub function
        result = self.exports.sub(2,2)
        self.assertEqual(result, 0)

    def test_args_add(self):
        # check the types for results and parameter of the function "add"
        # param is I32, I32 and result is I32
        add_function = self.exports.add
        self.assertTypes(add_function, FunctionTypes([I32, I32], [I32])) # result will fail
        
    def test_import_sum(self):
        # test the imported python function sum.
        sum_function = self.exports.addsum(5,2)
        self.assertEqual(sum_function, 7)
        
    def test_global_read(self):
        # test reading value of global
        read_seven = self.exports.read_global()
        self.assertEqual(read_seven, 7) 
        
    def test_global_write(self):
        # test writing value of global
        self.exports.write_global(5)
        read_seven = self.exports.read_global()
        self.assertEqual(read_seven, 5) 
        
# Hi don't forget to add me         
if __name__ == "__main__":
    main()

-->

Then you can then run this test like so:

# make sure you are in examples/wasm
$ python test_wasm.py

test_add (__main__.Test) ... ok
test_args_add (__main__.Test) ... ok
test_global_read (__main__.Test) ... ok
test_global_write (__main__.Test) ... ok
test_import_sum (__main__.Test) ... ok
test_sub (__main__.Test) ... ok

----------------------------------------------------------------------
Ran 6 tests in 0.001s

OK

wasmite's People

Contributors

yusuf8ahmed avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rhhersi

wasmite's Issues

Test case assert API is non-intuitive

I have a few notes/suggestions on this

  1. assertTypes and types are used specifically for arguments, but their name does not communicate this.
  2. I'm not sure why types and result are necessary, I generally trust people to construct lists on their own.
  3. I think there would be value offering a FuncType typed NamedTuple class and offering a combined assertFuncType that compares this value to the params and result of the provided value.
    e.g.
class FuncType(NamedTuple):
    args: List[Type]
    result: Type
...
class WasmiteCase(unittest.TestCase):
    ...
    def assertFuncType(self, func, func_type):
        assert func.type.params == func_type.params
        assert func.type.results == func_type.result
class Test(wasmite.TestWasm):
    ...
    def test_args_add(self):
        # check that the param types of the function "add" is I32, I32
        add_function = self.exports.add
        self.assertFuncType(add_function, FuncType([I32, I32], I32))

Error when load wasm file

Hi Yusuf,

I generate a wasm file using emscripten with option ENVIRONMENT=web
I try to use your solution to run UT, when I load the model, I have this message

None
Traceback (most recent call last):
File "main.py", line 30, in
class Test(WasmiteCase):
File "main.py", line 32, in Test
module = WasiModule(fileWasm)
File "/home/build/.local/lib/python3.8/site-packages/wasmite/globals.py", line 33, in init
self._import_module()
File "/home/build/.local/lib/python3.8/site-packages/wasmite/wasi.py", line 20, in _import_module
self.import_object = wasi_env.generate_import_object(self.store, wasi_version)
TypeError: argument 'wasi_version': 'NoneType' object cannot be interpreted as an integer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.