vala-tests r8 - trunk/tests/examples



Author: malureau
Date: Fri Oct 17 20:39:52 2008
New Revision: 8
URL: http://svn.gnome.org/viewvc/vala-tests?rev=8&view=rev

Log:
Add wiki language features samples

Added:
   trunk/tests/examples/advanced.test   (contents, props changed)
   trunk/tests/examples/advanced.vala
   trunk/tests/examples/basic.test   (contents, props changed)
   trunk/tests/examples/basic.vala
   trunk/tests/examples/list.test   (contents, props changed)
   trunk/tests/examples/list.vala
   trunk/tests/examples/properties-construction.test   (contents, props changed)
   trunk/tests/examples/properties-construction.vala
   trunk/tests/examples/properties.test   (contents, props changed)
   trunk/tests/examples/properties.vala
   trunk/tests/examples/string.test   (contents, props changed)
   trunk/tests/examples/string.vala
   trunk/tests/examples/update.sh   (contents, props changed)

Added: trunk/tests/examples/advanced.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/advanced.test	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+SRCDIR=../tests/examples # this is not nice
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+set -e
+
+TESTNAME=`basename $0 .test`
+
+$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
+./$TESTNAME

Added: trunk/tests/examples/advanced.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/advanced.vala	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,39 @@
+/* Advanced Vala sample code */
+
+using GLib;
+
+/* class derived from GObject */
+public class AdvancedSample : Object {
+
+    /* automatic property, data field is implicit */
+    public string name { get; construct set; }
+
+    /* signal */
+    public signal void foo ();
+
+    /* creation method, setting construction property */
+    public AdvancedSample (string name) {
+        this.name = name;
+    }
+
+    /* public instance method */
+    public void run () {
+        /* assigning anonymous method as signal handler */
+        this.foo += s => {
+            stdout.printf ("Lambda expression %s!\n", name);
+        };
+
+        /* Calling lambda expression */
+        this.foo ();
+    }
+
+    /* application entry point */
+    public static int main (string[] args) {
+        foreach (string arg in args) {
+            var sample = new AdvancedSample (arg);
+            sample.run ();
+            /* "sample" is freed as block ends */
+        }
+        return 0;
+    }
+}

Added: trunk/tests/examples/basic.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/basic.test	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+SRCDIR=../tests/examples # this is not nice
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+set -e
+
+TESTNAME=`basename $0 .test`
+
+$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
+./$TESTNAME

Added: trunk/tests/examples/basic.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/basic.vala	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,23 @@
+/* Basic Vala sample code */
+
+using GLib;
+
+/* class derived from GObject */
+public class BasicSample : Object {
+
+    /* public instance method */
+    public void run () {
+        stdout.printf ("Hello World\n");
+    }
+
+    /* application entry point */
+    public static int main (string[] args) {
+        // instantiate this class, assigning the instance to
+        // a type-inferred variable
+        var sample = new BasicSample ();
+        // call the run method
+        sample.run ();
+        // return from this main method
+        return 0;
+    }
+}

Added: trunk/tests/examples/list.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/list.test	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+SRCDIR=../tests/examples # this is not nice
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+set -e
+
+TESTNAME=`basename $0 .test`
+
+$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
+./$TESTNAME

Added: trunk/tests/examples/list.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/list.vala	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,22 @@
+using GLib;
+
+public static int main (string[] args) {
+    List<string> list = new List<string> ();
+    list.append ("one");
+    list.append ("two");
+    list.append ("three");
+    
+    stdout.printf ("list.length () = %u\n", list.length ());
+
+    // Traditional iteration
+    for (int i = 0; i < list.length (); i++) {
+        stdout.printf ("%s\n", list.nth_data (i));
+    }
+
+    // Comfortable iteration
+    foreach (string element in list) {
+        stdout.printf ("%s\n", element);
+    }
+
+    return 0;
+}

Added: trunk/tests/examples/properties-construction.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/properties-construction.test	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+SRCDIR=../tests/examples # this is not nice
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+set -e
+
+TESTNAME=`basename $0 .test`
+
+$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
+./$TESTNAME

Added: trunk/tests/examples/properties-construction.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/properties-construction.vala	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,81 @@
+using GLib;
+
+public class MyProperty : Object {
+
+    private static uint step = 0;
+    private int _c_g_s_prop;
+    private int _c_o_prop;
+    private int _g_s_prop;
+
+    public int construct_only_prop {
+        construct {
+            stdout.printf ("==== Step %u: construct_only ====\n", step);
+            stdout.printf ("construct_only (before): %d\n", _c_o_prop);
+            _c_o_prop = value;
+            stdout.printf ("construct_only (after): %d\n\n", _c_o_prop);
+            step++;
+        }
+        get {
+            return _c_o_prop;
+        }
+    }
+
+    public int construct_get_set_prop {
+        construct set {
+            stdout.printf ("==== Step %u: construct_get_set ====\n", step);
+            stdout.printf ("construct_get_set (before): %d\n", _c_g_s_prop);
+            _c_g_s_prop = value;
+            stdout.printf ("construct_get_set (after): %d\n\n", _c_g_s_prop);
+            step++;
+        }
+        get {
+            return _c_g_s_prop;
+        }
+    }
+
+    public int get_set_prop {
+        set {
+            stdout.printf ("======= Step %u: get_set =======\n", step);
+            stdout.printf ("get_set_prop (before): %d\n", _g_s_prop);
+            _g_s_prop = value;
+            stdout.printf ("get_set_prop (after): %d\n\n", _g_s_prop);
+            step++;
+        }
+        get {
+            return _g_s_prop;
+        }
+    }
+
+    construct {
+        stdout.printf ("================ Step %u: constructor =================\n", step);
+        stdout.printf ("construct_only, construct_set_get, get_set %d, %d, %d\n\n",
+                       this.construct_only_prop, this.construct_get_set_prop,
+                       this.get_set_prop);
+        step++;
+        this.get_set_prop = 5; // You shouldn't do that! Better set some fields in construct.
+        stdout.printf ("construct_only, construct_set_get, get_set %d, %d, %d\n",
+                       this.construct_only_prop, this.construct_get_set_prop,
+                       this.get_set_prop);
+        stdout.printf ("================== end of constructor =================\n\n");
+    }
+
+    public MyProperty (int construct_only_prop,
+                       int construct_get_set_prop,
+                       int get_set_prop) {
+        this.construct_only_prop = construct_only_prop;
+        this.construct_get_set_prop = construct_get_set_prop;
+        this.get_set_prop = get_set_prop;
+    }
+}
+
+public class Test {
+
+    public static void main (string[] args) {
+        stdout.printf ("=========== Construction process: MyProperty (1, 2, 3) ==========\n\n");
+        var prop_test = new MyProperty (1, 2, 3);
+        stdout.printf ("================== End of construction process ==================\n\n");
+        stdout.printf ("======== Set construct_get_set = 222, get_set_prop = 333 ========\n\n");
+        prop_test.construct_get_set_prop = 222;
+        prop_test.get_set_prop = 333;
+    }
+}

Added: trunk/tests/examples/properties.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/properties.test	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+SRCDIR=../tests/examples # this is not nice
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+set -e
+
+TESTNAME=`basename $0 .test`
+
+$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
+./$TESTNAME

Added: trunk/tests/examples/properties.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/properties.vala	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,52 @@
+using GLib;
+
+public class Sample : Object {
+
+    private string automatic { get; set; }
+
+    private string _name;
+    [Notify]                // will be unnecessary with Vala 0.3.5
+		public string name {
+        get { return _name; }
+        set { _name = value; }
+    }
+
+    private string _read_only;
+    public string read_only {
+        get { return _read_only; }
+    }
+
+    public Sample (string name) {
+        this.name = name;
+    }
+
+    construct {
+        _automatic = "InitialAutomatic";
+        _read_only = "InitialReadOnly";
+    }
+
+    public void run () {
+        notify += (s, p) => {
+            stdout.printf ("property `%s' has changed!\n", p.name);
+        };
+
+        automatic = "TheNewAutomatic";
+        name = "TheNewName";
+
+        // The following statement would be rejected
+        // read_only = "TheNewReadOnly";
+
+        stdout.printf ("automatic: %s\n", automatic);
+        stdout.printf ("name: %s\n", name);
+        stdout.printf ("read_only: %s\n", read_only);
+        stdout.printf ("automatic: %s\n", automatic);
+    }
+
+    static int main (string[] args) {
+        var test = new Sample ("InitialName");
+
+        test.run ();
+
+        return 0;
+    }
+}

Added: trunk/tests/examples/string.test
==============================================================================
--- (empty file)
+++ trunk/tests/examples/string.test	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+SRCDIR=../tests/examples # this is not nice
+
+if [ "x$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+set -e
+
+TESTNAME=`basename $0 .test`
+
+$VALAC -o $TESTNAME $SRCDIR/$TESTNAME.vala
+./$TESTNAME

Added: trunk/tests/examples/string.vala
==============================================================================
--- (empty file)
+++ trunk/tests/examples/string.vala	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,86 @@
+using GLib;
+
+static void println (string str) {
+    stdout.printf ("%s\n", str);
+}
+
+static int main (string[] args) {
+
+    // Concatenating strings
+
+    string a = "Concatenated ";
+    string b = "string";
+    string c = a + b;
+    println (c);
+
+    // Building strings
+
+    var builder = new StringBuilder ();
+    builder.append ("built ");
+    builder.prepend ("String ");
+    builder.append ("StringBuilder");
+    builder.append_unichar ('.');
+    builder.insert (13, "by ");
+    println (builder.str);
+
+    // Building strings by format
+
+    string formatted = "PI %s equals %g.".printf ("approximately", Math.PI);
+    println (formatted);
+
+    // Comparing strings
+
+    a = "foo";
+    b = "foo";
+    if (a == b) {
+        println ("String == operator compares content, not reference.");
+    } else {
+        println ("Shouldn't happen.");
+    }
+
+    // Switch statement
+
+    string s = "vala";
+    switch (s) {
+    case "java":
+        println ("Shouldn't happen.");
+        break;
+    case "vala":
+        println ("Switch statement works fine with strings.");
+        break;
+    case "ruby":
+        println ("Shouldn't happen.");
+        break;
+    }
+
+    // Verbatim strings
+
+    string verbatim = """This is a so-called "verbatim string".
+Verbatim strings don't process escape sequences, such as \n, \t, \\, etc.
+They may contain quotes and may span multiple lines. Since Vala 0.3.3""";
+    println (verbatim);
+
+    // Some string operations
+
+    println ("from lower case to upper case".up ());
+    println ("reversed string".reverse ());
+    println ("...substring...".substring (3, 9));
+
+    // String dimensions
+
+    string dessert = "crÃme brÃlÃe";
+    print ("The string '%s' is %ld characters long and is stored in %ld bytes\n",
+           dessert, dessert.len (), dessert.size ());
+
+    // Regular expressions
+
+    try {
+        var regex = new Regex ("(jaguar|tiger|leopard)");
+        s = "wolf, tiger, eagle, jaguar, leopard, bear";
+        println (regex.replace (s, s.size (), 0, "kitty"));
+    } catch (RegexError e) {
+        warning (e.message);
+    }
+
+    return 0;
+}
\ No newline at end of file

Added: trunk/tests/examples/update.sh
==============================================================================
--- (empty file)
+++ trunk/tests/examples/update.sh	Fri Oct 17 20:39:52 2008
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+set -e
+
+for file in *.vala ; do
+
+test=`basename $file .vala`
+cat >$test.test <<EOF
+#!/bin/sh
+
+SRCDIR=../tests/examples # this is not nice
+
+if [ "x\$VALAC" = "x" ] ; then
+  VALAC=valac
+  SRCDIR=.
+fi
+
+set -e
+
+TESTNAME=\`basename \$0 .test\`
+
+\$VALAC -o \$TESTNAME \$SRCDIR/\$TESTNAME.vala
+./\$TESTNAME
+EOF
+
+chmod +x $test.test
+
+done
\ No newline at end of file



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