Skip to content

Commit

Permalink
chore: extract icu-messageformat-parser test cases into their own tes…
Browse files Browse the repository at this point in the history
…t case files (#3763)

Co-authored-by: long.ho <long.ho@cloudkitchens.com>
  • Loading branch information
pyrocat101 and long.ho committed Sep 6, 2022
1 parent ac722ed commit 2ce29a2
Show file tree
Hide file tree
Showing 114 changed files with 7,099 additions and 7,160 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ bazel-out

packages/babel-plugin-formatjs/integration-tests/node_modules
packages/ts-transformer/integration-tests/node_modules
packages/icu-messageformat-parser/integration-tests/node_modules
packages/cli/integration-tests/node_modules
packages/react-intl/examples/node_modules
packages/babel-plugin-formatjs/node_modules
Expand Down
7 changes: 6 additions & 1 deletion packages/icu-messageformat-parser/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ jest_test(
"tests/**/*.tsx",
"tests/**/*.snap",
]),
deps = SRC_DEPS,
deps = [
"//:node_modules/@types/lodash",
"//:node_modules/fast-glob",
"//:node_modules/lodash",
"//packages/icu-messageformat-parser-tests:test-cases-copy",
] + SRC_DEPS,
)

js_binary(
Expand Down
2 changes: 2 additions & 0 deletions packages/icu-messageformat-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ export function parse(message: string, opts: ParserOptions = {}) {
return result.val
}
export * from './types'
// only for testing
export const _Parser = Parser
36 changes: 36 additions & 0 deletions packages/icu-messageformat-parser/integration-tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("//tools:index.bzl", "generate_src_file")
load("@npm//:defs.bzl", "npm_link_all_packages")

npm_link_all_packages(name = "node_modules")

TEST_CASES = glob(["test_cases/*"])

# [ts_script(
# name = "gen_parser_test_%s" % f.split("/")[-1],
# outs = ["%s.tmp" % f.split("/")[-1]],
# args = [
# "--input",
# f
# ],
# data = [
# ":node_modules/@formatjs/icu-messageformat-parser",
# "//:node_modules/lodash",
# f
# ],
# entry_point = "run-parser.ts",
# ) for f in TEST_CASES]

[generate_src_file(
name = "gen_parser_test_%s" % f.split("/")[-1],
src = f,
data = [
":node_modules/@formatjs/icu-messageformat-parser",
"//:node_modules/lodash",
f
],
args = [
"--input %s/%s" % (package_name(),f),
],
entry_point = "run-parser.ts",
) for f in TEST_CASES]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"@formatjs/icu-messageformat-parser": "workspace:*"
}
}
38 changes: 38 additions & 0 deletions packages/icu-messageformat-parser/integration-tests/run-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {_Parser} from '@formatjs/icu-messageformat-parser'
import fs from 'fs'
import minimist from 'minimist'

interface Args extends minimist.ParsedArgs {
input: string
out: string
}

function run({input, out}: Args) {
console.log(__dirname)
const sections = fs.readFileSync(input, 'utf-8').split('\n---\n')
const message = sections[0]
const snapshotOptions = JSON.parse(sections[1])
const options = {...snapshotOptions}
// Note: allow locale to be specified via an identifier.
if (options.locale) {
options.locale = new Intl.Locale(options.locale)
}

const parsed = new _Parser(message, options).parse()

// If the mismatch is intentional, use `UPDATE_SNAPSHOT=1` env var
// with bazel run to update the snapshot.
fs.writeFileSync(
out,
[
message,
JSON.stringify(snapshotOptions, null, 2),
JSON.stringify(parsed, null, 2),
].join('\n---\n'),
'utf-8'
)
}

if (require.main === module) {
run(minimist<Args>(process.argv.slice(2)))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{a}
---
{}
---
{
"val": [
{
"type": 1,
"value": "a",
"location": {
"start": {
"offset": 0,
"line": 1,
"column": 1
},
"end": {
"offset": 3,
"line": 1,
"column": 4
}
}
}
],
"err": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
a {b}
c
---
{}
---
{
"val": [
{
"type": 0,
"value": "a ",
"location": {
"start": {
"offset": 0,
"line": 1,
"column": 1
},
"end": {
"offset": 2,
"line": 1,
"column": 3
}
}
},
{
"type": 1,
"value": "b",
"location": {
"start": {
"offset": 2,
"line": 1,
"column": 3
},
"end": {
"offset": 5,
"line": 1,
"column": 6
}
}
},
{
"type": 0,
"value": " \nc",
"location": {
"start": {
"offset": 5,
"line": 1,
"column": 6
},
"end": {
"offset": 8,
"line": 2,
"column": 2
}
}
}
],
"err": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{0, date, ::yyyy.MM.dd G 'at' HH:mm:ss vvvv}
---
{}
---
{
"val": [
{
"type": 3,
"value": "0",
"location": {
"start": {
"offset": 0,
"line": 1,
"column": 1
},
"end": {
"offset": 44,
"line": 1,
"column": 45
}
},
"style": {
"type": 1,
"pattern": "yyyy.MM.dd G 'at' HH:mm:ss vvvv",
"location": {
"start": {
"offset": 10,
"line": 1,
"column": 11
},
"end": {
"offset": 43,
"line": 1,
"column": 44
}
},
"parsedOptions": {}
}
}
],
"err": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{0, date, ::EEE, MMM d, ''yy}
---
{}
---
{
"val": [
{
"type": 3,
"value": "0",
"location": {
"start": {
"offset": 0,
"line": 1,
"column": 1
},
"end": {
"offset": 29,
"line": 1,
"column": 30
}
},
"style": {
"type": 1,
"pattern": "EEE, MMM d, ''yy",
"location": {
"start": {
"offset": 10,
"line": 1,
"column": 11
},
"end": {
"offset": 28,
"line": 1,
"column": 29
}
},
"parsedOptions": {}
}
}
],
"err": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{0, date, ::h:mm a}
---
{}
---
{
"val": [
{
"type": 3,
"value": "0",
"location": {
"start": {
"offset": 0,
"line": 1,
"column": 1
},
"end": {
"offset": 19,
"line": 1,
"column": 20
}
},
"style": {
"type": 1,
"pattern": "h:mm a",
"location": {
"start": {
"offset": 10,
"line": 1,
"column": 11
},
"end": {
"offset": 18,
"line": 1,
"column": 19
}
},
"parsedOptions": {}
}
}
],
"err": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{0, date, ::J}
---
{
"locale": "und-u-hc-h12",
"shouldParseSkeletons": true,
"requiresOtherClause": true
}
---
{
"val": [
{
"type": 3,
"value": "0",
"location": {
"start": {
"offset": 0,
"line": 1,
"column": 1
},
"end": {
"offset": 14,
"line": 1,
"column": 15
}
},
"style": {
"type": 1,
"pattern": "H",
"location": {
"start": {
"offset": 10,
"line": 1,
"column": 11
},
"end": {
"offset": 13,
"line": 1,
"column": 14
}
},
"parsedOptions": {
"hourCycle": "h23",
"hour": "numeric"
}
}
}
],
"err": null
}
Loading

0 comments on commit 2ce29a2

Please sign in to comment.