diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2016-03-20 18:53:01 +0200 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2016-03-20 18:53:01 +0200 |
commit | 4bb665a577a90e3bc0d2e832d6950f0323a72492 (patch) | |
tree | 9e4409866bd8bec973de92e9667646c51f651310 | |
parent | a3d49ca1ce5e6844387e8daedd2fc0713f36e540 (diff) | |
parent | 3eea1703ffb7559cc7d7454ced119aa45c167c9f (diff) | |
download | meson-4bb665a577a90e3bc0d2e832d6950f0323a72492.zip meson-4bb665a577a90e3bc0d2e832d6950f0323a72492.tar.gz meson-4bb665a577a90e3bc0d2e832d6950f0323a72492.tar.bz2 |
Merge pull request #464 from tp-m/bool-to-string-and-to-int
Add bool to_string() and to_int() methods.
-rw-r--r-- | mesonbuild/interpreter.py | 26 | ||||
-rw-r--r-- | test cases/common/42 string formatting/meson.build | 6 | ||||
-rw-r--r-- | test cases/common/68 number arithmetic/meson.build | 3 |
3 files changed, 35 insertions, 0 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index e20641d..f4b6958 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -2017,6 +2017,30 @@ class Interpreter(): reduced_pos = [reduced_pos] return (reduced_pos, reduced_kw) + def bool_method_call(self, obj, method_name, args): + obj = self.to_native(obj) + (posargs, _) = self.reduce_arguments(args) + if method_name == 'to_string': + if len(posargs) == 0: + if obj == True: + return 'true' + else: + return 'false' + elif len(posargs) == 2 and isinstance(posargs[0], str) and isinstance(posargs[1], str): + if obj == True: + return posargs[0] + else: + return posargs[1] + else: + raise InterpreterException('bool.to_string() must have either no arguments or exactly two string arguments.') + elif method_name == 'to_int': + if obj == True: + return 1 + else: + return 0 + else: + raise InterpreterException('Unknown method "%s" for a boolean.' % method_name) + def string_method_call(self, obj, method_name, args): obj = self.to_native(obj) (posargs, _) = self.reduce_arguments(args) @@ -2089,6 +2113,8 @@ class Interpreter(): obj = obj.get_value() if isinstance(obj, str): return self.string_method_call(obj, method_name, args) + if isinstance(obj, bool): + return self.bool_method_call(obj, method_name, args) if isinstance(obj, list): return self.array_method_call(obj, method_name, self.reduce_arguments(args)[0]) if not isinstance(obj, InterpreterObject): diff --git a/test cases/common/42 string formatting/meson.build b/test cases/common/42 string formatting/meson.build index 99855b3..c2ee151 100644 --- a/test cases/common/42 string formatting/meson.build +++ b/test cases/common/42 string formatting/meson.build @@ -45,3 +45,9 @@ assert('#include <foo/bar.h>'.underscorify() == '_include__foo_bar_h_', 'Broken assert('Do SomeThing 09'.underscorify() == 'Do_SomeThing_09', 'Broken underscorify') assert('3'.to_int() == 3, 'String int conversion does not work.') + +assert(true.to_string() == 'true', 'bool string conversion failed') +assert(false.to_string() == 'false', 'bool string conversion failed') +assert(true.to_string('yes', 'no') == 'yes', 'bool string conversion with args failed') +assert(false.to_string('yes', 'no') == 'no', 'bool string conversion with args failed') +assert('@0@'.format(true) == 'true', 'bool string formatting failed') diff --git a/test cases/common/68 number arithmetic/meson.build b/test cases/common/68 number arithmetic/meson.build index 3872a11..4b98d73 100644 --- a/test cases/common/68 number arithmetic/meson.build +++ b/test cases/common/68 number arithmetic/meson.build @@ -32,3 +32,6 @@ assert(not(3 > 4), 'Gt broken') assert(4 >= 3, 'Gte broken') assert(not(3 >= 4), 'Gte broken') assert(3 >= 3, 'Gte broken') + +assert(true.to_int() == 1,'bool to_int() broken') +assert(false.to_int() == 0,'bool to_int() broken') |