TestNativePlatform.fromJson constructor
Creates a new TestNativePlatform with properties from a JSON string.
The jsonText must be a valid JSON string representing a JSON object,
and the values for the NativePlatform properties are extracted from
the object's values for keys with the properties' names.
Example:
var testNativePlatform = TestNativePlatform.fromJson('''{
"operatingSystem": "linux",
"operatingSystemVersion": "MockVersion.2.3",
"numberOfProcessors": 42,
"lineTerminator": "\r",
"packageConfig": null
}''')'
The JSON object's value for such a key must be either a string or null.
Throws a FormatException if the jsonText string is not valid
JSON text, or the JSON text does not represent a JSON object.
The property values must have the correct type for the corresponding
NativePlatform property, or be null.
If the map value of a property key is not a string, integer, boolean,
list of strings, or map with string keys and values,
as required by the corresponding property,
a FormatException is also thrown.
Other keys are ignored. Missing property keys,
or keys with a null value, leave the property undefined.
The toJson method will leave out undefined properties, so
TestNativePlatform.fromJson(TestNativePlatform.toJson()) can create
an exact copy at a later time.
(To create a copy right now, TestNativePlatform.copyWith()
is easier and more efficient.)
Implementation
@visibleForTesting
factory TestNativePlatform.fromJson(String jsonText) {
Object? jsonObject = jsonDecode(jsonText);
if (jsonObject is! Map<String, Object?>) {
throw FormatException('Not a JSON object', jsonText);
}
// Read out the values that require special validation first.
var environment = _getJsonProperty<Map<String, Object?>>(
jsonObject,
json_key.environment,
jsonText,
);
var executableArguments = _getJsonProperty<List<Object?>>(
jsonObject,
json_key.executableArguments,
jsonText,
);
var script = _getJsonProperty<String>(jsonObject, 'script', jsonText);
return TestNativePlatform(
// Check that environment values are strings. Filter out null values.
environment: environment == null
? null
: <String, String>{
for (var MapEntry(:key, :value) in environment.entries)
if (value != null)
key: (value is String
? value
: _failJsonParse<String>(
'environment[$key]',
value,
jsonText,
)),
},
executable: _getJsonProperty<String>(
jsonObject,
json_key.executable,
jsonText,
),
// Check the executable arguments are strings.
executableArguments: executableArguments == null
? null
: <String>[
for (var argument in executableArguments)
argument is String
? argument
: _failJsonParse<String>(
'executableArguments',
argument,
jsonText,
),
],
lineTerminator: _getJsonProperty<String>(
jsonObject,
json_key.lineTerminator,
jsonText,
),
localeName: _getJsonProperty<String>(
jsonObject,
json_key.localeName,
jsonText,
),
localHostname: _getJsonProperty<String>(
jsonObject,
json_key.localHostname,
jsonText,
),
numberOfProcessors: _getJsonProperty<int>(
jsonObject,
json_key.numberOfProcessors,
jsonText,
),
operatingSystem: _getJsonProperty<String>(
jsonObject,
json_key.operatingSystem,
jsonText,
),
operatingSystemVersion: _getJsonProperty<String>(
jsonObject,
json_key.operatingSystemVersion,
jsonText,
),
packageConfig: _getJsonProperty<String>(
jsonObject,
json_key.packageConfig,
jsonText,
),
pathSeparator: _getJsonProperty<String>(
jsonObject,
json_key.pathSeparator,
jsonText,
),
resolvedExecutable: _getJsonProperty<String>(
jsonObject,
json_key.resolvedExecutable,
jsonText,
),
// Checks that the script is a valid URI.
script: script == null ? null : Uri.parse(script),
stdinSupportsAnsi: _getJsonProperty<bool>(
jsonObject,
json_key.stdinSupportsAnsi,
jsonText,
),
stdoutSupportsAnsi: _getJsonProperty<bool>(
jsonObject,
json_key.stdoutSupportsAnsi,
jsonText,
),
version: _getJsonProperty<String>(jsonObject, json_key.version, jsonText),
);
}