]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0233: Vim9: string() output of enum is problematic v9.1.0233
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 31 Mar 2024 16:45:35 +0000 (18:45 +0200)
committerChristian Brabandt <cb@256bit.org>
Sun, 31 Mar 2024 16:45:35 +0000 (18:45 +0200)
Problem:  Vim9: string() output of enum is problematic
Solution: Make string() output for an enum consistent with that of a
          regular object (Yegappan Lakshmanan).

closes: #14343

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/doc/builtin.txt
src/testdir/test_vim9_enum.vim
src/version.c
src/vim9class.c

index 9e57a8cc4a7148932beafc954d4b5b435a92efbc..088daa75a35705a242f602513ab17116d72b86f4 100644 (file)
@@ -9599,7 +9599,7 @@ string({expr})    Return {expr} converted to a String.  If {expr} is a Number,
                        Class           class SomeName
                        Object          object of SomeName {lnum: 1, col: 3}
                        Enum            enum EnumName
-                       EnumValue       enum.value
+                       EnumValue       enum name.value {name: str, ordinal: nr}
 
                When a |List| or |Dictionary| has a recursive reference it is
                replaced by "[...]" or "{...}".  Using eval() on the result
index ceea32fe4f315c5b8cd981691304425b70c72427..5f9fbff6d33603e9559d79b43bd3b642a201e1df 100644 (file)
@@ -913,7 +913,23 @@ def Test_enum_string()
       Ford
     endenum
     assert_equal("enum Car", string(Car))
-    assert_equal("Car.Honda", string(Car.Honda))
+    assert_equal("enum Car.Honda {name: 'Honda', ordinal: 0}", string(Car.Honda))
+  END
+  v9.CheckSourceSuccess(lines)
+
+  # customized string function
+  lines =<< trim END
+    vim9script
+    enum Dir
+      North,
+      South
+
+      def string(): string
+        return $'Dir.{this.name}'
+      enddef
+    endenum
+    assert_equal('Dir.North', string(Dir.North))
+    assert_equal('Dir.South', string(Dir.South))
   END
   v9.CheckSourceSuccess(lines)
 enddef
@@ -938,7 +954,7 @@ def Test_enum_import()
     assert_equal(true, s1 == mod.Star.Orion)
     assert_equal(2, mod.Star.Pisces.ordinal)
     var l1: list<mod.Star> = mod.Star.values
-    assert_equal("Star.Orion", string(l1[1]))
+    assert_equal("enum Star.Orion {name: 'Orion', ordinal: 1}", string(l1[1]))
     assert_equal(s1, l1[1])
 
     def Fn()
@@ -946,7 +962,7 @@ def Test_enum_import()
       assert_equal(true, s2 == mod.Star.Orion)
       assert_equal(2, mod.Star.Pisces.ordinal)
       var l2: list<mod.Star> = mod.Star.values
-      assert_equal("Star.Orion", string(l2[1]))
+      assert_equal("enum Star.Orion {name: 'Orion', ordinal: 1}", string(l2[1]))
       assert_equal(s2, l2[1])
     enddef
     Fn()
@@ -1251,9 +1267,9 @@ def Test_enum_this_in_constructor()
   var lines =<< trim END
     vim9script
     enum A
-      Red("A.Red"),
-      Blue("A.Blue"),
-      Green("A.Green")
+      Red("enum A.Red {name: 'Red', ordinal: 0}"),
+      Blue("enum A.Blue {name: 'Blue', ordinal: 1}"),
+      Green("enum A.Green {name: 'Green', ordinal: 2}")
 
       def new(s: string)
         assert_equal(s, string(this))
@@ -1457,7 +1473,7 @@ def Test_enum_class_variable()
   v9.CheckSourceSuccess(lines)
 enddef
 
-" Test for converting an enum value to a string and then back to an enum value
+" Test for converting a string to an enum value
 def Test_enum_eval()
   var lines =<< trim END
     vim9script
@@ -1465,10 +1481,11 @@ def Test_enum_eval()
       Red,
       Blue
     endenum
-    var s: string = string(Color.Blue)
-    var e = eval(s)
+    var e = eval('Color.Blue')
     assert_equal(Color.Blue, e)
     assert_equal(1, e.ordinal)
+    assert_fails("eval('Color.Green')", 'E1422: Enum value "Green" not found in enum "Color"')
+    assert_fails("var x = eval('Color')", 'E1421: Enum "Color" cannot be used as a value')
   END
   v9.CheckSourceSuccess(lines)
 enddef
index 5069f721e9d007792b0b159b16b14ff603a81e8a..1f748c82601f74bc098a069f78fe6786b4eca2a5 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    233,
 /**/
     232,
 /**/
index 2d60ba95a6584d6577d31e154e1663e7b3464a17..fc4e002857fbe2f089dffae19080e26b70b1852b 100644 (file)
@@ -3844,16 +3844,18 @@ object_string(
        class_T *cl = obj == NULL ? NULL : obj->obj_class;
        if (cl != NULL && IS_ENUM(cl))
        {
+           ga_concat(&ga, (char_u *)"enum ");
            ga_concat(&ga, cl->class_name);
-           char_u *name = ((typval_T *)(obj + 1))->vval.v_string;
+           char_u *enum_name = ((typval_T *)(obj + 1))->vval.v_string;
            ga_concat(&ga, (char_u *)".");
-           ga_concat(&ga, name);
-           return ga.ga_data;
+           ga_concat(&ga, enum_name);
+       }
+       else
+       {
+           ga_concat(&ga, (char_u *)"object of ");
+           ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
+                   : cl->class_name);
        }
-
-       ga_concat(&ga, (char_u *)"object of ");
-       ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
-               : cl->class_name);
        if (cl != NULL)
        {
            ga_concat(&ga, (char_u *)" {");