1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import sys
import unittest
from os import path
import mozpack.path as mozpath
import mozunit
import yaml
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
sys.path.append(path.join(path.dirname(__file__), ".."))
from init.generate_static_pref_list import generate_code
test_data_path = mozpath.abspath(mozpath.dirname(__file__))
test_data_path = mozpath.join(test_data_path, "data")
# A single good input with lots of different combinations.
good_input = """
- name: my.bool
type: bool
value: false
mirror: never
- name: my.int
type: int32_t
value: -123
mirror: once
do_not_use_directly: false
rust: false
- mirror: always
value: 999
type: uint32_t
name: my.uint
rust: true
- name: my.float # A comment.
type: float # A comment.
do_not_use_directly: true # A comment.
value: 0.0f # A comment.
mirror: once # A comment.
rust: true # A comment.
# A comment.
- name: my.string
type: String
value: foo"bar # The double quote needs escaping.
mirror: never
include: foobar.h
# A comment.
- name: my.string2
type: String
value: "foobar" # This string is quoted.
mirror: never
# A comment.
- name: my.atomic.bool
type: RelaxedAtomicBool
value: true
mirror: always
rust: true
# A comment.
- name: my.datamutex.string
type: DataMutexString
value: "foobar" # This string is quoted.
mirror: always
# Mirrored string-valued prefs are interesting in Rust.
- name: my.datamutex.string.rust
type: DataMutexString
value: ""
mirror: always
rust: true
# YAML+Python interprets `10 + 10 * 20` as a string, and so it is printed
# unchanged.
- name: my.atomic.int
type: ReleaseAcquireAtomicInt32
value: 10 + 10 * 20
mirror: always
do_not_use_directly: true # A comment.
# YAML+Python changes `0x44` to `68` because it interprets the value as an
# integer.
- name: my.atomic.uint
type: SequentiallyConsistentAtomicUint32
value: 0x44
mirror: once
# YAML+Python changes `.4455667` to `0.4455667` because it interprets the value
# as a float.
- name: my-dashed.atomic.float
type: AtomicFloat
value: .4455667
mirror: never
include: <math.h>
"""
# The corresponding code for good_input.
good = {}
good[
"static_pref_list_all_h"
] = """\
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
#include "mozilla/StaticPrefList_my.h"
#include "mozilla/StaticPrefList_my_dashed.h"
"""
good[
"static_prefs_all_h"
] = """\
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
#include "mozilla/StaticPrefs_my.h"
#include "mozilla/StaticPrefs_my_dashed.h"
"""
good["static_pref_list_group_h"] = {
"my": """\
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
NEVER_PREF("my.bool", bool, false)
ONCE_PREF(
"my.int",
my_int,
my_int_AtStartup,
int32_t, -123
)
ALWAYS_PREF(
"my.uint",
my_uint,
my_uint,
uint32_t, 999
)
ONCE_PREF(
"my.float",
my_float,
my_float_AtStartup_DoNotUseDirectly,
float, 0.0f
)
NEVER_PREF("my.string", String, "foo\\"bar")
NEVER_PREF("my.string2", String, "foobar")
ALWAYS_PREF(
"my.atomic.bool",
my_atomic_bool,
my_atomic_bool,
RelaxedAtomicBool, true
)
ALWAYS_DATAMUTEX_PREF(
"my.datamutex.string",
my_datamutex_string,
my_datamutex_string,
DataMutexString, "foobar"_ns
)
ALWAYS_DATAMUTEX_PREF(
"my.datamutex.string.rust",
my_datamutex_string_rust,
my_datamutex_string_rust,
DataMutexString, ""_ns
)
ALWAYS_PREF(
"my.atomic.int",
my_atomic_int,
my_atomic_int_DoNotUseDirectly,
ReleaseAcquireAtomicInt32, 10 + 10 * 20
)
ONCE_PREF(
"my.atomic.uint",
my_atomic_uint,
my_atomic_uint_AtStartup,
SequentiallyConsistentAtomicUint32, 68
)
""",
"my_dashed": """\
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
NEVER_PREF("my-dashed.atomic.float", AtomicFloat, 0.4455667)
""",
}
good["static_prefs_group_h"] = {
"my": """\
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
// Include it to gain access to StaticPrefs::my_*.
#ifndef mozilla_StaticPrefs_my_h
#define mozilla_StaticPrefs_my_h
#include "foobar.h"
#include "mozilla/StaticPrefListBegin.h"
#include "mozilla/StaticPrefList_my.h"
#include "mozilla/StaticPrefListEnd.h"
#endif // mozilla_StaticPrefs_my_h
"""
}
good[
"static_prefs_c_getters_cpp"
] = """\
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
extern "C" uint32_t StaticPrefs_my_uint() {
return mozilla::StaticPrefs::my_uint();
}
extern "C" float StaticPrefs_my_float_AtStartup_DoNotUseDirectly() {
return mozilla::StaticPrefs::my_float_AtStartup_DoNotUseDirectly();
}
extern "C" bool StaticPrefs_my_atomic_bool() {
return mozilla::StaticPrefs::my_atomic_bool();
}
extern "C" void StaticPrefs_my_datamutex_string_rust(nsACString *result) {
const auto preflock = mozilla::StaticPrefs::my_datamutex_string_rust();
result->Append(*preflock);
}
"""
good[
"static_prefs_rs"
] = """\
// This file was generated by generate_static_pref_list.py from (string input). DO NOT EDIT.
pub use nsstring::nsCString;
extern "C" {
pub fn StaticPrefs_my_uint() -> u32;
pub fn StaticPrefs_my_float_AtStartup_DoNotUseDirectly() -> f32;
pub fn StaticPrefs_my_atomic_bool() -> bool;
pub fn StaticPrefs_my_datamutex_string_rust(result: *mut nsstring::nsACString);
}
#[macro_export]
macro_rules! pref {
("my.uint") => (unsafe { $crate::StaticPrefs_my_uint() });
("my.float") => (unsafe { $crate::StaticPrefs_my_float_AtStartup_DoNotUseDirectly() });
("my.atomic.bool") => (unsafe { $crate::StaticPrefs_my_atomic_bool() });
("my.datamutex.string.rust") => (unsafe { let mut result = $crate::nsCString::new(); $crate::StaticPrefs_my_datamutex_string_rust(&mut *result); result });
}
"""
# A lot of bad inputs, each with an accompanying error message. Listed in order
# of the relevant `error` calls within generate_static_pref_list.py.
bad_inputs = [
(
"""
- invalidkey: 3
""",
"invalid key `invalidkey`",
),
(
"""
- type: int32_t
""",
"missing `name` key",
),
(
"""
- name: 99
""",
"non-string `name` value `99`",
),
(
"""
- name: name_with_no_dot
""",
"`name` value `name_with_no_dot` lacks a '.'",
),
(
"""
- name: pref.is.defined.more.than.once
type: bool
value: false
mirror: never
- name: pref.is.defined.more.than.once
type: int32_t
value: 111
mirror: always
""",
"`pref.is.defined.more.than.once` pref is defined more than once",
),
(
"""
- name: your.pref
type: bool
value: false
mirror: never
- name: my.pref
type: bool
value: false
mirror: never
""",
"`my.pref` pref must come before `your.pref` pref",
),
(
"""
- name: missing.type.key
value: false
mirror: never
""",
"missing `type` key for pref `missing.type.key`",
),
(
"""
- name: invalid.type.value
type: const char*
value: true
mirror: never
""",
"invalid `type` value `const char*` for pref `invalid.type.value`",
),
(
"""
- name: missing.value.key
type: int32_t
mirror: once
""",
"missing `value` key for pref `missing.value.key`",
),
(
"""
- name: non-string.value
type: String
value: 3.45
mirror: once
""",
"non-string `value` value `3.45` for `String` pref `non-string.value`; add double quotes",
),
(
"""
- name: invalid.boolean.value
type: bool
value: true || false
mirror: once
""",
"invalid boolean value `true || false` for pref `invalid.boolean.value`",
),
(
"""
- name: missing.mirror.key
type: int32_t
value: 3
""",
"missing `mirror` key for pref `missing.mirror.key`",
),
(
"""
- name: invalid.mirror.value
type: bool
value: true
mirror: sometimes
""",
"invalid `mirror` value `sometimes` for pref `invalid.mirror.value`",
),
(
"""
- name: non-boolean.do_not_use_directly.value
type: bool
value: true
mirror: always
do_not_use_directly: 0
""",
"non-boolean `do_not_use_directly` value `0` for pref "
"`non-boolean.do_not_use_directly.value`",
),
(
"""
- name: do_not_use_directly.uselessly.set
type: int32_t
value: 0
mirror: never
do_not_use_directly: true
""",
"`do_not_use_directly` uselessly set with `mirror` value `never` for "
"pref `do_not_use_directly.uselessly.set`",
),
(
"""
- name: non-string.include.value
type: bool
value: true
mirror: always
include: 33
""",
"non-string `include` value `33` for pref `non-string.include.value`",
),
(
"""
- name: include.value.starts.with
type: float
value: M_PI
mirror: never
include: <cmath
""",
"`include` value `<cmath` starts with `<` but does not end with `>` for "
"pref `include.value.starts.with`",
),
(
"""
- name: non-boolean.rust.value
type: bool
value: true
mirror: always
rust: 1
""",
"non-boolean `rust` value `1` for pref `non-boolean.rust.value`",
),
(
"""
- name: rust.uselessly.set
type: int32_t
value: 0
mirror: never
rust: true
""",
"`rust` uselessly set with `mirror` value `never` for pref "
"`rust.uselessly.set`",
),
]
class TestGenerateStaticPrefList(unittest.TestCase):
"""
Unit tests for generate_static_pref_list.py.
"""
def test_good(self):
"Test various pieces of good input."
inp = StringIO(good_input)
pref_list = yaml.safe_load(inp)
code = generate_code(pref_list, "(string input)")
self.assertEqual(good["static_pref_list_all_h"], code["static_pref_list_all_h"])
self.assertEqual(good["static_prefs_all_h"], code["static_prefs_all_h"])
self.assertEqual(
good["static_pref_list_group_h"]["my"],
code["static_pref_list_group_h"]["my"],
)
self.assertEqual(
good["static_pref_list_group_h"]["my_dashed"],
code["static_pref_list_group_h"]["my_dashed"],
)
self.assertEqual(
good["static_prefs_group_h"]["my"], code["static_prefs_group_h"]["my"]
)
self.assertEqual(
good["static_prefs_c_getters_cpp"], code["static_prefs_c_getters_cpp"]
)
self.assertEqual(good["static_prefs_rs"], code["static_prefs_rs"])
def test_bad(self):
"Test various pieces of bad input."
for input_string, expected in bad_inputs:
inp = StringIO(input_string)
try:
pref_list = yaml.safe_load(inp)
generate_code(pref_list, "(string input")
self.assertEqual(0, 1)
except ValueError as e:
self.assertEqual(str(e), expected)
if __name__ == "__main__":
mozunit.main()
|