summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYegappan Lakshmanan <yegappan@yahoo.com>2024-03-31 18:45:35 +0200
committerChristian Brabandt <cb@256bit.org>2024-03-31 18:45:35 +0200
commit3cf121ed31f7a022e2ae6585391434d9c88e9792 (patch)
tree01acd88732929577e53b138a1f251e6a971dac43
parent5df961a1bc5ed14d0b5aa04ef59e9079313c268d (diff)
patch 9.1.0233: Vim9: string() output of enum is problematicv9.1.0233
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>
-rw-r--r--runtime/doc/builtin.txt2
-rw-r--r--src/testdir/test_vim9_enum.vim35
-rw-r--r--src/version.c2
-rw-r--r--src/vim9class.c16
4 files changed, 38 insertions, 17 deletions
diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index 9e57a8cc4a..088daa75a3 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -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
diff --git a/src/testdir/test_vim9_enum.vim b/src/testdir/test_vim9_enum.vim
index ceea32fe4f..5f9fbff6d3 100644
--- 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
diff --git a/src/version.c b/src/version.c
index 5069f721e9..1f748c8260 100644
--- 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,
diff --git a/src/vim9class.c b/src/vim9class.c
index 2d60ba95a6..fc4e002857 100644
--- 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);
+ }
+ 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 *)" {");