aboutsummaryrefslogtreecommitdiffstats
path: root/spec/lib/string_spec.moon
blob: c78760962816ae2f0f2a37d530460f567b938ed7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 [1 2 3]))
        (expect= \'["a" "b" "c"]\' (string/str ["a" "b" "c"]))
      '

    it "stringifies structs", ->
      COPILOT\eval_once '
        (expect= \'{a: 1 b: true c: "hello"}\'
          (string/str {"a" 1
                       "b" true
                       "c" "hello"}))
      '

    it "stringifies deeply", ->
      COPILOT\eval_once '
        (expect= "{a: {b: [1 2 3]}}"
          (string/str {"a" {"b" [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 ["hello"]))
        (expect= "helloworld" (string/concat ["hello" "world"]))
        (expect= "helloobeautifulworld" (string/concat ["hello" "o" "beautiful" "world"]))
      '

    it "takes custom separator", ->
      COPILOT\eval_once '
        (expect= "a, b, c" (string/concat ", " ["a" "b" "c"]))
        (expect= "hello world" (string/concat " " ["hello" "world"]))
        (expect= "hello o beautiful world" (string/concat " " ["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" [0.9 0.2 1]))
        (expect= "i_am_snek" (string/join "_" "i" "am" "snek"))
      '