Skip to content
This repository has been archived by the owner on Jul 30, 2024. It is now read-only.

Test all node #30

Merged
merged 7 commits into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,37 @@ scala:
- 2.12.9
- 2.13.0

env:
- TRAVIS_NODE_VERSION="12.5.0"

jdk:
- openjdk8
- openjdk11

env:
- TRAVIS_NODE_VERSION="12.9.1" JOB_NAME="test"

script:
- sbt ++$TRAVIS_SCALA_VERSION nodejs_v8/test nodejs_v10/test current/test

matrix:
include:
- scala: 2.13.0
jdk: openjdk11
env: TRAVIS_NODE_VERSION="12.9.1" JOB_NAME="format and doc"
script: sbt ++$TRAVIS_SCALA_VERSION scalafmtSbtCheck scalafmtCheck test:scalafmtCheck current/doc core/doc
- scala: 2.13.0
jdk: openjdk11
env: TRAVIS_NODE_VERSION="8.16.1" JOB_NAME="test"
script: sbt ++$TRAVIS_SCALA_VERSION nodejs_v8/test
- scala: 2.13.0
jdk: openjdk11
env: TRAVIS_NODE_VERSION="10.16.3" JOB_NAME="test"
script: sbt ++$TRAVIS_SCALA_VERSION nodejs_v8/test nodejs_v10/test

install:
- rm -rf ~/.nvm &&
git clone https://github.com/nvm-sh/nvm.git ~/.nvm &&
(cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`) &&
source ~/.nvm/nvm.sh && nvm install $TRAVIS_NODE_VERSION

script:
- sbt ++$TRAVIS_SCALA_VERSION scalafmtSbtCheck scalafmtCheck test:scalafmtCheck clean current/doc core/doc test

before_cache:
- rm -fv $HOME/.ivy2/.sbt.ivy.lock
- find $HOME/.ivy2/cache -name "ivydata-*.properties" -print -delete
Expand Down
63 changes: 0 additions & 63 deletions app/current/src/test/scala/nodejs/ConsoleTest.scala

This file was deleted.

160 changes: 7 additions & 153 deletions app/current/src/test/scala/nodejs/buffer/BufferTest.scala
Original file line number Diff line number Diff line change
@@ -1,164 +1,18 @@
package io.scalajs.nodejs.buffer
package nodejs.buffer

import io.scalajs.collection.Iterator.Entry
import io.scalajs.nodejs.buffer
import io.scalajs.nodejs.buffer.Buffer
import org.scalatest.FunSpec

import scala.scalajs.js
import scala.scalajs.js.typedarray.{ArrayBuffer, DataView, Uint8Array}

/**
* Buffer Tests
*/
class BufferTest extends FunSpec {

describe("Buffer") {
describe("instance members") {
it("should sort buffers") {
val buf1 = Buffer.from("ABC")
val buf2 = Buffer.from("BCD")
val buf3 = Buffer.from("ABCD")

assert(buf1.compare(buf1) == 0)
assert(buf1.compare(buf2) == -1)
assert(buf1.compare(buf3) == -1)
assert(buf2.compare(buf1) == 1)
assert(buf2.compare(buf3) == 1)
}

it("should support iterating entries [classic]") {
val buf = Buffer.from("Hello!")
val it = buf.entries()
var result: Entry[js.Any] = null
do {
result = it.next()
if (!result.done) info(s"value: ${result.value}")
} while (!result.done)
}

it("should support iterating entries [Scala]") {
val buf = Buffer.from("Hello!")
for (value <- buf.entries()) info(s"value: $value")
}

it("should support buffer property") {
val arrayBuffer = new ArrayBuffer(16)
val buf = Buffer.from(arrayBuffer)
assert(buf.buffer === arrayBuffer)
}

it("should support byteOffset property") {
import scala.scalajs.js.typedarray.Int8Array
val nodeBuffer = Buffer.from(js.Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
val typedarray = new Int8Array(nodeBuffer.buffer, nodeBuffer.byteOffset, nodeBuffer.length)
assert(typedarray.mkString == new Int8Array(js.Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)).mkString)
}

it("should support byteLength for specific types") {
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be") == 12)
assert(Buffer.byteLength("\u00bd + \u00bc = \u00be", "utf8") == 12)
assert(Buffer.byteLength(Buffer.alloc(12)) == 12)
assert(Buffer.byteLength(new Uint8Array(12)) == 12)
assert(Buffer.byteLength(new DataView(new ArrayBuffer(12))) == 12)
assert(Buffer.byteLength(new ArrayBuffer(12)) == 12)
}

it("should support fill") {
val otherBuf = Buffer.from("abcdef")
assert(Buffer.alloc(10).fill(otherBuf).toString() == "abcdefabcd")
}
}

describe("class members") {
it("should support fields") {
assert(Buffer.poolSize > 0)
}

it("should create buffers from strings") {
val bufA = Buffer.from("Hello ")
info(s"bufA => ${bufA.toString()}")
val bufB = Buffer.from("World")
info(s"bufB => ${bufB.toString()}")
val bufC = bufA + bufB
info(s"bufC => ${bufC.toString()}, length = ${bufC.byteLength()}")

assert(bufA.toString() == "Hello ")
assert(bufB.toString() == "World")
assert(bufC.byteLength() == 11)
}

it("should create buffers from buffers") {
val buffer = Buffer.from("hello")
assert(Buffer.from(buffer).toString() == "hello")

// TODO: when Scala.js added TypedArray.from
// val uints = Uint8Array.from(???)
// assert(Buffer.from(uints).toString() == "worlds")
}

it("should support concat") {
val buffers = js.Array(Buffer.from("abc"), Buffer.from("def"), Buffer.from("ghijk"))
assert(Buffer.compare(Buffer.concat(buffers), Buffer.from("abcdefghijk")) == 0)
assert(Buffer.compare(Buffer.concat(buffers, 5), Buffer.from("abcde")) == 0)

val uints: js.Array[Uint8Array] = js.Array(Buffer.from("abc"), Buffer.from("def"), Buffer.from("ghijk"))
assert(Buffer.compare(Buffer.concat(uints), Buffer.from("abcdefghijk")) == 0)
assert(Buffer.compare(Buffer.concat(uints, 5), Buffer.from("abcde")) == 0)
}

it("should support isBufrer") {
assert(!Buffer.isBuffer(null))
assert(!Buffer.isBuffer(js.Object()))
assert(!Buffer.isBuffer(js.Array(1, 2, 3)))
assert(Buffer.isBuffer(Buffer.from("hello")))
}

it("should support isEncoding") {
assert(!Buffer.isEncoding(null))
assert(!Buffer.isEncoding(""))
assert(Buffer.isEncoding("utf8"))
assert(Buffer.isEncoding("UTF-8"))
}

it("should support writeBigInt64BE, writeBigInt64LE, writeBigInt64BE and writeBigInt64BE") {
val buf = Buffer.allocUnsafe(8)
val v = js.Dynamic.global.BigInt("0x0102030405060708")
buf.writeBigInt64BE(v, 0);
assert(Buffer.compare(buf, Buffer.from(js.Array(1, 2, 3, 4, 5, 6, 7, 8))) === 0)
}
}

describe("module members") {
it("should support transcode") {
// package object method
assert(buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") == "hello")
assert(buffer.transcode(Buffer.from("€"), "utf8", "ascii").toString("ascii") == "?")

// extension method
assert(Buffer.transcode(Buffer.from("hello"), "utf8", "ascii").toString("ascii") == "hello")
assert(Buffer.transcode(Buffer.from("€"), "utf8", "ascii").toString("ascii") == "?")
}

it("should support fields") {
// package object method
assert(buffer.INSPECT_MAX_BYTES > 0)
assert(buffer.kMaxLength > 0)

// extension method
assert(Buffer.INSPECT_MAX_BYTES > 0)
assert(Buffer.kMaxLength > 0)
}

it("should support constants") {
// package object method
assert(buffer.constants.MAX_LENGTH > 0)
assert(buffer.constants.MAX_STRING_LENGTH > 0)

// extension method
assert(Buffer.constants.MAX_LENGTH > 0)
assert(Buffer.constants.MAX_STRING_LENGTH > 0)
}
}
it("should support writeBigInt64BE, writeBigInt64LE, writeBigInt64BE and writeBigInt64BE") {
val buf = Buffer.allocUnsafe(8)
val v = js.Dynamic.global.BigInt("0x0102030405060708")
buf.writeBigInt64BE(v, 0);
assert(Buffer.compare(buf, Buffer.from(js.Array(1, 2, 3, 4, 5, 6, 7, 8))) === 0)
}

}
39 changes: 39 additions & 0 deletions app/nodejs-v10/src/test/scala/nodejs/ConsoleTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package nodejs

import io.scalajs.nodejs.console_module.{Console, ConsoleOptions}
import io.scalajs.nodejs.fs.Fs
import org.scalatest.{BeforeAndAfterEach, FunSpec}

import scala.scalajs.js

class ConsoleTest extends FunSpec with BeforeAndAfterEach {

private val logFileName = "x.nodejs10.ConsoleTest"

override def afterEach(): Unit = {
if (Fs.existsSync(logFileName)) Fs.unlinkSync(logFileName)
}

it("have table added in v10.0.0") {
Console.table(js.Array("x", "y"))
}

it("have timeLog added in v10.7.0") {
val label = "yay"
Console.time(label)
Console.timeLog(label)
Console.timeEnd(label)
}

it("have constructor(options) added in v10.0.0") {
val console = new Console(
new ConsoleOptions(
stdout = io.scalajs.nodejs.process.stdout
)
)

val label = "yay"
console.time(label)
console.timeEnd(label)
}
}
42 changes: 42 additions & 0 deletions app/nodejs-v8/src/test/scala/nodejs/ConsoleTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package nodejs

import io.scalajs.nodejs.console_module.Console
import io.scalajs.nodejs.fs.Fs
import org.scalatest.{BeforeAndAfterEach, FunSpec}

import scala.scalajs.js.JavaScriptException

class ConsoleTest extends FunSpec with BeforeAndAfterEach {

private val logFileName = "x.nodejs8.ConsoleTest"

override def afterEach(): Unit = {
if (Fs.existsSync(logFileName)) Fs.unlinkSync(logFileName)
}

private val failingWritable = Fs.createWriteStream(logFileName)
failingWritable.close(_ => {})

it("have constructor(stdout, stderr, ignoreErrors) added in v8.0.0") {
val looseConsole = new Console(
stdout = failingWritable,
stderr = failingWritable,
ignoreErrors = true
)
looseConsole.log("ok")
}

it("should support ignoreErrors") {
assume(TestEnvironment.isExecutedInNode10OrNewer)
val strictConsole = new Console(
stdout = failingWritable,
stderr = failingWritable,
ignoreErrors = false
)

val ex = intercept[JavaScriptException] {
strictConsole.log("ok")
}
assert(ex.getMessage().contains("write after end"))
}
}
16 changes: 16 additions & 0 deletions app/nodejs-v8/src/test/scala/nodejs/TestEnvironment.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package nodejs
import io.scalajs.nodejs.buffer.Buffer
import io.scalajs.nodejs.child_process.ChildProcess

object TestEnvironment {

private lazy val nodeMajorVersion: Int =
ChildProcess.execSync("node -v").asInstanceOf[Buffer].toString().drop(1).takeWhile(_.isDigit).toInt

def isExecutedInExactNode12: Boolean = nodeMajorVersion == 12
def isExecutedInExactNode10: Boolean = nodeMajorVersion == 10
def isExecutedInExactNode8: Boolean = nodeMajorVersion == 8

def isExecutedInNode12OrNewer: Boolean = nodeMajorVersion >= 12
def isExecutedInNode10OrNewer: Boolean = nodeMajorVersion >= 10
}
Loading