summary refs log tree commit diff stats
path: root/tests/js
diff options
context:
space:
mode:
authorDaniil Yarancev <21169548+Yardanico@users.noreply.github.com>2018-01-07 21:02:00 +0300
committerGitHub <noreply@github.com>2018-01-07 21:02:00 +0300
commitfb44c522e6173528efa8035ecc459c84887d0167 (patch)
treea2f5e98606be265981a5f72748896967033e23d7 /tests/js
parentccf99fa5ce4fe992fb80dc89271faa51456c3fa5 (diff)
parente23ea64c41e101d4e1d933f0b015f51cc6c2f7de (diff)
downloadNim-fb44c522e6173528efa8035ecc459c84887d0167.tar.gz
Merge pull request #1 from nim-lang/devel
upstream
Diffstat (limited to 'tests/js')
-rw-r--r--tests/js/tarrayboundscheck.nim44
-rw-r--r--tests/js/tasync.nim32
-rw-r--r--tests/js/tcodegendeclproc.nim11
-rw-r--r--tests/js/tcodegendeclvar.nim10
-rw-r--r--tests/js/test2.nim15
-rw-r--r--tests/js/trefbyvar.nim2
-rw-r--r--tests/js/ttimes.nim53
7 files changed, 146 insertions, 21 deletions
diff --git a/tests/js/tarrayboundscheck.nim b/tests/js/tarrayboundscheck.nim
new file mode 100644
index 000000000..f0eaeb89d
--- /dev/null
+++ b/tests/js/tarrayboundscheck.nim
@@ -0,0 +1,44 @@
+discard """
+  output: '''idx out of bounds: -1
+month out of bounds: 0
+Jan
+Feb
+Mar
+Apr
+May
+Jun
+Jul
+Aug
+Sep
+Oct
+Nov
+Dec
+month out of bounds: 13
+idx out of bounds: 14
+'''
+"""
+
+{.push boundChecks:on.}
+
+# see issue #6532:
+# js backend 0.17.3: array bounds check for non zero based arrays is buggy
+
+proc test_arrayboundscheck() =
+  var months: array[1..12, string] =
+    ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
+     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
+
+  var indices = [0,1,2,3,4,5,6,7,8,9,10,11,12,13]
+
+  for i in -1 .. 14:
+    try:
+      let idx = indices[i]
+      try:
+        echo months[idx]
+      except:
+        echo "month out of bounds: ", idx
+    except:
+      echo "idx out of bounds: ", i
+
+test_arrayboundscheck()
+{.pop.}
\ No newline at end of file
diff --git a/tests/js/tasync.nim b/tests/js/tasync.nim
new file mode 100644
index 000000000..34ef97b8b
--- /dev/null
+++ b/tests/js/tasync.nim
@@ -0,0 +1,32 @@
+discard """
+  disabled: true
+  output: '''
+x
+e
+'''
+"""
+
+import asyncjs
+
+# demonstrate forward definition
+# for js
+proc y(e: int): Future[string] {.async.}
+
+proc e: int {.discardable.} =
+  echo "e"
+  return 2
+
+proc x(e: int): Future[void] {.async.} =
+  var s = await y(e)
+  echo s
+  e()
+
+proc y(e: int): Future[string] {.async.} =
+  if e > 0:
+    return await y(0)
+  else:
+    return "x"
+
+
+discard x(2)
+
diff --git a/tests/js/tcodegendeclproc.nim b/tests/js/tcodegendeclproc.nim
new file mode 100644
index 000000000..3acf0bc13
--- /dev/null
+++ b/tests/js/tcodegendeclproc.nim
@@ -0,0 +1,11 @@
+discard """
+  output: '''
+-1
+8
+'''
+  ccodecheck: "'console.log(-1); function fac_' \\d+ '(n_' \\d+ ')'"
+"""
+proc fac(n: int): int {.codegenDecl: "console.log(-1); function $2($3)".} =
+  return n
+
+echo fac(8)
diff --git a/tests/js/tcodegendeclvar.nim b/tests/js/tcodegendeclvar.nim
new file mode 100644
index 000000000..645443ef7
--- /dev/null
+++ b/tests/js/tcodegendeclvar.nim
@@ -0,0 +1,10 @@
+discard """
+  output: '''
+-1
+2
+'''
+  ccodecheck: "'console.log(-1); var v_' \\d+ ' = [2]'"
+"""
+
+var v {.codegenDecl: "console.log(-1); var $2".} = 2
+echo v
diff --git a/tests/js/test2.nim b/tests/js/test2.nim
index 0f460d6f8..0bfb99139 100644
--- a/tests/js/test2.nim
+++ b/tests/js/test2.nim
@@ -2,7 +2,9 @@ discard """
   output: '''foo
 js 3.14
 7
-1'''
+1
+-21550
+-21550'''
 """
 
 # This file tests the JavaScript generator
@@ -40,3 +42,14 @@ procThatRefersToConst() # Call bar before it is defined
 proc procThatRefersToConst() =
   var i = 0 # Use a var index, otherwise nim will constfold foo[0]
   echo someConst[i] # JS exception here: foo is still not initialized (undefined)
+
+# bug #6753
+let x = -1861876800
+const y = 86400
+echo (x - (y - 1)) div y # Now gives `-21550`
+
+proc foo09() =
+    let x = -1861876800
+    const y = 86400
+    echo (x - (y - 1)) div y # Still gives `-21551`
+foo09()
diff --git a/tests/js/trefbyvar.nim b/tests/js/trefbyvar.nim
index d440fcc64..5b168044e 100644
--- a/tests/js/trefbyvar.nim
+++ b/tests/js/trefbyvar.nim
@@ -66,4 +66,4 @@ proc initTypeA1(a: int; b: string; c: pointer = nil): TypeA1 =
   result.c_impl = c
 
 let x = initTypeA1(1, "a")
-doAssert($x == "(a_impl: 1, b_impl: a, c_impl: ...)")
+doAssert($x == "(a_impl: 1, b_impl: \"a\", c_impl: ...)")
diff --git a/tests/js/ttimes.nim b/tests/js/ttimes.nim
index 2868c6d0f..63a4bb04f 100644
--- a/tests/js/ttimes.nim
+++ b/tests/js/ttimes.nim
@@ -1,4 +1,3 @@
-# test times module with js
 discard """
   action: run
 """
@@ -9,21 +8,37 @@ import times
 # Tue 19 Jan 03:14:07 GMT 2038
 
 block yeardayTest:
-  # check if yearday attribute is properly set on TimeInfo creation
-  doAssert fromSeconds(2147483647).getGMTime().yearday == 18
-
-block localTimezoneTest:
-  # check if timezone is properly set during Time to TimeInfo conversion
-  doAssert fromSeconds(2147483647).getLocalTime().timezone == getTimezone()
-
-block timestampPersistenceTest:
-  # check if timestamp persists during TimeInfo to Time conversion
-  const
-    timeString = "2017-03-21T12:34:56+03:00"
-    timeStringGmt = "2017-03-21T09:34:56+00:00"
-    timeStringGmt2 = "2017-03-21T08:34:56+00:00"
-    fmt = "yyyy-MM-dd'T'HH:mm:sszzz"
-  # XXX Check which one is the right solution here:
-
-  let x = $timeString.parse(fmt).toTime().getGMTime()
-  doAssert x == timeStringGmt or x == timeStringGmt2
+  doAssert fromUnix(2147483647).utc.yearday == 18
+
+block localTime:
+  var local = now()
+  let utc = local.utc
+  doAssert local.toTime == utc.toTime
+
+let a = fromUnix(1_000_000_000)
+let b = fromUnix(1_500_000_000)
+doAssert b - a == 500_000_000
+
+# Because we can't change the timezone JS uses, we define a simple static timezone for testing.
+
+proc staticZoneInfoFromUtc(time: Time): ZonedTime =
+  result.utcOffset = -7200
+  result.isDst = false
+  result.adjTime = (time.toUnix + 7200).Time
+
+proc staticZoneInfoFromTz(adjTime: Time): ZonedTIme =
+  result.utcOffset = -7200
+  result.isDst = false
+  result.adjTime = adjTime
+
+let utcPlus2 = Timezone(zoneInfoFromUtc: staticZoneInfoFromUtc, zoneInfoFromTz: staticZoneInfoFromTz, name: "")
+
+block timezoneTests:
+  let dt = initDateTime(01, mJan, 2017, 12, 00, 00, utcPlus2)
+  doAssert $dt == "2017-01-01T12:00:00+02:00"
+  doAssert $dt.utc == "2017-01-01T10:00:00+00:00"
+  doAssert $dt.utc.inZone(utcPlus2) == $dt
+
+doAssert $initDateTime(01, mJan, 1911, 12, 00, 00, utc()) == "1911-01-01T12:00:00+00:00"
+# See #6752
+# doAssert $initDateTime(01, mJan, 1900, 12, 00, 00, utc()) == "0023-01-01T12:00:00+00:00"
\ No newline at end of file