From 9f6544e2d5a7ad153a03ee36254dccf4900377d2 Mon Sep 17 00:00:00 2001 From: s-ol Date: Wed, 19 Aug 2020 15:32:59 +0200 Subject: add TestPilot:eval_once --- spec/lang/do_spec.moon | 28 ++++++++++++---------------- spec/lang/fn_spec.moon | 22 ++++++++-------------- spec/lang/literals_spec.moon | 6 +++--- spec/test_setup.moon | 5 +++++ 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/spec/lang/do_spec.moon b/spec/lang/do_spec.moon index eecc326..67b7b33 100644 --- a/spec/lang/do_spec.moon +++ b/spec/lang/do_spec.moon @@ -5,29 +5,25 @@ describe "do", -> COPILOT = TestPilot '' it "can be empty", -> - COPILOT.active_module\spit '(do)' - COPILOT\tick! - assert.is.true COPILOT.active_module.root\is_const! - assert.is.nil COPILOT.active_module.root.result + rt = COPILOT\eval_once '(do)' + assert.is.true rt\is_const! + assert.is.nil rt.result it "returns the last result, if any", -> - COPILOT.active_module\spit '(do 1 2 3)' - COPILOT\tick! - assert.is.true COPILOT.active_module.root\is_const! - assert.is.equal (Constant.num 3), COPILOT.active_module.root.result + rt = COPILOT\eval_once '(do 1 2 3)' + assert.is.true rt\is_const! + assert.is.equal (Constant.num 3), rt.result - COPILOT.active_module\spit '(do 1 2 (trace 3))' - COPILOT\tick! - assert.is.true COPILOT.active_module.root\is_const! - assert.is.nil COPILOT.active_module.root.result + rt = COPILOT\eval_once '(do 1 2 (trace 3))' + assert.is.true rt\is_const! + assert.is.nil rt.result it "passes through side-effects", -> - COPILOT.active_module\spit ' + rt = COPILOT\eval_once ' (import* time) (do (every 0.5 "bang! side-effect") 3)' - COPILOT\tick! - assert.is.false COPILOT.active_module.root\is_const! - assert.is.equal (Constant.num 3), COPILOT.active_module.root.result + assert.is.false rt\is_const! + assert.is.equal (Constant.num 3), rt.result diff --git a/spec/lang/fn_spec.moon b/spec/lang/fn_spec.moon index a95eac1..3686c91 100644 --- a/spec/lang/fn_spec.moon +++ b/spec/lang/fn_spec.moon @@ -5,53 +5,47 @@ describe "function", -> COPILOT = TestPilot '' it "returns constant results when constant", -> - COPILOT.active_module\spit ' + rt = COPILOT\eval_once ' (import* math) (defn my-plus (a b) (+ a b)) (my-plus 2 3)' - COPILOT\tick! - assert.is.true COPILOT.active_module.root\is_const! - result = assert COPILOT.active_module.root.result - assert.is.equal (Constant.num 5), result + assert.is.true rt\is_const! + assert.is.equal (Constant.num 5), rt.result it "checks argument arity when invoked", -> - COPILOT.active_module\spit ' + err = assert.has.error -> COPILOT\eval_once ' ([1]import* math) ([2]defn my-plus (a b) ([4]+ a b)) ([3]my-plus 2)' - err = assert.has.error COPILOT\tick assert.matches "argument error: expected 2 arguments, found 1", err assert.matches "while invoking function 'my%-plus' at %[3%]", err - COPILOT.active_module\spit ' + err = assert.has.error -> COPILOT\eval_once ' ([1]import* math) ([2]defn my-plus (a b) ([4]+ a b)) ([3]my-plus 2 3 4)' - err = assert.has.error COPILOT\tick assert.matches "argument error: expected 2 arguments, found 3", err assert.matches "while invoking function 'my%-plus' at %[3%]", err it "can be anonymously invoked", -> - COPILOT.active_module\spit ' + rt = COPILOT\eval_once ' ([1] ([2]fn (a b) b) 3 4)' - COPILOT\tick! - assert.is.equal (Constant.num 4), COPILOT.active_module.root\const! + assert.is.equal (Constant.num 4), rt\const! - COPILOT.active_module\spit ' + err = assert.has.error -> COPILOT\eval_once ' ([1] ([2]fn (a b) b) 3)' - err = assert.has.error COPILOT\tick assert.matches "argument error: expected 2 arguments, found 1", err assert.matches "while invoking function %(unnamed%) at %[1%]", err diff --git a/spec/lang/literals_spec.moon b/spec/lang/literals_spec.moon index 44d0d7d..3db010d 100644 --- a/spec/lang/literals_spec.moon +++ b/spec/lang/literals_spec.moon @@ -2,7 +2,7 @@ import TestPilot from require 'spec.test_setup' import T, Struct, Array, Constant from require 'alv' describe "literal", -> - test = TestPilot ' + TestPilot ' (def str "hello" num 2 bool true @@ -10,8 +10,8 @@ describe "literal", -> sqre ([7]array 1 2 3 4)) (export*)' - assert.is.true test.active_module.root\is_const! - scope = (assert test.active_module.root.result)\unwrap T.scope + assert.is.true COPILOT.active_module.root\is_const! + scope = (assert COPILOT.active_module.root.result)\unwrap T.scope it "string is parsed and returned correctly", -> assert.is.equal (Constant.str 'hello'), (scope\get 'str')\const! diff --git a/spec/test_setup.moon b/spec/test_setup.moon index 965b2e7..2bcd9df 100644 --- a/spec/test_setup.moon +++ b/spec/test_setup.moon @@ -32,6 +32,11 @@ class TestPilot extends Copilot end_eval: => @active_module.registry\end_eval! next_tick: => @T += 1 + eval_once: (code) => + @active_module\spit code + @tick! + @active_module.root + --- poll for changes and tick. tick: => return unless @last_modules.__root -- cgit v1.2.3