aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s+removethis@s-ol.nu>2022-02-05 12:20:36 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-02 14:24:49 +0000
commit7470fee507f49a7b26dd6ecfb262cc4e0da73f18 (patch)
tree407eb2d1a4377f1ddea462f2764981c34c08e03d
parentbase.match: predicate fn matching (diff)
downloadalive-7470fee507f49a7b26dd6ecfb262cc4e0da73f18.tar.gz
alive-7470fee507f49a7b26dd6ecfb262cc4e0da73f18.zip
lib: test and extend string module
-rw-r--r--alv-lib/string.moon34
-rw-r--r--alv/type.moon5
-rw-r--r--spec/lib/string_spec.moon70
3 files changed, 106 insertions, 3 deletions
diff --git a/alv-lib/string.moon b/alv-lib/string.moon
index 7508e8d..e4b007c 100644
--- a/alv-lib/string.moon
+++ b/alv-lib/string.moon
@@ -1,4 +1,4 @@
-import PureOp, Constant, Input, T, any from require 'alv.base'
+import PureOp, Constant, Input, T, Array, any from require 'alv.base'
str = Constant.meta
meta:
@@ -8,7 +8,35 @@ str = Constant.meta
value: class extends PureOp
pattern: any!\rep 1, nil
type: T.str
- tick: => @out\set table.concat [tostring i! for i in *@inputs]
+ tick: =>
+ strings = [i\type!\pp i!, true for i in *@inputs]
+ @out\set table.concat strings
+
+join = Constant.meta
+ meta:
+ name: 'join'
+ summary: "Concatenate/stringify values (with separator)"
+ examples: { '(join separator v1 [v2…])' }
+ value: class extends PureOp
+ pattern: any.str + any!\rep 1, nil
+ type: T.str
+ tick: =>
+ strings = [i\type!\pp i!, true for i in *@inputs[2]]
+ @out\set table.concat strings, @inputs[1]!
+
+str_arr = any ((typ) -> typ.__class == Array and typ.type == T.str), "str[]"
+concat = Constant.meta
+ meta:
+ name: 'concat'
+ summary: "Concatenate string arrays."
+ examples: { '(concat [separator] parts)' }
+ value: class extends PureOp
+ pattern: -any.str + str_arr
+ type: T.str
+
+ tick: =>
+ { separator, parts } = @unwrap_all!
+ @out\set table.concat parts, separator
Constant.meta
meta:
@@ -17,3 +45,5 @@ Constant.meta
value:
:str, '..': str
+ :join
+ :concat
diff --git a/alv/type.moon b/alv/type.moon
index 765cab2..2e392d8 100644
--- a/alv/type.moon
+++ b/alv/type.moon
@@ -39,6 +39,7 @@ class Type
--- pretty-print a value of this type.
-- @function pp
-- @tparam any value
+ -- @tparam[opt] bool raw whether to print "raw" (strings without quotes)
-- @treturn string
--- check two values of this type for equality.
@@ -75,7 +76,9 @@ class Type
--
-- @type Primitive
class Primitive extends Type
- pp: (value) =>
+ pp: (value, raw) =>
+ return tostring value if raw
+
switch @name
when 'str'
string.format '%q', value
diff --git a/spec/lib/string_spec.moon b/spec/lib/string_spec.moon
new file mode 100644
index 0000000..859c402
--- /dev/null
+++ b/spec/lib/string_spec.moon
@@ -0,0 +1,70 @@
+import TestPilot from require 'spec.test_setup'
+import T, Array from require 'alv'
+
+describe "string", ->
+ test = TestPilot '', '(import* testing) (import string)\n'
+
+ describe "str", ->
+ it "stringifies all primitives", ->
+ COPILOT\eval_once '
+ (expect= "hello" (string/str "hello"))
+ (expect= "1" (string/str 1))
+ (expect= "0.5" (string/str 0.5))
+ (expect= "true" (string/str true))
+ (expect= "false" (string/str false))
+ '
+
+ it "stringifies arrays", ->
+ COPILOT\eval_once '
+ (expect= "[1 2 3]" (string/str (array 1 2 3)))
+ (expect= \'["a" "b" "c"]\' (string/str (array "a" "b" "c")))
+ '
+
+ it "stringifies structs", ->
+ COPILOT\eval_once '
+ (expect= \'{a: 1 b: true c: "hello"}\'
+ (string/str (struct "a" 1
+ "b" true
+ "c" "hello")))
+ '
+
+ it "stringifies deeply", ->
+ COPILOT\eval_once '
+ (expect= "{a: {b: [1 2 3]}}"
+ (string/str (struct "a" (struct "b" (array 1 2 3)))))
+ '
+
+ it "joins multiple arguments", ->
+ COPILOT\eval_once '
+ (expect= "helloworld" (string/str "hello" "world"))
+ (expect= "here is 1 apple" (string/str "here is " 1 " apple"))
+ (expect= "this statement is true" (string/str "this statement is " true))
+ (expect= "false is a word." (string/str false " is a word."))
+ '
+
+ it "is aliased as ..", ->
+ COPILOT\eval_once '(expect= string/str string/..)'
+
+ describe "concat", ->
+ it "concatenates string-arrays", ->
+ COPILOT\eval_once '
+ (expect= "hello" (string/concat (array "hello")))
+ (expect= "helloworld" (string/concat (array "hello" "world")))
+ (expect= "helloobeautifulworld" (string/concat (array "hello" "o" "beautiful" "world")))
+ '
+
+ it "takes custom separator", ->
+ COPILOT\eval_once '
+ (expect= "a, b, c" (string/concat ", " (array "a" "b" "c")))
+ (expect= "hello world" (string/concat " " (array "hello" "world")))
+ (expect= "hello o beautiful world" (string/concat " " (array "hello" "o" "beautiful" "world")))
+ '
+
+ describe "join", ->
+ it "concatenates and stringifies", ->
+ COPILOT\eval_once '
+ (expect= "that is 1 beautiful tree" (string/join " " "that is" 1 "beautiful tree"))
+ (expect= "my favorite color is [0.9 0.2 1]" (string/join " " "my favorite color is" (array 0.9 0.2 1)))
+ (expect= "i_am_snek" (string/join "_" "i" "am" "snek"))
+ '
+