summary refs log tree commit diff stats
path: root/tests/js/tstringitems.nim
blob: 20aed6e8bfe3c8d31bb7ab73d88c8f8c60f32d04 (plain) (blame)
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
71
72
73
74
75
76
77
78
discard """
  output: '''Hello
Hello'''
"""

block: # bug #2581
  const someVars = [ "Hello" ]
  var someVars2 = [ "Hello" ]

  proc getSomeVar: string =
      for i in someVars:
          if i == "Hello":
              result = i
              break

  proc getSomeVar2: string =
      for i in someVars2:
          if i == "Hello":
              result = i
              break

  echo getSomeVar()
  echo getSomeVar2()

block: # Test compile-time binary data generation, invalid unicode
  proc signatureMaker(): string {. compiletime .} =
    const signatureBytes = [137, 80, 78, 71, 13, 10, 26, 10]
    result = ""
    for c in signatureBytes: result.add chr(c)

  const cSig = signatureMaker()

  var rSig = newString(8)
  rSig[0] = chr(137)
  rSig[1] = chr(80)
  rSig[2] = chr(78)
  rSig[3] = chr(71)
  rSig[4] = chr(13)
  rSig[5] = chr(10)
  rSig[6] = chr(26)
  rSig[7] = chr(10)

  doAssert(rSig == cSig)

block: # Test unicode strings
  const constStr = "Привет!"
  var jsStr : cstring
  {.emit: """`jsStr`[0] = "Привет!";""".}

  doAssert($jsStr == constStr)
  var runtimeStr = "При"
  runtimeStr &= "вет!"

  doAssert(runtimeStr == constStr)

block: # Conversions from/to cstring
  proc stringSaysHelloInRussian(s: cstring): bool =
    {.emit: """`result` = (`s` === "Привет!");""".}

  doAssert(stringSaysHelloInRussian("Привет!"))

  const constStr = "Привет!"
  doAssert(stringSaysHelloInRussian(constStr))

  var rtStr = "Привет!"
  doAssert(stringSaysHelloInRussian(rtStr))

block: # String case of
  const constStr = "Привет!"
  var s = "Привет!"

  case s
  of constStr: discard
  else: doAssert(false)

  case s
  of "Привет!": discard
  else: doAssert(false)