aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-02-01 21:29:10 +0000
committers-ol <s-ol@users.noreply.github.com>2020-02-01 21:29:10 +0000
commitf4b5543b33de9ce6e5c3a1bcc65db36eb3eb5b03 (patch)
tree636cbcbbcd8be60d39cba2e4cf04b4edc7011233 /spec
parentfix README typo (diff)
downloadalive-f4b5543b33de9ce6e5c3a1bcc65db36eb3eb5b03.tar.gz
alive-f4b5543b33de9ce6e5c3a1bcc65db36eb3eb5b03.zip
first-class scopes
Diffstat (limited to 'spec')
-rw-r--r--spec/ast_spec.moon11
-rw-r--r--spec/scope_spec.moon104
2 files changed, 111 insertions, 4 deletions
diff --git a/spec/ast_spec.moon b/spec/ast_spec.moon
index 6e5ca32..5c1e1a7 100644
--- a/spec/ast_spec.moon
+++ b/spec/ast_spec.moon
@@ -1,4 +1,5 @@
import Atom, Xpr from require 'ast'
+import Scope from require 'scope'
describe 'Atom', ->
expand = (typ, str, ...) ->
@@ -8,11 +9,13 @@ describe 'Atom', ->
describe 'sym', ->
it 'expand correctly', ->
- env = { a: 1, b: 2, c: 44, 'long_name': 'str',
- 'name/with/slash': {} }
+ values = { a: 1, b: 2, c: 44, 'long_name': 'str',
+ 'name/with/slash': 3 }
- for k,v in pairs env
- assert.is.equal v, expand 'sym', k, env
+ scope = Scope.from_table values
+
+ for k,v in pairs values
+ assert.is.equal v, expand 'sym', k, scope
describe 'num', ->
it 'expand correctly', ->
diff --git a/spec/scope_spec.moon b/spec/scope_spec.moon
new file mode 100644
index 0000000..f00763d
--- /dev/null
+++ b/spec/scope_spec.moon
@@ -0,0 +1,104 @@
+import Op, Const from require 'base'
+import Scope from require 'scope'
+
+class TestOp extends Op
+ new: (...) => super ...
+
+describe 'Scope', ->
+ describe 'constifies', ->
+ scope = Scope!
+
+ test 'numbers', ->
+ scope\set_raw 'num', 3
+
+ got = scope\get 'num'
+ assert.is.equal 'num', got.type
+ assert.is.equal 3, got.value
+
+ test 'strings', ->
+ scope\set_raw 'str', "im a happy string"
+
+ got = scope\get 'str'
+ assert.is.equal 'str', got.type
+ assert.is.equal "im a happy string", got.value
+
+ test 'Consts', ->
+ pi = Const 'num', 3.14
+ scope\set_raw 'pi', pi
+
+ assert.is.equal pi, scope\get 'pi'
+
+ test 'Opdefs', ->
+ scope\set_raw 'test', TestOp
+
+ got = scope\get 'test'
+ assert.is.equal 'opdef', got.type
+ assert.is.equal TestOp, got.value
+
+ test 'tables', ->
+ pi = Const 'num', 3.14
+ scope\set_raw 'math', { :pi }
+
+ math = scope\get 'math'
+ assert.is.equal Scope, math.__class
+ assert.is.equal pi, math\get 'pi'
+ assert.is.equal pi, scope\get 'math/pi'
+
+ it 'constifies in from_table', ->
+ pi = Const 'num', 3.14
+ scope = Scope.from_table {
+ num: 3
+ str: "im a happy string"
+ :pi
+ math: :pi
+ test: TestOp
+ }
+
+ got = scope\get 'num'
+ assert.is.equal 'num', got.type
+ assert.is.equal 3, got.value
+
+ got = scope\get 'str'
+ assert.is.equal 'str', got.type
+ assert.is.equal "im a happy string", got.value
+
+ assert.is.equal pi, scope\get 'pi'
+
+ got = scope\get 'test'
+ assert.is.equal 'opdef', got.type
+ assert.is.equal TestOp, got.value
+
+ assert.is.equal Scope, (scope\get 'math').__class
+ assert.is.equal pi, (scope\get 'math')\get 'pi'
+ assert.is.equal pi, scope\get 'math/pi'
+
+ it 'gets from nested scopes', ->
+ root = Scope!
+ a = Scope!
+ b = Scope!
+
+ pi = Const 'num', 3.14
+ b\set 'test', pi
+ a\set 'child', b
+ root\set 'deep', a
+
+ assert.is.equal pi, root\get 'deep/child/test'
+
+ describe 'inheritance', ->
+ root = Scope!
+ root\set_raw 'hidden', 1234
+ root\set_raw 'inherited', "inherited string"
+
+ scope = Scope nil, root
+
+ it 'allows access', ->
+ got = scope\get 'inherited'
+ assert.is.equal 'str', got.type
+ assert.is.equal "inherited string", got.value
+
+ it 'can keep defs', ->
+ scope\set_raw 'hidden', "overwritten"
+
+ got = scope\get 'hidden'
+ assert.is.equal 'str', got.type
+ assert.is.equal "overwritten", got.value