mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00

Some checks failed
Graphics CI / gg-regressions (push) Waiting to run
vlib modules CI / build-module-docs (push) Waiting to run
Sanitized CI / sanitize-undefined-clang (push) Waiting to run
Sanitized CI / sanitize-undefined-gcc (push) Waiting to run
Sanitized CI / tests-sanitize-address-clang (push) Waiting to run
Sanitized CI / sanitize-address-msvc (push) Waiting to run
Sanitized CI / sanitize-address-gcc (push) Waiting to run
Sanitized CI / sanitize-memory-clang (push) Waiting to run
sdl CI / v-compiles-sdl-examples (push) Waiting to run
Time CI / time-linux (push) Waiting to run
Time CI / time-macos (push) Waiting to run
Time CI / time-windows (push) Waiting to run
toml CI / toml-module-pass-external-test-suites (push) Waiting to run
Tools CI / tools-linux (clang) (push) Waiting to run
Tools CI / tools-linux (gcc) (push) Waiting to run
Tools CI / tools-linux (tcc) (push) Waiting to run
Tools CI / tools-macos (clang) (push) Waiting to run
Tools CI / tools-windows (gcc) (push) Waiting to run
Tools CI / tools-windows (msvc) (push) Waiting to run
Tools CI / tools-windows (tcc) (push) Waiting to run
Tools CI / tools-docker-ubuntu-musl (push) Waiting to run
vab CI / vab-compiles-v-examples (push) Waiting to run
vab CI / v-compiles-os-android (push) Waiting to run
json decoder benchmark CI / json-encode-benchmark (push) Has been cancelled
95 lines
2.2 KiB
V
95 lines
2.2 KiB
V
import x.json2.decoder2 as json
|
|
|
|
enum Bar {
|
|
a
|
|
b
|
|
c = 10
|
|
}
|
|
|
|
type BarAlias = Bar
|
|
|
|
fn test_number_decode() {
|
|
assert json.decode[Bar]('0')! == Bar.a
|
|
assert json.decode[Bar]('1')! == Bar.b
|
|
assert json.decode[Bar]('10')! == Bar.c
|
|
|
|
assert json.decode[BarAlias]('0')! == Bar.a
|
|
assert json.decode[BarAlias]('1')! == Bar.b
|
|
assert json.decode[BarAlias]('10')! == Bar.c
|
|
}
|
|
|
|
fn test_number_decode_fails() {
|
|
if _ := json.decode[Bar]('2') {
|
|
assert false
|
|
} else {
|
|
if err is json.JsonDecodeError {
|
|
assert err.line == 1
|
|
assert err.character == 1
|
|
assert err.message == 'Data: Number value: `2` does not match any field in enum: &Bar'
|
|
}
|
|
}
|
|
|
|
if _ := json.decode[BarAlias]('2') {
|
|
assert false
|
|
} else {
|
|
if err is json.JsonDecodeError {
|
|
assert err.line == 1
|
|
assert err.character == 1
|
|
assert err.message == 'Data: Number value: `2` does not match any field in enum: &BarAlias'
|
|
}
|
|
}
|
|
}
|
|
|
|
fn test_string_decode() {
|
|
assert json.decode[Bar]('"a"')! == Bar.a
|
|
assert json.decode[Bar]('"b"')! == Bar.b
|
|
assert json.decode[Bar]('"c"')! == Bar.c
|
|
|
|
assert json.decode[BarAlias]('"a"')! == Bar.a
|
|
assert json.decode[BarAlias]('"b"')! == Bar.b
|
|
assert json.decode[BarAlias]('"c"')! == Bar.c
|
|
}
|
|
|
|
fn test_string_decode_fails() {
|
|
if _ := json.decode[Bar]('"d"') {
|
|
assert false
|
|
} else {
|
|
if err is json.JsonDecodeError {
|
|
assert err.line == 1
|
|
assert err.character == 1
|
|
assert err.message == 'Data: String value: `d` does not match any field in enum: &Bar'
|
|
}
|
|
}
|
|
|
|
if _ := json.decode[BarAlias]('"d"') {
|
|
assert false
|
|
} else {
|
|
if err is json.JsonDecodeError {
|
|
assert err.line == 1
|
|
assert err.character == 1
|
|
assert err.message == 'Data: String value: `d` does not match any field in enum: &BarAlias'
|
|
}
|
|
}
|
|
}
|
|
|
|
fn test_invalid_decode_fails() {
|
|
if _ := json.decode[Bar]('true') {
|
|
assert false
|
|
} else {
|
|
if err is json.JsonDecodeError {
|
|
assert err.line == 1
|
|
assert err.character == 1
|
|
assert err.message == 'Data: Expected number or string value for enum, got: boolean'
|
|
}
|
|
}
|
|
|
|
if _ := json.decode[BarAlias]('true') {
|
|
assert false
|
|
} else {
|
|
if err is json.JsonDecodeError {
|
|
assert err.line == 1
|
|
assert err.character == 1
|
|
assert err.message == 'Data: Expected number or string value for enum, got: boolean'
|
|
}
|
|
}
|
|
}
|