Skip to content
Snippets Groups Projects
cmix_test.go 1.74 KiB
Newer Older
Jono Wenger's avatar
Jono Wenger committed
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 xx foundation                                             //
Jono Wenger's avatar
Jono Wenger committed
//                                                                            //
// Use of this source code is governed by a license that can be found in the  //
// LICENSE file.                                                              //
Jono Wenger's avatar
Jono Wenger committed
////////////////////////////////////////////////////////////////////////////////

//go:build js && wasm

Jono Wenger's avatar
Jono Wenger committed
package wasm
Jono Wenger's avatar
Jono Wenger committed

import (
Jono Wenger's avatar
Jono Wenger committed
	"reflect"
	"testing"
)

// Tests that the map representing Cmix returned by newCmixJS contains all of
// the methods on Cmix.
func Test_newCmixJS(t *testing.T) {
	cmixType := reflect.TypeOf(&Cmix{})

	cmix := newCmixJS(&bindings.Cmix{})
	if len(cmix) != cmixType.NumMethod() {
		t.Errorf("Cmix JS object does not have all methods."+
			"\nexpected: %d\nreceived: %d", cmixType.NumMethod(), len(cmix))
	}

	for i := 0; i < cmixType.NumMethod(); i++ {
		method := cmixType.Method(i)

		if _, exists := cmix[method.Name]; !exists {
			t.Errorf("Method %s does not exist.", method.Name)
		}
	}
}
Jono Wenger's avatar
Jono Wenger committed

// Tests that Cmix has all the methods that [bindings.Cmix] has.
func Test_CmixMethods(t *testing.T) {
	cmixType := reflect.TypeOf(&Cmix{})
	binCmixType := reflect.TypeOf(&bindings.Cmix{})

	if binCmixType.NumMethod() != cmixType.NumMethod() {
		t.Errorf("WASM Cmix object does not have all methods from bindings."+
			"\nexpected: %d\nreceived: %d",
			binCmixType.NumMethod(), cmixType.NumMethod())
	}

	for i := 0; i < binCmixType.NumMethod(); i++ {
		method := binCmixType.Method(i)

		if _, exists := cmixType.MethodByName(method.Name); !exists {
			t.Errorf("Method %s does not exist.", method.Name)
		}
	}
}