git.s-ol.nu alive / 6802f2c
lib: test and extend string module s-ol 6 months ago
3 changed file(s) with 106 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
3838 --- pretty-print a value of this type.
3939 -- @function pp
4040 -- @tparam any value
41 -- @tparam[opt] bool raw whether to print "raw" (strings without quotes)
4142 -- @treturn string
4243
4344 --- check two values of this type for equality.
7475 --
7576 -- @type Primitive
7677 class Primitive extends Type
77 pp: (value) =>
78 pp: (value, raw) =>
79 return tostring value if raw
80
7881 switch @name
7982 when 'str'
8083 string.format '%q', value
0 import PureOp, Constant, Input, T, any from require 'alv.base'
0 import PureOp, Constant, Input, T, Array, any from require 'alv.base'
11
22 str = Constant.meta
33 meta:
77 value: class extends PureOp
88 pattern: any!\rep 1, nil
99 type: T.str
10 tick: => @out\set table.concat [tostring i! for i in *@inputs]
10 tick: =>
11 strings = [i\type!\pp i!, true for i in *@inputs]
12 @out\set table.concat strings
13
14 join = Constant.meta
15 meta:
16 name: 'join'
17 summary: "Concatenate/stringify values (with separator)"
18 examples: { '(join separator v1 [v2…])' }
19 value: class extends PureOp
20 pattern: any.str + any!\rep 1, nil
21 type: T.str
22 tick: =>
23 strings = [i\type!\pp i!, true for i in *@inputs[2]]
24 @out\set table.concat strings, @inputs[1]!
25
26 str_arr = any ((typ) -> typ.__class == Array and typ.type == T.str), "str[]"
27 concat = Constant.meta
28 meta:
29 name: 'concat'
30 summary: "Concatenate string arrays."
31 examples: { '(concat [separator] parts)' }
32 value: class extends PureOp
33 pattern: -any.str + str_arr
34 type: T.str
35
36 tick: =>
37 { separator, parts } = @unwrap_all!
38 @out\set table.concat parts, separator
1139
1240 Constant.meta
1341 meta:
1644
1745 value:
1846 :str, '..': str
47 :join
48 :concat
0 import TestPilot from require 'spec.test_setup'
1 import T, Array from require 'alv'
2
3 describe "string", ->
4 test = TestPilot '', '(import* testing) (import string)\n'
5
6 describe "str", ->
7 it "stringifies all primitives", ->
8 COPILOT\eval_once '
9 (expect= "hello" (string/str "hello"))
10 (expect= "1" (string/str 1))
11 (expect= "0.5" (string/str 0.5))
12 (expect= "true" (string/str true))
13 (expect= "false" (string/str false))
14 '
15
16 it "stringifies arrays", ->
17 COPILOT\eval_once '
18 (expect= "[1 2 3]" (string/str (array 1 2 3)))
19 (expect= \'["a" "b" "c"]\' (string/str (array "a" "b" "c")))
20 '
21
22 it "stringifies structs", ->
23 COPILOT\eval_once '
24 (expect= \'{a: 1 b: true c: "hello"}\'
25 (string/str (struct "a" 1
26 "b" true
27 "c" "hello")))
28 '
29
30 it "stringifies deeply", ->
31 COPILOT\eval_once '
32 (expect= "{a: {b: [1 2 3]}}"
33 (string/str (struct "a" (struct "b" (array 1 2 3)))))
34 '
35
36 it "joins multiple arguments", ->
37 COPILOT\eval_once '
38 (expect= "helloworld" (string/str "hello" "world"))
39 (expect= "here is 1 apple" (string/str "here is " 1 " apple"))
40 (expect= "this statement is true" (string/str "this statement is " true))
41 (expect= "false is a word." (string/str false " is a word."))
42 '
43
44 it "is aliased as ..", ->
45 COPILOT\eval_once '(expect= string/str string/..)'
46
47 describe "concat", ->
48 it "concatenates string-arrays", ->
49 COPILOT\eval_once '
50 (expect= "hello" (string/concat (array "hello")))
51 (expect= "helloworld" (string/concat (array "hello" "world")))
52 (expect= "helloobeautifulworld" (string/concat (array "hello" "o" "beautiful" "world")))
53 '
54
55 it "takes custom separator", ->
56 COPILOT\eval_once '
57 (expect= "a, b, c" (string/concat ", " (array "a" "b" "c")))
58 (expect= "hello world" (string/concat " " (array "hello" "world")))
59 (expect= "hello o beautiful world" (string/concat " " (array "hello" "o" "beautiful" "world")))
60 '
61
62 describe "join", ->
63 it "concatenates and stringifies", ->
64 COPILOT\eval_once '
65 (expect= "that is 1 beautiful tree" (string/join " " "that is" 1 "beautiful tree"))
66 (expect= "my favorite color is [0.9 0.2 1]" (string/join " " "my favorite color is" (array 0.9 0.2 1)))
67 (expect= "i_am_snek" (string/join "_" "i" "am" "snek"))
68 '
69