Skip to content

Commit

Permalink
Merge pull request #31 from sciencesakura/refactortest
Browse files Browse the repository at this point in the history
Refactor test data to use actual output from Java's ObjectOutputStrea…
  • Loading branch information
sciencesakura authored Oct 30, 2024
2 parents 1e889ee + eb2b6ab commit ad57b57
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 213 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.github/
node_modules/
scripts/
src/
testdata/

.editorconfig
biome.json
Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"files": {
"ignoreUnknown": false,
"ignore": []
"ignore": ["**/testdata-*"]
},
"formatter": {
"enabled": true,
Expand Down
7 changes: 5 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"compile": "run-s compile:*",
"compile:cjs": "tsc -m node16 && renamer --find '/\\.js$/' --replace .cjs 'dist/**'",
"compile:esm": "tsc -d && renamer --find '/\\.js$/' --replace .mjs 'dist/**'",
"gen-testdatacode": "groovy scripts/gen-testdatacode.groovy testdata/",
"test": "vitest run",
"check": "run-s lint test",
"check:ci": "run-s lint:ci test",
Expand Down Expand Up @@ -50,7 +51,6 @@
"homepage": "https://github.com/sciencesakura/mutf-8",
"devDependencies": {
"@biomejs/biome": "^1.9.2",
"@types/node": "^20.17.1",
"npm-run-all2": "^7.0.1",
"renamer": "^5.0.2",
"rimraf": "^6.0.1",
Expand Down
49 changes: 49 additions & 0 deletions scripts/gen-testdatacode.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: MIT

import groovy.json.*
import static java.io.ObjectStreamConstants.*

def header = '''\
// This file is generated by scripts/gen-testdatacode.groovy
'''

def generateTestdataCode = { jsonFile ->
def code = new StringBuilder(header)
code << 'const testdata = ['
new JsonSlurper().parse(jsonFile).each {
def name = it.name
def text = it.text
def binary = new ByteArrayOutputStream().with {
new ObjectOutputStream(it).withCloseable { it.writeUTF(text) }
def serial = it.toByteArray()
// off | field | value
// -----|----------------|--------------------------------
// 0 | magic | STREAM_MAGIC
// 2 | version | STREAM_VERSION
// 4 | tag | TC_BLOCKDATA
// 5 | size | sizeof(length) + sizeof(bytes)
// 6 | length | sizeof(bytes)
// 8 | bytes | MUTF-8 encoded string
assert serial[4] == TC_BLOCKDATA
serial[8..<serial.length].collect(Byte::toUnsignedInt)
}
def jsName = StringEscapeUtils.escapeJavaScript(name)
def jsText = StringEscapeUtils.escapeJavaScript(text)
def jsBinary = binary.collect { '0x' + Integer.toHexString(it) }.join(', ')
code << """
| {
| name: "$jsName",
| text: "$jsText",
| binary: new Uint8Array([$jsBinary]),
| },""".stripMargin()
}
code << '\n];'
code << '\nexport default testdata;\n'
code.toString()
}

new File(this.args[0]).eachFileMatch(~/.*\.json/) { file ->
def name = file.name.replaceFirst(/\.json$/, '')
def outFile = new File('src', "testdata-${name}.mts")
outFile.text = generateTestdataCode(file)
}
Loading

0 comments on commit ad57b57

Please sign in to comment.