0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

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>
This commit is contained in:
Yegappan Lakshmanan
2024-03-31 18:45:35 +02:00
committed by Christian Brabandt
parent 5df961a1bc
commit 3cf121ed31
4 changed files with 38 additions and 17 deletions

View File

@@ -9599,7 +9599,7 @@ string({expr}) Return {expr} converted to a String. If {expr} is a Number,
Class class SomeName Class class SomeName
Object object of SomeName {lnum: 1, col: 3} Object object of SomeName {lnum: 1, col: 3}
Enum enum EnumName 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 When a |List| or |Dictionary| has a recursive reference it is
replaced by "[...]" or "{...}". Using eval() on the result replaced by "[...]" or "{...}". Using eval() on the result

View File

@@ -913,7 +913,23 @@ def Test_enum_string()
Ford Ford
endenum endenum
assert_equal("enum Car", string(Car)) 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 END
v9.CheckSourceSuccess(lines) v9.CheckSourceSuccess(lines)
enddef enddef
@@ -938,7 +954,7 @@ def Test_enum_import()
assert_equal(true, s1 == mod.Star.Orion) assert_equal(true, s1 == mod.Star.Orion)
assert_equal(2, mod.Star.Pisces.ordinal) assert_equal(2, mod.Star.Pisces.ordinal)
var l1: list<mod.Star> = mod.Star.values 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]) assert_equal(s1, l1[1])
def Fn() def Fn()
@@ -946,7 +962,7 @@ def Test_enum_import()
assert_equal(true, s2 == mod.Star.Orion) assert_equal(true, s2 == mod.Star.Orion)
assert_equal(2, mod.Star.Pisces.ordinal) assert_equal(2, mod.Star.Pisces.ordinal)
var l2: list<mod.Star> = mod.Star.values 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]) assert_equal(s2, l2[1])
enddef enddef
Fn() Fn()
@@ -1251,9 +1267,9 @@ def Test_enum_this_in_constructor()
var lines =<< trim END var lines =<< trim END
vim9script vim9script
enum A enum A
Red("A.Red"), Red("enum A.Red {name: 'Red', ordinal: 0}"),
Blue("A.Blue"), Blue("enum A.Blue {name: 'Blue', ordinal: 1}"),
Green("A.Green") Green("enum A.Green {name: 'Green', ordinal: 2}")
def new(s: string) def new(s: string)
assert_equal(s, string(this)) assert_equal(s, string(this))
@@ -1457,7 +1473,7 @@ def Test_enum_class_variable()
v9.CheckSourceSuccess(lines) v9.CheckSourceSuccess(lines)
enddef 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() def Test_enum_eval()
var lines =<< trim END var lines =<< trim END
vim9script vim9script
@@ -1465,10 +1481,11 @@ def Test_enum_eval()
Red, Red,
Blue Blue
endenum endenum
var s: string = string(Color.Blue) var e = eval('Color.Blue')
var e = eval(s)
assert_equal(Color.Blue, e) assert_equal(Color.Blue, e)
assert_equal(1, e.ordinal) 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 END
v9.CheckSourceSuccess(lines) v9.CheckSourceSuccess(lines)
enddef enddef

View File

@@ -704,6 +704,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
233,
/**/ /**/
232, 232,
/**/ /**/

View File

@@ -3844,16 +3844,18 @@ object_string(
class_T *cl = obj == NULL ? NULL : obj->obj_class; class_T *cl = obj == NULL ? NULL : obj->obj_class;
if (cl != NULL && IS_ENUM(cl)) if (cl != NULL && IS_ENUM(cl))
{ {
ga_concat(&ga, (char_u *)"enum ");
ga_concat(&ga, cl->class_name); 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, (char_u *)".");
ga_concat(&ga, name); ga_concat(&ga, enum_name);
return ga.ga_data;
} }
else
{
ga_concat(&ga, (char_u *)"object of "); ga_concat(&ga, (char_u *)"object of ");
ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]" ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
: cl->class_name); : cl->class_name);
}
if (cl != NULL) if (cl != NULL)
{ {
ga_concat(&ga, (char_u *)" {"); ga_concat(&ga, (char_u *)" {");