diff options
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/cell_spec.moon | 18 | ||||
| -rw-r--r-- | spec/const_spec.moon | 122 | ||||
| -rw-r--r-- | spec/parsing_spec.moon | 30 | ||||
| -rw-r--r-- | spec/scope_spec.moon | 48 | ||||
| -rw-r--r-- | spec/value_spec.moon | 112 |
5 files changed, 160 insertions, 170 deletions
diff --git a/spec/cell_spec.moon b/spec/cell_spec.moon index b31ef99..fcf9861 100644 --- a/spec/cell_spec.moon +++ b/spec/cell_spec.moon @@ -1,31 +1,31 @@ -import Cell, RootCell, Const, Scope, globals from require 'core' +import Cell, RootCell, Value, Scope, globals from require 'core' import Registry from require 'core.registry' import Logger from require 'logger' Logger.init 'silent' -hello_world = Cell nil, { (Const.sym 'hello'), (Const.str 'world') } -two_plus_two = Cell nil, { (Const.sym '+'), (Const.num 2), (Const.num 2) } +hello_world = Cell nil, { (Value.sym 'hello'), (Value.str 'world') } +two_plus_two = Cell nil, { (Value.sym '+'), (Value.num 2), (Value.num 2) } describe 'Cell', -> it 'supports quoting', -> with hello_world\quote! assert.is.equal Cell, .__class - assert.is.equal (Const.sym 'hello'), \head! - assert.is.same { Const.str 'world' }, \tail! + assert.is.equal (Value.sym 'hello'), \head! + assert.is.same { Value.str 'world' }, \tail! with two_plus_two\quote! assert.is.equal Cell, .__class - assert.is.equal (Const.sym '+'), \head! - assert.is.same { (Const.num 2), (Const.num 2) }, \tail! + assert.is.equal (Value.sym '+'), \head! + assert.is.same { (Value.num 2), (Value.num 2) }, \tail! describe 'RootCell', -> test 'head is always "do"', -> cell = RootCell\parse {} - assert.is.equal (Const.sym 'do'), cell\head! + assert.is.equal (Value.sym 'do'), cell\head! cell = RootCell nil, { hello_world, two_plus_two } - assert.is.equal (Const.sym 'do'), cell\head! + assert.is.equal (Value.sym 'do'), cell\head! test 'tail is all children', -> cell = RootCell\parse {} diff --git a/spec/const_spec.moon b/spec/const_spec.moon deleted file mode 100644 index 4def3a6..0000000 --- a/spec/const_spec.moon +++ /dev/null @@ -1,122 +0,0 @@ -import Const, Op, Action, Scope from require 'core' -import Logger from require 'logger' -Logger.init 'silent' - -class TestOp extends Op - new: (...) => super ... - -class TestAction extends Action - new: (...) => - -describe 'Const', -> - describe 'wraps', -> - test 'numbers', -> - got = Const.wrap 3 - assert.is.equal 'num', got.type - assert.is.equal 3, got.value - - test 'strings', -> - got = Const.wrap "im a happy string" - assert.is.equal 'str', got.type - assert.is.equal "im a happy string", got.value - - test 'Consts', -> - pi = Const 'num', 3.14 - got = Const.wrap pi - - assert.is.equal pi, got - - test 'Opdefs', -> - got = Const.wrap TestOp - - assert.is.equal 'opdef', got.type - assert.is.equal TestOp, got.value - - test 'Bultins', -> - got = Const.wrap TestAction - - assert.is.equal 'builtin', got.type - assert.is.equal TestAction, got.value - - test 'Scopes', -> - sub = Scope! - got = Const.wrap sub - - assert.is.equal 'scope', got.type - assert.is.equal sub, got.value - - test 'tables', -> - pi = Const 'num', 3.14 - got = Const.wrap { :pi } - - assert.is.equal 'scope', got.type - assert.is.equal pi, got.value\get 'pi' - - describe 'unwraps', -> - test 'get!, getc!', -> - assert.is.equal 3.14, (Const.num 3.14)\getc! - assert.is.equal 'hi', (Const.str 'hi')\getc! - assert.is.equal 'hi', (Const.sym 'hi')\getc! - - assert.is.equal 3.14, (Const.num 3.14)\get! - assert.is.equal 'hi', (Const.str 'hi')\get! - assert.is.equal 'hi', (Const.sym 'hi')\get! - - test 'with type assert', -> - assert.is.equal 3.14, (Const.num 3.14)\getc 'num' - assert.is.equal 'hi', (Const.str 'hi')\getc 'str' - assert.is.equal 'hi', (Const.sym 'hi')\getc 'sym' - assert.has_error -> (Const.num 3.14)\getc 'sym' - assert.has_error -> (Const.str 'hi')\getc 'num' - assert.has_error -> (Const.sym 'hi')\getc 'str' - - assert.is.equal 3.14, (Const.num 3.14)\get 'num' - assert.is.equal 'hi', (Const.str 'hi')\get 'str' - assert.is.equal 'hi', (Const.sym 'hi')\get 'sym' - assert.has_error -> (Const.num 3.14)\get 'sym' - assert.has_error -> (Const.str 'hi')\get 'num' - assert.has_error -> (Const.sym 'hi')\get 'str' - - describe 'checks equality', -> - test 'using the type', -> - val = Const 'num', 3 - assert.is.equal (Const.num 3), val - assert.not.equal (Const.str '3'), val - - val = Const 'str', 'hello' - assert.is.equal (Const.str 'hello'), val - assert.not.equal (Const.sym 'hello'), val - - test 'using the value', -> - val = Const 'num', 3 - assert.is.equal (Const.num 3), val - assert.not.equal (Const.num 4), val - - describe 'evaluates literal', -> - test 'constants to themselves', -> - assert_noop = (val) -> assert.is.equal val, val\eval! - - assert_noop Const.num 2 - assert_noop Const.str 'hello' - - test 'symbols in the scope', -> - scope = with Scope! - \set 'number', Const.num 3 - \set 'hello', Const.str "world" - \set 'goodbye', Const.sym "again" - - assert_eval = (sym, val) -> - const = Const.sym sym - assert.is.equal val, const\eval scope - - assert_eval 'number', Const.num 3 - assert_eval 'hello', Const.str "world" - assert_eval 'goodbye', Const.sym "again" - - describe 'quotes literals', -> - test 'as themselves', -> - assert_noop = (val) -> assert.is.equal val, val\quote! - - assert_noop Const.num 2 - assert_noop Const.str 'hello' - assert_noop Const.sym 'world' diff --git a/spec/parsing_spec.moon b/spec/parsing_spec.moon index 2e7da7f..ddf760f 100644 --- a/spec/parsing_spec.moon +++ b/spec/parsing_spec.moon @@ -1,5 +1,5 @@ import space, atom, expr, explist, cell, program, comment from require 'core.parsing' -import Const from require 'core' +import Value from require 'core' import Logger from require 'logger' Logger.init 'silent' @@ -16,48 +16,48 @@ describe 'atom parsing', -> test 'symbols', -> sym = verify_parse_nope atom, 'some-toast nope' assert.is.equal 'sym', sym.type - assert.is.equal 'some-toast', sym\getc! + assert.is.equal 'some-toast', sym\unwrap! assert.is.equal 'some-toast', sym\stringify! describe 'numbers', -> it 'parses ints', -> num = verify_parse_nope atom, '1234 nope' assert.is.equal 'num', num.type - assert.is.equal 1234, num\getc! + assert.is.equal 1234, num\unwrap! assert.is.equal '1234', num\stringify! it 'parses floats', -> num = verify_parse_nope atom, '0.123 nope' assert.is.equal 'num', num.type - assert.is.equal 0.123, num\getc! + assert.is.equal 0.123, num\unwrap! num = verify_parse_nope atom, '.123 nope' assert.is.equal 'num', num.type - assert.is.equal 0.123, num\getc! + assert.is.equal 0.123, num\unwrap! num = verify_parse_nope atom, '0. nope' assert.is.equal 'num', num.type - assert.is.equal 0, num\getc! + assert.is.equal 0, num\unwrap! describe 'strings', -> it 'parses double-quote strings', -> str = verify_parse_nope atom, '"help some stuff!" nope' assert.is.equal 'str', str.type - assert.is.equal 'help some stuff!', str\getc! + assert.is.equal 'help some stuff!', str\unwrap! it 'parses single-quote strings', -> str = verify_parse_nope atom, "'help some stuff!' nope" assert.is.equal 'str', str.type - assert.is.equal "help some stuff!", str\getc! + assert.is.equal "help some stuff!", str\unwrap! it 'handles escapes', -> str = verify_parse_nope atom, '"string with \\"quote\\"s and \\\\" nope' assert.is.equal 'str', str.type - assert.is.equal 'string with \"quote\"s and \\', str\getc! + assert.is.equal 'string with \"quote\"s and \\', str\unwrap! str = verify_parse_nope atom, "'string with \\'quote\\'s and \\\\' nope" assert.is.equal 'str', str.type - assert.is.equal "string with \'quote\'s and \\", str\getc! + assert.is.equal "string with \'quote\'s and \\", str\unwrap! describe 'Cell', -> test 'basic parsing', -> @@ -65,9 +65,9 @@ describe 'Cell', -> "friend" )' assert.is.equal 3, #node.children - assert.is.equal (Const.num 3), node.children[1] - assert.is.equal (Const.sym 'ok-yes'), node.children[2] - assert.is.equal (Const.str 'friend'), node.children[3] + assert.is.equal (Value.num 3), node.children[1] + assert.is.equal (Value.sym 'ok-yes'), node.children[2] + assert.is.equal (Value.str 'friend'), node.children[3] test 'tag parsing', -> node = verify_parse cell, '([42]tagged 2)' @@ -88,8 +88,8 @@ describe 'RootCell parsing', -> node = verify_parse program, str assert.is.equal 2, #node.children - assert.is.equal (Const.num 3), node.children[1] - assert.is.equal (Const.sym 'ok-yes'), node.children[2] + assert.is.equal (Value.num 3), node.children[1] + assert.is.equal (Value.sym 'ok-yes'), node.children[2] it 'at the front of the string', -> verify ' 3\tok-yes' diff --git a/spec/scope_spec.moon b/spec/scope_spec.moon index 9e7ab80..471bbce 100644 --- a/spec/scope_spec.moon +++ b/spec/scope_spec.moon @@ -1,4 +1,4 @@ -import Scope, Const, Op from require 'core' +import Scope, Value, Op from require 'core' import Logger from require 'logger' Logger.init 'silent' @@ -12,27 +12,27 @@ describe 'Scope', -> test 'numbers', -> scope\set_raw 'num', 3 - got = scope\get 'num' + got = (scope\get 'num')\const! 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' + got = (scope\get 'str')\const! assert.is.equal 'str', got.type assert.is.equal "im a happy string", got.value - test 'Consts', -> - pi = Const 'num', 3.14 + test 'Values', -> + pi = Value 'num', 3.14 scope\set_raw 'pi', pi - assert.is.equal pi, scope\get 'pi' + assert.is.equal pi, (scope\get 'pi')\const! test 'Opdefs', -> scope\set_raw 'test', TestOp - got = scope\get 'test' + got = (scope\get 'test')\const! assert.is.equal 'opdef', got.type assert.is.equal TestOp, got.value @@ -40,22 +40,22 @@ describe 'Scope', -> sub = Scope! scope\set_raw 'sub', sub - got = scope\get 'sub' + got = (scope\get 'sub')\const! assert.is.equal 'scope', got.type assert.is.equal sub, got.value test 'tables', -> - pi = Const 'num', 3.14 + pi = Value 'num', 3.14 scope\set_raw 'math', { :pi } - got = scope\get 'math' + got = (scope\get 'math')\const! assert.is.equal 'scope', got.type assert.is.equal Scope, got.value.__class - assert.is.equal pi, got.value\get 'pi' - assert.is.equal pi, scope\get 'math/pi' + assert.is.equal pi, (got.value\get 'pi')\const! + assert.is.equal pi, (scope\get 'math/pi')\const! - it 'constifies in from_table', -> - pi = Const 'num', 3.14 + it 'wraps Values in from_table', -> + pi = Value 'num', 3.14 scope = Scope.from_table { num: 3 str: "im a happy string" @@ -64,35 +64,35 @@ describe 'Scope', -> test: TestOp } - got = scope\get 'num' + got = (scope\get 'num')\const! assert.is.equal 'num', got.type assert.is.equal 3, got.value - got = scope\get 'str' + got = (scope\get 'str')\const! assert.is.equal 'str', got.type assert.is.equal "im a happy string", got.value - assert.is.equal pi, scope\get 'pi' + assert.is.equal pi, (scope\get 'pi')\const! - got = scope\get 'test' + got = (scope\get 'test')\const! assert.is.equal 'opdef', got.type assert.is.equal TestOp, got.value - got = scope\get 'math' + got = (scope\get 'math')\const! assert.is.equal 'scope', got.type - assert.is.equal pi, scope\get 'math/pi' + assert.is.equal pi, (scope\get 'math/pi')\const! it 'gets from nested scopes', -> root = Scope! a = Scope! b = Scope! - pi = Const 'num', 3.14 + pi = Value 'num', 3.14 b\set_raw 'test', pi a\set_raw 'child', b root\set_raw 'deep', a - assert.is.equal pi, root\get 'deep/child/test' + assert.is.equal pi, (root\get 'deep/child/test')\const! describe 'inheritance', -> root = Scope! @@ -102,13 +102,13 @@ describe 'Scope', -> scope = Scope nil, root it 'allows access', -> - got = scope\get 'inherited' + got = (scope\get 'inherited')\const! 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' + got = (scope\get 'hidden')\const! assert.is.equal 'str', got.type assert.is.equal "overwritten", got.value diff --git a/spec/value_spec.moon b/spec/value_spec.moon new file mode 100644 index 0000000..bc68c5b --- /dev/null +++ b/spec/value_spec.moon @@ -0,0 +1,112 @@ +import Value, Result, Op, Action, Scope from require 'core' +import Logger from require 'logger' +Logger.init 'silent' + +class TestOp extends Op + new: (...) => super ... + +class TestAction extends Action + new: (...) => + +describe 'Value', -> + describe 'wraps', -> + test 'numbers', -> + got = Value.wrap 3 + assert.is.equal 'num', got.type + assert.is.equal 3, got.value + + test 'strings', -> + got = Value.wrap "im a happy string" + assert.is.equal 'str', got.type + assert.is.equal "im a happy string", got.value + + test 'Values', -> + pi = Value 'num', 3.14 + got = Value.wrap pi + + assert.is.equal pi, got + + test 'Opdefs', -> + got = Value.wrap TestOp + + assert.is.equal 'opdef', got.type + assert.is.equal TestOp, got.value + + test 'Bultins', -> + got = Value.wrap TestAction + + assert.is.equal 'builtin', got.type + assert.is.equal TestAction, got.value + + test 'Scopes', -> + sub = Scope! + got = Value.wrap sub + + assert.is.equal 'scope', got.type + assert.is.equal sub, got.value + + test 'tables', -> + pi = Value 'num', 3.14 + got = Value.wrap { :pi } + + assert.is.equal 'scope', got.type + assert.is.equal pi, (got.value\get 'pi')\const! + + describe 'unwraps', -> + test 'unwrap!', -> + assert.is.equal 3.14, (Value.num 3.14)\unwrap! + assert.is.equal 'hi', (Value.str 'hi')\unwrap! + assert.is.equal 'hi', (Value.sym 'hi')\unwrap! + + test 'with type assert', -> + assert.is.equal 3.14, (Value.num 3.14)\unwrap 'num' + assert.is.equal 'hi', (Value.str 'hi')\unwrap 'str' + assert.is.equal 'hi', (Value.sym 'hi')\unwrap 'sym' + assert.has_error -> (Value.num 3.14)\unwrap 'sym' + assert.has_error -> (Value.str 'hi')\unwrap 'num' + assert.has_error -> (Value.sym 'hi')\unwrap 'str' + + describe 'checks equality', -> + test 'using the type', -> + val = Value 'num', 3 + assert.is.equal (Value.num 3), val + assert.not.equal (Value.str '3'), val + + val = Value 'str', 'hello' + assert.is.equal (Value.str 'hello'), val + assert.not.equal (Value.sym 'hello'), val + + test 'using the value', -> + val = Value 'num', 3 + assert.is.equal (Value.num 3), val + assert.not.equal (Value.num 4), val + + describe 'evaluates literal', -> + test 'numbers to consts', -> + assert_noop = (val) -> + assert.is.equal val, val\eval!\const! + + assert_noop Value.num 2 + assert_noop Value.str 'hello' + + test 'symbols in the scope', -> + scope = with Scope! + \set 'number', Result value: Value.num 3 + \set 'hello', Result value: Value.str "world" + \set 'goodbye', Result value: Value.sym "again" + + assert_eval = (sym, val) -> + const = Value.sym sym + assert.is.equal val, (const\eval scope)\const! + + assert_eval 'number', Value.num 3 + assert_eval 'hello', Value.str "world" + assert_eval 'goodbye', Value.sym "again" + + describe 'quotes literals', -> + test 'as themselves', -> + assert_noop = (val) -> assert.is.equal val, val\quote! + + assert_noop Value.num 2 + assert_noop Value.str 'hello' + assert_noop Value.sym 'world' |
