[vala/wip/issue/327: 12/27] Added with statement tests



commit 14d90328d4be1a0d0a84a6825b0ecc5ef42c4bdc
Author: Nick Schrader <nick schrader mailbox org>
Date:   Mon Mar 30 21:29:18 2020 -0300

    Added with statement tests

 tests/Makefile.am                            |  6 ++
 tests/basic-types/with-value-type.vala       | 10 ++++
 tests/objects/with-expression.vala           | 34 +++++++++++
 tests/objects/with-instance.vala             | 67 +++++++++++++++++++++
 tests/objects/with-nested.vala               | 88 ++++++++++++++++++++++++++++
 tests/semantic/with-no-such-member.test      |  9 +++
 tests/semantic/with-no-such-with-member.test |  9 +++
 7 files changed, 223 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index af8fe4d74..4e8ed72b2 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -40,6 +40,7 @@ TESTS = \
        basic-types/gvariants.vala \
        basic-types/gvariants-unboxing-safe.vala \
        basic-types/null.vala \
+       basic-types/with-value-type.vala \
        basic-types/bug570846.test \
        basic-types/bug571486.vala \
        basic-types/bug591552.vala \
@@ -499,6 +500,9 @@ TESTS = \
        objects/bug795225-3.test \
        objects/bug795225-4.test \
        objects/bug795521.vala \
+       objects/with-expression.vala \
+       objects/with-instance.vala \
+       objects/with-nested.vala \
        errors/catch-error-code.vala \
        errors/catch-in-finally.vala \
        errors/default-gtype.vala \
@@ -919,6 +923,8 @@ TESTS = \
        semantic/unary-unsupported-increment.test \
        semantic/unary-unsupported-minus.test \
        semantic/unary-unsupported-negation.test \
+       semantic/with-no-such-member.test \
+       semantic/with-no-such-with-member.test \
        semantic/yield-call-requires-async-context.test \
        semantic/yield-call-requires-async-method.test \
        semantic/yield-creation-requires-async-context.test \
diff --git a/tests/basic-types/with-value-type.vala b/tests/basic-types/with-value-type.vala
new file mode 100644
index 000000000..5b028eab4
--- /dev/null
+++ b/tests/basic-types/with-value-type.vala
@@ -0,0 +1,10 @@
+void main() {
+    int i = 0;
+    string s;
+
+    with(i) { 
+        s = i.to_string();
+    }
+
+    assert (s == "0");
+}
diff --git a/tests/objects/with-expression.vala b/tests/objects/with-expression.vala
new file mode 100644
index 000000000..07d1f27b3
--- /dev/null
+++ b/tests/objects/with-expression.vala
@@ -0,0 +1,34 @@
+class Foo {
+    public static int factory_called = 0;
+    public static Foo factory() {
+        factory_called++;
+        return new Foo();
+    }
+
+    public static int method_called = 0;
+    public void method() {
+        method_called++;
+    }
+}
+
+void test() {
+    var foo = new Foo();
+    with(foo)
+        method();
+    
+    with(new Foo())
+        method();
+    
+    with(Foo.factory()) {
+        method();
+        method();
+    }
+
+    assert(Foo.method_called == 4);
+    assert(Foo.factory_called == 1);
+}
+
+void main() {
+    test();
+}
+
diff --git a/tests/objects/with-instance.vala b/tests/objects/with-instance.vala
new file mode 100644
index 000000000..72f87965e
--- /dev/null
+++ b/tests/objects/with-instance.vala
@@ -0,0 +1,67 @@
+class Foo {
+       public int field;
+    public string prop { get; set; }
+
+    public bool method_called = false;
+    public void method() {
+        method_called = true;
+    }
+}
+
+class Bar : Foo { }
+
+class TestFoo {
+    public int class_field;
+
+    public void test() {
+        var foo = new Foo();
+        var local_field = 0;
+    
+        with(foo) {
+            field = 10;
+            prop = "prop";
+            method();
+    
+            local_field = 20;
+            class_field = 30;
+        }
+    
+        assert (foo.field == 10);
+        assert (foo.prop == "prop");
+        assert (foo.method_called);
+    
+        assert (local_field == 20);
+        assert (class_field == 30);
+    }
+}
+
+// Copy and paste TestFoo, change Foo to Bar
+class TestBar {
+    public int class_field;
+
+    public void test() {
+        var foo = new Bar();
+        var local_field = 0;
+    
+        with(foo) {
+            field = 10;
+            prop = "prop";
+            method();
+    
+            local_field = 20;
+            class_field = 30;
+        }
+    
+        assert (foo.field == 10);
+        assert (foo.prop == "prop");
+        assert (foo.method_called);
+    
+        assert (local_field == 20);
+        assert (class_field == 30);
+    }
+}
+
+void main() {
+    new TestFoo().test();
+    new TestBar().test();
+}
\ No newline at end of file
diff --git a/tests/objects/with-nested.vala b/tests/objects/with-nested.vala
new file mode 100644
index 000000000..8405213d6
--- /dev/null
+++ b/tests/objects/with-nested.vala
@@ -0,0 +1,88 @@
+class Foo {
+    public int field;
+}
+
+class Bar {
+    public int field;
+}
+
+class Test {
+    public int field;
+
+    void method_local() {
+        var field = 0;
+        var foo = new Foo();
+
+        with (foo) {
+            field = 10;
+            //with.field = 20;
+            this.field = 30;
+        }
+
+        assert (foo.field == 10);
+        //assert (field == 20);
+        assert (this.field == 30);
+    }
+
+    void nested() {
+        var field = 0;
+        var foo = new Foo();
+        var bar = new Bar();
+
+        with (foo) {
+            field = 100;
+            with (bar) {
+                field = 200;
+                //with.with.field = 300;
+            }
+        }
+
+        assert (foo.field == 100);
+        assert (bar.field == 200);
+        //assert (field == 300);
+
+        /*with (foo) {
+            field = 1000;
+            with (bar) {
+                field = with.field;
+                this.field = 2000;
+            }
+        }
+
+        assert (bar.field == 1000);
+        assert (this.field == 2000);*/
+    }
+
+    void nested_useless() {
+        var field = 0;
+        var foo = new Foo();
+        var bar = new Bar();
+
+        /*with (foo) {
+            with.this.field = 10000;
+            with.foo.field = 20000;
+        }
+
+        assert (this.field == 10000);
+        assert (foo.field == 20000);
+
+        with (foo) {
+            with (bar) {
+                field = 30000;
+                with.field = 40000;
+                with.with.field = 50000;
+            }
+        }
+
+        assert (bar.field == 30000)
+        assert (foo.field == 40000);
+        assert (field == 50000);*/
+    }
+
+    static int main() {
+        new Test().method_local();
+        new Test().nested();
+        new Test().nested_useless();
+        return 0;
+    }
+}
\ No newline at end of file
diff --git a/tests/semantic/with-no-such-member.test b/tests/semantic/with-no-such-member.test
new file mode 100644
index 000000000..07bcc6281
--- /dev/null
+++ b/tests/semantic/with-no-such-member.test
@@ -0,0 +1,9 @@
+Invalid Code
+
+class Foo { }
+
+void main() {
+    with (new Foo()) {
+        field = 100;
+    }
+}
diff --git a/tests/semantic/with-no-such-with-member.test b/tests/semantic/with-no-such-with-member.test
new file mode 100644
index 000000000..d8fe1e239
--- /dev/null
+++ b/tests/semantic/with-no-such-with-member.test
@@ -0,0 +1,9 @@
+Invalid Code
+
+class Foo { }
+
+void main() {
+    with (new Foo()) {
+        with.field = 10;
+    }
+}


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