Skip to content

Commit 7f84939

Browse files
committed
reduce capability contract test compile boilerplate
1 parent 284c3bf commit 7f84939

1 file changed

Lines changed: 32 additions & 80 deletions

File tree

vibes/capability_contracts_test.go

Lines changed: 32 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,10 @@ func (stdlibContractLeakProbeCapability) CapabilityContracts() map[string]Capabi
482482
}
483483

484484
func TestCapabilityContractRejectsInvalidArguments(t *testing.T) {
485-
engine := MustNewEngine(Config{})
486-
script, err := engine.Compile(`def run()
485+
script := compileScriptDefault(t, `def run()
487486
probe.call("bad")
488487
end`)
489-
if err != nil {
490-
t.Fatalf("compile failed: %v", err)
491-
}
488+
var err error
492489

493490
invocations := 0
494491
_, err = script.Call(context.Background(), "run", nil, CallOptions{
@@ -506,13 +503,10 @@ end`)
506503
}
507504

508505
func TestCapabilityContractRejectsInvalidReturnValue(t *testing.T) {
509-
engine := MustNewEngine(Config{})
510-
script, err := engine.Compile(`def run()
506+
script := compileScriptDefault(t, `def run()
511507
probe.call(1)
512508
end`)
513-
if err != nil {
514-
t.Fatalf("compile failed: %v", err)
515-
}
509+
var err error
516510

517511
invocations := 0
518512
_, err = script.Call(context.Background(), "run", nil, CallOptions{
@@ -537,13 +531,10 @@ end`)
537531
}
538532

539533
func TestDuplicateCapabilityContractsFailBinding(t *testing.T) {
540-
engine := MustNewEngine(Config{})
541-
script, err := engine.Compile(`def run()
534+
script := compileScriptDefault(t, `def run()
542535
1
543536
end`)
544-
if err != nil {
545-
t.Fatalf("compile failed: %v", err)
546-
}
537+
var err error
547538

548539
_, err = script.Call(context.Background(), "run", nil, CallOptions{
549540
Capabilities: []CapabilityAdapter{
@@ -560,15 +551,12 @@ end`)
560551
}
561552

562553
func TestCapabilityContractsDoNotAttachByGlobalBuiltinName(t *testing.T) {
563-
engine := MustNewEngine(Config{})
564-
script, err := engine.Compile(`def run()
554+
script := compileScriptDefault(t, `def run()
565555
base = { a: 1 }
566556
override = { b: 2 }
567557
base.merge(override)
568558
end`)
569-
if err != nil {
570-
t.Fatalf("compile failed: %v", err)
571-
}
559+
var err error
572560

573561
result, err := script.Call(context.Background(), "run", nil, CallOptions{
574562
Capabilities: []CapabilityAdapter{unrelatedNamedContractCapability{}},
@@ -585,13 +573,10 @@ end`)
585573
}
586574

587575
func TestCapabilityContractsTraverseInstanceValues(t *testing.T) {
588-
engine := MustNewEngine(Config{})
589-
script, err := engine.Compile(`def run()
576+
script := compileScriptDefault(t, `def run()
590577
box.call("bad")
591578
end`)
592-
if err != nil {
593-
t.Fatalf("compile failed: %v", err)
594-
}
579+
var err error
595580

596581
invocations := 0
597582
_, err = script.Call(context.Background(), "run", nil, CallOptions{
@@ -611,13 +596,10 @@ end`)
611596
}
612597

613598
func TestCapabilityContractsTraverseClassValues(t *testing.T) {
614-
engine := MustNewEngine(Config{})
615-
script, err := engine.Compile(`def run()
599+
script := compileScriptDefault(t, `def run()
616600
holder.call("bad")
617601
end`)
618-
if err != nil {
619-
t.Fatalf("compile failed: %v", err)
620-
}
602+
var err error
621603

622604
invocations := 0
623605
_, err = script.Call(context.Background(), "run", nil, CallOptions{
@@ -637,14 +619,11 @@ end`)
637619
}
638620

639621
func TestCapabilityContractsBindForFactoryReturnedBuiltins(t *testing.T) {
640-
engine := MustNewEngine(Config{})
641-
script, err := engine.Compile(`def run()
622+
script := compileScriptDefault(t, `def run()
642623
worker = factory.make()
643624
worker.call("bad")
644625
end`)
645-
if err != nil {
646-
t.Fatalf("compile failed: %v", err)
647-
}
626+
var err error
648627

649628
invocations := 0
650629
_, err = script.Call(context.Background(), "run", nil, CallOptions{
@@ -664,14 +643,11 @@ end`)
664643
}
665644

666645
func TestCapabilityContractsBindAfterReceiverMutation(t *testing.T) {
667-
engine := MustNewEngine(Config{})
668-
script, err := engine.Compile(`def run()
646+
script := compileScriptDefault(t, `def run()
669647
mut.install()
670648
mut.call("bad")
671649
end`)
672-
if err != nil {
673-
t.Fatalf("compile failed: %v", err)
674-
}
650+
var err error
675651

676652
invocations := 0
677653
_, err = script.Call(context.Background(), "run", nil, CallOptions{
@@ -691,13 +667,10 @@ end`)
691667
}
692668

693669
func TestCapabilityContractsAreScopedPerAdapter(t *testing.T) {
694-
engine := MustNewEngine(Config{})
695-
script, err := engine.Compile(`def run()
670+
script := compileScriptDefault(t, `def run()
696671
foo.call("ok")
697672
end`)
698-
if err != nil {
699-
t.Fatalf("compile failed: %v", err)
700-
}
673+
var err error
701674

702675
invocations := 0
703676
result, err := script.Call(context.Background(), "run", nil, CallOptions{
@@ -718,14 +691,11 @@ end`)
718691
}
719692

720693
func TestCapabilityContractsBindAfterSiblingScopeMutation(t *testing.T) {
721-
engine := MustNewEngine(Config{})
722-
script, err := engine.Compile(`def run()
694+
script := compileScriptDefault(t, `def run()
723695
publisher.install()
724696
peer.call("bad")
725697
end`)
726-
if err != nil {
727-
t.Fatalf("compile failed: %v", err)
728-
}
698+
var err error
729699

730700
invocations := 0
731701
_, err = script.Call(context.Background(), "run", nil, CallOptions{
@@ -745,14 +715,11 @@ end`)
745715
}
746716

747717
func TestCapabilityContractsDoNotAttachToForeignBuiltinsByName(t *testing.T) {
748-
engine := MustNewEngine(Config{})
749-
script, err := engine.Compile(`def run()
718+
script := compileScriptDefault(t, `def run()
750719
publisher.install()
751720
publisher.call("ok")
752721
end`)
753-
if err != nil {
754-
t.Fatalf("compile failed: %v", err)
755-
}
722+
var err error
756723

757724
shared := &foreignBuiltinRef{}
758725
invocations := 0
@@ -774,15 +741,12 @@ end`)
774741
}
775742

776743
func TestCapabilityContractsBindAfterArgumentMutation(t *testing.T) {
777-
engine := MustNewEngine(Config{})
778-
script, err := engine.Compile(`def run()
744+
script := compileScriptDefault(t, `def run()
779745
target = {}
780746
cap.install(target)
781747
target.call("bad")
782748
end`)
783-
if err != nil {
784-
t.Fatalf("compile failed: %v", err)
785-
}
749+
var err error
786750

787751
invocations := 0
788752
_, err = script.Call(context.Background(), "run", nil, CallOptions{
@@ -802,15 +766,12 @@ end`)
802766
}
803767

804768
func TestCapabilityContractsDoNotHijackForeignBuiltinsFromArguments(t *testing.T) {
805-
engine := MustNewEngine(Config{})
806-
script, err := engine.Compile(`def run()
769+
script := compileScriptDefault(t, `def run()
807770
target = { passthrough: foreign.call }
808771
cap2.install(target)
809772
target.passthrough("ok")
810773
end`)
811-
if err != nil {
812-
t.Fatalf("compile failed: %v", err)
813-
}
774+
var err error
814775

815776
invocations := 0
816777
result, err := script.Call(context.Background(), "run", nil, CallOptions{
@@ -831,15 +792,12 @@ end`)
831792
}
832793

833794
func TestCapabilityContractsDoNotHijackReceiverStoredForeignBuiltins(t *testing.T) {
834-
engine := MustNewEngine(Config{})
835-
script, err := engine.Compile(`def run()
795+
script := compileScriptDefault(t, `def run()
836796
cap.foreign = { a: 1 }.merge
837797
cap.touch()
838798
cap.foreign({ b: 2 })
839799
end`)
840-
if err != nil {
841-
t.Fatalf("compile failed: %v", err)
842-
}
800+
var err error
843801

844802
result, err := script.Call(context.Background(), "run", nil, CallOptions{
845803
Capabilities: []CapabilityAdapter{
@@ -858,8 +816,7 @@ end`)
858816
}
859817

860818
func TestCapabilityContractsDoNotAttachToExpandedStdlibBuiltinsByName(t *testing.T) {
861-
engine := MustNewEngine(Config{})
862-
script, err := engine.Compile(`def run()
819+
script := compileScriptDefault(t, `def run()
863820
cap.touch()
864821
parsed = JSON.parse("{\"name\":\"alex\"}")
865822
{
@@ -871,9 +828,7 @@ func TestCapabilityContractsDoNotAttachToExpandedStdlibBuiltinsByName(t *testing
871828
remap_value: { name: "Alex" }.remap_keys({ name: :player_name }).fetch(:player_name)
872829
}
873830
end`)
874-
if err != nil {
875-
t.Fatalf("compile failed: %v", err)
876-
}
831+
var err error
877832

878833
result, err := script.Call(context.Background(), "run", nil, CallOptions{
879834
Capabilities: []CapabilityAdapter{
@@ -908,8 +863,7 @@ end`)
908863
}
909864

910865
func TestCapabilityContractsStayEnforcedThroughExpandedStdlibTransforms(t *testing.T) {
911-
engine := MustNewEngine(Config{})
912-
script, err := engine.Compile(`def call_through_transforms()
866+
script := compileScriptDefault(t, `def call_through_transforms()
913867
hash_handler = { handler: probe.call }.remap_keys({ handler: :run }).fetch(:run)
914868
chunk_handler = [probe.call].chunk(1).first.first
915869
{
@@ -927,9 +881,7 @@ def fail_through_chunk()
927881
handler = [probe.call].chunk(1).first.first
928882
handler("bad")
929883
end`)
930-
if err != nil {
931-
t.Fatalf("compile failed: %v", err)
932-
}
884+
var err error
933885

934886
successInvocations := 0
935887
okResult, err := script.Call(context.Background(), "call_through_transforms", nil, CallOptions{

0 commit comments

Comments
 (0)