diff options
| author | s-ol <s+removethis@s-ol.nu> | 2025-04-05 17:48:08 +0000 |
|---|---|---|
| committer | s-ol <s+removethis@s-ol.nu> | 2025-04-07 10:33:17 +0000 |
| commit | 6d05263540b4a50f8d2ccb70b69ed6760e2ab691 (patch) | |
| tree | 36c130068e18712181a6d7db095ed8092d12b694 /spec | |
| parent | propagate dynamic scope in require/import (diff) | |
| download | alive-6d05263540b4a50f8d2ccb70b69ed6760e2ab691.tar.gz alive-6d05263540b4a50f8d2ccb70b69ed6760e2ab691.zip | |
template strings as syntax sugar for Ops
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/internal/parsing_spec.moon | 42 |
1 files changed, 33 insertions, 9 deletions
diff --git a/spec/internal/parsing_spec.moon b/spec/internal/parsing_spec.moon index 534155f..da70824 100644 --- a/spec/internal/parsing_spec.moon +++ b/spec/internal/parsing_spec.moon @@ -1,10 +1,10 @@ -import space, atom, cell, program, comment from require 'alv.parsing' +import space, atom, cell, program, tplstr, comment from require 'alv.parsing' import Constant from require 'alv' import Logger from require 'alv.logger' Logger\init 'silent' verify_parse = (parser, val) -> - with assert parser\match val + with assert (parser\match val), "unable to parse '#{val}'" assert.is.equal val, \stringify! verify_parse_nope = (parser, val) -> @@ -90,13 +90,6 @@ describe 'Cell', -> assert.is.equal 2, #node.children assert.is.equal 42, node.tag.value - test 'template strings', -> - node = verify_parse cell, '( hi #" string with #3#5 some #(= "contents") " - "friend" )' - - assert.is.equal 3, #node.children - assert.is.equal (Constant.str 'friend'), node.children[3] - describe 'RootCell parsing', -> describe 'handles whitespace', -> verify = (str) -> @@ -115,6 +108,37 @@ describe 'RootCell parsing', -> it 'everywhere', -> verify ' 3\tok-yes\n' +describe 'TemplateString parsing', -> + test 'basic parsing', -> + verify_parse tplstr, '$hi"me"' + verify_parse tplstr, '$[123]hi"me"' + node = verify_parse tplstr, '$[123]hi" string with $3$5 some $(= "contents") "' + + assert.is.equal 5, #node.children + assert.is.equal (Constant.sym 'hi'), node.children[1] + assert.is.same {' string with ', '', ' some ', ' '}, node.children[2]\eval!.result! + assert.is.equal (Constant.num 3), node.children[3] + assert.is.equal (Constant.num 5), node.children[4] + assert.is.equal (Constant.sym '='), node.children[5]\head! + + test 'can be empty', -> + node = verify_parse tplstr, '$hi""' + assert.is.equal 2, #node.children + assert.is.equal (Constant.sym 'hi'), node.children[1] + assert.is.same {''}, node.children[2]\eval!.result! + + test 'can contain escapes', -> + node = verify_parse tplstr, '$hi"hi\nmy \\$friend"' + assert.is.same {'hi\nmy $friend'}, node.children[2]\eval!.result! + + node = verify_parse tplstr, '$hi"\\$(this would\\${3}be invalid!"' + assert.is.same {'$(this would${3}be invalid!'}, node.children[2]\eval!.result! + + test 'can be applied', -> + node = verify_parse tplstr, '$[123]hi" string with $3$5 some $(= "contents") "' + substituted = node.__class.subst node.children[2]\eval!.result!, {123, "foot", " where "} + assert.is.equal " string with 123foot some where ", substituted + test 'whitespace', -> assert.is.equal ' ', space\match ' ' assert.is.equal '\n\t ', space\match '\n\t ' |
