github.com/metacurrency/holochain@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/ribosome_test.go (about)

     1  package holochain
     2  
     3  import (
     4  	"fmt"
     5  	. "github.com/smartystreets/goconvey/convey"
     6  	"testing"
     7  )
     8  
     9  func TestCreateRibosome(t *testing.T) {
    10  	Convey("should fail to create a ribosome based from bad ribosome type", t, func() {
    11  		_, err := CreateRibosome(nil, &Zome{RibosomeType: "foo", Code: "some code"})
    12  		So(err.Error(), ShouldEqual, "Invalid ribosome name. Must be one of: js, zygo")
    13  	})
    14  	Convey("should create a ribosome based from a good schema type", t, func() {
    15  		v, err := CreateRibosome(nil, &Zome{RibosomeType: ZygoRibosomeType, Code: `(+ 1 1)`})
    16  		z := v.(*ZygoRibosome)
    17  		So(err, ShouldBeNil)
    18  		So(fmt.Sprintf("%v", z.lastResult), ShouldEqual, "&{2 <nil>}")
    19  	})
    20  }
    21  
    22  func TestValidExposure(t *testing.T) {
    23  	Convey("public context for zome only functions should be invalid", t, func() {
    24  		fn := FunctionDef{} // zome only is default
    25  		So(fn.ValidExposure(PUBLIC_EXPOSURE), ShouldBeFalse)
    26  		So(fn.ValidExposure(ZOME_EXPOSURE), ShouldBeTrue)
    27  	})
    28  	Convey("public context for public functions should be valid", t, func() {
    29  		fn := FunctionDef{Exposure: PUBLIC_EXPOSURE}
    30  		So(fn.ValidExposure(PUBLIC_EXPOSURE), ShouldBeTrue)
    31  		So(fn.ValidExposure(ZOME_EXPOSURE), ShouldBeTrue)
    32  	})
    33  }
    34  
    35  func TestValidationFailedErr(t *testing.T) {
    36  	Convey("it should build the default validation failed err", t, func() {
    37  		So(ValidationFailed(), ShouldEqual, ValidationFailedErr)
    38  	})
    39  	Convey("it should build the custom validation failed err", t, func() {
    40  		So(ValidationFailed("just because").Error(), ShouldEqual, ValidationFailedErrMsg+": just because")
    41  	})
    42  	Convey("it should test errors", t, func() {
    43  		So(IsValidationFailedErr(ValidationFailed("just because")), ShouldBeTrue)
    44  		So(IsValidationFailedErr(ValidationFailed()), ShouldBeTrue)
    45  		So(IsValidationFailedErr(ErrHashNotFound), ShouldBeFalse)
    46  	})
    47  
    48  }