]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Add a basic parameter check for [Print] methods
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 21 Nov 2019 07:37:32 +0000 (08:37 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 21 Nov 2019 08:16:27 +0000 (09:16 +0100)
and add tests to increase coverage

Introdruced with ea8cd97480a7a560cfd8ae3f060f63638b7d9de4

tests/Makefile.am
tests/methods/print-attribute-invalid.test [new file with mode: 0644]
tests/methods/print-attribute.vala [new file with mode: 0644]
vala/valamethod.vala

index 9d2e5cc6d953db17b745741481b82d3be07f6585..37612b87f9df54059e92c1bf6bdf02a287080c4a 100644 (file)
@@ -152,6 +152,8 @@ TESTS = \
        methods/bug791283.vala \
        methods/argument-array-initilizer.vala \
        methods/generics.vala \
+       methods/print-attribute.vala \
+       methods/print-attribute-invalid.test \
        methods/printf-invalid.test \
        methods/printf-constructor.vala \
        methods/printf-constructor-invalid.test \
diff --git a/tests/methods/print-attribute-invalid.test b/tests/methods/print-attribute-invalid.test
new file mode 100644 (file)
index 0000000..43e0321
--- /dev/null
@@ -0,0 +1,8 @@
+Invalid Code
+
+[Print]
+void foo (int i, string s) {
+}
+
+void main () {
+}
diff --git a/tests/methods/print-attribute.vala b/tests/methods/print-attribute.vala
new file mode 100644 (file)
index 0000000..c69e595
--- /dev/null
@@ -0,0 +1,43 @@
+errordomain FooError {
+       FAIL
+}
+
+class Foo {
+       [Print]
+       public void foo (string s) {
+               assert (this != null);
+               assert (s == "4711Footrue");
+       }
+}
+
+[Print]
+void foo (string s) {
+       assert (s == "232.7182footrue");
+}
+
+[Print]
+void bar (string s) throws Error {
+       assert (s == "423.1415barfalse");
+       throw new FooError.FAIL ("bar");
+}
+
+void main () {
+       {
+               foo (23, 2.7182f, "foo", true);
+       }
+
+       bool reached = false;
+       try {
+               bar (42, 3.1415f, "bar", false);
+       } catch (FooError.FAIL e) {
+               reached = true;
+       } catch {
+               assert_not_reached ();
+       }
+       assert (reached);
+
+       {
+               var f = new Foo ();
+               f.foo (4711, "Foo", true);
+       }
+}
index 4af74c759348b7380a8adef92976227ac8c626f1..a105a0d0f94d5564e0108651afba20880ea8640d 100644 (file)
@@ -808,6 +808,11 @@ public class Vala.Method : Subroutine, Callable {
                        Report.error (parameters[0].source_reference, "Named parameter required before `...'");
                }
 
+               if (get_attribute ("Print") != null && (parameters.size != 1 || parameters[0].variable_type.type_symbol != context.analyzer.string_type.type_symbol)) {
+                       error = true;
+                       Report.error (source_reference, "[Print] methods must have exactly one parameter of type `string'");
+               }
+
                var optional_param = false;
                foreach (Parameter param in parameters) {
                        param.check (context);