-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from sciencesakura/refactortest
Refactor test data to use actual output from Java's ObjectOutputStrea…
- Loading branch information
Showing
11 changed files
with
238 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
.github/ | ||
node_modules/ | ||
scripts/ | ||
src/ | ||
testdata/ | ||
|
||
.editorconfig | ||
biome.json | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.