Mercurial > vim
changeset 34709:a1d0ef54a24b v9.1.0233
patch 9.1.0233: Vim9: string() output of enum is problematic
Commit: https://github.com/vim/vim/commit/3cf121ed31f7a022e2ae6585391434d9c88e9792
Author: Yegappan Lakshmanan <yegappan@yahoo.com>
Date: Sun Mar 31 18:45:35 2024 +0200
patch 9.1.0233: Vim9: string() output of enum is problematic
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>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 31 Mar 2024 19:00:02 +0200 |
parents | 4dfddd4e4286 |
children | 72eb51a7cfcd |
files | runtime/doc/builtin.txt src/testdir/test_vim9_enum.vim src/version.c src/vim9class.c |
diffstat | 4 files changed, 38 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -9599,7 +9599,7 @@ string({expr}) Return {expr} converted t 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
--- a/src/testdir/test_vim9_enum.vim +++ b/src/testdir/test_vim9_enum.vim @@ -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
--- a/src/version.c +++ b/src/version.c @@ -705,6 +705,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 233, +/**/ 232, /**/ 231,
--- a/src/vim9class.c +++ b/src/vim9class.c @@ -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); } - - ga_concat(&ga, (char_u *)"object of "); - ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]" - : cl->class_name); + else + { + 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 *)" {");