[vala/staging] vala: Add a basic parameter check for [Print] methods



commit a6112f0e7fe849a19fb26db1a56d55dc81659d25
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Thu Nov 21 08:37:32 2019 +0100

    vala: Add a basic parameter check for [Print] methods
    
    and add tests to increase coverage
    
    Introdruced with ea8cd97480a7a560cfd8ae3f060f63638b7d9de4

 tests/Makefile.am                          |  2 ++
 tests/methods/print-attribute-invalid.test |  8 ++++++
 tests/methods/print-attribute.vala         | 43 ++++++++++++++++++++++++++++++
 vala/valamethod.vala                       |  5 ++++
 4 files changed, 58 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9d2e5cc6d..37612b87f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..43e0321fd
--- /dev/null
+++ b/tests/methods/print-attribute-invalid.test
@@ -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
index 000000000..c69e59575
--- /dev/null
+++ b/tests/methods/print-attribute.vala
@@ -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);
+       }
+}
diff --git a/vala/valamethod.vala b/vala/valamethod.vala
index 4af74c759..a105a0d0f 100644
--- a/vala/valamethod.vala
+++ b/vala/valamethod.vala
@@ -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);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]